blob: 59f4e403c8eb48cc460ea3ed3370cba25a733b17 [file] [log] [blame]
plars2be4eff2002-11-01 15:44:39 +00001/*
2 *
subrata_modak4bb656a2009-02-26 12:02:09 +00003 * Copyright (c) Wipro Technologies, 2002. All Rights Reserved.
plars2be4eff2002-11-01 15:44:39 +00004 *
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars2be4eff2002-11-01 15:44:39 +000018 */
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_modak4bb656a2009-02-26 12:02:09 +000065 * -p : Pause for SIGUSR1 before starting
plars2be4eff2002-11-01 15:44:39 +000066 * -t : Turn on syscall timing.
67 *
68 *************************************************************************/
69
70#include <stdio.h>
mridge4cce6252004-09-13 21:47:38 +000071#include <string.h>
plars2be4eff2002-11-01 15:44:39 +000072#include <errno.h>
73#include <pwd.h>
74#include <linux/utsname.h>
75
76#include "test.h"
plars2be4eff2002-11-01 15:44:39 +000077
subrata_modak4bb656a2009-02-26 12:02:09 +000078#define MAX_LENGTH __NEW_UTS_LEN
plars2be4eff2002-11-01 15:44:39 +000079
80char *TCID = "sethostname03";
81int TST_TOTAL = 1;
plars2be4eff2002-11-01 15:44:39 +000082
83static char ltpthost[] = "ltphost";
84static char hname[MAX_LENGTH];
Wanlong Gao354ebb42012-12-07 10:10:04 +080085
plars2be4eff2002-11-01 15:44:39 +000086static char nobody_uid[] = "nobody";
87struct passwd *ltpuser;
88
89static void setup(void);
90static void cleanup(void);
91
subrata_modak56207ce2009-03-23 13:35:39 +000092int main(int ac, char **av)
plars2be4eff2002-11-01 15:44:39 +000093{
94 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020095 const char *msg;
plars2be4eff2002-11-01 15:44:39 +000096
Garrett Cooper45e285d2010-11-22 12:19:25 -080097 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
plars2be4eff2002-11-01 15:44:39 +000098 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080099
plars2be4eff2002-11-01 15:44:39 +0000100 }
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 Cooper2c282152010-12-16 00:55:50 -0800107
Caspar Zhangd59a6592013-03-07 14:59:12 +0800108 tst_count = 0;
plars2be4eff2002-11-01 15:44:39 +0000109
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_modak56207ce2009-03-23 13:35:39 +0000117 "expected error; errno: %d : %s",
118 TEST_ERRNO, strerror(TEST_ERRNO));
plars2be4eff2002-11-01 15:44:39 +0000119 }
plars2be4eff2002-11-01 15:44:39 +0000120
Garrett Cooper2c282152010-12-16 00:55:50 -0800121 }
plars2be4eff2002-11-01 15:44:39 +0000122
123 /* cleanup and exit */
124 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800125 tst_exit();
plars2be4eff2002-11-01 15:44:39 +0000126
plars2be4eff2002-11-01 15:44:39 +0000127}
128
129/*
130 * setup() - performs all one time setup for this test.
131 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400132void setup(void)
plars2be4eff2002-11-01 15:44:39 +0000133{
134 int ret;
135
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200136 tst_require_root(NULL);
137
plars2be4eff2002-11-01 15:44:39 +0000138 tst_sig(NOFORK, DEF_HANDLER, cleanup);
139
plars2be4eff2002-11-01 15:44:39 +0000140 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000141 if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800142 tst_brkm(TBROK, NULL, "Required user \"nobody\" not"
subrata_modak56207ce2009-03-23 13:35:39 +0000143 " present");
plars2be4eff2002-11-01 15:44:39 +0000144 }
145
146 if (seteuid(ltpuser->pw_uid) == -1) {
147 tst_resm(TWARN, "seteuid failed to "
subrata_modak56207ce2009-03-23 13:35:39 +0000148 "to set the effective uid to %d", ltpuser->pw_uid);
plars2be4eff2002-11-01 15:44:39 +0000149 perror("seteuid");
150 }
151
152 /* Keep current hostname */
subrata_modak56207ce2009-03-23 13:35:39 +0000153 if ((ret = gethostname(hname, sizeof(hname))) < 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800154 tst_brkm(TBROK, NULL, "gethostname() failed while"
subrata_modak56207ce2009-03-23 13:35:39 +0000155 " getting current host name");
plars2be4eff2002-11-01 15:44:39 +0000156 }
157
plars2be4eff2002-11-01 15:44:39 +0000158 TEST_PAUSE;
159
160}
161
162/*
163 * cleanup() - performs all one time cleanup for this test
164 * completion or premature exit.
165 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400166void cleanup(void)
plars2be4eff2002-11-01 15:44:39 +0000167{
168 int ret;
plars2be4eff2002-11-01 15:44:39 +0000169
subrata_modak56207ce2009-03-23 13:35:39 +0000170 /* Set effective user id back to root/super user */
plars2be4eff2002-11-01 15:44:39 +0000171 if (seteuid(0) == -1) {
172 tst_resm(TWARN, "seteuid failed to "
subrata_modak56207ce2009-03-23 13:35:39 +0000173 "to set the effective uid to root");
plars2be4eff2002-11-01 15:44:39 +0000174 perror("seteuid");
175 }
176
177 /* Restore host name */
subrata_modak56207ce2009-03-23 13:35:39 +0000178 if ((ret = sethostname(hname, strlen(hname))) < 0) {
plars2be4eff2002-11-01 15:44:39 +0000179 tst_resm(TWARN, "sethostname() failed while restoring"
subrata_modak56207ce2009-03-23 13:35:39 +0000180 " hostname to \"%s\"", hname);
subrata_modak4bb656a2009-02-26 12:02:09 +0000181 }
plars2be4eff2002-11-01 15:44:39 +0000182
Chris Dearmanec6edca2012-10-17 19:54:01 -0700183}