geniconvtbl: small cstyle fix
[unleashed.git] / kernel / syscall / systeminfo.c
blobf137a8373fd0e78bfe1901cd225f328dab922795
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 2014 Garrett D'Amore <garrett@damore.org>
24 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All rights reserved. */
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/sysmacros.h>
34 #include <sys/systm.h>
35 #include <sys/tuneable.h>
36 #include <sys/errno.h>
37 #include <sys/cred.h>
38 #include <sys/utsname.h>
39 #include <sys/systeminfo.h>
40 #include <sys/unistd.h>
41 #include <sys/debug.h>
42 #include <sys/bootconf.h>
43 #include <sys/socket.h>
44 #include <sys/policy.h>
45 #include <net/if.h>
46 #include <sys/sunddi.h>
47 #include <sys/promif.h>
48 #include <sys/zone.h>
49 #include <sys/model.h>
50 #include <netinet/inetutil.h>
52 static void get_netif_name(char *, char *);
54 long
55 systeminfo(int command, char *buf, long count)
57 int error = 0;
58 long strcnt, getcnt;
59 const char *kstr;
60 char hostidp[HW_HOSTID_LEN];
62 if (count < 0 && command != SI_SET_HOSTNAME &&
63 command != SI_SET_SRPC_DOMAIN)
64 return (set_errno(EINVAL));
67 * Deal with the common "get a string" case first.
69 switch (command) {
70 case SI_SYSNAME:
71 if (PTOU(curproc)->u_flags & U_FLAG_ALTUNAME) {
72 kstr = alt_sysname;
73 } else {
74 kstr = utsname.sysname;
76 break;
77 case SI_HOSTNAME:
78 kstr = uts_nodename();
79 break;
80 case SI_RELEASE:
81 if (PTOU(curproc)->u_flags & U_FLAG_ALTUNAME) {
82 kstr = alt_release;
83 } else {
84 kstr = utsname.release;
86 break;
87 case SI_VERSION:
88 if (PTOU(curproc)->u_flags & U_FLAG_ALTUNAME) {
89 kstr = alt_version;
90 } else {
91 kstr = utsname.version;
93 break;
94 case SI_MACHINE:
95 kstr = utsname.machine;
96 break;
97 #ifdef _LP64
98 case SI_ARCHITECTURE_64:
99 case SI_ARCHITECTURE_K:
100 kstr = architecture;
101 break;
102 case SI_ARCHITECTURE_32:
103 case SI_ARCHITECTURE:
104 kstr = architecture_32;
105 break;
106 case SI_ARCHITECTURE_NATIVE:
107 kstr = get_udatamodel() == DATAMODEL_NATIVE ?
108 architecture : architecture_32;
109 break;
110 #else
111 case SI_ARCHITECTURE_K:
112 case SI_ARCHITECTURE_32:
113 case SI_ARCHITECTURE:
114 case SI_ARCHITECTURE_NATIVE:
115 kstr = architecture;
116 break;
117 #endif
118 case SI_HW_SERIAL:
119 (void) snprintf(hostidp, sizeof (hostidp), "%u",
120 zone_get_hostid(curzone));
121 kstr = hostidp;
122 break;
123 case SI_HW_PROVIDER:
124 kstr = hw_provider;
125 break;
126 case SI_SRPC_DOMAIN:
127 kstr = curproc->p_zone->zone_domain;
128 break;
129 case SI_PLATFORM:
130 kstr = platform;
131 break;
132 case SI_ISALIST:
133 kstr = isa_list;
134 break;
135 default:
136 kstr = NULL;
137 break;
140 if (kstr != NULL) {
141 strcnt = strlen(kstr);
142 if (count > 0) {
143 if (count <= strcnt) {
144 getcnt = count - 1;
145 if (subyte(buf + getcnt, 0) < 0)
146 return (set_errno(EFAULT));
147 } else {
148 getcnt = strcnt + 1;
150 if (copyout(kstr, buf, getcnt))
151 return (set_errno(EFAULT));
153 return (strcnt + 1);
156 switch (command) {
157 case SI_DHCP_CACHE:
159 char *tmp;
160 unsigned int tlen, octlen;
162 if (dhcack == NULL) {
163 tmp = "";
164 strcnt = 0;
165 } else {
167 * If the interface didn't have a name (bindable
168 * driver) to begin with, it might have one now.
169 * So, re-run strplumb_get_netdev_path() to see
170 * if one can be established at this time.
172 if (netdev_path == NULL || netdev_path[0] == '\0') {
173 netdev_path = strplumb_get_netdev_path();
176 * If the interface name has not yet been resolved
177 * and a validnetdev_path[] was stashed by
178 * loadrootmodules in swapgeneric.c, or established
179 * above, resolve the interface name now.
181 if (dhcifname[0] == '\0' &&
182 netdev_path != NULL && netdev_path[0] != '\0') {
183 get_netif_name(netdev_path, dhcifname);
187 * Form reply:
188 * IFNAMESIZ array of dhcp i/f
189 * hexascii representation of dhcp reply
191 octlen = dhcacklen * 2 + 1;
192 tlen = octlen + IFNAMSIZ;
193 tmp = kmem_alloc(tlen, KM_SLEEP);
194 (void) strncpy(tmp, dhcifname, IFNAMSIZ);
195 if (octet_to_hexascii(dhcack, dhcacklen,
196 &tmp[IFNAMSIZ], &octlen) != 0) {
197 kmem_free(tmp, tlen);
198 error = EINVAL;
199 break;
200 } else {
201 strcnt = IFNAMSIZ + octlen;
205 if (count > 0) {
206 if (count <= strcnt) {
207 getcnt = count - 1;
208 if (subyte((buf + getcnt), 0) < 0)
209 goto fail;
210 } else {
211 getcnt = strcnt + 1;
213 if (copyout(tmp, buf, getcnt))
214 goto fail;
216 if (strcnt != 0)
217 kmem_free(tmp, tlen);
218 return (strcnt + 1);
219 fail:
220 if (strcnt != 0)
221 kmem_free(tmp, tlen);
222 error = EFAULT;
223 break;
226 case SI_SET_HOSTNAME:
228 size_t len;
229 char name[SYS_NMLN];
230 char *name_to_use;
232 if ((error = secpolicy_systeminfo(CRED())) != 0)
233 break;
235 name_to_use = uts_nodename();
236 if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0)
237 break;
240 * Must be non-NULL string and string
241 * must be less than SYS_NMLN chars.
243 if (len < 2 || (len == SYS_NMLN && name[SYS_NMLN-1] != '\0')) {
244 error = EINVAL;
245 break;
249 * Copy the name into the relevant zone's nodename.
251 (void) strcpy(name_to_use, name);
254 * Notify other interested parties that the nodename was set
256 if (name_to_use == utsname.nodename) /* global zone nodename */
257 nodename_set();
259 return (len);
262 case SI_SET_SRPC_DOMAIN:
264 char name[SYS_NMLN];
265 size_t len;
267 if ((error = secpolicy_systeminfo(CRED())) != 0)
268 break;
269 if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0)
270 break;
272 * If string passed in is longer than length
273 * allowed for domain name, fail.
275 if (len == SYS_NMLN && name[SYS_NMLN-1] != '\0') {
276 error = EINVAL;
277 break;
280 (void) strcpy(curproc->p_zone->zone_domain, name);
281 return (len);
284 default:
285 error = EINVAL;
286 break;
289 return (set_errno(error));
293 * i_path_find_node: Internal routine used by path_to_devinfo
294 * to locate a given nodeid in the device tree.
296 struct i_path_findnode {
297 pnode_t nodeid;
298 dev_info_t *dip;
301 static int
302 i_path_find_node(dev_info_t *dev, void *arg)
304 struct i_path_findnode *f = (struct i_path_findnode *)arg;
307 if (ddi_get_nodeid(dev) == (int)f->nodeid) {
308 f->dip = dev;
309 return (DDI_WALK_TERMINATE);
311 return (DDI_WALK_CONTINUE);
315 * Return the devinfo node to a boot device
317 static dev_info_t *
318 path_to_devinfo(char *path)
320 struct i_path_findnode fn;
321 extern dev_info_t *top_devinfo;
324 * Get the nodeid of the given pathname, if such a mapping exists.
326 fn.dip = NULL;
327 fn.nodeid = prom_finddevice(path);
328 if (fn.nodeid != OBP_BADNODE) {
330 * Find the nodeid in our copy of the device tree and return
331 * whatever name we used to bind this node to a driver.
333 ddi_walk_devs(top_devinfo, i_path_find_node, (void *)(&fn));
336 return (fn.dip);
340 * Determine the network interface name from the device path argument.
342 static void
343 get_netif_name(char *devname, char *ifname)
345 dev_info_t *dip;
346 major_t ndev;
347 char *name;
348 int unit;
350 dip = path_to_devinfo(devname);
351 if (dip == NULL) {
352 cmn_err(CE_WARN, "get_netif_name: "
353 "can't bind driver for '%s'\n", devname);
354 return;
357 ndev = ddi_driver_major(dip);
358 if (ndev == -1) {
359 cmn_err(CE_WARN, "get_netif_name: "
360 "no driver bound to '%s'\n", devname);
361 return;
364 name = ddi_major_to_name(ndev);
365 if (name == NULL) {
366 cmn_err(CE_WARN, "get_netif_name: "
367 "no name for major number %d\n", ndev);
368 return;
371 unit = i_ddi_devi_get_ppa(dip);
372 if (unit < 0) {
373 cmn_err(CE_WARN, "get_netif_name: "
374 "illegal unit number %d\n", unit);
375 return;
378 (void) snprintf(ifname, IFNAMSIZ, "%s%d", name, unit);