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 if (PTOU(curproc
)->u_flags
& U_FLAG_ALTUNAME
) {
74 kstr
= utsname
.sysname
;
78 kstr
= uts_nodename();
81 if (PTOU(curproc
)->u_flags
& U_FLAG_ALTUNAME
) {
84 kstr
= utsname
.release
;
88 if (PTOU(curproc
)->u_flags
& U_FLAG_ALTUNAME
) {
91 kstr
= utsname
.version
;
95 kstr
= utsname
.machine
;
98 case SI_ARCHITECTURE_64
:
99 case SI_ARCHITECTURE_K
:
102 case SI_ARCHITECTURE_32
:
103 case SI_ARCHITECTURE
:
104 kstr
= architecture_32
;
106 case SI_ARCHITECTURE_NATIVE
:
107 kstr
= get_udatamodel() == DATAMODEL_NATIVE
?
108 architecture
: architecture_32
;
111 case SI_ARCHITECTURE_K
:
112 case SI_ARCHITECTURE_32
:
113 case SI_ARCHITECTURE
:
114 case SI_ARCHITECTURE_NATIVE
:
119 (void) snprintf(hostidp
, sizeof (hostidp
), "%u",
120 zone_get_hostid(curzone
));
127 kstr
= curproc
->p_zone
->zone_domain
;
141 strcnt
= strlen(kstr
);
143 if (count
<= strcnt
) {
145 if (subyte(buf
+ getcnt
, 0) < 0)
146 return (set_errno(EFAULT
));
150 if (copyout(kstr
, buf
, getcnt
))
151 return (set_errno(EFAULT
));
160 unsigned int tlen
, octlen
;
162 if (dhcack
== NULL
) {
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
);
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
);
201 strcnt
= IFNAMSIZ
+ octlen
;
206 if (count
<= strcnt
) {
208 if (subyte((buf
+ getcnt
), 0) < 0)
213 if (copyout(tmp
, buf
, getcnt
))
217 kmem_free(tmp
, tlen
);
221 kmem_free(tmp
, tlen
);
226 case SI_SET_HOSTNAME
:
232 if ((error
= secpolicy_systeminfo(CRED())) != 0)
235 name_to_use
= uts_nodename();
236 if ((error
= copyinstr(buf
, name
, SYS_NMLN
, &len
)) != 0)
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')) {
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 */
262 case SI_SET_SRPC_DOMAIN
:
267 if ((error
= secpolicy_systeminfo(CRED())) != 0)
269 if ((error
= copyinstr(buf
, name
, SYS_NMLN
, &len
)) != 0)
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') {
280 (void) strcpy(curproc
->p_zone
->zone_domain
, name
);
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
{
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
) {
309 return (DDI_WALK_TERMINATE
);
311 return (DDI_WALK_CONTINUE
);
315 * Return the devinfo node to a boot device
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.
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
));
340 * Determine the network interface name from the device path argument.
343 get_netif_name(char *devname
, char *ifname
)
350 dip
= path_to_devinfo(devname
);
352 cmn_err(CE_WARN
, "get_netif_name: "
353 "can't bind driver for '%s'\n", devname
);
357 ndev
= ddi_driver_major(dip
);
359 cmn_err(CE_WARN
, "get_netif_name: "
360 "no driver bound to '%s'\n", devname
);
364 name
= ddi_major_to_name(ndev
);
366 cmn_err(CE_WARN
, "get_netif_name: "
367 "no name for major number %d\n", ndev
);
371 unit
= i_ddi_devi_get_ppa(dip
);
373 cmn_err(CE_WARN
, "get_netif_name: "
374 "illegal unit number %d\n", unit
);
378 (void) snprintf(ifname
, IFNAMSIZ
, "%s%d", name
, unit
);