plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 33 | /* $Id: gethostid01.c,v 1.23 2009/03/23 13:35:42 subrata_modak Exp $ */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | /********************************************************** |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 35 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 36 | * OS Test - Silicon Graphics, Inc. |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 37 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 38 | * TEST IDENTIFIER : gethostid01 |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 39 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 40 | * EXECUTED BY : anyone |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 41 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | * TEST TITLE : Basic test for gethostid(2) |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 43 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | * PARENT DOCUMENT : usctpl01 |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 45 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | * TEST CASE TOTAL : 1 |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 47 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | * WALL CLOCK TIME : 1 |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 49 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | * CPU TYPES : ALL |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 51 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | * AUTHOR : William Roske |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 53 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | * CO-PILOT : Dave Fenner |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 55 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | * DATE STARTED : 03/30/92 |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 57 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | * INITIAL RELEASE : UNICOS 7.0 |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 59 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | * TEST CASES |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 61 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | * 1.) gethostid(2) returns...(See Description) |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 63 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | * INPUT SPECIFICATIONS |
| 65 | * The standard options for system call tests are accepted. |
| 66 | * (See the parse_opts(3) man page). |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 67 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | * DURATION |
| 69 | * Terminates - with frequency and infinite modes. |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 70 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 77 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | * ENVIRONMENTAL NEEDS |
| 79 | * No run-time environmental needs. |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 80 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | * SPECIAL PROCEDURAL REQUIREMENTS |
| 82 | * None |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 83 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | * INTERCASE DEPENDENCIES |
| 85 | * None |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 86 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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). |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 92 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | * Setup: |
| 94 | * Setup signal handling. |
| 95 | * Pause for SIGUSR1 if option specified. |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 96 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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. |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 103 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | * Cleanup: |
| 105 | * Print errno log and/or timing stats if options given |
plars | 3b73cf3 | 2002-12-31 16:00:18 +0000 | [diff] [blame] | 106 | * |
| 107 | * History: |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 108 | * 12/2002 Paul Larson - Added functional test to compare |
plars | 3b73cf3 | 2002-12-31 16:00:18 +0000 | [diff] [blame] | 109 | * output from hostid command and gethostid() |
mridge | 131efcc | 2006-01-31 15:50:11 +0000 | [diff] [blame] | 110 | * |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 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 |
mridge | 131efcc | 2006-01-31 15:50:11 +0000 | [diff] [blame] | 116 | * the second 64 bit check doesn't clobber the first 64 bit |
| 117 | * check |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 118 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/ |
| 120 | |
| 121 | #include <errno.h> |
| 122 | #include <string.h> |
| 123 | #include <signal.h> |
vapier | 1f01fa4 | 2006-02-22 00:19:56 +0000 | [diff] [blame] | 124 | #include <unistd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | |
| 126 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | |
plars | 3b73cf3 | 2002-12-31 16:00:18 +0000 | [diff] [blame] | 128 | #define HOSTIDLEN 40 |
mridge | 131efcc | 2006-01-31 15:50:11 +0000 | [diff] [blame] | 129 | /* Bitmasks for the 64 bit operating system checks */ |
| 130 | #define FIRST_64_CHKBIT 0x01 |
| 131 | #define SECOND_64_CHKBIT 0x02 |
plars | 3b73cf3 | 2002-12-31 16:00:18 +0000 | [diff] [blame] | 132 | |
vapier | aa35472 | 2006-05-26 06:26:37 +0000 | [diff] [blame] | 133 | void setup(); |
| 134 | void cleanup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 136 | char *TCID = "gethostid01"; |
| 137 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 139 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 141 | int lc, i, j; /* loop counters */ |
| 142 | int bit_64 = 0; /* used when compiled 64bit on some 64bit machines */ |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 143 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | 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; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 150 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 151 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 153 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 155 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 157 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | TEST(gethostid()); |
vapier | 9c9943f | 2007-07-16 04:29:02 +0000 | [diff] [blame] | 160 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 161 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 162 | tst_resm(TFAIL | TTERRNO, "gethostid failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 163 | continue; /* next loop for MTKERNEL */ |
| 164 | } |
| 165 | sprintf(hostid, "%08lx", TEST_RETURN); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 167 | 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); |
plars | 3b73cf3 | 2002-12-31 16:00:18 +0000 | [diff] [blame] | 175 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 176 | name[strlen(name) - 1] = 0; |
plars | 3b73cf3 | 2002-12-31 16:00:18 +0000 | [diff] [blame] | 177 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 178 | if (strstr(hostid, "000000")) |
| 179 | tst_resm(TCONF, "Host ID has not been set."); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 180 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 181 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 226 | tst_resm(TPASS, |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 227 | "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 Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 235 | } |
| 236 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 237 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 238 | cleanup(); |
Garrett Cooper | ebf7df2 | 2010-12-19 01:52:15 -0800 | [diff] [blame] | 239 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 240 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 241 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 242 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 243 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 244 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 245 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 246 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 247 | TEST_PAUSE; |
plars | 3b73cf3 | 2002-12-31 16:00:18 +0000 | [diff] [blame] | 248 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 249 | tst_tmpdir(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 250 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 251 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 252 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 253 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 254 | tst_rmdir(); |
plars | 3b73cf3 | 2002-12-31 16:00:18 +0000 | [diff] [blame] | 255 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 256 | } |