blob: 24a377dafe8a7174d07e5c0e37f97df0e670fbd4 [file] [log] [blame]
plars2be4eff2002-11-01 15:44:39 +00001/*
2 *
3 * Copyright (c) Wipro Technologies, 2002. All Rights Reserved.
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 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/*********************************************************************
22 * TEST IDENTIFIER : sethostname01
subrata_modak4bb656a2009-02-26 12:02:09 +000023 *
plars2be4eff2002-11-01 15:44:39 +000024 * EXECUTED BY : root / superuser
subrata_modak4bb656a2009-02-26 12:02:09 +000025 *
plars2be4eff2002-11-01 15:44:39 +000026 * TEST TITLE : Basic test for sethostname(2)
27 *
28 * TEST CASE TOTAL : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000029 *
plars2be4eff2002-11-01 15:44:39 +000030 * 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 *
subrata_modak56207ce2009-03-23 13:35:39 +000036 * DESCRIPTION$
plars2be4eff2002-11-01 15:44:39 +000037 * This is a Phase I test for the sethostname(2) system call.
38 * It is intended to provide a limited exposure of the system call.
39 *
40 * Setup:
41 * Setup signal handling.
42 * Save the current hostname.
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 * Log the errno and Issue a FAIL message.
50 * Otherwise, Issue a PASS message.
51 * Cleanup:
52 * Restore old host name.
53 * Print errno log and/or timing stats if options given
54 *
55 * Usage: <for command-line>
56 * sethostname01 [-c n] [-i n] [-I x] [-P x] [-p] [-t] [-h]
57 * where, -c n : Run n copies concurrently.
58 * -i n : Execute test n times.
59 * -I x : Execute test for x seconds.
subrata_modak4bb656a2009-02-26 12:02:09 +000060 * -p : Pause for SIGUSR1 before starting
plars2be4eff2002-11-01 15:44:39 +000061 * -P x : Pause for x seconds between iterations.
62 * -t : Turn on syscall timing.
63 * -h : Display usage information
64 *
65 *
66 ******************************************************************/
67
mridge4cce6252004-09-13 21:47:38 +000068#include <string.h>
plars2be4eff2002-11-01 15:44:39 +000069#include <errno.h>
70#include <linux/utsname.h>
71
72#include "test.h"
plars2be4eff2002-11-01 15:44:39 +000073
74#define MAX_LENGTH __NEW_UTS_LEN
75
plars2be4eff2002-11-01 15:44:39 +000076static void setup();
77static void cleanup();
78
79char *TCID = "sethostname01";
80int TST_TOTAL = 1;
subrata_modak56207ce2009-03-23 13:35:39 +000081static char hname[MAX_LENGTH]; /* host name */
plars2be4eff2002-11-01 15:44:39 +000082
subrata_modak56207ce2009-03-23 13:35:39 +000083int main(int ac, char **av)
plars2be4eff2002-11-01 15:44:39 +000084{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020085 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020086 const char *msg;
plars2be4eff2002-11-01 15:44:39 +000087
subrata_modak56207ce2009-03-23 13:35:39 +000088 char ltphost[] = "ltphost"; /* temporary host name to set */
plars2be4eff2002-11-01 15:44:39 +000089
Garrett Cooper45e285d2010-11-22 12:19:25 -080090 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
plars2be4eff2002-11-01 15:44:39 +000091 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080092
plars2be4eff2002-11-01 15:44:39 +000093 }
94
95 /* Do initial setup. */
96 setup();
97
98 /* check -c option for looping. */
subrata_modak56207ce2009-03-23 13:35:39 +000099 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars2be4eff2002-11-01 15:44:39 +0000100
Caspar Zhangd59a6592013-03-07 14:59:12 +0800101 tst_count = 0;
plars2be4eff2002-11-01 15:44:39 +0000102
subrata_modak56207ce2009-03-23 13:35:39 +0000103 /* Call sethostname(2) */
104 TEST(sethostname(ltphost, sizeof(ltphost)));
plars2be4eff2002-11-01 15:44:39 +0000105
subrata_modak56207ce2009-03-23 13:35:39 +0000106 /* check return code */
107 if (TEST_RETURN == -1) {
108 tst_resm(TFAIL, "sethostname() failed , errno=%d : %s",
109 TEST_ERRNO, strerror(TEST_ERRNO));
110 } else {
subrata_modak923b23f2009-11-02 13:57:16 +0000111 tst_resm(TPASS, "sethostname() returned %ld,"
subrata_modak56207ce2009-03-23 13:35:39 +0000112 " Hostname set to \"%s\"", TEST_RETURN,
113 ltphost);
114 }
plars2be4eff2002-11-01 15:44:39 +0000115
Garrett Cooper2c282152010-12-16 00:55:50 -0800116 }
plars2be4eff2002-11-01 15:44:39 +0000117
118 /* cleanup and exit */
119 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800120 tst_exit();
plars2be4eff2002-11-01 15:44:39 +0000121
Garrett Cooper2c282152010-12-16 00:55:50 -0800122}
plars2be4eff2002-11-01 15:44:39 +0000123
124/*
subrata_modak4bb656a2009-02-26 12:02:09 +0000125 * setup() - performs all one time setup for this test.
plars2be4eff2002-11-01 15:44:39 +0000126 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400127void setup(void)
plars2be4eff2002-11-01 15:44:39 +0000128{
129 int ret;
130
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200131 tst_require_root(NULL);
plars2be4eff2002-11-01 15:44:39 +0000132
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200133 tst_sig(NOFORK, DEF_HANDLER, cleanup);
plars2be4eff2002-11-01 15:44:39 +0000134
135 /* Store the existing hostname to retain it before exiting */
subrata_modak56207ce2009-03-23 13:35:39 +0000136 if ((ret = gethostname(hname, sizeof(hname))) < 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800137 tst_brkm(TBROK, NULL, "gethostname() failed while getting"
subrata_modak56207ce2009-03-23 13:35:39 +0000138 " current host name");
subrata_modak4bb656a2009-02-26 12:02:09 +0000139 }
plars2be4eff2002-11-01 15:44:39 +0000140
plars2be4eff2002-11-01 15:44:39 +0000141 TEST_PAUSE;
142
Garrett Cooper2c282152010-12-16 00:55:50 -0800143}
plars2be4eff2002-11-01 15:44:39 +0000144
subrata_modak4bb656a2009-02-26 12:02:09 +0000145/*
146 * cleanup() - performs all one time cleanup for this test
147 * completion or premature exit.
plars2be4eff2002-11-01 15:44:39 +0000148 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400149void cleanup(void)
plars2be4eff2002-11-01 15:44:39 +0000150{
151 int ret;
152
plars2be4eff2002-11-01 15:44:39 +0000153 /* Set the host name back to original name */
subrata_modak56207ce2009-03-23 13:35:39 +0000154 if ((ret = sethostname(hname, strlen(hname))) < 0) {
plars2be4eff2002-11-01 15:44:39 +0000155 tst_resm(TWARN, "sethostname() failed while restoring"
subrata_modak56207ce2009-03-23 13:35:39 +0000156 " hostname to \"%s\": %s", hname, strerror(errno));
subrata_modak4bb656a2009-02-26 12:02:09 +0000157 }
plars2be4eff2002-11-01 15:44:39 +0000158
Chris Dearmanec6edca2012-10-17 19:54:01 -0700159}