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]
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>
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>
46 #include <sys/sunddi.h>
47 #include <sys/promif.h>
49 #include <sys/model.h>
50 #include <netinet/inetutil.h>
52 static void get_netif_name(char *, char *);
55 systeminfo(int command
, char *buf
, long count
)
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.
71 kstr
= utsname
.sysname
;
74 kstr
= uts_nodename();
77 kstr
= utsname
.release
;
80 kstr
= utsname
.version
;
83 kstr
= utsname
.machine
;
86 case SI_ARCHITECTURE_64
:
87 case SI_ARCHITECTURE_K
:
90 case SI_ARCHITECTURE_32
:
92 kstr
= architecture_32
;
94 case SI_ARCHITECTURE_NATIVE
:
95 kstr
= get_udatamodel() == DATAMODEL_NATIVE
?
96 architecture
: architecture_32
;
99 case SI_ARCHITECTURE_K
:
100 case SI_ARCHITECTURE_32
:
101 case SI_ARCHITECTURE
:
102 case SI_ARCHITECTURE_NATIVE
:
107 (void) snprintf(hostidp
, sizeof (hostidp
), "%u",
108 zone_get_hostid(curzone
));
115 kstr
= curproc
->p_zone
->zone_domain
;
129 strcnt
= strlen(kstr
);
131 if (count
<= strcnt
) {
133 if (subyte(buf
+ getcnt
, 0) < 0)
134 return (set_errno(EFAULT
));
138 if (copyout(kstr
, buf
, getcnt
))
139 return (set_errno(EFAULT
));
148 unsigned int tlen
, octlen
;
150 if (dhcack
== NULL
) {
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
);
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
);
189 strcnt
= IFNAMSIZ
+ octlen
;
194 if (count
<= strcnt
) {
196 if (subyte((buf
+ getcnt
), 0) < 0)
201 if (copyout(tmp
, buf
, getcnt
))
205 kmem_free(tmp
, tlen
);
209 kmem_free(tmp
, tlen
);
214 case SI_SET_HOSTNAME
:
220 if ((error
= secpolicy_systeminfo(CRED())) != 0)
223 name_to_use
= uts_nodename();
224 if ((error
= copyinstr(buf
, name
, SYS_NMLN
, &len
)) != 0)
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')) {
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 */
250 case SI_SET_SRPC_DOMAIN
:
255 if ((error
= secpolicy_systeminfo(CRED())) != 0)
257 if ((error
= copyinstr(buf
, name
, SYS_NMLN
, &len
)) != 0)
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') {
268 (void) strcpy(curproc
->p_zone
->zone_domain
, name
);
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
{
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
) {
297 return (DDI_WALK_TERMINATE
);
299 return (DDI_WALK_CONTINUE
);
303 * Return the devinfo node to a boot device
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.
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
));
328 * Determine the network interface name from the device path argument.
331 get_netif_name(char *devname
, char *ifname
)
338 dip
= path_to_devinfo(devname
);
340 cmn_err(CE_WARN
, "get_netif_name: "
341 "can't bind driver for '%s'\n", devname
);
345 ndev
= ddi_driver_major(dip
);
347 cmn_err(CE_WARN
, "get_netif_name: "
348 "no driver bound to '%s'\n", devname
);
352 name
= ddi_major_to_name(ndev
);
354 cmn_err(CE_WARN
, "get_netif_name: "
355 "no name for major number %d\n", ndev
);
359 unit
= i_ddi_devi_get_ppa(dip
);
361 cmn_err(CE_WARN
, "get_netif_name: "
362 "illegal unit number %d\n", unit
);
366 (void) snprintf(ifname
, IFNAMSIZ
, "%s%d", name
, unit
);