Home | History | Annotate | Download | only in syslog
      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 (at) 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 "test.h"
     73 
     74 #define UNEXP_RET_VAL	-1
     75 
     76 struct 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 */
     83 };
     84 
     85 char *TCID = "syslog11";
     86 static int testno;
     87 static char buf;
     88 static struct passwd *ltpuser;
     89 
     90 static void setup(void);
     91 static void cleanup(void);
     92 static int setup1(void);
     93 static void cleanup1(void);
     94 
     95 #define syslog(arg1, arg2, arg3) syscall(__NR_syslog, arg1, arg2, arg3)
     96 
     97 static struct test_case_t tdat[] = {
     98 	/* Type 0 and 1 are currently not implemented, always returns success */
     99 	{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" },                     */
    108 
    109 	{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"},
    113 };
    114 
    115 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
    116 
    117 int main(int argc, char **argv)
    118 {
    119 	int lc;
    120 
    121 	tst_parse_opts(argc, argv, NULL, NULL);
    122 	setup();
    123 
    124 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    125 		/* reset tst_count in case we are looping */
    126 		tst_count = 0;
    127 
    128 		for (testno = 0; testno < TST_TOTAL; ++testno) {
    129 
    130 			if (tdat[testno].setup && tdat[testno].setup()) {
    131 				/* Setup failed, skip this testcase */
    132 				continue;
    133 			}
    134 
    135 			TEST(syslog(tdat[testno].type, tdat[testno].buf,
    136 				    tdat[testno].len));
    137 
    138 			if (TEST_RETURN == UNEXP_RET_VAL) {
    139 				if (TEST_ERRNO == EPERM && geteuid() != 0) {
    140 					tst_resm(TPASS,
    141 						 "syslog() passed for %s (non-root EPERM is OK)",
    142 						 tdat[testno].desc);
    143 				} else {
    144 					tst_resm(TFAIL,
    145 						 "syslog() failed for %s: errno "
    146 						 "%d (%s)", tdat[testno].desc,
    147 						 TEST_ERRNO, strerror(errno));
    148 				}
    149 			} else {
    150 				tst_resm(TPASS, "syslog() successful for %s",
    151 					 tdat[testno].desc);
    152 			}
    153 
    154 			if (tdat[testno].cleanup) {
    155 				tdat[testno].cleanup();
    156 			}
    157 		}
    158 	}
    159 	cleanup();
    160 
    161 	tst_exit();
    162 }
    163 
    164 int setup1(void)
    165 {
    166 	/* Change effective user id to nodody */
    167 	if (seteuid(ltpuser->pw_uid) == -1) {
    168 		tst_resm(TBROK, "seteuid failed to set the effective"
    169 			 " uid to %d", ltpuser->pw_uid);
    170 		return 1;
    171 	}
    172 	return 0;
    173 }
    174 
    175 void cleanup1(void)
    176 {
    177 	/* Change effective user id to root */
    178 	if (seteuid(0) == -1) {
    179 		tst_brkm(TBROK, NULL, "seteuid failed to set the effective"
    180 			 " uid to root");
    181 	}
    182 }
    183 
    184 /*
    185  * setup()
    186  *	performs all ONE TIME setup for this test
    187  */
    188 void setup(void)
    189 {
    190 	tst_require_root();
    191 
    192 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    193 
    194 	/* Check for nobody_uid user id */
    195 	if ((ltpuser = getpwnam("nobody")) == NULL) {
    196 		tst_brkm(TBROK, NULL, "nobody user id doesn't exist");
    197 	}
    198 
    199 	/* Pause if that option was specified
    200 	 * TEST_PAUSE contains the code to fork the test with the -c option.
    201 	 */
    202 	TEST_PAUSE;
    203 }
    204 
    205 /*
    206  * cleanup()
    207  *	performs all ONE TIME cleanup for this test at
    208  *	completion or premature exit
    209  */
    210 void cleanup(void)
    211 {
    212 
    213 }
    214