blob: 765809658a1e600b008a0c4303e295f286d3c5ff [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenkoa02a4e92017-10-05 15:19:25 +02002/*
3 * uname -- print system information
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00004 * Copyright (C) 1989-1999 Free Software Foundation, Inc.
5 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00007 */
Eric Andersenc8fdb561999-10-26 05:21:02 +00008/* Option Example
Denis Vlasenko059138f2009-01-19 16:32:23 +00009 * -s, --sysname SunOS
10 * -n, --nodename rocky8
11 * -r, --release 4.0
12 * -v, --version
13 * -m, --machine sun
14 * -a, --all SunOS rocky8 4.0 sun
Manuel Novoa III cad53642003-03-19 09:13:01 +000015 *
Denis Vlasenko059138f2009-01-19 16:32:23 +000016 * The default behavior is equivalent to '-s'.
17 *
18 * David MacKenzie <djm@gnu.ai.mit.edu>
19 *
20 * GNU coreutils 6.10:
21 * Option: struct Example(s):
22 * utsname
23 * field:
24 * -s, --kernel-name sysname Linux
25 * -n, --nodename nodename localhost.localdomain
26 * -r, --kernel-release release 2.6.29
27 * -v, --kernel-version version #1 SMP Sun Jan 11 20:52:37 EST 2009
28 * -m, --machine machine x86_64 i686
29 * -p, --processor (none) x86_64 i686
30 * -i, --hardware-platform (none) x86_64 i386
31 * NB: vanilla coreutils reports "unknown" -p and -i,
32 * x86_64 and i686/i386 shown above are Fedora's inventions.
33 * -o, --operating-system (none) GNU/Linux
34 * -a, --all: all of the above, in the order shown.
35 * If -p or -i is not known, don't show them
36 */
Denis Vlasenko059138f2009-01-19 16:32:23 +000037/* Busyboxed by Erik Andersen
38 *
39 * Before 2003: Glenn McGrath and Manuel Novoa III
40 * Further size reductions.
41 * Mar 16, 2003: Manuel Novoa III (mjn3@codepoet.org)
42 * Now does proper error checking on i/o. Plus some further space savings.
43 * Jan 2009:
44 * Fix handling of -a to not print "unknown", add -o and -i support.
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010046//config:config UNAME
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020047//config: bool "uname (3.7 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010048//config: default y
49//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020050//config: uname is used to print system information.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010051//config:
52//config:config UNAME_OSNAME
53//config: string "Operating system name"
54//config: default "GNU/Linux"
55//config: depends on UNAME
56//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020057//config: Sets the operating system name reported by uname -o. The
58//config: default is "GNU/Linux".
Denys Vlasenkoc49638b2017-07-16 16:43:41 +020059//config:
60//can't use "ARCH" for this applet, all hell breaks loose in build system :)
Denys Vlasenko483405a2017-07-18 20:17:51 +020061//config:config BB_ARCH
Denys Vlasenkoae178ce2017-07-19 14:32:54 +020062//config: bool "arch (1.6 kb)"
Denys Vlasenkoc49638b2017-07-16 16:43:41 +020063//config: default y
64//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020065//config: Same as uname -m.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010066
Denys Vlasenko819b47a2017-08-03 03:29:32 +020067// APPLET_NOFORK:name main location suid_type help
68//applet:IF_UNAME(APPLET_NOFORK( uname, uname, BB_DIR_BIN, BB_SUID_DROP, uname))
69//applet:IF_BB_ARCH(APPLET_NOFORK(arch, uname, BB_DIR_BIN, BB_SUID_DROP, arch))
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010070
Denys Vlasenko483405a2017-07-18 20:17:51 +020071//kbuild:lib-$(CONFIG_UNAME) += uname.o
72//kbuild:lib-$(CONFIG_BB_ARCH) += uname.o
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010073
74/* BB_AUDIT SUSv3 compliant */
75/* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */
Manuel Novoa III cad53642003-03-19 09:13:01 +000076
Pere Orga34425382011-03-31 14:43:25 +020077//usage:#define uname_trivial_usage
Mike Frysinger29ed5802014-01-31 00:28:42 -050078//usage: "[-amnrspvio]"
Pere Orga34425382011-03-31 14:43:25 +020079//usage:#define uname_full_usage "\n\n"
80//usage: "Print system information\n"
Pere Orga34425382011-03-31 14:43:25 +020081//usage: "\n -a Print all"
82//usage: "\n -m The machine (hardware) type"
83//usage: "\n -n Hostname"
Mike Frysinger29ed5802014-01-31 00:28:42 -050084//usage: "\n -r Kernel release"
85//usage: "\n -s Kernel name (default)"
Pere Orga34425382011-03-31 14:43:25 +020086//usage: "\n -p Processor type"
Mike Frysinger29ed5802014-01-31 00:28:42 -050087//usage: "\n -v Kernel version"
88//usage: "\n -i The hardware platform"
89//usage: "\n -o OS name"
Pere Orga34425382011-03-31 14:43:25 +020090//usage:
91//usage:#define uname_example_usage
92//usage: "$ uname -a\n"
93//usage: "Linux debian 2.4.23 #2 Tue Dec 23 17:09:10 MST 2003 i686 GNU/Linux\n"
94
Denys Vlasenkoc49638b2017-07-16 16:43:41 +020095//usage:#define arch_trivial_usage
96//usage: ""
97//usage:#define arch_full_usage "\n\n"
98//usage: "Print system architecture"
99
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000100#include "libbb.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200101/* After libbb.h, since it needs sys/types.h on some systems */
102#include <sys/utsname.h>
Eric Andersenc8fdb561999-10-26 05:21:02 +0000103
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000104typedef struct {
105 struct utsname name;
Denis Vlasenko059138f2009-01-19 16:32:23 +0000106 char processor[sizeof(((struct utsname*)NULL)->machine)];
107 char platform[sizeof(((struct utsname*)NULL)->machine)];
Ron Yorston64ed5f02015-07-12 16:06:37 +0100108 char os[sizeof(CONFIG_UNAME_OSNAME)];
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000109} uname_info_t;
Eric Andersenc8fdb561999-10-26 05:21:02 +0000110
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200111#if ENABLE_UNAME
112#define options "snrvmpioa"
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000113static const unsigned short utsname_offset[] = {
Denis Vlasenko059138f2009-01-19 16:32:23 +0000114 offsetof(uname_info_t, name.sysname), /* -s */
115 offsetof(uname_info_t, name.nodename), /* -n */
116 offsetof(uname_info_t, name.release), /* -r */
117 offsetof(uname_info_t, name.version), /* -v */
118 offsetof(uname_info_t, name.machine), /* -m */
119 offsetof(uname_info_t, processor), /* -p */
120 offsetof(uname_info_t, platform), /* -i */
121 offsetof(uname_info_t, os), /* -o */
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000122};
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200123#endif
Eric Andersenc8fdb561999-10-26 05:21:02 +0000124
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000125int uname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200126int uname_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Eric Andersenc8fdb561999-10-26 05:21:02 +0000127{
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000128 uname_info_t uname_info;
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200129 IF_UNAME(const char *unknown_str = "unknown";)
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200130#if ENABLE_UNAME
Denys Vlasenko82d1c1f2017-12-31 17:30:02 +0100131 unsigned toprint = (1 << 4); /* "arch" = "uname -m" */
132
Denys Vlasenko483405a2017-07-18 20:17:51 +0200133 if (!ENABLE_BB_ARCH || applet_name[0] == 'u') {
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200134# if ENABLE_LONG_OPTS
135 static const char uname_longopts[] ALIGN1 =
136 /* name, has_arg, val */
137 "all\0" No_argument "a"
138 "kernel-name\0" No_argument "s"
139 "nodename\0" No_argument "n"
140 "kernel-release\0" No_argument "r"
141 "release\0" No_argument "r"
142 "kernel-version\0" No_argument "v"
143 "machine\0" No_argument "m"
144 "processor\0" No_argument "p"
145 "hardware-platform\0" No_argument "i"
146 "operating-system\0" No_argument "o"
147 ;
148# endif
Denys Vlasenko036585a2017-08-08 16:38:18 +0200149 toprint = getopt32long(argv, options, uname_longopts);
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200150 if (argv[optind]) { /* coreutils-6.9 compat */
151 bb_show_usage();
152 }
153 if (toprint & (1 << 8)) { /* -a => all opts on */
154 toprint = (1 << 8) - 1;
155 unknown_str = ""; /* -a does not print unknown fields */
156 }
157 if (toprint == 0) { /* no opts => -s (sysname) */
158 toprint = 1;
159 }
160 } /* if "uname" */
161#endif
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000162
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000163 uname(&uname_info.name); /* never fails */
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000164
165#if defined(__sparc__) && defined(__linux__)
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200166 {
167 char *fake_sparc = getenv("FAKE_SPARC");
168 if (fake_sparc && (fake_sparc[0] | 0x20) == 'y') {
169 strcpy(uname_info.name.machine, "sparc");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000170 }
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200171 }
172#endif
Denys Vlasenko483405a2017-07-18 20:17:51 +0200173 if (ENABLE_BB_ARCH && (!ENABLE_UNAME || applet_name[0] == 'a')) {
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200174 puts(uname_info.name.machine);
175 } else {
176#if ENABLE_UNAME
177 /* "uname" */
178 const char *fmt;
179 const unsigned short *delta;
180
181 strcpy(uname_info.processor, unknown_str);
182 strcpy(uname_info.platform, unknown_str);
183 strcpy(uname_info.os, CONFIG_UNAME_OSNAME);
Denys Vlasenko663ae522017-08-03 03:46:14 +0200184# if ENABLE_FEDORA_COMPAT
Denys Vlasenkoc49638b2017-07-16 16:43:41 +0200185 /* Fedora does something like this */
186 strcpy(uname_info.processor, uname_info.name.machine);
187 strcpy(uname_info.platform, uname_info.name.machine);
188 if (uname_info.platform[0] == 'i'
189 && uname_info.platform[1]
190 && uname_info.platform[2] == '8'
191 && uname_info.platform[3] == '6'
192 ) {
193 uname_info.platform[1] = '3';
194 }
195# endif
196 delta = utsname_offset;
197 fmt = " %s" + 1;
198 do {
199 if (toprint & 1) {
200 const char *p = (char *)(&uname_info) + *delta;
201 if (p[0]) {
202 printf(fmt, p);
203 fmt = " %s";
204 }
205 }
206 ++delta;
207 } while (toprint >>= 1);
208 bb_putchar('\n');
209#endif
210 }
Eric Andersenc8fdb561999-10-26 05:21:02 +0000211
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000212 fflush_stdout_and_exit(EXIT_SUCCESS); /* coreutils-6.9 compat */
Eric Andersenc8fdb561999-10-26 05:21:02 +0000213}