1/* 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 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 * 19 */ 20 21/********************************************************************* 22 * TEST IDENTIFIER : sethostname01 23 * 24 * EXECUTED BY : root / superuser 25 * 26 * TEST TITLE : Basic test for sethostname(2) 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 * 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. 60 * -p : Pause for SIGUSR1 before starting 61 * -P x : Pause for x seconds between iterations. 62 * -t : Turn on syscall timing. 63 * -h : Display usage information 64 * 65 * 66 ******************************************************************/ 67 68#include <string.h> 69#include <errno.h> 70#include <linux/utsname.h> 71 72#include "test.h" 73 74#define MAX_LENGTH __NEW_UTS_LEN 75 76static void setup(); 77static void cleanup(); 78 79char *TCID = "sethostname01"; 80int TST_TOTAL = 1; 81static char hname[MAX_LENGTH]; /* host name */ 82 83int main(int ac, char **av) 84{ 85 int lc; 86 87 char ltphost[] = "ltphost"; /* temporary host name to set */ 88 89 tst_parse_opts(ac, av, NULL, NULL); 90 91 /* Do initial setup. */ 92 setup(); 93 94 /* check -c option for looping. */ 95 for (lc = 0; TEST_LOOPING(lc); lc++) { 96 97 tst_count = 0; 98 99 /* Call sethostname(2) */ 100 TEST(sethostname(ltphost, sizeof(ltphost))); 101 102 /* check return code */ 103 if (TEST_RETURN == -1) { 104 tst_resm(TFAIL, "sethostname() failed , errno=%d : %s", 105 TEST_ERRNO, strerror(TEST_ERRNO)); 106 } else { 107 tst_resm(TPASS, "sethostname() returned %ld," 108 " Hostname set to \"%s\"", TEST_RETURN, 109 ltphost); 110 } 111 112 } 113 114 /* cleanup and exit */ 115 cleanup(); 116 tst_exit(); 117 118} 119 120/* 121 * setup() - performs all one time setup for this test. 122 */ 123void setup(void) 124{ 125 int ret; 126 127 tst_require_root(); 128 129 tst_sig(NOFORK, DEF_HANDLER, cleanup); 130 131 /* Store the existing hostname to retain it before exiting */ 132 if ((ret = gethostname(hname, sizeof(hname))) < 0) { 133 tst_brkm(TBROK, NULL, "gethostname() failed while getting" 134 " current host name"); 135 } 136 137 TEST_PAUSE; 138 139} 140 141/* 142 * cleanup() - performs all one time cleanup for this test 143 * completion or premature exit. 144 */ 145void cleanup(void) 146{ 147 int ret; 148 149 /* Set the host name back to original name */ 150 if ((ret = sethostname(hname, strlen(hname))) < 0) { 151 tst_resm(TWARN, "sethostname() failed while restoring" 152 " hostname to \"%s\": %s", hname, strerror(errno)); 153 } 154 155} 156