robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 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 |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 13 | * with this program; if not, write the Free Software Foundation, Inc., |
| 14 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 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 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 35 | * |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 36 | * Setup: |
| 37 | * Setup signal handling. |
| 38 | * Test caller is superuser |
| 39 | * Check existence of user nobody |
| 40 | * Pause for SIGUSR1 if option specified. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 41 | * |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 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 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 49 | * |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 50 | * Cleanup: |
| 51 | * Print errno log and/or timing stats if options given |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 52 | * |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 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. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 64 | * |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 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" |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 73 | |
| 74 | #define UNEXP_RET_VAL -1 |
| 75 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 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 */ |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 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 | |
robbiew | 8777808 | 2003-06-13 18:15:35 +0000 | [diff] [blame] | 95 | #define syslog(arg1, arg2, arg3) syscall(__NR_syslog, arg1, arg2, arg3) |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 96 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | static struct test_case_t tdat[] = { |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 98 | /* Type 0 and 1 are currently not implemented, always returns success */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 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" }, */ |
robbiew | 63e607a | 2003-04-03 15:52:39 +0000 | [diff] [blame] | 108 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 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"}, |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); |
| 116 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | int main(int argc, char **argv) |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 118 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 119 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 120 | const char *msg; |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 121 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 122 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 123 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 124 | } |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 125 | setup(); |
| 126 | |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 127 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 128 | /* reset tst_count in case we are looping */ |
| 129 | tst_count = 0; |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 130 | |
| 131 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
| 132 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 133 | if (tdat[testno].setup && tdat[testno].setup()) { |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 134 | /* Setup failed, skip this testcase */ |
| 135 | continue; |
| 136 | } |
| 137 | |
| 138 | TEST(syslog(tdat[testno].type, tdat[testno].buf, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 139 | tdat[testno].len)); |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 140 | |
| 141 | if (TEST_RETURN == UNEXP_RET_VAL) { |
vapier | cdef788 | 2006-02-26 20:33:55 +0000 | [diff] [blame] | 142 | if (TEST_ERRNO == EPERM && geteuid() != 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | tst_resm(TPASS, |
| 144 | "syslog() passed for %s (non-root EPERM is OK)", |
| 145 | tdat[testno].desc); |
vapier | cdef788 | 2006-02-26 20:33:55 +0000 | [diff] [blame] | 146 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 147 | tst_resm(TFAIL, |
| 148 | "syslog() failed for %s: errno " |
| 149 | "%d (%s)", tdat[testno].desc, |
| 150 | TEST_ERRNO, strerror(errno)); |
vapier | cdef788 | 2006-02-26 20:33:55 +0000 | [diff] [blame] | 151 | } |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 152 | } else { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 153 | tst_resm(TPASS, "syslog() successful for %s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | tdat[testno].desc); |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 155 | } |
| 156 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 157 | if (tdat[testno].cleanup) { |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 158 | tdat[testno].cleanup(); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | cleanup(); |
| 163 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 164 | tst_exit(); |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 165 | } |
| 166 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | int setup1(void) |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 168 | { |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 172 | " uid to %d", ltpuser->pw_uid); |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 173 | return 1; |
| 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 178 | void cleanup1(void) |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 179 | { |
| 180 | /* Change effective user id to root */ |
| 181 | if (seteuid(0) == -1) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 182 | tst_brkm(TBROK, NULL, "seteuid failed to set the effective" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 183 | " uid to root"); |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * setup() |
| 189 | * performs all ONE TIME setup for this test |
| 190 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 191 | void setup(void) |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 192 | { |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 193 | tst_require_root(NULL); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 194 | |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 195 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 196 | |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 197 | /* Check for nobody_uid user id */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 198 | if ((ltpuser = getpwnam("nobody")) == NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 199 | tst_brkm(TBROK, NULL, "nobody user id doesn't exist"); |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 200 | } |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 213 | void cleanup(void) |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 214 | { |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 215 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 216 | } |