1/*
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
20 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
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 */
33/* $Id: gethostid01.c,v 1.23 2009/03/23 13:35:42 subrata_modak Exp $ */
34/**********************************************************
35 *
36 *    OS Test - Silicon Graphics, Inc.
37 *
38 *    TEST IDENTIFIER	: gethostid01
39 *
40 *    EXECUTED BY	: anyone
41 *
42 *    TEST TITLE	: Basic test for gethostid(2)
43 *
44 *    PARENT DOCUMENT	: usctpl01
45 *
46 *    TEST CASE TOTAL	: 1
47 *
48 *    WALL CLOCK TIME	: 1
49 *
50 *    CPU TYPES		: ALL
51 *
52 *    AUTHOR		: William Roske
53 *
54 *    CO-PILOT		: Dave Fenner
55 *
56 *    DATE STARTED	: 03/30/92
57 *
58 *    INITIAL RELEASE	: UNICOS 7.0
59 *
60 *    TEST CASES
61 *
62 * 	1.) gethostid(2) returns...(See Description)
63 *
64 *    INPUT SPECIFICATIONS
65 * 	The standard options for system call tests are accepted.
66 *	(See the parse_opts(3) man page).
67 *
68 *    DURATION
69 * 	Terminates - with frequency and infinite modes.
70 *
71 *    SIGNALS
72 * 	Uses SIGUSR1 to pause before test if option set.
73 * 	(See the parse_opts(3) man page).
74 *
75 *    RESOURCES
76 * 	None
77 *
78 *    ENVIRONMENTAL NEEDS
79 *      No run-time environmental needs.
80 *
81 *    SPECIAL PROCEDURAL REQUIREMENTS
82 * 	None
83 *
84 *    INTERCASE DEPENDENCIES
85 * 	None
86 *
87 *    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).
92 *
93 * 	Setup:
94 * 	  Setup signal handling.
95 *	  Pause for SIGUSR1 if option specified.
96 *
97 * 	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.
103 *
104 * 	Cleanup:
105 * 	  Print errno log and/or timing stats if options given
106 *
107 * 	History:
108 * 	  12/2002 Paul Larson - Added functional test to compare
109 * 	  	output from hostid command and gethostid()
110 *
111 *        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
116 *              the second 64 bit check doesn't clobber the first 64 bit
117 *              check
118 *
119 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
120
121#include <errno.h>
122#include <string.h>
123#include <signal.h>
124#include <unistd.h>
125
126#include "test.h"
127
128#define HOSTIDLEN 40
129/* Bitmasks for the 64 bit operating system checks */
130#define FIRST_64_CHKBIT  0x01
131#define SECOND_64_CHKBIT 0x02
132
133void setup();
134void cleanup();
135
136char *TCID = "gethostid01";
137int TST_TOTAL = 1;
138
139int main(int ac, char **av)
140{
141	int lc, i, j;		/* loop counters */
142	int bit_64 = 0;
143	char *result;
144	char name[HOSTIDLEN], name2[HOSTIDLEN], hostid[HOSTIDLEN],
145	    hostid2[HOSTIDLEN], *hostid3, hex[2] = "0x";
146	char hex_64[8] = "ffffffff";
147	FILE *fp;
148
149	tst_parse_opts(ac, av, NULL, NULL);
150
151	setup();
152
153	for (lc = 0; TEST_LOOPING(lc); lc++) {
154
155		tst_count = 0;
156
157		TEST(gethostid());
158
159		if (TEST_RETURN == -1) {
160			tst_resm(TFAIL | TTERRNO, "gethostid failed");
161			continue;	/* next loop for MTKERNEL */
162		}
163		sprintf(hostid, "%08lx", TEST_RETURN);
164
165		if (system("hostid > hostid.x") == -1)
166			tst_brkm(TFAIL, cleanup,
167				 "system() returned errno %d", errno);
168		if ((fp = fopen("hostid.x", "r")) == NULL)
169			tst_brkm(TFAIL, cleanup, "fopen failed");
170		if (fgets(name, HOSTIDLEN, fp) == NULL)
171			tst_brkm(TFAIL, cleanup, "fgets failed");
172		fclose(fp);
173
174		name[strlen(name) - 1] = 0;
175
176		if (strstr(hostid, "000000"))
177			tst_resm(TCONF, "Host ID has not been set.");
178
179		if (strcmp(name, hostid) == 0) {
180			tst_resm(TPASS,
181				 "Hostid command and gethostid both report "
182				 "hostid is %s", hostid);
183		} else {
184
185			/*
186			 * Some distros add an "0x" to the front of the
187			 * `hostid` output. We compare the first 2
188			 * characters of the `hostid` output with "0x",
189			 * if it's equal, remove these first 2
190			 * characters & re-test. -RW
191			 */
192			if (name[0] == hex[0] && name[1] == hex[1])
193				for (i = 0; i < 38; i++)
194					name2[i] = name[i + 2];
195			else
196				strncpy(name2, name, HOSTIDLEN);
197
198			/*
199			 * This code handles situations where ffffffff
200			 * is appended. Fixed to not clobber the first
201			 * check with the 2nd check MR
202			 */
203
204			if (0 == strncmp(hostid, hex_64, 8))
205				bit_64 |= FIRST_64_CHKBIT;
206
207			if (0 == strncmp(name2, hex_64, 8))
208				bit_64 |= SECOND_64_CHKBIT;
209
210			if (bit_64 & FIRST_64_CHKBIT)
211				for (j = 0; j < 8; j++)
212					hostid2[j] = hostid[j + 8];
213			else
214				strncpy(hostid2, hostid,
215					strlen(hostid) + 1);
216
217			if (bit_64 & SECOND_64_CHKBIT)
218				for (j = 0; j < 9; j++)
219					name2[j] = name2[j + 8];
220
221			if ((result = strstr(hostid2, name2)) != NULL) {
222				hostid3 = strdup(name2);
223
224				tst_resm(TPASS,
225					 "Hostid command reports "
226					 "hostid is %s, and gethostid "
227					 "reports %s", name2, hostid3);
228			} else
229				tst_resm(TFAIL,
230					 "Hostid command reports "
231					 "hostid is %s, but gethostid "
232					 "reports %s", name2, hostid2);
233		}
234	}
235
236	cleanup();
237	tst_exit();
238}
239
240void setup(void)
241{
242
243	tst_sig(FORK, DEF_HANDLER, cleanup);
244
245	TEST_PAUSE;
246
247	tst_tmpdir();
248}
249
250void cleanup(void)
251{
252	tst_rmdir();
253
254}
255