blob: 18c6d8efdbca232522136ec670496841e76ab8e0 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc8fdb561999-10-26 05:21:02 +00002/* uname -- print system information
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00003 * Copyright (C) 1989-1999 Free Software Foundation, Inc.
4 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00006 */
Eric Andersenc8fdb561999-10-26 05:21:02 +00007/* Option Example
Denis Vlasenko059138f2009-01-19 16:32:23 +00008 * -s, --sysname SunOS
9 * -n, --nodename rocky8
10 * -r, --release 4.0
11 * -v, --version
12 * -m, --machine sun
13 * -a, --all SunOS rocky8 4.0 sun
Manuel Novoa III cad53642003-03-19 09:13:01 +000014 *
Denis Vlasenko059138f2009-01-19 16:32:23 +000015 * The default behavior is equivalent to '-s'.
16 *
17 * David MacKenzie <djm@gnu.ai.mit.edu>
18 *
19 * GNU coreutils 6.10:
20 * Option: struct Example(s):
21 * utsname
22 * field:
23 * -s, --kernel-name sysname Linux
24 * -n, --nodename nodename localhost.localdomain
25 * -r, --kernel-release release 2.6.29
26 * -v, --kernel-version version #1 SMP Sun Jan 11 20:52:37 EST 2009
27 * -m, --machine machine x86_64 i686
28 * -p, --processor (none) x86_64 i686
29 * -i, --hardware-platform (none) x86_64 i386
30 * NB: vanilla coreutils reports "unknown" -p and -i,
31 * x86_64 and i686/i386 shown above are Fedora's inventions.
32 * -o, --operating-system (none) GNU/Linux
33 * -a, --all: all of the above, in the order shown.
34 * If -p or -i is not known, don't show them
35 */
Denis Vlasenko059138f2009-01-19 16:32:23 +000036/* Busyboxed by Erik Andersen
37 *
38 * Before 2003: Glenn McGrath and Manuel Novoa III
39 * Further size reductions.
40 * Mar 16, 2003: Manuel Novoa III (mjn3@codepoet.org)
41 * Now does proper error checking on i/o. Plus some further space savings.
42 * Jan 2009:
43 * Fix handling of -a to not print "unknown", add -o and -i support.
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010045//config:config UNAME
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020046//config: bool "uname (3.7 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010047//config: default y
48//config: help
49//config: uname is used to print system information.
50//config:
51//config:config UNAME_OSNAME
52//config: string "Operating system name"
53//config: default "GNU/Linux"
54//config: depends on UNAME
55//config: help
56//config: Sets the operating system name reported by uname -o. The
57//config: default is "GNU/Linux".
Denys Vlasenkoc49638b2017-07-16 16:43:41 +020058//config:
59//can't use "ARCH" for this applet, all hell breaks loose in build system :)
Denys Vlasenko483405a2017-07-18 20:17:51 +020060//config:config BB_ARCH
Denys Vlasenkoc49638b2017-07-16 16:43:41 +020061//config: bool "arch"
62//config: default y
63//config: help
64//config: Same as uname -m.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010065
66//applet:IF_UNAME(APPLET(uname, BB_DIR_BIN, BB_SUID_DROP))
Denys Vlasenko483405a2017-07-18 20:17:51 +020067// APPLET_ODDNAME:name main location suid_type help
68//applet:IF_BB_ARCH(APPLET_ODDNAME(arch, uname, BB_DIR_BIN, BB_SUID_DROP, arch))
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010069
Denys Vlasenko483405a2017-07-18 20:17:51 +020070//kbuild:lib-$(CONFIG_UNAME) += uname.o
71//kbuild:lib-$(CONFIG_BB_ARCH) += uname.o
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010072
73/* BB_AUDIT SUSv3 compliant */
74/* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */
Manuel Novoa III cad53642003-03-19 09:13:01 +000075
Pere Orga34425382011-03-31 14:43:25 +020076//usage:#define uname_trivial_usage
Mike Frysinger29ed5802014-01-31 00:28:42 -050077//usage: "[-amnrspvio]"
Pere Orga34425382011-03-31 14:43:25 +020078//usage:#define uname_full_usage "\n\n"
79//usage: "Print system information\n"
Pere Orga34425382011-03-31 14:43:25 +020080//usage: "\n -a Print all"
81//usage: "\n -m The machine (hardware) type"
82//usage: "\n -n Hostname"
Mike Frysinger29ed5802014-01-31 00:28:42 -050083//usage: "\n -r Kernel release"
84//usage: "\n -s Kernel name (default)"
Pere Orga34425382011-03-31 14:43:25 +020085//usage: "\n -p Processor type"
Mike Frysinger29ed5802014-01-31 00:28:42 -050086//usage: "\n -v Kernel version"
87//usage: "\n -i The hardware platform"
88//usage: "\n -o OS name"
Pere Orga34425382011-03-31 14:43:25 +020089//usage:
90//usage:#define uname_example_usage
91//usage: "$ uname -a\n"
92//usage: "Linux debian 2.4.23 #2 Tue Dec 23 17:09:10 MST 2003 i686 GNU/Linux\n"
93
Denys Vlasenkoc49638b2017-07-16 16:43:41 +020094//usage:#define arch_trivial_usage
95//usage: ""
96//usage:#define arch_full_usage "\n\n"
97//usage: "Print system architecture"
98
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000099#include "libbb.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200100/* After libbb.h, since it needs sys/types.h on some systems */
101#include <sys/utsname.h>
Eric Andersenc8fdb561999-10-26 05:21:02 +0000102
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000103typedef struct {
104 struct utsname name;
Denis Vlasenko059138f2009-01-19 16:32:23 +0000105 char processor[sizeof(((struct utsname*)NULL)->machine)];
106 char platform[sizeof(((struct utsname*)NULL)->machine)];
Ron Yorston64ed5f02015-07-12 16:06:37 +0100107 char os[sizeof(CONFIG_UNAME_OSNAME)];
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000108} uname_info_t;
Eric Andersenc8fdb561999-10-26 05:21:02 +0000109
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200110#if ENABLE_UNAME
111#define options "snrvmpioa"
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000112static const unsigned short utsname_offset[] = {
Denis Vlasenko059138f2009-01-19 16:32:23 +0000113 offsetof(uname_info_t, name.sysname), /* -s */
114 offsetof(uname_info_t, name.nodename), /* -n */
115 offsetof(uname_info_t, name.release), /* -r */
116 offsetof(uname_info_t, name.version), /* -v */
117 offsetof(uname_info_t, name.machine), /* -m */
118 offsetof(uname_info_t, processor), /* -p */
119 offsetof(uname_info_t, platform), /* -i */
120 offsetof(uname_info_t, os), /* -o */
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000121};
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200122#endif
Eric Andersenc8fdb561999-10-26 05:21:02 +0000123
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000124int uname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200125int uname_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Eric Andersenc8fdb561999-10-26 05:21:02 +0000126{
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000127 uname_info_t uname_info;
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200128 IF_UNAME(const char *unknown_str = "unknown";)
Denis Vlasenko038eec72009-01-19 16:38:30 +0000129 unsigned toprint;
Eric Andersenc8fdb561999-10-26 05:21:02 +0000130
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200131 toprint = (1 << 4); /* "arch" = "uname -m" */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000132
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200133#if ENABLE_UNAME
Denys Vlasenko483405a2017-07-18 20:17:51 +0200134 if (!ENABLE_BB_ARCH || applet_name[0] == 'u') {
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200135# if ENABLE_LONG_OPTS
136 static const char uname_longopts[] ALIGN1 =
137 /* name, has_arg, val */
138 "all\0" No_argument "a"
139 "kernel-name\0" No_argument "s"
140 "nodename\0" No_argument "n"
141 "kernel-release\0" No_argument "r"
142 "release\0" No_argument "r"
143 "kernel-version\0" No_argument "v"
144 "machine\0" No_argument "m"
145 "processor\0" No_argument "p"
146 "hardware-platform\0" No_argument "i"
147 "operating-system\0" No_argument "o"
148 ;
149# endif
150 IF_LONG_OPTS(applet_long_options = uname_longopts);
151 toprint = getopt32(argv, options);
152 if (argv[optind]) { /* coreutils-6.9 compat */
153 bb_show_usage();
154 }
155 if (toprint & (1 << 8)) { /* -a => all opts on */
156 toprint = (1 << 8) - 1;
157 unknown_str = ""; /* -a does not print unknown fields */
158 }
159 if (toprint == 0) { /* no opts => -s (sysname) */
160 toprint = 1;
161 }
162 } /* if "uname" */
163#endif
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000164
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000165 uname(&uname_info.name); /* never fails */
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000166
167#if defined(__sparc__) && defined(__linux__)
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200168 {
169 char *fake_sparc = getenv("FAKE_SPARC");
170 if (fake_sparc && (fake_sparc[0] | 0x20) == 'y') {
171 strcpy(uname_info.name.machine, "sparc");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000172 }
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200173 }
174#endif
Denys Vlasenko483405a2017-07-18 20:17:51 +0200175 if (ENABLE_BB_ARCH && (!ENABLE_UNAME || applet_name[0] == 'a')) {
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200176 puts(uname_info.name.machine);
177 } else {
178#if ENABLE_UNAME
179 /* "uname" */
180 const char *fmt;
181 const unsigned short *delta;
182
183 strcpy(uname_info.processor, unknown_str);
184 strcpy(uname_info.platform, unknown_str);
185 strcpy(uname_info.os, CONFIG_UNAME_OSNAME);
186# if 0
187 /* Fedora does something like this */
188 strcpy(uname_info.processor, uname_info.name.machine);
189 strcpy(uname_info.platform, uname_info.name.machine);
190 if (uname_info.platform[0] == 'i'
191 && uname_info.platform[1]
192 && uname_info.platform[2] == '8'
193 && uname_info.platform[3] == '6'
194 ) {
195 uname_info.platform[1] = '3';
196 }
197# endif
198 delta = utsname_offset;
199 fmt = " %s" + 1;
200 do {
201 if (toprint & 1) {
202 const char *p = (char *)(&uname_info) + *delta;
203 if (p[0]) {
204 printf(fmt, p);
205 fmt = " %s";
206 }
207 }
208 ++delta;
209 } while (toprint >>= 1);
210 bb_putchar('\n');
211#endif
212 }
Eric Andersenc8fdb561999-10-26 05:21:02 +0000213
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000214 fflush_stdout_and_exit(EXIT_SUCCESS); /* coreutils-6.9 compat */
Eric Andersenc8fdb561999-10-26 05:21:02 +0000215}