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 : 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 43 | * |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 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. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 50 | * |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 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 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 58 | * |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 59 | * Cleanup: |
| 60 | * Print errno log and/or timing stats if options given |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 61 | * |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 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. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 73 | * |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 74 | ****************************************************************/ |
| 75 | |
| 76 | #include <errno.h> |
| 77 | #include <pwd.h> |
| 78 | #include <sys/types.h> |
| 79 | #include <unistd.h> |
robbiew | c9fa35f | 2004-01-26 22:25:31 +0000 | [diff] [blame] | 80 | #include <signal.h> |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 81 | #include <linux/unistd.h> |
| 82 | #include "test.h" |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 83 | |
| 84 | #define EXP_RET_VAL -1 |
| 85 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 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 */ |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | char *TCID = "syslog12"; |
| 97 | static int testno; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 98 | |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 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 | |
robbiew | 8777808 | 2003-06-13 18:15:35 +0000 | [diff] [blame] | 107 | #define syslog(arg1, arg2, arg3) syscall(__NR_syslog, arg1, arg2, arg3) |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 108 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | static struct test_case_t tdat[] = { |
| 110 | {100, &buf, 0, EINVAL, NULL, NULL, "invalid type/command"}, |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 111 | {2, NULL, 0, EINVAL, NULL, NULL, "NULL buffer argument"}, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 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"}, |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); |
| 119 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 120 | void timeout(int sig) |
robbiew | c9fa35f | 2004-01-26 22:25:31 +0000 | [diff] [blame] | 121 | { |
robbiew | 9ae3de9 | 2004-01-26 22:57:59 +0000 | [diff] [blame] | 122 | tst_resm(TWARN, "syslog() timeout after 1s" |
| 123 | " for %s", tdat[testno].desc); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 124 | } |
robbiew | c9fa35f | 2004-01-26 22:25:31 +0000 | [diff] [blame] | 125 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 126 | int main(int argc, char **argv) |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 127 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 128 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 129 | const char *msg; |
robbiew | c9fa35f | 2004-01-26 22:25:31 +0000 | [diff] [blame] | 130 | struct sigaction sa; |
vapier | af93373 | 2006-02-26 20:18:52 +0000 | [diff] [blame] | 131 | int ret; |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 132 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 133 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 134 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | setup(); |
| 138 | |
robbiew | c9fa35f | 2004-01-26 22:25:31 +0000 | [diff] [blame] | 139 | memset(&sa, 0, sizeof(struct sigaction)); |
| 140 | sa.sa_handler = timeout; |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 141 | sa.sa_flags = 0; |
robbiew | c9fa35f | 2004-01-26 22:25:31 +0000 | [diff] [blame] | 142 | sigaction(SIGALRM, &sa, NULL); |
| 143 | |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 144 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 145 | /* reset tst_count in case we are looping */ |
| 146 | tst_count = 0; |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 147 | |
| 148 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
| 149 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 150 | if (tdat[testno].setup && tdat[testno].setup()) { |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 151 | /* Setup failed, skip this testcase */ |
| 152 | continue; |
| 153 | } |
| 154 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 155 | alarm(1); |
robbiew | c9fa35f | 2004-01-26 22:25:31 +0000 | [diff] [blame] | 156 | |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 157 | TEST(syslog(tdat[testno].type, tdat[testno].buf, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 158 | tdat[testno].len)); |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 159 | |
robbiew | c9fa35f | 2004-01-26 22:25:31 +0000 | [diff] [blame] | 160 | alarm(0); |
vapier | af93373 | 2006-02-26 20:18:52 +0000 | [diff] [blame] | 161 | /* 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 164 | if ((ret == EXP_RET_VAL) && |
| 165 | (TEST_ERRNO == tdat[testno].exp_errno)) { |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 166 | tst_resm(TPASS, "syslog() failed as expected" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | " for %s : errno %d", |
| 168 | tdat[testno].desc, TEST_ERRNO); |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 169 | } else { |
| 170 | tst_resm(TFAIL, "syslog() returned " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | "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); |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 176 | } |
| 177 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 178 | if (tdat[testno].cleanup) { |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 179 | tdat[testno].cleanup(); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | cleanup(); |
| 184 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 185 | tst_exit(); |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 186 | } |
| 187 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 188 | int setup1(void) |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 189 | { |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 193 | " uid to %d", ltpuser->pw_uid); |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 194 | return 1; |
| 195 | } |
| 196 | return 0; |
| 197 | } |
| 198 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 199 | void cleanup1(void) |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 200 | { |
| 201 | /* Change effective user id to root */ |
| 202 | if (seteuid(0) == -1) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 203 | tst_brkm(TBROK, NULL, "seteuid failed to set the effective" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 204 | " uid to root"); |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * setup() |
| 210 | * performs all ONE TIME setup for this test |
| 211 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 212 | void setup(void) |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 213 | { |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 214 | tst_require_root(NULL); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 215 | |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 216 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 217 | |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 218 | /* Check for nobody_uid user id */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 219 | if ((ltpuser = getpwnam("nobody")) == NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 220 | tst_brkm(TBROK, NULL, "nobody user id doesn't exist"); |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 221 | } |
| 222 | |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 223 | /* 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 234 | void cleanup(void) |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 235 | { |
robbiew | 4946e5a | 2003-01-01 20:38:17 +0000 | [diff] [blame] | 236 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 237 | } |