1/*
2 * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 *
16 */
17/**********************************************************
18 *
19 *    TEST IDENTIFIER   : syslog11
20 *
21 *    EXECUTED BY       : root / superuser
22 *
23 *    TEST TITLE        : Basic tests for syslog(2)
24 *
25 *    TEST CASE TOTAL   : 11
26 *
27 *    AUTHOR            : Madhu T L <madhu.tarikere@wipro.com>
28 *
29 *    SIGNALS
30 *      Uses SIGUSR1 to pause before test if option set.
31 *      (See the parse_opts(3) man page).
32 *
33 *    DESCRIPTION
34 *	Verify that, syslog(2) is successful for type ranging from 1 to 8
35 *
36 *	Setup:
37 *	  Setup signal handling.
38 *	  Test caller is superuser
39 *	  Check existence of user nobody
40 *	  Pause for SIGUSR1 if option specified.
41 *
42 *	Test:
43 *	 Loop if the proper options are given.
44 *	  Execute system call
45 *	  Check return value, if not successful,
46 *		 Issue FAIL message
47 *	  Otherwise,
48 *		Issue PASS message
49 *
50 *	Cleanup:
51 *	  Print errno log and/or timing stats if options given
52 *
53 * USAGE:  <for command-line>
54 *  syslog11 [-c n] [-e] [-f] [-h] [-i n] [-I x] [-p] [-P x] [-t]
55 *		where,  -c n : Run n copies concurrently.
56 *			-e   : Turn on errno logging.
57 *			-f   : Turn off functional testing
58 *			-h   : Show help screen
59 *			-i n : Execute test n times.
60 *			-I x : Execute test for x seconds.
61 *			-p   : Pause for SIGUSR1 before starting
62 *			-P x : Pause for x seconds between iterations.
63 *			-t   : Turn on syscall timing.
64 *
65 ****************************************************************/
66
67#include <errno.h>
68#include <pwd.h>
69#include <sys/types.h>
70#include <unistd.h>
71#include <linux/unistd.h>
72#include <sys/syscall.h>
73#include "test.h"
74#include "safe_macros.h"
75
76#define UNEXP_RET_VAL	-1
77
78struct test_case_t {		/* test case structure */
79	int type;		/* 1st arg. */
80	char *buf;		/* 2nd arg. */
81	int len;		/* 3rd arg. */
82	int (*setup) (void);	/* Individual setup routine */
83	void (*cleanup) (void);	/* Individual cleanup routine */
84	char *desc;		/* Test description */
85};
86
87char *TCID = "syslog11";
88static int testno;
89static char buf;
90static struct passwd *ltpuser;
91
92static void setup(void);
93static void cleanup(void);
94static int setup1(void);
95static void cleanup1(void);
96
97#define syslog(arg1, arg2, arg3) syscall(__NR_syslog, arg1, arg2, arg3)
98
99static struct test_case_t tdat[] = {
100	/* Type 0 and 1 are currently not implemented, always returns success */
101	{0, &buf, 0, NULL, NULL, "type 0/Close the log"},
102	{1, &buf, 0, NULL, NULL, "type 1/Open the log"},
103	{2, &buf, 0, NULL, NULL, "type 2/Read from the log"},
104	{3, &buf, 0, NULL, NULL, "type 3/Read ring buffer"},
105	{3, &buf, 0, setup1, cleanup1, "type 3/Read ring buffer for non-root "
106	 "user"},
107	/* Next two lines will clear dmesg. Uncomment if that is okay. -Robbie Williamson */
108	/*    { 4, &buf, 0, NULL, NULL, "type 4/Read and clear ring buffer" },            */
109	/*    { 5, &buf, 0, NULL, NULL, "type 5/Clear ring buffer" },                     */
110
111	{8, NULL, 1, NULL, NULL, "type 8/Set log level to 1"},
112	{8, NULL, 7, NULL, NULL, "type 8/Set log level to 7(default)"},
113	{6, NULL, 0, NULL, NULL, "type 6/Disable printk's to console"},
114	{7, NULL, 0, NULL, NULL, "type 7/Enable printk's to console"},
115};
116
117int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
118
119int main(int argc, char **argv)
120{
121	int lc;
122
123	tst_parse_opts(argc, argv, NULL, NULL);
124	setup();
125
126	for (lc = 0; TEST_LOOPING(lc); lc++) {
127		/* reset tst_count in case we are looping */
128		tst_count = 0;
129
130		for (testno = 0; testno < TST_TOTAL; ++testno) {
131
132			if (tdat[testno].setup && tdat[testno].setup()) {
133				/* Setup failed, skip this testcase */
134				continue;
135			}
136
137			TEST(syslog(tdat[testno].type, tdat[testno].buf,
138				    tdat[testno].len));
139
140			if (TEST_RETURN == UNEXP_RET_VAL) {
141				if (TEST_ERRNO == EPERM && geteuid() != 0) {
142					tst_resm(TPASS,
143						 "syslog() passed for %s (non-root EPERM is OK)",
144						 tdat[testno].desc);
145				} else {
146					tst_resm(TFAIL,
147						 "syslog() failed for %s: errno "
148						 "%d (%s)", tdat[testno].desc,
149						 TEST_ERRNO, strerror(errno));
150				}
151			} else {
152				tst_resm(TPASS, "syslog() successful for %s",
153					 tdat[testno].desc);
154			}
155
156			if (tdat[testno].cleanup) {
157				tdat[testno].cleanup();
158			}
159		}
160	}
161	cleanup();
162
163	tst_exit();
164}
165
166int setup1(void)
167{
168	/* Change effective user id to nodody */
169	if (seteuid(ltpuser->pw_uid) == -1) {
170		tst_resm(TBROK, "seteuid failed to set the effective"
171			 " uid to %d", ltpuser->pw_uid);
172		return 1;
173	}
174	return 0;
175}
176
177void cleanup1(void)
178{
179	/* Change effective user id to root */
180	SAFE_SETEUID(NULL, 0);
181}
182
183/*
184 * setup()
185 *	performs all ONE TIME setup for this test
186 */
187void setup(void)
188{
189	tst_require_root();
190
191	tst_sig(NOFORK, DEF_HANDLER, cleanup);
192
193	/* Check for nobody_uid user id */
194	if ((ltpuser = getpwnam("nobody")) == NULL) {
195		tst_brkm(TBROK, NULL, "nobody user id doesn't exist");
196	}
197
198	/* Pause if that option was specified
199	 * TEST_PAUSE contains the code to fork the test with the -c option.
200	 */
201	TEST_PAUSE;
202}
203
204/*
205 * cleanup()
206 *	performs all ONE TIME cleanup for this test at
207 *	completion or premature exit
208 */
209void cleanup(void)
210{
211
212}
213