summaryrefslogtreecommitdiff
path: root/usr/src/cmd/machid/machid.c
blob: c3d57b91f93fba0287a7f25dfd3108d304a72383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1993-1994, by Sun Microsystems, Inc.
 */

/*
 * This program replicates the function of the links from a machine name
 * (such as sun4c) through /usr/kvm to true or false as appropriate.  It
 * knows the correct special cases.
 *
 * IMPORTANT NOTE:
 *
 * Do not modify this program to know about additional special cases or
 * reflect new platforms or instruction set architectures.  This is a
 * deprecated interface and strictly for backwards compatibility.  This
 * is psarc/1992/171.  Note the following excerpt from the opinion:
 *
 *    It is most important to note that the manual page states in
 *    the NOTES section:  "The machid family of commands is
 *    obsolete.  Use uname -p and uname -m instead."
 *
 *    The intent of Kernel Architecture Project team is to provide
 *    only enough functionality to mimic the existing definitions
 *    on the SPARC and Intel x86 versions of Solaris 2.x.  No new
 *    identifiers will ever be added to the documented and
 *    undocumented identifiers listed above.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <sys/systeminfo.h>

static char	static_buf[SYS_NMLN];
static char	*progname;

static void get_info_item(int command, char **buf, long *count);

/* ARGSUSED */
int
main(int argc, char *argv[], char *envp[])
{
	char	*buf = &static_buf[0];
	long	buflen = SYS_NMLN;

	if ((progname = strrchr(argv[0], '/')) == NULL)
		progname = argv[0];
	else
		progname++;

	/*
	 * First possible match is on the processor type.
	 *
	 * Special case for architectures: i386 matches i486 and visa versa.
	 */
	get_info_item(SI_ARCHITECTURE, &buf, &buflen);
	if (strcmp(buf, progname) == 0)
		return (0);
	if ((strcmp(buf, "i386") == 0 && strcmp(progname, "i486") == 0) ||
	    (strcmp(buf, "i486") == 0 && strcmp(progname, "i386") == 0))
		return (0);

	/*
	 * Next possible match is the machine, or more exactly, the value
	 * which would be returned by uname(2) in the machine field or uname(1)
	 * with the -m option.  For historical reasons this is really is
	 * often a class of platforms which are identical to userland processes
	 * such as sun4c, sun4m, etc.
	 */
	get_info_item(SI_MACHINE, &buf, &buflen);
	if (strcmp(buf, progname) == 0)
		return (0);

	/*
	 * Finally, match the vendor.  We hardwire in one historical match.
	 */
	get_info_item(SI_HW_PROVIDER, &buf, &buflen);
	if (strcmp(buf, progname) == 0)
		return (0);
	if (strcasecmp(buf, "Sun_Microsystems") == 0 &&
	    strcmp("sun", progname) == 0)
		return (0);

	return (255);
}

/*
 * get_info_item is a wrapper around the sysinfo system call. It makes sure
 * the buffer is large enough, returning a larger buffer if needed.  On
 * unrecoverable error, it exits.  An error message doesn't help and makes
 * this tiny program link stdio and maybe deal with internationalization,
 * so the best thing is to die silently.  Note that the larger buffer is
 * retained for later use.  Reality is that the buffer will always be big
 * enough, but this is coded to the spec rather than implementation.
 */
static void
get_info_item(int command, char **buf, long *count)
{
	long	error;

	error = sysinfo(command, *buf, *count);
	if (error > *count) {
		*count = error;
		if (*buf != static_buf) {
			free(*buf);
		}
		*buf = (char *) malloc(*count);
		error = sysinfo(command, *buf, *count);
	}

	if (error == -1)
		exit(-1);
}