blob: d8945059b1e6cf3a2742391e37d302de85eb5e11 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars865695b2001-08-27 22:15:12 +000022 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 *
32 */
subrata_modak56207ce2009-03-23 13:35:39 +000033/* $Id: gethostid01.c,v 1.23 2009/03/23 13:35:42 subrata_modak Exp $ */
plars865695b2001-08-27 22:15:12 +000034/**********************************************************
vapier9c9943f2007-07-16 04:29:02 +000035 *
plars865695b2001-08-27 22:15:12 +000036 * OS Test - Silicon Graphics, Inc.
vapier9c9943f2007-07-16 04:29:02 +000037 *
plars865695b2001-08-27 22:15:12 +000038 * TEST IDENTIFIER : gethostid01
vapier9c9943f2007-07-16 04:29:02 +000039 *
plars865695b2001-08-27 22:15:12 +000040 * EXECUTED BY : anyone
vapier9c9943f2007-07-16 04:29:02 +000041 *
plars865695b2001-08-27 22:15:12 +000042 * TEST TITLE : Basic test for gethostid(2)
vapier9c9943f2007-07-16 04:29:02 +000043 *
plars865695b2001-08-27 22:15:12 +000044 * PARENT DOCUMENT : usctpl01
vapier9c9943f2007-07-16 04:29:02 +000045 *
plars865695b2001-08-27 22:15:12 +000046 * TEST CASE TOTAL : 1
vapier9c9943f2007-07-16 04:29:02 +000047 *
plars865695b2001-08-27 22:15:12 +000048 * WALL CLOCK TIME : 1
vapier9c9943f2007-07-16 04:29:02 +000049 *
plars865695b2001-08-27 22:15:12 +000050 * CPU TYPES : ALL
vapier9c9943f2007-07-16 04:29:02 +000051 *
plars865695b2001-08-27 22:15:12 +000052 * AUTHOR : William Roske
vapier9c9943f2007-07-16 04:29:02 +000053 *
plars865695b2001-08-27 22:15:12 +000054 * CO-PILOT : Dave Fenner
vapier9c9943f2007-07-16 04:29:02 +000055 *
plars865695b2001-08-27 22:15:12 +000056 * DATE STARTED : 03/30/92
vapier9c9943f2007-07-16 04:29:02 +000057 *
plars865695b2001-08-27 22:15:12 +000058 * INITIAL RELEASE : UNICOS 7.0
vapier9c9943f2007-07-16 04:29:02 +000059 *
plars865695b2001-08-27 22:15:12 +000060 * TEST CASES
vapier9c9943f2007-07-16 04:29:02 +000061 *
plars865695b2001-08-27 22:15:12 +000062 * 1.) gethostid(2) returns...(See Description)
vapier9c9943f2007-07-16 04:29:02 +000063 *
plars865695b2001-08-27 22:15:12 +000064 * INPUT SPECIFICATIONS
65 * The standard options for system call tests are accepted.
66 * (See the parse_opts(3) man page).
vapier9c9943f2007-07-16 04:29:02 +000067 *
plars865695b2001-08-27 22:15:12 +000068 * DURATION
69 * Terminates - with frequency and infinite modes.
vapier9c9943f2007-07-16 04:29:02 +000070 *
plars865695b2001-08-27 22:15:12 +000071 * SIGNALS
72 * Uses SIGUSR1 to pause before test if option set.
73 * (See the parse_opts(3) man page).
74 *
75 * RESOURCES
76 * None
vapier9c9943f2007-07-16 04:29:02 +000077 *
plars865695b2001-08-27 22:15:12 +000078 * ENVIRONMENTAL NEEDS
79 * No run-time environmental needs.
vapier9c9943f2007-07-16 04:29:02 +000080 *
plars865695b2001-08-27 22:15:12 +000081 * SPECIAL PROCEDURAL REQUIREMENTS
82 * None
vapier9c9943f2007-07-16 04:29:02 +000083 *
plars865695b2001-08-27 22:15:12 +000084 * INTERCASE DEPENDENCIES
85 * None
vapier9c9943f2007-07-16 04:29:02 +000086 *
plars865695b2001-08-27 22:15:12 +000087 * DETAILED DESCRIPTION
88 * This is a Phase I test for the gethostid(2) system call. It is intended
89 * to provide a limited exposure of the system call, for now. It
90 * should/will be extended when full functional tests are written for
91 * gethostid(2).
vapier9c9943f2007-07-16 04:29:02 +000092 *
plars865695b2001-08-27 22:15:12 +000093 * Setup:
94 * Setup signal handling.
95 * Pause for SIGUSR1 if option specified.
vapier9c9943f2007-07-16 04:29:02 +000096 *
plars865695b2001-08-27 22:15:12 +000097 * Test:
98 * Loop if the proper options are given.
99 * Execute system call
100 * Check return code, if system call failed (return=-1)
101 * Log the errno and Issue a FAIL message.
102 * Otherwise, Issue a PASS message.
vapier9c9943f2007-07-16 04:29:02 +0000103 *
plars865695b2001-08-27 22:15:12 +0000104 * Cleanup:
105 * Print errno log and/or timing stats if options given
plars3b73cf32002-12-31 16:00:18 +0000106 *
107 * History:
vapier9c9943f2007-07-16 04:29:02 +0000108 * 12/2002 Paul Larson - Added functional test to compare
plars3b73cf32002-12-31 16:00:18 +0000109 * output from hostid command and gethostid()
mridge131efcc2006-01-31 15:50:11 +0000110 *
vapier9c9943f2007-07-16 04:29:02 +0000111 * 01/2003 Robbie Williamson - Added code to handle
112 * distros that add "0x" to beginning of `hostid`
113 * output.
114 *
115 * 01/31/2006 Marty Ridgeway - Corrected 64 bit check so
mridge131efcc2006-01-31 15:50:11 +0000116 * the second 64 bit check doesn't clobber the first 64 bit
117 * check
vapier9c9943f2007-07-16 04:29:02 +0000118 *
plars865695b2001-08-27 22:15:12 +0000119 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
120
121#include <errno.h>
122#include <string.h>
123#include <signal.h>
vapier1f01fa42006-02-22 00:19:56 +0000124#include <unistd.h>
plars865695b2001-08-27 22:15:12 +0000125
126#include "test.h"
plars865695b2001-08-27 22:15:12 +0000127
plars3b73cf32002-12-31 16:00:18 +0000128#define HOSTIDLEN 40
mridge131efcc2006-01-31 15:50:11 +0000129/* Bitmasks for the 64 bit operating system checks */
130#define FIRST_64_CHKBIT 0x01
131#define SECOND_64_CHKBIT 0x02
plars3b73cf32002-12-31 16:00:18 +0000132
vapieraa354722006-05-26 06:26:37 +0000133void setup();
134void cleanup();
plars865695b2001-08-27 22:15:12 +0000135
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200136char *TCID = "gethostid01";
137int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +0000138
subrata_modak56207ce2009-03-23 13:35:39 +0000139int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000140{
subrata_modak56207ce2009-03-23 13:35:39 +0000141 int lc, i, j; /* loop counters */
142 int bit_64 = 0; /* used when compiled 64bit on some 64bit machines */
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200143 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000144 char *result;
145 char name[HOSTIDLEN], name2[HOSTIDLEN], hostid[HOSTIDLEN],
146 hostid2[HOSTIDLEN], *hostid3, hex[2] = "0x";
147 char hex_64[8] = "ffffffff";
148 FILE *fp;
plars865695b2001-08-27 22:15:12 +0000149
Garrett Cooper45e285d2010-11-22 12:19:25 -0800150 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800151 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000152
subrata_modak56207ce2009-03-23 13:35:39 +0000153 setup();
plars865695b2001-08-27 22:15:12 +0000154
subrata_modak56207ce2009-03-23 13:35:39 +0000155 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars865695b2001-08-27 22:15:12 +0000156
Caspar Zhangd59a6592013-03-07 14:59:12 +0800157 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000158
subrata_modak56207ce2009-03-23 13:35:39 +0000159 TEST(gethostid());
vapier9c9943f2007-07-16 04:29:02 +0000160
subrata_modak56207ce2009-03-23 13:35:39 +0000161 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800162 tst_resm(TFAIL | TTERRNO, "gethostid failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000163 continue; /* next loop for MTKERNEL */
164 }
165 sprintf(hostid, "%08lx", TEST_RETURN);
plars865695b2001-08-27 22:15:12 +0000166
Cyril Hrubise38b9612014-06-02 17:20:57 +0200167 if (system("hostid > hostid.x") == -1)
168 tst_brkm(TFAIL, cleanup,
169 "system() returned errno %d", errno);
170 if ((fp = fopen("hostid.x", "r")) == NULL)
171 tst_brkm(TFAIL, cleanup, "fopen failed");
172 if (fgets(name, HOSTIDLEN, fp) == NULL)
173 tst_brkm(TFAIL, cleanup, "fgets failed");
174 fclose(fp);
plars3b73cf32002-12-31 16:00:18 +0000175
Cyril Hrubise38b9612014-06-02 17:20:57 +0200176 name[strlen(name) - 1] = 0;
plars3b73cf32002-12-31 16:00:18 +0000177
Cyril Hrubise38b9612014-06-02 17:20:57 +0200178 if (strstr(hostid, "000000"))
179 tst_resm(TCONF, "Host ID has not been set.");
Garrett Cooper2c282152010-12-16 00:55:50 -0800180
Cyril Hrubise38b9612014-06-02 17:20:57 +0200181 if (strcmp(name, hostid) == 0) {
182 tst_resm(TPASS,
183 "Hostid command and gethostid both report "
184 "hostid is %s", hostid);
185 } else {
186
187 /*
188 * Some distros add an "0x" to the front of the
189 * `hostid` output. We compare the first 2
190 * characters of the `hostid` output with "0x",
191 * if it's equal, remove these first 2
192 * characters & re-test. -RW
193 */
194 if (name[0] == hex[0] && name[1] == hex[1])
195 for (i = 0; i < 38; i++)
196 name2[i] = name[i + 2];
197 else
198 strncpy(name2, name, HOSTIDLEN);
199
200 /*
201 * This code handles situations where ffffffff
202 * is appended. Fixed to not clobber the first
203 * check with the 2nd check MR
204 */
205
206 if (0 == strncmp(hostid, hex_64, 8))
207 bit_64 |= FIRST_64_CHKBIT;
208
209 if (0 == strncmp(name2, hex_64, 8))
210 bit_64 |= SECOND_64_CHKBIT;
211
212 if (bit_64 & FIRST_64_CHKBIT)
213 for (j = 0; j < 8; j++)
214 hostid2[j] = hostid[j + 8];
215 else
216 strncpy(hostid2, hostid,
217 strlen(hostid) + 1);
218
219 if (bit_64 & SECOND_64_CHKBIT)
220 for (j = 0; j < 9; j++)
221 name2[j] = name2[j + 8];
222
223 if ((result = strstr(hostid2, name2)) != NULL) {
224 hostid3 = strdup(name2);
225
subrata_modak56207ce2009-03-23 13:35:39 +0000226 tst_resm(TPASS,
Cyril Hrubise38b9612014-06-02 17:20:57 +0200227 "Hostid command reports "
228 "hostid is %s, and gethostid "
229 "reports %s", name2, hostid3);
230 } else
231 tst_resm(TFAIL,
232 "Hostid command reports "
233 "hostid is %s, but gethostid "
234 "reports %s", name2, hostid2);
Garrett Cooper2c282152010-12-16 00:55:50 -0800235 }
236 }
plars865695b2001-08-27 22:15:12 +0000237
subrata_modak56207ce2009-03-23 13:35:39 +0000238 cleanup();
Garrett Cooperebf7df22010-12-19 01:52:15 -0800239 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800240}
plars865695b2001-08-27 22:15:12 +0000241
Mike Frysingerc57fba52014-04-09 18:56:30 -0400242void setup(void)
plars865695b2001-08-27 22:15:12 +0000243{
Garrett Cooper2c282152010-12-16 00:55:50 -0800244
subrata_modak56207ce2009-03-23 13:35:39 +0000245 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000246
subrata_modak56207ce2009-03-23 13:35:39 +0000247 TEST_PAUSE;
plars3b73cf32002-12-31 16:00:18 +0000248
subrata_modak56207ce2009-03-23 13:35:39 +0000249 tst_tmpdir();
Garrett Cooper2c282152010-12-16 00:55:50 -0800250}
plars865695b2001-08-27 22:15:12 +0000251
Mike Frysingerc57fba52014-04-09 18:56:30 -0400252void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000253{
subrata_modak56207ce2009-03-23 13:35:39 +0000254 tst_rmdir();
plars3b73cf32002-12-31 16:00:18 +0000255
Chris Dearmanec6edca2012-10-17 19:54:01 -0700256}