blob: 4d98fde25bd7b5bda06b4d4cd690a8f06e8ad60a [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
46//config: bool "uname"
47//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".
58
59//applet:IF_UNAME(APPLET(uname, BB_DIR_BIN, BB_SUID_DROP))
60
61//kbuild:lib-$(CONFIG_UNAME) += uname.o
62
63/* BB_AUDIT SUSv3 compliant */
64/* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */
Manuel Novoa III cad53642003-03-19 09:13:01 +000065
Pere Orga34425382011-03-31 14:43:25 +020066//usage:#define uname_trivial_usage
Mike Frysinger29ed5802014-01-31 00:28:42 -050067//usage: "[-amnrspvio]"
Pere Orga34425382011-03-31 14:43:25 +020068//usage:#define uname_full_usage "\n\n"
69//usage: "Print system information\n"
Pere Orga34425382011-03-31 14:43:25 +020070//usage: "\n -a Print all"
71//usage: "\n -m The machine (hardware) type"
72//usage: "\n -n Hostname"
Mike Frysinger29ed5802014-01-31 00:28:42 -050073//usage: "\n -r Kernel release"
74//usage: "\n -s Kernel name (default)"
Pere Orga34425382011-03-31 14:43:25 +020075//usage: "\n -p Processor type"
Mike Frysinger29ed5802014-01-31 00:28:42 -050076//usage: "\n -v Kernel version"
77//usage: "\n -i The hardware platform"
78//usage: "\n -o OS name"
Pere Orga34425382011-03-31 14:43:25 +020079//usage:
80//usage:#define uname_example_usage
81//usage: "$ uname -a\n"
82//usage: "Linux debian 2.4.23 #2 Tue Dec 23 17:09:10 MST 2003 i686 GNU/Linux\n"
83
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000084#include "libbb.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +020085/* After libbb.h, since it needs sys/types.h on some systems */
86#include <sys/utsname.h>
Eric Andersenc8fdb561999-10-26 05:21:02 +000087
Manuel Novoa III 6509f922001-12-05 04:21:30 +000088typedef struct {
89 struct utsname name;
Denis Vlasenko059138f2009-01-19 16:32:23 +000090 char processor[sizeof(((struct utsname*)NULL)->machine)];
91 char platform[sizeof(((struct utsname*)NULL)->machine)];
Ron Yorston64ed5f02015-07-12 16:06:37 +010092 char os[sizeof(CONFIG_UNAME_OSNAME)];
Manuel Novoa III 6509f922001-12-05 04:21:30 +000093} uname_info_t;
Eric Andersenc8fdb561999-10-26 05:21:02 +000094
Denis Vlasenko059138f2009-01-19 16:32:23 +000095static const char options[] ALIGN1 = "snrvmpioa";
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +000096static const unsigned short utsname_offset[] = {
Denis Vlasenko059138f2009-01-19 16:32:23 +000097 offsetof(uname_info_t, name.sysname), /* -s */
98 offsetof(uname_info_t, name.nodename), /* -n */
99 offsetof(uname_info_t, name.release), /* -r */
100 offsetof(uname_info_t, name.version), /* -v */
101 offsetof(uname_info_t, name.machine), /* -m */
102 offsetof(uname_info_t, processor), /* -p */
103 offsetof(uname_info_t, platform), /* -i */
104 offsetof(uname_info_t, os), /* -o */
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000105};
Eric Andersenc8fdb561999-10-26 05:21:02 +0000106
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000107int uname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000108int uname_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenc8fdb561999-10-26 05:21:02 +0000109{
Denys Vlasenkof3b92d32009-06-19 12:10:38 +0200110#if ENABLE_LONG_OPTS
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200111 static const char uname_longopts[] ALIGN1 =
Denis Vlasenko447ab182009-03-14 18:41:19 +0000112 /* name, has_arg, val */
113 "all\0" No_argument "a"
114 "kernel-name\0" No_argument "s"
115 "nodename\0" No_argument "n"
116 "kernel-release\0" No_argument "r"
117 "release\0" No_argument "r"
118 "kernel-version\0" No_argument "v"
119 "machine\0" No_argument "m"
120 "processor\0" No_argument "p"
121 "hardware-platform\0" No_argument "i"
122 "operating-system\0" No_argument "o"
123 ;
124#endif
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000125 uname_info_t uname_info;
Eric Andersenc8fdb561999-10-26 05:21:02 +0000126#if defined(__sparc__) && defined(__linux__)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 char *fake_sparc = getenv("FAKE_SPARC");
Eric Andersenc8fdb561999-10-26 05:21:02 +0000128#endif
Denis Vlasenko059138f2009-01-19 16:32:23 +0000129 const char *unknown_str = "unknown";
130 const char *fmt;
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000131 const unsigned short *delta;
Denis Vlasenko038eec72009-01-19 16:38:30 +0000132 unsigned toprint;
Eric Andersenc8fdb561999-10-26 05:21:02 +0000133
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200134 IF_LONG_OPTS(applet_long_options = uname_longopts);
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000135 toprint = getopt32(argv, options);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000136
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000137 if (argv[optind]) { /* coreutils-6.9 compat */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000138 bb_show_usage();
139 }
140
Denis Vlasenko059138f2009-01-19 16:32:23 +0000141 if (toprint & (1 << 8)) { /* -a => all opts on */
142 toprint = (1 << 8) - 1;
143 unknown_str = ""; /* -a does not print unknown fields */
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000144 }
145
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000146 if (toprint == 0) { /* no opts => -s (sysname) */
147 toprint = 1;
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000148 }
149
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000150 uname(&uname_info.name); /* never fails */
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000151
152#if defined(__sparc__) && defined(__linux__)
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000153 if (fake_sparc && (fake_sparc[0] | 0x20) == 'y') {
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000154 strcpy(uname_info.name.machine, "sparc");
155 }
156#endif
Denis Vlasenko059138f2009-01-19 16:32:23 +0000157 strcpy(uname_info.processor, unknown_str);
158 strcpy(uname_info.platform, unknown_str);
Ron Yorston64ed5f02015-07-12 16:06:37 +0100159 strcpy(uname_info.os, CONFIG_UNAME_OSNAME);
Denis Vlasenko059138f2009-01-19 16:32:23 +0000160#if 0
161 /* Fedora does something like this */
162 strcpy(uname_info.processor, uname_info.name.machine);
163 strcpy(uname_info.platform, uname_info.name.machine);
164 if (uname_info.platform[0] == 'i'
165 && uname_info.platform[1]
166 && uname_info.platform[2] == '8'
167 && uname_info.platform[3] == '6'
168 ) {
169 uname_info.platform[1] = '3';
170 }
171#endif
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000172
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000173 delta = utsname_offset;
Denis Vlasenko059138f2009-01-19 16:32:23 +0000174 fmt = " %s" + 1;
Manuel Novoa III fa15f702002-03-25 02:37:20 +0000175 do {
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000176 if (toprint & 1) {
Denis Vlasenko059138f2009-01-19 16:32:23 +0000177 const char *p = (char *)(&uname_info) + *delta;
178 if (p[0]) {
179 printf(fmt, p);
180 fmt = " %s";
Manuel Novoa III fa15f702002-03-25 02:37:20 +0000181 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000182 }
Manuel Novoa III fa15f702002-03-25 02:37:20 +0000183 ++delta;
184 } while (toprint >>= 1);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000185 bb_putchar('\n');
Eric Andersenc8fdb561999-10-26 05:21:02 +0000186
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000187 fflush_stdout_and_exit(EXIT_SUCCESS); /* coreutils-6.9 compat */
Eric Andersenc8fdb561999-10-26 05:21:02 +0000188}