plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 3 | * Copyright (c) Wipro Technologies, 2002. All Rights Reserved. |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /**************************************************************************** |
| 21 | * TEST IDENTIFIER : sethostname03 |
| 22 | * |
| 23 | * EXECUTED BY : root / superuser |
| 24 | * |
| 25 | * TEST TITLE : test for EPERM error value when sethostname(2) is |
| 26 | * called from as non superuser |
| 27 | * |
| 28 | * TEST CASE TOTAL : 1 |
| 29 | * |
| 30 | * AUTHOR : Suresh Babu V. <suresh.babu@wipro.com> |
| 31 | * |
| 32 | * SIGNALS |
| 33 | * Uses SIGUSR1 to pause before test if option set. |
| 34 | * (See the parse_opts(3) man page). |
| 35 | * |
| 36 | * DESCRIPTION |
| 37 | * Verify that, sethostname(2) returns -1 and sets errno to EPERM |
| 38 | * if the effective userid id of the caller is not super-user. |
| 39 | * |
| 40 | * Setup: |
| 41 | * Setup signal handling. |
| 42 | * Save the current host name. |
| 43 | * Pause for SIGUSR1 if option specified. |
| 44 | * |
| 45 | * Test: |
| 46 | * Loop if the proper options are given. |
| 47 | * Execute system call |
| 48 | * Check return code, if system call failed (return=-1) |
| 49 | * if errno set == expected errno |
| 50 | * Issue sys call fails with expected return value and errno. |
| 51 | * Otherwise, |
| 52 | * Issue sys call fails with unexpected errno. |
| 53 | * |
| 54 | * Cleanup: |
| 55 | * Restore old hostname |
| 56 | * Print errno log and/or timing stats if options given |
| 57 | * |
| 58 | * Usage: <for command-line> |
| 59 | * sethostname03 [-c n] [-e] [-i n] [-I x] [-P x] [-p] [-t] |
| 60 | * where, -c n : Run n copies concurrently. |
| 61 | * -e : Turn on errno logging. |
| 62 | * -i n : Execute test n times. |
| 63 | * -I x : Execute test for x seconds. |
| 64 | * -P x : Pause for x seconds between iterations. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 65 | * -p : Pause for SIGUSR1 before starting |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 66 | * -t : Turn on syscall timing. |
| 67 | * |
| 68 | *************************************************************************/ |
| 69 | |
| 70 | #include <stdio.h> |
mridge | 4cce625 | 2004-09-13 21:47:38 +0000 | [diff] [blame] | 71 | #include <string.h> |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 72 | #include <errno.h> |
| 73 | #include <pwd.h> |
| 74 | #include <linux/utsname.h> |
| 75 | |
| 76 | #include "test.h" |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 77 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 78 | #define MAX_LENGTH __NEW_UTS_LEN |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 79 | |
| 80 | char *TCID = "sethostname03"; |
| 81 | int TST_TOTAL = 1; |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 82 | |
| 83 | static char ltpthost[] = "ltphost"; |
| 84 | static char hname[MAX_LENGTH]; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 85 | |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 86 | static char nobody_uid[] = "nobody"; |
| 87 | struct passwd *ltpuser; |
| 88 | |
| 89 | static void setup(void); |
| 90 | static void cleanup(void); |
| 91 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 92 | int main(int ac, char **av) |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 93 | { |
| 94 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 95 | const char *msg; |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 96 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 97 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 98 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 99 | |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* Do initial setup */ |
| 103 | setup(); |
| 104 | |
| 105 | /* Check for looping state if -i option is given */ |
| 106 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 107 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 108 | tst_count = 0; |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 109 | |
| 110 | /* call sethostname() */ |
| 111 | TEST(sethostname(ltpthost, sizeof(ltpthost))); |
| 112 | |
| 113 | if ((TEST_RETURN == -1) && (TEST_ERRNO == EPERM)) { |
| 114 | tst_resm(TPASS, "Expected Failure; Got EPERM"); |
| 115 | } else { |
| 116 | tst_resm(TFAIL, "call failed to produce " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | "expected error; errno: %d : %s", |
| 118 | TEST_ERRNO, strerror(TEST_ERRNO)); |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 119 | } |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 120 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 121 | } |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 122 | |
| 123 | /* cleanup and exit */ |
| 124 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 125 | tst_exit(); |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 126 | |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* |
| 130 | * setup() - performs all one time setup for this test. |
| 131 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 132 | void setup(void) |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 133 | { |
| 134 | int ret; |
| 135 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 136 | tst_require_root(NULL); |
| 137 | |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 138 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 139 | |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 140 | /* Switch to nobody user for correct error code collection */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 141 | if ((ltpuser = getpwnam(nobody_uid)) == NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 142 | tst_brkm(TBROK, NULL, "Required user \"nobody\" not" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | " present"); |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | if (seteuid(ltpuser->pw_uid) == -1) { |
| 147 | tst_resm(TWARN, "seteuid failed to " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 148 | "to set the effective uid to %d", ltpuser->pw_uid); |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 149 | perror("seteuid"); |
| 150 | } |
| 151 | |
| 152 | /* Keep current hostname */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 153 | if ((ret = gethostname(hname, sizeof(hname))) < 0) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 154 | tst_brkm(TBROK, NULL, "gethostname() failed while" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 155 | " getting current host name"); |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 156 | } |
| 157 | |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 158 | TEST_PAUSE; |
| 159 | |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * cleanup() - performs all one time cleanup for this test |
| 164 | * completion or premature exit. |
| 165 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 166 | void cleanup(void) |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 167 | { |
| 168 | int ret; |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 169 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 170 | /* Set effective user id back to root/super user */ |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 171 | if (seteuid(0) == -1) { |
| 172 | tst_resm(TWARN, "seteuid failed to " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 173 | "to set the effective uid to root"); |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 174 | perror("seteuid"); |
| 175 | } |
| 176 | |
| 177 | /* Restore host name */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 178 | if ((ret = sethostname(hname, strlen(hname))) < 0) { |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 179 | tst_resm(TWARN, "sethostname() failed while restoring" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 180 | " hostname to \"%s\"", hname); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 181 | } |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 182 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 183 | } |