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 : syslog12
20 *
21 * EXECUTED BY : root / superuser
22 *
23 * TEST TITLE : Checking error conditions for syslog(2)
24 *
25 * TEST CASE TOTAL : 7
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,
35 * 1. syslog(2) fails with EINVAL for invalid type/command
36 * 2. syslog(2) fails with EFAULT for buffer outside program's accessible
37 * address space.
38 * 3. syslog(2) fails with EINVAL for NULL buffer argument.
39 * 4. syslog(2) fails with EINVAL for length arg. set to negative value.
40 * 5. syslog(2) fails with EPERM for non-root user.
41 * 6. syslog(2) fails with EINVAL for console level less than 0.
42 * 7. syslog(2) fails with EINVAL for console level greater than 8.
43 *
44 * Setup:
45 * Setup signal handling.
46 * Test caller is superuser
47 * Check existence of user nobody
48 * Set expected errnos
49 * Pause for SIGUSR1 if option specified.
50 *
51 * Test:
52 * Loop if the proper options are given.
53 * Execute system call
54 * Check return value and errno, if matching,
55 * Issue PASS message
56 * Otherwise,
57 * Issue FAIL message
58 *
59 * Cleanup:
60 * Print errno log and/or timing stats if options given
61 *
62 * USAGE: <for command-line>
63 * syslog12 [-c n] [-e] [-f] [-h] [-i n] [-I x] [-p] [-P x] [-t]
64 * where, -c n : Run n copies concurrently.
65 * -e : Turn on errno logging.
66 * -f : Turn off functional testing
67 * -h : Show help screen
68 * -i n : Execute test n times.
69 * -I x : Execute test for x seconds.
70 * -p : Pause for SIGUSR1 before starting
71 * -P x : Pause for x seconds between iterations.
72 * -t : Turn on syscall timing.
73 *
74 ****************************************************************/
75
76 #include <errno.h>
77 #include <pwd.h>
78 #include <sys/types.h>
79 #include <unistd.h>
80 #include <signal.h>
81 #include <linux/unistd.h>
82 #include "test.h"
83
84 #define EXP_RET_VAL -1
85
86 struct test_case_t { /* test case structure */
87 int type; /* 1st arg */
88 char *buf; /* 2nd arg */
89 int len; /* 3rd arg */
90 int exp_errno; /* Expected errno */
91 int (*setup) (void); /* Individual setup routine */
92 void (*cleanup) (void); /* Individual cleanup routine */
93 char *desc; /* Test description */
94 };
95
96 char *TCID = "syslog12";
97 static int testno;
98
99 static char buf;
100 static struct passwd *ltpuser;
101
102 static void setup(void);
103 static void cleanup(void);
104 static int setup1(void);
105 static void cleanup1(void);
106
107 #define syslog(arg1, arg2, arg3) syscall(__NR_syslog, arg1, arg2, arg3)
108
109 static struct test_case_t tdat[] = {
110 {100, &buf, 0, EINVAL, NULL, NULL, "invalid type/command"},
111 {2, NULL, 0, EINVAL, NULL, NULL, "NULL buffer argument"},
112 {3, &buf, -1, EINVAL, NULL, NULL, "negative length argument"},
113 {2, &buf, 0, EPERM, setup1, cleanup1, "non-root user"},
114 {8, &buf, -1, EINVAL, NULL, NULL, "console level less than 0"},
115 {8, &buf, 9, EINVAL, NULL, NULL, "console level greater than 8"},
116 };
117
118 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
119
timeout(int sig)120 void timeout(int sig)
121 {
122 tst_resm(TWARN, "syslog() timeout after 1s"
123 " for %s", tdat[testno].desc);
124 }
125
main(int argc,char ** argv)126 int main(int argc, char **argv)
127 {
128 int lc;
129 struct sigaction sa;
130 int ret;
131
132 tst_parse_opts(argc, argv, NULL, NULL);
133
134 setup();
135
136 memset(&sa, 0, sizeof(struct sigaction));
137 sa.sa_handler = timeout;
138 sa.sa_flags = 0;
139 sigaction(SIGALRM, &sa, NULL);
140
141 for (lc = 0; TEST_LOOPING(lc); lc++) {
142 /* reset tst_count in case we are looping */
143 tst_count = 0;
144
145 for (testno = 0; testno < TST_TOTAL; ++testno) {
146
147 if (tdat[testno].setup && tdat[testno].setup()) {
148 /* Setup failed, skip this testcase */
149 continue;
150 }
151
152 alarm(1);
153
154 TEST(syslog(tdat[testno].type, tdat[testno].buf,
155 tdat[testno].len));
156
157 alarm(0);
158 /* syslog returns an int, so we need to turn the long
159 * TEST_RETURN into an int to test with */
160 ret = TEST_RETURN;
161 if ((ret == EXP_RET_VAL) &&
162 (TEST_ERRNO == tdat[testno].exp_errno)) {
163 tst_resm(TPASS, "syslog() failed as expected"
164 " for %s : errno %d",
165 tdat[testno].desc, TEST_ERRNO);
166 } else {
167 tst_resm(TFAIL, "syslog() returned "
168 "unexpected results for %s ; returned"
169 " %d (expected %d), errno %d (expected"
170 " %d)", tdat[testno].desc,
171 ret, EXP_RET_VAL, TEST_ERRNO,
172 tdat[testno].exp_errno);
173 }
174
175 if (tdat[testno].cleanup) {
176 tdat[testno].cleanup();
177 }
178 }
179 }
180 cleanup();
181
182 tst_exit();
183 }
184
setup1(void)185 int setup1(void)
186 {
187 /* Change effective user id to nodody */
188 if (seteuid(ltpuser->pw_uid) == -1) {
189 tst_resm(TBROK, "seteuid failed to set the effective"
190 " uid to %d", ltpuser->pw_uid);
191 return 1;
192 }
193 return 0;
194 }
195
cleanup1(void)196 void cleanup1(void)
197 {
198 /* Change effective user id to root */
199 if (seteuid(0) == -1) {
200 tst_brkm(TBROK, NULL, "seteuid failed to set the effective"
201 " uid to root");
202 }
203 }
204
205 /*
206 * setup()
207 * performs all ONE TIME setup for this test
208 */
setup(void)209 void setup(void)
210 {
211 tst_require_root();
212
213 tst_sig(NOFORK, DEF_HANDLER, cleanup);
214
215 /* Check for nobody_uid user id */
216 if ((ltpuser = getpwnam("nobody")) == NULL) {
217 tst_brkm(TBROK, NULL, "nobody user id doesn't exist");
218 }
219
220 /* Pause if that option was specified
221 * TEST_PAUSE contains the code to fork the test with the -c option.
222 */
223 TEST_PAUSE;
224 }
225
226 /*
227 * cleanup()
228 * performs all ONE TIME cleanup for this test at
229 * completion or premature exit
230 */
cleanup(void)231 void cleanup(void)
232 {
233
234 }
235