blob: 0571f7c32be6b67588eb475f1139e573e33cdffd [file] [log] [blame]
robbiew4946e5a2003-01-01 20:38:17 +00001/*
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
Wanlong Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiew4946e5a2003-01-01 20:38:17 +000015 *
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.
subrata_modak4bb656a2009-02-26 12:02:09 +000043 *
robbiew4946e5a2003-01-01 20:38:17 +000044 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000050 *
robbiew4946e5a2003-01-01 20:38:17 +000051 * 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
subrata_modak4bb656a2009-02-26 12:02:09 +000058 *
robbiew4946e5a2003-01-01 20:38:17 +000059 * Cleanup:
60 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000061 *
robbiew4946e5a2003-01-01 20:38:17 +000062 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000073 *
robbiew4946e5a2003-01-01 20:38:17 +000074 ****************************************************************/
75
76#include <errno.h>
77#include <pwd.h>
78#include <sys/types.h>
79#include <unistd.h>
robbiewc9fa35f2004-01-26 22:25:31 +000080#include <signal.h>
robbiew4946e5a2003-01-01 20:38:17 +000081#include <linux/unistd.h>
82#include "test.h"
robbiew4946e5a2003-01-01 20:38:17 +000083
84#define EXP_RET_VAL -1
85
subrata_modak56207ce2009-03-23 13:35:39 +000086struct 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 */
robbiew4946e5a2003-01-01 20:38:17 +000094};
95
96char *TCID = "syslog12";
97static int testno;
Wanlong Gao354ebb42012-12-07 10:10:04 +080098
robbiew4946e5a2003-01-01 20:38:17 +000099static char buf;
100static struct passwd *ltpuser;
101
102static void setup(void);
103static void cleanup(void);
104static int setup1(void);
105static void cleanup1(void);
106
robbiew87778082003-06-13 18:15:35 +0000107#define syslog(arg1, arg2, arg3) syscall(__NR_syslog, arg1, arg2, arg3)
robbiew4946e5a2003-01-01 20:38:17 +0000108
subrata_modak56207ce2009-03-23 13:35:39 +0000109static struct test_case_t tdat[] = {
110 {100, &buf, 0, EINVAL, NULL, NULL, "invalid type/command"},
Garrett Cooper45e285d2010-11-22 12:19:25 -0800111 {2, NULL, 0, EINVAL, NULL, NULL, "NULL buffer argument"},
subrata_modak56207ce2009-03-23 13:35:39 +0000112 {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"},
robbiew4946e5a2003-01-01 20:38:17 +0000116};
117
118int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
119
subrata_modak56207ce2009-03-23 13:35:39 +0000120void timeout(int sig)
robbiewc9fa35f2004-01-26 22:25:31 +0000121{
robbiew9ae3de92004-01-26 22:57:59 +0000122 tst_resm(TWARN, "syslog() timeout after 1s"
123 " for %s", tdat[testno].desc);
subrata_modak4bb656a2009-02-26 12:02:09 +0000124}
robbiewc9fa35f2004-01-26 22:25:31 +0000125
subrata_modak56207ce2009-03-23 13:35:39 +0000126int main(int argc, char **argv)
robbiew4946e5a2003-01-01 20:38:17 +0000127{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200128 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200129 const char *msg;
robbiewc9fa35f2004-01-26 22:25:31 +0000130 struct sigaction sa;
vapieraf933732006-02-26 20:18:52 +0000131 int ret;
robbiew4946e5a2003-01-01 20:38:17 +0000132
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800134 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew4946e5a2003-01-01 20:38:17 +0000135 }
136
137 setup();
138
robbiewc9fa35f2004-01-26 22:25:31 +0000139 memset(&sa, 0, sizeof(struct sigaction));
140 sa.sa_handler = timeout;
subrata_modak4bb656a2009-02-26 12:02:09 +0000141 sa.sa_flags = 0;
robbiewc9fa35f2004-01-26 22:25:31 +0000142 sigaction(SIGALRM, &sa, NULL);
143
robbiew4946e5a2003-01-01 20:38:17 +0000144 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800145 /* reset tst_count in case we are looping */
146 tst_count = 0;
robbiew4946e5a2003-01-01 20:38:17 +0000147
148 for (testno = 0; testno < TST_TOTAL; ++testno) {
149
subrata_modak56207ce2009-03-23 13:35:39 +0000150 if (tdat[testno].setup && tdat[testno].setup()) {
robbiew4946e5a2003-01-01 20:38:17 +0000151 /* Setup failed, skip this testcase */
152 continue;
153 }
154
subrata_modak4bb656a2009-02-26 12:02:09 +0000155 alarm(1);
robbiewc9fa35f2004-01-26 22:25:31 +0000156
robbiew4946e5a2003-01-01 20:38:17 +0000157 TEST(syslog(tdat[testno].type, tdat[testno].buf,
subrata_modak56207ce2009-03-23 13:35:39 +0000158 tdat[testno].len));
robbiew4946e5a2003-01-01 20:38:17 +0000159
robbiewc9fa35f2004-01-26 22:25:31 +0000160 alarm(0);
vapieraf933732006-02-26 20:18:52 +0000161 /* syslog returns an int, so we need to turn the long
162 * TEST_RETURN into an int to test with */
163 ret = TEST_RETURN;
subrata_modak56207ce2009-03-23 13:35:39 +0000164 if ((ret == EXP_RET_VAL) &&
165 (TEST_ERRNO == tdat[testno].exp_errno)) {
robbiew4946e5a2003-01-01 20:38:17 +0000166 tst_resm(TPASS, "syslog() failed as expected"
subrata_modak56207ce2009-03-23 13:35:39 +0000167 " for %s : errno %d",
168 tdat[testno].desc, TEST_ERRNO);
robbiew4946e5a2003-01-01 20:38:17 +0000169 } else {
170 tst_resm(TFAIL, "syslog() returned "
subrata_modak56207ce2009-03-23 13:35:39 +0000171 "unexpected results for %s ; returned"
172 " %d (expected %d), errno %d (expected"
173 " %d)", tdat[testno].desc,
174 ret, EXP_RET_VAL, TEST_ERRNO,
175 tdat[testno].exp_errno);
robbiew4946e5a2003-01-01 20:38:17 +0000176 }
177
subrata_modak56207ce2009-03-23 13:35:39 +0000178 if (tdat[testno].cleanup) {
robbiew4946e5a2003-01-01 20:38:17 +0000179 tdat[testno].cleanup();
180 }
181 }
182 }
183 cleanup();
184
Garrett Cooper53740502010-12-16 00:04:01 -0800185 tst_exit();
robbiew4946e5a2003-01-01 20:38:17 +0000186}
187
subrata_modak56207ce2009-03-23 13:35:39 +0000188int setup1(void)
robbiew4946e5a2003-01-01 20:38:17 +0000189{
190 /* Change effective user id to nodody */
191 if (seteuid(ltpuser->pw_uid) == -1) {
192 tst_resm(TBROK, "seteuid failed to set the effective"
subrata_modak56207ce2009-03-23 13:35:39 +0000193 " uid to %d", ltpuser->pw_uid);
robbiew4946e5a2003-01-01 20:38:17 +0000194 return 1;
195 }
196 return 0;
197}
198
subrata_modak56207ce2009-03-23 13:35:39 +0000199void cleanup1(void)
robbiew4946e5a2003-01-01 20:38:17 +0000200{
201 /* Change effective user id to root */
202 if (seteuid(0) == -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800203 tst_brkm(TBROK, NULL, "seteuid failed to set the effective"
subrata_modak56207ce2009-03-23 13:35:39 +0000204 " uid to root");
robbiew4946e5a2003-01-01 20:38:17 +0000205 }
206}
207
208/*
209 * setup()
210 * performs all ONE TIME setup for this test
211 */
subrata_modak56207ce2009-03-23 13:35:39 +0000212void setup(void)
robbiew4946e5a2003-01-01 20:38:17 +0000213{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200214 tst_require_root(NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -0800215
robbiew4946e5a2003-01-01 20:38:17 +0000216 tst_sig(NOFORK, DEF_HANDLER, cleanup);
217
robbiew4946e5a2003-01-01 20:38:17 +0000218 /* Check for nobody_uid user id */
subrata_modak56207ce2009-03-23 13:35:39 +0000219 if ((ltpuser = getpwnam("nobody")) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800220 tst_brkm(TBROK, NULL, "nobody user id doesn't exist");
robbiew4946e5a2003-01-01 20:38:17 +0000221 }
222
robbiew4946e5a2003-01-01 20:38:17 +0000223 /* Pause if that option was specified
224 * TEST_PAUSE contains the code to fork the test with the -c option.
225 */
226 TEST_PAUSE;
227}
228
229/*
230 * cleanup()
231 * performs all ONE TIME cleanup for this test at
232 * completion or premature exit
233 */
subrata_modak56207ce2009-03-23 13:35:39 +0000234void cleanup(void)
robbiew4946e5a2003-01-01 20:38:17 +0000235{
robbiew4946e5a2003-01-01 20:38:17 +0000236
Wanlong Gao354ebb42012-12-07 10:10:04 +0800237}