xref: /onnv-gate/usr/src/lib/libcmd/common/uname.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1992-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                                                                      *
204887Schin ***********************************************************************/
214887Schin #pragma prototyped
224887Schin /*
234887Schin  * David Korn
244887Schin  * Glenn Fowler
254887Schin  * AT&T Research
264887Schin  *
274887Schin  * uname
284887Schin  */
294887Schin 
304887Schin static const char usage[] =
318462SApril.Chin@Sun.COM "[-?\n@(#)$Id: uname (AT&T Research) 2007-04-19 $\n]"
324887Schin USAGE_LICENSE
334887Schin "[+NAME?uname - identify the current system ]"
344887Schin "[+DESCRIPTION?By default \buname\b writes the operating system name to"
354887Schin "	standard output. When options are specified, one or more"
364887Schin "	system characteristics are written to standard output, space"
37*12068SRoger.Faulkner@Oracle.COM "	separated, on a single line. When more than one option is specified"
384887Schin "	the output is in the order specfied by the \b-A\b option below."
394887Schin "	Unsupported option values are listed as \a[option]]\a. If any unknown"
404887Schin "	options are specified then the local \b/usr/bin/uname\b is called.]"
414887Schin "[+?If any \aname\a operands are specified then the \bsysinfo\b(2) values"
424887Schin "	for each \aname\a are listed, separated by space, on one line."
434887Schin "	\bgetconf\b(1), a pre-existing \astandard\a interface, provides"
444887Schin "	access to the same information; vendors should spend more time"
454887Schin "	using standards than inventing them.]"
464887Schin "[+?Selected information is printed in the same order as the options below.]"
474887Schin "[a:all?Equivalent to \b-snrvmpio\b.]"
484887Schin "[s:system|sysname|kernel-name?The detailed kernel name. This is the default.]"
494887Schin "[n:nodename?The hostname or nodename.]"
504887Schin "[r:release|kernel-release?The kernel release level.]"
514887Schin "[v:version|kernel-version?The kernel version level.]"
524887Schin "[m:machine?The name of the hardware type the system is running on.]"
534887Schin "[p:processor?The name of the processor instruction set architecture.]"
544887Schin "[i:implementation|platform|hardware-platform?The hardware implementation;"
554887Schin "	this is \b--host-id\b on some systems.]"
564887Schin "[o:operating-system?The generic operating system name.]"
574887Schin "[h:host-id|id?The host id in hex.]"
584887Schin "[d:domain?The domain name returned by \agetdomainname\a(2).]"
594887Schin "[R:extended-release?The extended release name.]"
604887Schin "[A:everything?Equivalent to \b-snrvmpiohdR\b.]"
614887Schin "[f:list?List all \bsysinfo\b(2) names and values, one per line.]"
624887Schin "[S:sethost?Set the hostname or nodename to \aname\a. No output is"
634887Schin "	written to standard output.]:[name]"
648462SApril.Chin@Sun.COM "\n"
658462SApril.Chin@Sun.COM "\n[ name ... ]\n"
668462SApril.Chin@Sun.COM "\n"
674887Schin "[+SEE ALSO?\bhostname\b(1), \bgetconf\b(1), \buname\b(2),"
684887Schin "	\bsysconf\b(2), \bsysinfo\b(2)]"
694887Schin ;
704887Schin 
714887Schin #if defined(__STDPP__directive) && defined(__STDPP__hide)
724887Schin __STDPP__directive pragma pp:hide getdomainname gethostid gethostname sethostname
734887Schin #else
744887Schin #define getdomainname	______getdomainname
754887Schin #define gethostid	______gethostid
764887Schin #define gethostname	______gethostname
774887Schin #define sethostname	______sethostname
784887Schin #endif
794887Schin 
804887Schin #include <cmd.h>
814887Schin #include <ctype.h>
824887Schin #include <proc.h>
834887Schin 
844887Schin #include "FEATURE/utsname"
854887Schin 
864887Schin #define MAXHOSTNAME	64
874887Schin 
884887Schin #if _lib_uname && _sys_utsname
894887Schin 
904887Schin #include <sys/utsname.h>
914887Schin 
924887Schin #endif
934887Schin 
944887Schin #if defined(__STDPP__directive) && defined(__STDPP__hide)
954887Schin __STDPP__directive pragma pp:nohide getdomainname gethostid gethostname sethostname
964887Schin #else
974887Schin #undef	getdomainname
984887Schin #undef	gethostid
994887Schin #undef	gethostname
1004887Schin #undef	sethostname
1014887Schin #endif
1024887Schin 
1034887Schin #if _lib_getdomainname
1044887Schin extern int	getdomainname(char*, size_t);
1054887Schin #endif
1064887Schin #if _lib_gethostid
10710898Sroland.mainz@nrubsig.org extern long	gethostid(void);
1084887Schin #endif
1094887Schin #if _lib_gethostname
1104887Schin extern int	gethostname(char*, size_t);
1114887Schin #endif
1124887Schin #if _lib_sethostname
1134887Schin extern int	sethostname(const char*, size_t);
1144887Schin #endif
1154887Schin 
1164887Schin #ifndef HOSTTYPE
1174887Schin #define HOSTTYPE	"unknown"
1184887Schin #endif
1194887Schin 
1204887Schin static const char	hosttype[] = HOSTTYPE;
1214887Schin 
1224887Schin #if !_lib_uname || !_sys_utsname
1234887Schin 
1244887Schin #if defined(__STDPP__)
1254887Schin #define SYSNAME		#(getprd machine)
1264887Schin #define RELEASE		#(getprd release)
1274887Schin #define VERSION		#(getprd version)
1284887Schin #define MACHINE		#(getprd architecture)
1294887Schin #else
1304887Schin #define SYSNAME		""
1314887Schin #define RELEASE		""
1324887Schin #define VERSION		""
1334887Schin #define MACHINE		""
1344887Schin #endif
1354887Schin 
1364887Schin struct utsname
1374887Schin {
1384887Schin 	char*	sysname;
1394887Schin 	char	nodename[MAXHOSTNAME];
1404887Schin 	char*	release;
1414887Schin 	char*	version;
1424887Schin 	char*	machine;
1434887Schin };
1444887Schin 
1454887Schin int
uname(register struct utsname * ut)1464887Schin uname(register struct utsname* ut)
1474887Schin {
1484887Schin #ifdef HOSTTYPE
1494887Schin 	char*		sys = 0;
1504887Schin 	char*		arch = 0;
1514887Schin 
1524887Schin 	if (*hosttype)
1534887Schin 	{
1544887Schin 		static char	buf[sizeof(hosttype)];
1554887Schin 
1564887Schin 		strcpy(buf, hosttype);
1574887Schin 		sys = buf;
1584887Schin 		if (arch = strchr(sys, '.'))
1594887Schin 		{
1604887Schin 			*arch++ = 0;
1614887Schin 			if (!*arch)
1624887Schin 				arch = 0;
1634887Schin 		}
1644887Schin 		if (!*sys)
1654887Schin 			sys = 0;
1664887Schin 	}
1674887Schin #endif
1684887Schin #ifdef _lib_gethostname
1694887Schin 	if (gethostname(ut->nodename, sizeof(ut->nodename) - 1))
1704887Schin 		return -1;
1714887Schin #else
1724887Schin 	strncpy(ut->nodename, "local", sizeof(ut->nodename) - 1);
1734887Schin #endif
1744887Schin #ifdef HOSTTYPE
1754887Schin 	if (!(ut->sysname = sys))
1764887Schin #endif
1774887Schin 	if (!*(ut->sysname = SYSNAME))
1784887Schin 		ut->sysname = ut->nodename;
1794887Schin #ifdef HOSTTYPE
1804887Schin 	if (!(ut->machine = arch))
1814887Schin #endif
1824887Schin 	ut->machine = MACHINE;
1834887Schin 	ut->release = RELEASE;
1844887Schin 	ut->version = VERSION;
1854887Schin 	return 0;
1864887Schin }
1874887Schin 
1884887Schin #endif
1894887Schin 
1904887Schin #define OPT_system		(1<<0)
1914887Schin #define OPT_nodename		(1<<1)
1924887Schin #define OPT_release		(1<<2)
1934887Schin #define OPT_version		(1<<3)
1944887Schin #define OPT_machine		(1<<4)
1954887Schin #define OPT_processor		(1<<5)
1964887Schin 
1974887Schin #define OPT_STANDARD		6
1984887Schin 
1994887Schin #define OPT_implementation	(1<<6)
2004887Schin #define OPT_operating_system	(1<<7)
2014887Schin 
2024887Schin #define OPT_ALL			8
2034887Schin 
2044887Schin #define OPT_hostid		(1<<8)
2054887Schin #define OPT_vendor		(1<<9)
2064887Schin #define OPT_domain		(1<<10)
2074887Schin #define OPT_machine_type	(1<<11)
2084887Schin #define OPT_base		(1<<12)
2094887Schin #define OPT_extended_release	(1<<13)
2104887Schin #define OPT_extra		(1<<14)
2114887Schin 
2124887Schin #define OPT_TOTAL		15
2134887Schin 
2144887Schin #define OPT_all			(1L<<29)
2154887Schin #define OPT_total		(1L<<30)
2164887Schin #define OPT_standard		((1<<OPT_STANDARD)-1)
2174887Schin 
2184887Schin #ifndef MACHINE
2194887Schin #if defined(__STDPP__)
2204887Schin #define MACHINE			#(getprd architecture)
2214887Schin #else
2224887Schin #define MACHINE			""
2234887Schin #endif
2244887Schin #endif
2254887Schin 
2264887Schin #ifndef HOSTTYPE
2274887Schin #define HOSTTYPE		"unknown"
2284887Schin #endif
2294887Schin 
2304887Schin #define extra(m)        do \
2314887Schin 			{ \
2324887Schin 				if ((char*)&ut.m[sizeof(ut.m)] > last) \
2334887Schin 					last = (char*)&ut.m[sizeof(ut.m)]; \
2344887Schin 			} while(0)
2354887Schin 
2364887Schin #define output(f,v,u)	do \
2374887Schin 			{ \
2384887Schin 				if ((flags&(f))&&(*(v)||(flags&(OPT_all|OPT_total))==OPT_all&&((f)&OPT_standard)||!(flags&(OPT_all|OPT_total)))) \
2394887Schin 				{ \
2404887Schin 					if (sep) \
2414887Schin 						sfputc(sfstdout, ' '); \
2424887Schin 					else \
2434887Schin 						sep = 1; \
2444887Schin 					if (*(v)) \
2454887Schin 						sfputr(sfstdout, v, -1); \
2464887Schin 					else \
2474887Schin 						sfprintf(sfstdout, "[%s]", u); \
2484887Schin 				} \
2494887Schin 			} while (0)
2504887Schin 
2514887Schin int
b_uname(int argc,char ** argv,void * context)2524887Schin b_uname(int argc, char** argv, void* context)
2534887Schin {
2544887Schin 	register long	flags = 0;
2554887Schin 	register int	sep = 0;
2564887Schin 	register int	n;
2574887Schin 	register char*	s;
2584887Schin 	char*		t;
2594887Schin 	char*		e;
2604887Schin 	char*		sethost = 0;
2614887Schin 	int		list = 0;
2624887Schin 	struct utsname	ut;
2634887Schin 	char		buf[257];
2644887Schin 
2654887Schin 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
2664887Schin 	for (;;)
2674887Schin 	{
2684887Schin 		switch (optget(argv, usage))
2694887Schin 		{
2704887Schin 		case 'a':
2714887Schin 			flags |= OPT_all|((1L<<OPT_ALL)-1);
2724887Schin 			continue;
2734887Schin 		case 'b':
2744887Schin 			flags |= OPT_base;
2754887Schin 			continue;
2764887Schin 		case 'c':
2774887Schin 			flags |= OPT_vendor;
2784887Schin 			continue;
2794887Schin 		case 'd':
2804887Schin 			flags |= OPT_domain;
2814887Schin 			continue;
2824887Schin 		case 'f':
2834887Schin 			list = 1;
2844887Schin 			continue;
2854887Schin 		case 'h':
2864887Schin 			flags |= OPT_hostid;
2874887Schin 			continue;
2884887Schin 		case 'i':
2894887Schin 			flags |= OPT_implementation;
2904887Schin 			continue;
2914887Schin 		case 'm':
2924887Schin 			flags |= OPT_machine;
2934887Schin 			continue;
2944887Schin 		case 'n':
2954887Schin 			flags |= OPT_nodename;
2964887Schin 			continue;
2974887Schin 		case 'o':
2984887Schin 			flags |= OPT_operating_system;
2994887Schin 			continue;
3004887Schin 		case 'p':
3014887Schin 			flags |= OPT_processor;
3024887Schin 			continue;
3034887Schin 		case 'r':
3044887Schin 			flags |= OPT_release;
3054887Schin 			continue;
3064887Schin 		case 's':
3074887Schin 			flags |= OPT_system;
3084887Schin 			continue;
3094887Schin 		case 't':
3104887Schin 			flags |= OPT_machine_type;
3114887Schin 			continue;
3124887Schin 		case 'v':
3134887Schin 			flags |= OPT_version;
3144887Schin 			continue;
3154887Schin 		case 'x':
3164887Schin 			flags |= OPT_extra;
3174887Schin 			continue;
3184887Schin 		case 'A':
3194887Schin 			flags |= OPT_total|((1L<<OPT_TOTAL)-1);
3204887Schin 			continue;
3214887Schin 		case 'R':
3224887Schin 			flags |= OPT_extended_release;
3234887Schin 			continue;
3244887Schin 		case 'S':
3254887Schin 			sethost = opt_info.arg;
3264887Schin 			continue;
3274887Schin 		case ':':
3284887Schin 			s = "/usr/bin/uname";
3294887Schin 			if (!streq(argv[0], s) && (!eaccess(s, X_OK) || !eaccess(s+=4, X_OK)))
3304887Schin 			{
3314887Schin 				argv[0] = s;
3328462SApril.Chin@Sun.COM 				return sh_run(context, argc, argv);
3334887Schin 			}
3344887Schin 			error(2, "%s", opt_info.arg);
3354887Schin 			break;
3364887Schin 		case '?':
3374887Schin 			error(ERROR_usage(2), "%s", opt_info.arg);
3384887Schin 			break;
3394887Schin 		}
3404887Schin 		break;
3414887Schin 	}
3424887Schin 	argv += opt_info.index;
3434887Schin 	if (error_info.errors || *argv && (flags || sethost) || sethost && flags)
3444887Schin 		error(ERROR_usage(2), "%s", optusage(NiL));
3454887Schin 	if (sethost)
3464887Schin 	{
3474887Schin #if _lib_sethostname
3484887Schin 		if (sethostname(sethost, strlen(sethost) + 1))
3494887Schin #else
3504887Schin #ifdef	ENOSYS
3514887Schin 		errno = ENOSYS;
3524887Schin #else
3534887Schin 		errno = EPERM;
3544887Schin #endif
3554887Schin #endif
3564887Schin 		error(ERROR_system(1), "%s: cannot set host name", sethost);
3574887Schin 	}
3584887Schin 	else if (list)
3594887Schin 		astconflist(sfstdout, NiL, ASTCONF_base|ASTCONF_defined|ASTCONF_lower|ASTCONF_quote|ASTCONF_matchcall, "CS|SI");
3604887Schin 	else if (*argv)
3614887Schin 	{
3624887Schin 		e = &buf[sizeof(buf)-1];
3634887Schin 		while (s = *argv++)
3644887Schin 		{
3654887Schin 			t = buf;
3664887Schin 			*t++ = 'C';
3674887Schin 			*t++ = 'S';
3684887Schin 			*t++ = '_';
3694887Schin 			while (t < e && (n = *s++))
3704887Schin 				*t++ = islower(n) ? toupper(n) : n;
3714887Schin 			*t = 0;
3728462SApril.Chin@Sun.COM 			sfprintf(sfstdout, "%s%c", *(t = astconf(buf, NiL, NiL)) ? t : *(t = astconf(buf+3, NiL, NiL)) ? t :  "unknown", *argv ? ' ' : '\n');
3734887Schin 		}
3744887Schin 	}
3754887Schin 	else
3764887Schin 	{
3774887Schin 		s = buf;
3784887Schin 		if (!flags)
3794887Schin 			flags = OPT_system;
3804887Schin 		memzero(&ut, sizeof(ut));
3814887Schin 		if (uname(&ut) < 0)
3824887Schin 			error(ERROR_usage(2), "information unavailable");
3834887Schin 		output(OPT_system, ut.sysname, "sysname");
3844887Schin 		if (flags & OPT_nodename)
3854887Schin 		{
3864887Schin #if !_mem_nodeext_utsname && _lib_gethostname
3874887Schin 			if (sizeof(ut.nodename) > 9 || gethostname(s, sizeof(buf)))
3884887Schin #endif
3894887Schin 			s = ut.nodename;
3904887Schin 			output(OPT_nodename, s, "nodename");
3914887Schin 		}
3924887Schin 		output(OPT_release, ut.release, "release");
3934887Schin 		output(OPT_version, ut.version, "version");
3944887Schin 		output(OPT_machine, ut.machine, "machine");
3954887Schin 		if (flags & OPT_processor)
3964887Schin 		{
3974887Schin 			if (!*(s = astconf("ARCHITECTURE", NiL, NiL)))
3984887Schin 				s = ut.machine;
3994887Schin 			output(OPT_processor, s, "processor");
4004887Schin 		}
4014887Schin 		if (flags & OPT_implementation)
4024887Schin 		{
4034887Schin 			if (!*(s = astconf("PLATFORM", NiL, NiL)) && !*(s = astconf("HW_NAME", NiL, NiL)))
4044887Schin 			{
4054887Schin 				if (t = strchr(hosttype, '.'))
4064887Schin 					t++;
4074887Schin 				else
4084887Schin 					t = (char*)hosttype;
4094887Schin 				strncpy(s = buf, t, sizeof(buf) - 1);
4104887Schin 			}
4114887Schin 			output(OPT_implementation, s, "implementation");
4124887Schin 		}
4134887Schin 		if (flags & OPT_operating_system)
4144887Schin 		{
4154887Schin 			s = astconf("OPERATING_SYSTEM", NiL, NiL);
4164887Schin 			if (!*s)
4174887Schin #ifdef _UNAME_os_DEFAULT
4184887Schin 				s = _UNAME_os_DEFAULT;
4194887Schin #else
4204887Schin 				s = ut.sysname;
4214887Schin #endif
4224887Schin 			output(OPT_operating_system, s, "operating-system");
4234887Schin 		}
4244887Schin 		if (flags & OPT_extended_release)
4254887Schin 		{
4264887Schin 			s = astconf("RELEASE", NiL, NiL);
4274887Schin 			output(OPT_extended_release, s, "extended-release");
4284887Schin 		}
4294887Schin #if _mem_idnumber_utsname
4304887Schin 		output(OPT_hostid, ut.idnumber, "hostid");
4314887Schin #else
4324887Schin 		if (flags & OPT_hostid)
4334887Schin 		{
4344887Schin 			if (!*(s = astconf("HW_SERIAL", NiL, NiL)))
4354887Schin #if _lib_gethostid
4364887Schin 				sfsprintf(s = buf, sizeof(buf), "%08x", gethostid());
4374887Schin #else
4384887Schin 				/*NOP*/;
4394887Schin #endif
4404887Schin 			output(OPT_hostid, s, "hostid");
4414887Schin 		}
4424887Schin #endif
4434887Schin 		if (flags & OPT_vendor)
4444887Schin 		{
4454887Schin 			s = astconf("HW_PROVIDER", NiL, NiL);
4464887Schin 			output(OPT_vendor, s, "vendor");
4474887Schin 		}
4484887Schin 		if (flags & OPT_domain)
4494887Schin 		{
4504887Schin 			if (!*(s = astconf("SRPC_DOMAIN", NiL, NiL)))
4514887Schin #if _lib_getdomainname
4524887Schin 				getdomainname(s, sizeof(buf));
4534887Schin #else
4544887Schin 				/*NOP*/;
4554887Schin #endif
4564887Schin 			output(OPT_domain, s, "domain");
4574887Schin 		}
4584887Schin #if _mem_m_type_utsname
4594887Schin 		s = ut.m_type;
4604887Schin #else
4614887Schin 		s = astconf("MACHINE", NiL, NiL);
4624887Schin #endif
4634887Schin 		output(OPT_machine_type, s, "m_type");
4644887Schin #if _mem_base_rel_utsname
4654887Schin 		s = ut.base_rel;
4664887Schin #else
4674887Schin 		s = astconf("BASE", NiL, NiL);
4684887Schin #endif
4694887Schin 		output(OPT_base, s, "base_rel");
4704887Schin 		if (flags & OPT_extra)
4714887Schin 		{
4724887Schin 			char*	last = (char*)&ut;
4734887Schin 
4744887Schin 			extra(sysname);
4754887Schin 			extra(nodename);
4764887Schin 			extra(release);
4774887Schin 			extra(version);
4784887Schin 			extra(machine);
4794887Schin #if _mem_idnumber_utsname
4804887Schin 			extra(idnumber);
4814887Schin #endif
4824887Schin #if _mem_m_type_utsname
4834887Schin 			extra(m_type);
4844887Schin #endif
4854887Schin #if _mem_base_rel_utsname
4864887Schin 			extra(base_rel);
4874887Schin #endif
4884887Schin 			if (last < ((char*)(&ut + 1)))
4894887Schin 			{
4904887Schin 				s = t = last;
4914887Schin 				while (s < (char*)(&ut + 1))
4924887Schin 				{
4934887Schin 					if (!(n = *s++))
4944887Schin 					{
4954887Schin 						if ((s - t) > 1)
4964887Schin 						{
4974887Schin 							if (sep)
4984887Schin 								sfputc(sfstdout, ' ');
4994887Schin 							else
5004887Schin 								sep = 1;
5014887Schin 							sfputr(sfstdout, t, -1);
5024887Schin 						}
5034887Schin 						t = s;
5044887Schin 					}
5054887Schin 					else if (!isprint(n))
5064887Schin 						break;
5074887Schin 				}
5084887Schin 			}
5094887Schin 		}
5104887Schin 		if (sep)
5114887Schin 			sfputc(sfstdout, '\n');
5124887Schin 	}
5134887Schin 	return error_info.errors;
5144887Schin }
515