xref: /onnv-gate/usr/src/uts/common/syscall/systeminfo.c (revision 8662:18153249ee93)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51587Scb117521  * Common Development and Distribution License (the "License").
61587Scb117521  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8662SJordan.Vaughan@Sun.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All rights reserved.	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/sysmacros.h>
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate #include <sys/tuneable.h>
340Sstevel@tonic-gate #include <sys/errno.h>
350Sstevel@tonic-gate #include <sys/cred.h>
360Sstevel@tonic-gate #include <sys/utsname.h>
370Sstevel@tonic-gate #include <sys/systeminfo.h>
380Sstevel@tonic-gate #include <sys/unistd.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <sys/bootconf.h>
410Sstevel@tonic-gate #include <sys/socket.h>
420Sstevel@tonic-gate #include <sys/policy.h>
430Sstevel@tonic-gate #include <net/if.h>
440Sstevel@tonic-gate #include <sys/sunddi.h>
450Sstevel@tonic-gate #include <sys/promif.h>
460Sstevel@tonic-gate #include <sys/zone.h>
470Sstevel@tonic-gate #include <sys/model.h>
485648Ssetje #include <netinet/inetutil.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static void get_netif_name(char *, char *);
510Sstevel@tonic-gate 
520Sstevel@tonic-gate long
systeminfo(int command,char * buf,long count)530Sstevel@tonic-gate systeminfo(int command, char *buf, long count)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	int error = 0;
560Sstevel@tonic-gate 	long strcnt, getcnt;
570Sstevel@tonic-gate 	char *kstr;
58*8662SJordan.Vaughan@Sun.com 	char hostidp[HW_HOSTID_LEN];
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	if (count < 0 && command != SI_SET_HOSTNAME &&
610Sstevel@tonic-gate 	    command != SI_SET_SRPC_DOMAIN)
620Sstevel@tonic-gate 		return (set_errno(EINVAL));
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	/*
650Sstevel@tonic-gate 	 * Deal with the common "get a string" case first.
660Sstevel@tonic-gate 	 */
670Sstevel@tonic-gate 	switch (command) {
680Sstevel@tonic-gate 	case SI_SYSNAME:
690Sstevel@tonic-gate 		kstr = utsname.sysname;
700Sstevel@tonic-gate 		break;
710Sstevel@tonic-gate 	case SI_HOSTNAME:
720Sstevel@tonic-gate 		kstr = uts_nodename();
730Sstevel@tonic-gate 		break;
740Sstevel@tonic-gate 	case SI_RELEASE:
750Sstevel@tonic-gate 		kstr = utsname.release;
760Sstevel@tonic-gate 		break;
770Sstevel@tonic-gate 	case SI_VERSION:
780Sstevel@tonic-gate 		kstr = utsname.version;
790Sstevel@tonic-gate 		break;
800Sstevel@tonic-gate 	case SI_MACHINE:
810Sstevel@tonic-gate 		kstr = utsname.machine;
820Sstevel@tonic-gate 		break;
830Sstevel@tonic-gate #ifdef _LP64
840Sstevel@tonic-gate 	case SI_ARCHITECTURE_64:
850Sstevel@tonic-gate 	case SI_ARCHITECTURE_K:
860Sstevel@tonic-gate 		kstr = architecture;
870Sstevel@tonic-gate 		break;
880Sstevel@tonic-gate 	case SI_ARCHITECTURE_32:
890Sstevel@tonic-gate 	case SI_ARCHITECTURE:
900Sstevel@tonic-gate 		kstr = architecture_32;
910Sstevel@tonic-gate 		break;
920Sstevel@tonic-gate 	case SI_ARCHITECTURE_NATIVE:
930Sstevel@tonic-gate 		kstr = get_udatamodel() == DATAMODEL_NATIVE ?
945648Ssetje 		    architecture : architecture_32;
950Sstevel@tonic-gate 		break;
960Sstevel@tonic-gate #else
970Sstevel@tonic-gate 	case SI_ARCHITECTURE_K:
980Sstevel@tonic-gate 	case SI_ARCHITECTURE_32:
990Sstevel@tonic-gate 	case SI_ARCHITECTURE:
1000Sstevel@tonic-gate 	case SI_ARCHITECTURE_NATIVE:
1010Sstevel@tonic-gate 		kstr = architecture;
1020Sstevel@tonic-gate 		break;
1030Sstevel@tonic-gate #endif
1040Sstevel@tonic-gate 	case SI_HW_SERIAL:
105*8662SJordan.Vaughan@Sun.com 		(void) snprintf(hostidp, sizeof (hostidp), "%u",
106*8662SJordan.Vaughan@Sun.com 		    zone_get_hostid(curzone));
107*8662SJordan.Vaughan@Sun.com 		kstr = hostidp;
1080Sstevel@tonic-gate 		break;
1090Sstevel@tonic-gate 	case SI_HW_PROVIDER:
1100Sstevel@tonic-gate 		kstr = hw_provider;
1110Sstevel@tonic-gate 		break;
1120Sstevel@tonic-gate 	case SI_SRPC_DOMAIN:
1130Sstevel@tonic-gate 		kstr = curproc->p_zone->zone_domain;
1140Sstevel@tonic-gate 		break;
1150Sstevel@tonic-gate 	case SI_PLATFORM:
1160Sstevel@tonic-gate 		kstr = platform;
1170Sstevel@tonic-gate 		break;
1180Sstevel@tonic-gate 	case SI_ISALIST:
1190Sstevel@tonic-gate 		kstr = isa_list;
1200Sstevel@tonic-gate 		break;
1210Sstevel@tonic-gate 	default:
1220Sstevel@tonic-gate 		kstr = NULL;
1230Sstevel@tonic-gate 		break;
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	if (kstr != NULL) {
1271587Scb117521 		strcnt = strlen(kstr);
1281587Scb117521 		if (count > 0) {
1291587Scb117521 			if (count <= strcnt) {
1301587Scb117521 				getcnt = count - 1;
1311587Scb117521 				if (subyte(buf + getcnt, 0) < 0)
1321587Scb117521 					return (set_errno(EFAULT));
1331587Scb117521 			} else {
1341587Scb117521 				getcnt = strcnt + 1;
1351587Scb117521 			}
1361587Scb117521 			if (copyout(kstr, buf, getcnt))
1370Sstevel@tonic-gate 				return (set_errno(EFAULT));
1381587Scb117521 		}
1390Sstevel@tonic-gate 		return (strcnt + 1);
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	switch (command) {
1430Sstevel@tonic-gate 	case SI_DHCP_CACHE:
1440Sstevel@tonic-gate 	{
1450Sstevel@tonic-gate 		char	*tmp;
1465648Ssetje 		unsigned int tlen, octlen;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 		if (dhcack == NULL) {
1490Sstevel@tonic-gate 			tmp = "";
1500Sstevel@tonic-gate 			strcnt = 0;
1510Sstevel@tonic-gate 		} else {
1520Sstevel@tonic-gate 			/*
1534172Ssetje 			 * If the interface didn't have a name (bindable
1544172Ssetje 			 * driver) to begin with, it might have one now.
1554172Ssetje 			 * So, re-run strplumb_get_netdev_path() to see
1564172Ssetje 			 * if one can be established at this time.
1574172Ssetje 			 */
1584172Ssetje 			if (netdev_path == NULL || netdev_path[0] == '\0') {
1594172Ssetje 				netdev_path = strplumb_get_netdev_path();
1604172Ssetje 			}
1614172Ssetje 			/*
1620Sstevel@tonic-gate 			 * If the interface name has not yet been resolved
1635648Ssetje 			 * and a validnetdev_path[] was stashed by
1645648Ssetje 			 * loadrootmodules in swapgeneric.c, or established
1655648Ssetje 			 * above, resolve the interface name now.
1660Sstevel@tonic-gate 			 */
1675648Ssetje 			if (dhcifname[0] == '\0' &&
1680Sstevel@tonic-gate 			    netdev_path != NULL && netdev_path[0] != '\0') {
1695648Ssetje 				get_netif_name(netdev_path, dhcifname);
1700Sstevel@tonic-gate 			}
1710Sstevel@tonic-gate 
1725648Ssetje 			/*
1735648Ssetje 			 * Form reply:
1745648Ssetje 			 *  IFNAMESIZ array of dhcp i/f
1755648Ssetje 			 *  hexascii representation of dhcp reply
1765648Ssetje 			 */
1775648Ssetje 			octlen = dhcacklen * 2 + 1;
1785648Ssetje 			tlen = octlen + IFNAMSIZ;
1795648Ssetje 			tmp = kmem_alloc(tlen, KM_SLEEP);
1805648Ssetje 			(void) strncpy(tmp, dhcifname, IFNAMSIZ);
1815648Ssetje 			if (octet_to_hexascii(dhcack, dhcacklen,
1825648Ssetje 			    &tmp[IFNAMSIZ], &octlen) != 0) {
1835648Ssetje 				kmem_free(tmp, tlen);
1845648Ssetje 				error = EINVAL;
1855648Ssetje 				break;
1865648Ssetje 			} else {
1875648Ssetje 				strcnt = IFNAMSIZ + octlen;
1885648Ssetje 			}
1890Sstevel@tonic-gate 		}
1900Sstevel@tonic-gate 
1911587Scb117521 		if (count > 0) {
1921587Scb117521 			if (count <= strcnt) {
1931587Scb117521 				getcnt = count - 1;
1945648Ssetje 				if (subyte((buf + getcnt), 0) < 0)
1955648Ssetje 					goto fail;
1961587Scb117521 			} else {
1971587Scb117521 				getcnt = strcnt + 1;
1981587Scb117521 			}
1995648Ssetje 			if (copyout(tmp, buf, getcnt))
2005648Ssetje 				goto fail;
2010Sstevel@tonic-gate 		}
2025648Ssetje 		if (strcnt != 0)
2035648Ssetje 			kmem_free(tmp, tlen);
2040Sstevel@tonic-gate 		return (strcnt + 1);
2055648Ssetje fail:
2065648Ssetje 		if (strcnt != 0)
2075648Ssetje 			kmem_free(tmp, tlen);
2085648Ssetje 		error = EFAULT;
2095648Ssetje 		break;
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	case SI_SET_HOSTNAME:
2130Sstevel@tonic-gate 	{
2140Sstevel@tonic-gate 		size_t		len;
2150Sstevel@tonic-gate 		char		name[SYS_NMLN];
2160Sstevel@tonic-gate 		char		*name_to_use;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 		if ((error = secpolicy_systeminfo(CRED())) != 0)
2190Sstevel@tonic-gate 			break;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 		name_to_use = uts_nodename();
2220Sstevel@tonic-gate 		if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0)
2230Sstevel@tonic-gate 			break;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 		/*
2260Sstevel@tonic-gate 		 * Must be non-NULL string and string
2270Sstevel@tonic-gate 		 * must be less than SYS_NMLN chars.
2280Sstevel@tonic-gate 		 */
2290Sstevel@tonic-gate 		if (len < 2 || (len == SYS_NMLN && name[SYS_NMLN-1] != '\0')) {
2300Sstevel@tonic-gate 			error = EINVAL;
2310Sstevel@tonic-gate 			break;
2320Sstevel@tonic-gate 		}
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 		/*
2350Sstevel@tonic-gate 		 * Copy the name into the relevant zone's nodename.
2360Sstevel@tonic-gate 		 */
2370Sstevel@tonic-gate 		(void) strcpy(name_to_use, name);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 		/*
2400Sstevel@tonic-gate 		 * Notify other interested parties that the nodename was set
2410Sstevel@tonic-gate 		 */
2420Sstevel@tonic-gate 		if (name_to_use == utsname.nodename) /* global zone nodename */
2430Sstevel@tonic-gate 			nodename_set();
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 		return (len);
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	case SI_SET_SRPC_DOMAIN:
2490Sstevel@tonic-gate 	{
2500Sstevel@tonic-gate 		char name[SYS_NMLN];
2510Sstevel@tonic-gate 		size_t len;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 		if ((error = secpolicy_systeminfo(CRED())) != 0)
2540Sstevel@tonic-gate 			break;
2550Sstevel@tonic-gate 		if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0)
2560Sstevel@tonic-gate 			break;
2570Sstevel@tonic-gate 		/*
2580Sstevel@tonic-gate 		 * If string passed in is longer than length
2590Sstevel@tonic-gate 		 * allowed for domain name, fail.
2600Sstevel@tonic-gate 		 */
2610Sstevel@tonic-gate 		if (len == SYS_NMLN && name[SYS_NMLN-1] != '\0') {
2620Sstevel@tonic-gate 			error = EINVAL;
2630Sstevel@tonic-gate 			break;
2640Sstevel@tonic-gate 		}
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 		(void) strcpy(curproc->p_zone->zone_domain, name);
2670Sstevel@tonic-gate 		return (len);
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	default:
2710Sstevel@tonic-gate 		error = EINVAL;
2720Sstevel@tonic-gate 		break;
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	return (set_errno(error));
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate  * i_path_find_node: Internal routine used by path_to_devinfo
2800Sstevel@tonic-gate  * to locate a given nodeid in the device tree.
2810Sstevel@tonic-gate  */
2820Sstevel@tonic-gate struct i_path_findnode {
283789Sahrens 	pnode_t nodeid;
2840Sstevel@tonic-gate 	dev_info_t *dip;
2850Sstevel@tonic-gate };
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate static int
i_path_find_node(dev_info_t * dev,void * arg)2880Sstevel@tonic-gate i_path_find_node(dev_info_t *dev, void *arg)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	struct i_path_findnode *f = (struct i_path_findnode *)arg;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	if (ddi_get_nodeid(dev) == (int)f->nodeid) {
2940Sstevel@tonic-gate 		f->dip = dev;
2950Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate /*
3010Sstevel@tonic-gate  * Return the devinfo node to a boot device
3020Sstevel@tonic-gate  */
3030Sstevel@tonic-gate static dev_info_t *
path_to_devinfo(char * path)3040Sstevel@tonic-gate path_to_devinfo(char *path)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate 	struct i_path_findnode fn;
3070Sstevel@tonic-gate 	extern dev_info_t *top_devinfo;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	/*
3100Sstevel@tonic-gate 	 * Get the nodeid of the given pathname, if such a mapping exists.
3110Sstevel@tonic-gate 	 */
3120Sstevel@tonic-gate 	fn.dip = NULL;
3130Sstevel@tonic-gate 	fn.nodeid = prom_finddevice(path);
3140Sstevel@tonic-gate 	if (fn.nodeid != OBP_BADNODE) {
3150Sstevel@tonic-gate 		/*
3160Sstevel@tonic-gate 		 * Find the nodeid in our copy of the device tree and return
3170Sstevel@tonic-gate 		 * whatever name we used to bind this node to a driver.
3180Sstevel@tonic-gate 		 */
3190Sstevel@tonic-gate 		ddi_walk_devs(top_devinfo, i_path_find_node, (void *)(&fn));
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	return (fn.dip);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate /*
3260Sstevel@tonic-gate  * Determine the network interface name from the device path argument.
3270Sstevel@tonic-gate  */
3280Sstevel@tonic-gate static void
get_netif_name(char * devname,char * ifname)3290Sstevel@tonic-gate get_netif_name(char *devname, char *ifname)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 	dev_info_t	*dip;
3320Sstevel@tonic-gate 	major_t		ndev;
3330Sstevel@tonic-gate 	char		*name;
3340Sstevel@tonic-gate 	int		unit;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	dip = path_to_devinfo(devname);
3370Sstevel@tonic-gate 	if (dip == NULL) {
3380Sstevel@tonic-gate 		cmn_err(CE_WARN, "get_netif_name: "
3390Sstevel@tonic-gate 		    "can't bind driver for '%s'\n", devname);
3400Sstevel@tonic-gate 		return;
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	ndev = ddi_driver_major(dip);
3440Sstevel@tonic-gate 	if (ndev == -1) {
3450Sstevel@tonic-gate 		cmn_err(CE_WARN, "get_netif_name: "
3460Sstevel@tonic-gate 		    "no driver bound to '%s'\n", devname);
3470Sstevel@tonic-gate 		return;
3480Sstevel@tonic-gate 	}
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	name = ddi_major_to_name(ndev);
3510Sstevel@tonic-gate 	if (name == NULL) {
3520Sstevel@tonic-gate 		cmn_err(CE_WARN, "get_netif_name: "
3530Sstevel@tonic-gate 		    "no name for major number %d\n", ndev);
3540Sstevel@tonic-gate 		return;
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	unit = i_ddi_devi_get_ppa(dip);
3580Sstevel@tonic-gate 	if (unit < 0) {
3590Sstevel@tonic-gate 		cmn_err(CE_WARN, "get_netif_name: "
3600Sstevel@tonic-gate 		    "illegal unit number %d\n", unit);
3610Sstevel@tonic-gate 		return;
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	(void) snprintf(ifname, IFNAMSIZ, "%s%d", name, unit);
3650Sstevel@tonic-gate }
366