blob: 725257bcd3737af06df1973631aaceacc4ec4639 [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 : 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
subrata_modak4bb656a2009-02-26 12:02:09 +000035 *
robbiew4946e5a2003-01-01 20:38:17 +000036 * Setup:
37 * Setup signal handling.
38 * Test caller is superuser
39 * Check existence of user nobody
40 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000041 *
robbiew4946e5a2003-01-01 20:38:17 +000042 * 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
subrata_modak4bb656a2009-02-26 12:02:09 +000049 *
robbiew4946e5a2003-01-01 20:38:17 +000050 * Cleanup:
51 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000052 *
robbiew4946e5a2003-01-01 20:38:17 +000053 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000064 *
robbiew4946e5a2003-01-01 20:38:17 +000065 ****************************************************************/
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 "test.h"
robbiew4946e5a2003-01-01 20:38:17 +000073
74#define UNEXP_RET_VAL -1
75
subrata_modak56207ce2009-03-23 13:35:39 +000076struct test_case_t { /* test case structure */
77 int type; /* 1st arg. */
78 char *buf; /* 2nd arg. */
79 int len; /* 3rd arg. */
80 int (*setup) (void); /* Individual setup routine */
81 void (*cleanup) (void); /* Individual cleanup routine */
82 char *desc; /* Test description */
robbiew4946e5a2003-01-01 20:38:17 +000083};
84
85char *TCID = "syslog11";
86static int testno;
87static char buf;
88static struct passwd *ltpuser;
89
90static void setup(void);
91static void cleanup(void);
92static int setup1(void);
93static void cleanup1(void);
94
robbiew87778082003-06-13 18:15:35 +000095#define syslog(arg1, arg2, arg3) syscall(__NR_syslog, arg1, arg2, arg3)
robbiew4946e5a2003-01-01 20:38:17 +000096
subrata_modak56207ce2009-03-23 13:35:39 +000097static struct test_case_t tdat[] = {
robbiew4946e5a2003-01-01 20:38:17 +000098 /* Type 0 and 1 are currently not implemented, always returns success */
subrata_modak56207ce2009-03-23 13:35:39 +000099 {0, &buf, 0, NULL, NULL, "type 0/Close the log"},
100 {1, &buf, 0, NULL, NULL, "type 1/Open the log"},
101 {2, &buf, 0, NULL, NULL, "type 2/Read from the log"},
102 {3, &buf, 0, NULL, NULL, "type 3/Read ring buffer"},
103 {3, &buf, 0, setup1, cleanup1, "type 3/Read ring buffer for non-root "
104 "user"},
105 /* Next two lines will clear dmesg. Uncomment if that is okay. -Robbie Williamson */
106 /* { 4, &buf, 0, NULL, NULL, "type 4/Read and clear ring buffer" }, */
107 /* { 5, &buf, 0, NULL, NULL, "type 5/Clear ring buffer" }, */
robbiew63e607a2003-04-03 15:52:39 +0000108
subrata_modak56207ce2009-03-23 13:35:39 +0000109 {8, NULL, 1, NULL, NULL, "type 8/Set log level to 1"},
110 {8, NULL, 7, NULL, NULL, "type 8/Set log level to 7(default)"},
111 {6, NULL, 0, NULL, NULL, "type 6/Disable printk's to console"},
112 {7, NULL, 0, NULL, NULL, "type 7/Enable printk's to console"},
robbiew4946e5a2003-01-01 20:38:17 +0000113};
114
115int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
116
subrata_modak56207ce2009-03-23 13:35:39 +0000117int main(int argc, char **argv)
robbiew4946e5a2003-01-01 20:38:17 +0000118{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200119 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200120 const char *msg;
robbiew4946e5a2003-01-01 20:38:17 +0000121
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800123 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew4946e5a2003-01-01 20:38:17 +0000124 }
robbiew4946e5a2003-01-01 20:38:17 +0000125 setup();
126
robbiew4946e5a2003-01-01 20:38:17 +0000127 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800128 /* reset tst_count in case we are looping */
129 tst_count = 0;
robbiew4946e5a2003-01-01 20:38:17 +0000130
131 for (testno = 0; testno < TST_TOTAL; ++testno) {
132
subrata_modak56207ce2009-03-23 13:35:39 +0000133 if (tdat[testno].setup && tdat[testno].setup()) {
robbiew4946e5a2003-01-01 20:38:17 +0000134 /* Setup failed, skip this testcase */
135 continue;
136 }
137
138 TEST(syslog(tdat[testno].type, tdat[testno].buf,
subrata_modak56207ce2009-03-23 13:35:39 +0000139 tdat[testno].len));
robbiew4946e5a2003-01-01 20:38:17 +0000140
141 if (TEST_RETURN == UNEXP_RET_VAL) {
vapiercdef7882006-02-26 20:33:55 +0000142 if (TEST_ERRNO == EPERM && geteuid() != 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000143 tst_resm(TPASS,
144 "syslog() passed for %s (non-root EPERM is OK)",
145 tdat[testno].desc);
vapiercdef7882006-02-26 20:33:55 +0000146 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000147 tst_resm(TFAIL,
148 "syslog() failed for %s: errno "
149 "%d (%s)", tdat[testno].desc,
150 TEST_ERRNO, strerror(errno));
vapiercdef7882006-02-26 20:33:55 +0000151 }
robbiew4946e5a2003-01-01 20:38:17 +0000152 } else {
subrata_modak4bb656a2009-02-26 12:02:09 +0000153 tst_resm(TPASS, "syslog() successful for %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000154 tdat[testno].desc);
robbiew4946e5a2003-01-01 20:38:17 +0000155 }
156
subrata_modak56207ce2009-03-23 13:35:39 +0000157 if (tdat[testno].cleanup) {
robbiew4946e5a2003-01-01 20:38:17 +0000158 tdat[testno].cleanup();
159 }
160 }
161 }
162 cleanup();
163
Garrett Cooper53740502010-12-16 00:04:01 -0800164 tst_exit();
robbiew4946e5a2003-01-01 20:38:17 +0000165}
166
subrata_modak56207ce2009-03-23 13:35:39 +0000167int setup1(void)
robbiew4946e5a2003-01-01 20:38:17 +0000168{
169 /* Change effective user id to nodody */
170 if (seteuid(ltpuser->pw_uid) == -1) {
171 tst_resm(TBROK, "seteuid failed to set the effective"
subrata_modak56207ce2009-03-23 13:35:39 +0000172 " uid to %d", ltpuser->pw_uid);
robbiew4946e5a2003-01-01 20:38:17 +0000173 return 1;
174 }
175 return 0;
176}
177
subrata_modak56207ce2009-03-23 13:35:39 +0000178void cleanup1(void)
robbiew4946e5a2003-01-01 20:38:17 +0000179{
180 /* Change effective user id to root */
181 if (seteuid(0) == -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800182 tst_brkm(TBROK, NULL, "seteuid failed to set the effective"
subrata_modak56207ce2009-03-23 13:35:39 +0000183 " uid to root");
robbiew4946e5a2003-01-01 20:38:17 +0000184 }
185}
186
187/*
188 * setup()
189 * performs all ONE TIME setup for this test
190 */
subrata_modak56207ce2009-03-23 13:35:39 +0000191void setup(void)
robbiew4946e5a2003-01-01 20:38:17 +0000192{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200193 tst_require_root(NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -0800194
robbiew4946e5a2003-01-01 20:38:17 +0000195 tst_sig(NOFORK, DEF_HANDLER, cleanup);
196
robbiew4946e5a2003-01-01 20:38:17 +0000197 /* Check for nobody_uid user id */
subrata_modak56207ce2009-03-23 13:35:39 +0000198 if ((ltpuser = getpwnam("nobody")) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800199 tst_brkm(TBROK, NULL, "nobody user id doesn't exist");
robbiew4946e5a2003-01-01 20:38:17 +0000200 }
201
202 /* Pause if that option was specified
203 * TEST_PAUSE contains the code to fork the test with the -c option.
204 */
205 TEST_PAUSE;
206}
207
208/*
209 * cleanup()
210 * performs all ONE TIME cleanup for this test at
211 * completion or premature exit
212 */
subrata_modak56207ce2009-03-23 13:35:39 +0000213void cleanup(void)
robbiew4946e5a2003-01-01 20:38:17 +0000214{
robbiew4946e5a2003-01-01 20:38:17 +0000215
Wanlong Gao354ebb42012-12-07 10:10:04 +0800216}