dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / kernel / syscall / systeminfo.c
blob91c88bbba4aec85708e38fb4f7b0b93521f63e9f
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 kstr = utsname.sysname;
72 break;
73 case SI_HOSTNAME:
74 kstr = uts_nodename();
75 break;
76 case SI_RELEASE:
77 kstr = utsname.release;
78 break;
79 case SI_VERSION:
80 kstr = utsname.version;
81 break;
82 case SI_MACHINE:
83 kstr = utsname.machine;
84 break;
85 #ifdef _LP64
86 case SI_ARCHITECTURE_64:
87 case SI_ARCHITECTURE_K:
88 kstr = architecture;
89 break;
90 case SI_ARCHITECTURE_32:
91 case SI_ARCHITECTURE:
92 kstr = architecture_32;
93 break;
94 case SI_ARCHITECTURE_NATIVE:
95 kstr = get_udatamodel() == DATAMODEL_NATIVE ?
96 architecture : architecture_32;
97 break;
98 #else
99 case SI_ARCHITECTURE_K:
100 case SI_ARCHITECTURE_32:
101 case SI_ARCHITECTURE:
102 case SI_ARCHITECTURE_NATIVE:
103 kstr = architecture;
104 break;
105 #endif
106 case SI_HW_SERIAL:
107 (void) snprintf(hostidp, sizeof (hostidp), "%u",
108 zone_get_hostid(curzone));
109 kstr = hostidp;
110 break;
111 case SI_HW_PROVIDER:
112 kstr = hw_provider;
113 break;
114 case SI_SRPC_DOMAIN:
115 kstr = curproc->p_zone->zone_domain;
116 break;
117 case SI_PLATFORM:
118 kstr = platform;
119 break;
120 case SI_ISALIST:
121 kstr = isa_list;
122 break;
123 default:
124 kstr = NULL;
125 break;
128 if (kstr != NULL) {
129 strcnt = strlen(kstr);
130 if (count > 0) {
131 if (count <= strcnt) {
132 getcnt = count - 1;
133 if (subyte(buf + getcnt, 0) < 0)
134 return (set_errno(EFAULT));
135 } else {
136 getcnt = strcnt + 1;
138 if (copyout(kstr, buf, getcnt))
139 return (set_errno(EFAULT));
141 return (strcnt + 1);
144 switch (command) {
145 case SI_DHCP_CACHE:
147 char *tmp;
148 unsigned int tlen, octlen;
150 if (dhcack == NULL) {
151 tmp = "";
152 strcnt = 0;
153 } else {
155 * If the interface didn't have a name (bindable
156 * driver) to begin with, it might have one now.
157 * So, re-run strplumb_get_netdev_path() to see
158 * if one can be established at this time.
160 if (netdev_path == NULL || netdev_path[0] == '\0') {
161 netdev_path = strplumb_get_netdev_path();
164 * If the interface name has not yet been resolved
165 * and a validnetdev_path[] was stashed by
166 * loadrootmodules in swapgeneric.c, or established
167 * above, resolve the interface name now.
169 if (dhcifname[0] == '\0' &&
170 netdev_path != NULL && netdev_path[0] != '\0') {
171 get_netif_name(netdev_path, dhcifname);
175 * Form reply:
176 * IFNAMESIZ array of dhcp i/f
177 * hexascii representation of dhcp reply
179 octlen = dhcacklen * 2 + 1;
180 tlen = octlen + IFNAMSIZ;
181 tmp = kmem_alloc(tlen, KM_SLEEP);
182 (void) strncpy(tmp, dhcifname, IFNAMSIZ);
183 if (octet_to_hexascii(dhcack, dhcacklen,
184 &tmp[IFNAMSIZ], &octlen) != 0) {
185 kmem_free(tmp, tlen);
186 error = EINVAL;
187 break;
188 } else {
189 strcnt = IFNAMSIZ + octlen;
193 if (count > 0) {
194 if (count <= strcnt) {
195 getcnt = count - 1;
196 if (subyte((buf + getcnt), 0) < 0)
197 goto fail;
198 } else {
199 getcnt = strcnt + 1;
201 if (copyout(tmp, buf, getcnt))
202 goto fail;
204 if (strcnt != 0)
205 kmem_free(tmp, tlen);
206 return (strcnt + 1);
207 fail:
208 if (strcnt != 0)
209 kmem_free(tmp, tlen);
210 error = EFAULT;
211 break;
214 case SI_SET_HOSTNAME:
216 size_t len;
217 char name[SYS_NMLN];
218 char *name_to_use;
220 if ((error = secpolicy_systeminfo(CRED())) != 0)
221 break;
223 name_to_use = uts_nodename();
224 if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0)
225 break;
228 * Must be non-NULL string and string
229 * must be less than SYS_NMLN chars.
231 if (len < 2 || (len == SYS_NMLN && name[SYS_NMLN-1] != '\0')) {
232 error = EINVAL;
233 break;
237 * Copy the name into the relevant zone's nodename.
239 (void) strcpy(name_to_use, name);
242 * Notify other interested parties that the nodename was set
244 if (name_to_use == utsname.nodename) /* global zone nodename */
245 nodename_set();
247 return (len);
250 case SI_SET_SRPC_DOMAIN:
252 char name[SYS_NMLN];
253 size_t len;
255 if ((error = secpolicy_systeminfo(CRED())) != 0)
256 break;
257 if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0)
258 break;
260 * If string passed in is longer than length
261 * allowed for domain name, fail.
263 if (len == SYS_NMLN && name[SYS_NMLN-1] != '\0') {
264 error = EINVAL;
265 break;
268 (void) strcpy(curproc->p_zone->zone_domain, name);
269 return (len);
272 default:
273 error = EINVAL;
274 break;
277 return (set_errno(error));
281 * i_path_find_node: Internal routine used by path_to_devinfo
282 * to locate a given nodeid in the device tree.
284 struct i_path_findnode {
285 pnode_t nodeid;
286 dev_info_t *dip;
289 static int
290 i_path_find_node(dev_info_t *dev, void *arg)
292 struct i_path_findnode *f = (struct i_path_findnode *)arg;
295 if (ddi_get_nodeid(dev) == (int)f->nodeid) {
296 f->dip = dev;
297 return (DDI_WALK_TERMINATE);
299 return (DDI_WALK_CONTINUE);
303 * Return the devinfo node to a boot device
305 static dev_info_t *
306 path_to_devinfo(char *path)
308 struct i_path_findnode fn;
309 extern dev_info_t *top_devinfo;
312 * Get the nodeid of the given pathname, if such a mapping exists.
314 fn.dip = NULL;
315 fn.nodeid = prom_finddevice(path);
316 if (fn.nodeid != OBP_BADNODE) {
318 * Find the nodeid in our copy of the device tree and return
319 * whatever name we used to bind this node to a driver.
321 ddi_walk_devs(top_devinfo, i_path_find_node, (void *)(&fn));
324 return (fn.dip);
328 * Determine the network interface name from the device path argument.
330 static void
331 get_netif_name(char *devname, char *ifname)
333 dev_info_t *dip;
334 major_t ndev;
335 char *name;
336 int unit;
338 dip = path_to_devinfo(devname);
339 if (dip == NULL) {
340 cmn_err(CE_WARN, "get_netif_name: "
341 "can't bind driver for '%s'\n", devname);
342 return;
345 ndev = ddi_driver_major(dip);
346 if (ndev == -1) {
347 cmn_err(CE_WARN, "get_netif_name: "
348 "no driver bound to '%s'\n", devname);
349 return;
352 name = ddi_major_to_name(ndev);
353 if (name == NULL) {
354 cmn_err(CE_WARN, "get_netif_name: "
355 "no name for major number %d\n", ndev);
356 return;
359 unit = i_ddi_devi_get_ppa(dip);
360 if (unit < 0) {
361 cmn_err(CE_WARN, "get_netif_name: "
362 "illegal unit number %d\n", unit);
363 return;
366 (void) snprintf(ifname, IFNAMSIZ, "%s%d", name, unit);