plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 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 |
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 | /********************************************************************* |
| 22 | * TEST IDENTIFIER : sethostname01 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 23 | * |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 24 | * EXECUTED BY : root / superuser |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 25 | * |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 26 | * TEST TITLE : Basic test for sethostname(2) |
| 27 | * |
| 28 | * TEST CASE TOTAL : 1 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 29 | * |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 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 | * |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 36 | * DESCRIPTION$ |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 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. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 60 | * -p : Pause for SIGUSR1 before starting |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 61 | * -P x : Pause for x seconds between iterations. |
| 62 | * -t : Turn on syscall timing. |
| 63 | * -h : Display usage information |
| 64 | * |
| 65 | * |
| 66 | ******************************************************************/ |
| 67 | |
mridge | 4cce625 | 2004-09-13 21:47:38 +0000 | [diff] [blame] | 68 | #include <string.h> |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 69 | #include <errno.h> |
| 70 | #include <linux/utsname.h> |
| 71 | |
| 72 | #include "test.h" |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 73 | |
| 74 | #define MAX_LENGTH __NEW_UTS_LEN |
| 75 | |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 76 | static void setup(); |
| 77 | static void cleanup(); |
| 78 | |
| 79 | char *TCID = "sethostname01"; |
| 80 | int TST_TOTAL = 1; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | static char hname[MAX_LENGTH]; /* host name */ |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 82 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | int main(int ac, char **av) |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 84 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 85 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 86 | const char *msg; |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 87 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | char ltphost[] = "ltphost"; /* temporary host name to set */ |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 89 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 90 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 91 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 92 | |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* Do initial setup. */ |
| 96 | setup(); |
| 97 | |
| 98 | /* check -c option for looping. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 100 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 101 | tst_count = 0; |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 102 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | /* Call sethostname(2) */ |
| 104 | TEST(sethostname(ltphost, sizeof(ltphost))); |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 105 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | /* 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_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 111 | tst_resm(TPASS, "sethostname() returned %ld," |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | " Hostname set to \"%s\"", TEST_RETURN, |
| 113 | ltphost); |
| 114 | } |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 115 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 116 | } |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 117 | |
| 118 | /* cleanup and exit */ |
| 119 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 120 | tst_exit(); |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 121 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 122 | } |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 123 | |
| 124 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 125 | * setup() - performs all one time setup for this test. |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 126 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 127 | void setup(void) |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 128 | { |
| 129 | int ret; |
| 130 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 131 | tst_require_root(NULL); |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 132 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 133 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 134 | |
| 135 | /* Store the existing hostname to retain it before exiting */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 136 | if ((ret = gethostname(hname, sizeof(hname))) < 0) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 137 | tst_brkm(TBROK, NULL, "gethostname() failed while getting" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 138 | " current host name"); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 139 | } |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 140 | |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 141 | TEST_PAUSE; |
| 142 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 143 | } |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 144 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 145 | /* |
| 146 | * cleanup() - performs all one time cleanup for this test |
| 147 | * completion or premature exit. |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 148 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 149 | void cleanup(void) |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 150 | { |
| 151 | int ret; |
| 152 | |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 153 | /* Set the host name back to original name */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | if ((ret = sethostname(hname, strlen(hname))) < 0) { |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 155 | tst_resm(TWARN, "sethostname() failed while restoring" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 156 | " hostname to \"%s\": %s", hname, strerror(errno)); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 157 | } |
plars | 2be4eff | 2002-11-01 15:44:39 +0000 | [diff] [blame] | 158 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 159 | } |