6069 libdisasm: instrlen arch op should have a sane default
[illumos-gate.git] / usr / src / lib / libnsctl / common / machdep.c
blobd577995ad1655524e62c85582239f679830f1c01
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
27 #include <sys/utsname.h>
28 #include <fcntl.h>
29 #include <sys/param.h>
30 #include <unistd.h>
31 #include <stdarg.h>
32 #include <errno.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
37 #include "libnsctl.h"
38 #include <nsctl.h>
39 #include <sys/ncall/ncall.h>
44 * Internal routine to fetch all the current nodes that are
45 * considered 'up'.
46 * Returns the number of ncall_info structures that are valid
47 * returned via the nodelist pointer, or -1 on an error.
48 * If the call succeeds, then the memory returned via the
49 * nodelist pointer needs to be freed by the caller.
52 static int
53 nsc_getcurrentnodes(ncall_node_t **nodelist)
55 ncall_node_t *mynodelist;
56 int size;
57 int fd;
58 int rc = -1;
59 int save_errno = 0;
60 int ioctlcmd;
62 if (nodelist == NULL) {
63 errno = EINVAL;
64 return (-1);
66 *nodelist = NULL;
67 if ((fd = open("/dev/ncall", O_RDONLY)) < 0) {
68 return (-1);
70 if ((size = ioctl(fd, NC_IOC_GETNETNODES, NULL)) < 1) {
71 size = 1;
72 ioctlcmd = NC_IOC_GETNODE;
73 } else {
74 ioctlcmd = NC_IOC_GETNETNODES;
77 mynodelist = malloc(size * sizeof (*mynodelist));
78 if (mynodelist == NULL) {
79 save_errno = ENOMEM;
80 } else {
81 rc = ioctl(fd, ioctlcmd, mynodelist);
82 if (rc < 0) {
83 save_errno = errno;
84 free(mynodelist);
85 } else {
86 /* fixup return value for single node ioctl */
87 if (ioctlcmd == NC_IOC_GETNODE)
88 rc = 1;
89 *nodelist = mynodelist;
92 close(fd);
93 errno = save_errno;
94 return (rc);
99 * return the system id (the current value in the kernel
100 * currently running).
102 * on error return -1 and set errno.
105 nsc_getsystemid(int *id)
107 ncall_node_t node;
108 int rval = 0;
109 int save_errno = 0;
110 int fd;
112 *id = 0;
114 fd = open("/dev/ncall", O_RDONLY);
115 if (fd < 0)
116 return (-1);
118 memset(&node, 0, sizeof (node));
120 rval = ioctl(fd, NC_IOC_GETNODE, &node);
121 if (rval < 0)
122 save_errno = errno;
123 else {
124 *id = node.nc_nodeid;
126 * Return 0, not the mirror node id as returned
127 * from the ioctl.
129 rval = 0;
132 close(fd);
134 errno = save_errno;
135 return (rval);
140 * Runtime Solaris release checking.
142 * Compare the build release to the runtime release to check for an
143 * acceptable match.
145 * Arguments:
146 * build_ver - the string Solaris build release (e.g. "5.8")
147 * map - optional array of nsc_release_t defining
148 * acceptable build release / runtime release
149 * matches. If supplied, must end will a NULL
150 * array element. See src/head/nsctl.h for info.
151 * reqd - used to return the required OS versions if the
152 * return value is not -1. The returned string
153 * is readonly.
155 * Returns:
156 * TRUE - acceptable match
157 * FALSE - no match (component should not continue to run)
158 * -1 - error (errno is set)
162 nsc_check_release(const char *build_rel, nsc_release_t *map, char **reqd)
164 struct utsname uts;
165 nsc_release_t *mp;
166 const char *sep = ", ";
167 char *cp, *tofree, *last;
168 int rc;
170 if (reqd)
171 *reqd = NULL;
173 if (build_rel == NULL || *build_rel == '\0') {
174 errno = EINVAL;
175 return (-1);
178 /* assume that build_rel is the required release for now */
179 if (reqd)
180 *reqd = (char *)build_rel;
182 if (uname(&uts) < 0)
183 return (-1);
185 /* build release == runtime release is always acceptable */
186 if (strcmp(build_rel, uts.release) == 0)
187 return (TRUE);
189 if (map == NULL)
190 return (FALSE);
192 rc = FALSE;
193 tofree = NULL;
195 for (mp = map; mp->build != NULL && mp->runtime != NULL; mp++) {
196 if (strcmp(mp->build, build_rel) == 0) {
198 * found an entry for this build release
199 * - search for a match in the runtime releases
202 /* reset reqd to this entry */
203 if (reqd)
204 *reqd = (char *)mp->runtime;
207 * operate on a copy of the string since strtok
208 * is destructive.
210 tofree = cp = strdup(mp->runtime);
211 if (cp == NULL) {
212 errno = ENOMEM;
213 rc = -1;
214 break;
217 cp = strtok_r(cp, sep, &last);
218 while (cp != NULL) {
219 if (strcmp(cp, uts.release) == 0) {
220 rc = TRUE;
221 break;
224 cp = strtok_r(NULL, sep, &last);
227 break;
231 if (tofree)
232 free(tofree);
234 return (rc);
239 * return the system id corresponding to name
241 * on error return -1 and set errno.
244 nsc_name_to_id(char *name, int *id)
246 ncall_node_t *nodes;
247 int rval = 0;
248 int nodecnt;
249 int slot;
251 *id = 0;
253 nodecnt = nsc_getcurrentnodes(&nodes);
254 if (nodecnt < 0) {
255 rval = -1;
256 } else {
257 for (slot = 0; slot < nodecnt; slot++) {
258 if (strcmp(name, nodes[slot].nc_nodename) == 0) {
259 *id = nodes[slot].nc_nodeid;
260 break;
263 if (slot >= nodecnt) {
264 errno = ENOENT;
265 rval = -1;
267 free(nodes);
269 return (rval);
273 * return the node name corresponding to system id
275 * on error return -1 and set errno.
276 * The returned string has been strdup() and needs
277 * to be freed by the caller.
280 nsc_id_to_name(char **name, int id)
282 ncall_node_t *nodes;
283 int rval = 0;
284 int nodecnt;
285 int slot;
286 char *foundname;
288 *name = 0;
289 foundname = NULL;
291 nodecnt = nsc_getcurrentnodes(&nodes);
292 if (nodecnt < 0) {
293 rval = -1;
294 } else {
295 for (slot = 0; slot < nodecnt; slot++) {
296 if (nodes[slot].nc_nodeid == id) {
297 foundname = strdup(nodes[slot].nc_nodename);
298 if (foundname) {
299 *name = foundname;
300 } else {
301 errno = ENOMEM;
302 rval = -1;
304 break;
307 if (slot >= nodecnt) {
308 errno = ENOENT;
309 rval = -1;
311 free(nodes);
313 return (rval);