Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | a02a4e9 | 2017-10-05 15:19:25 +0200 | [diff] [blame^] | 2 | /* |
| 3 | * uname -- print system information |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 4 | * Copyright (C) 1989-1999 Free Software Foundation, Inc. |
| 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 7 | */ |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 8 | /* Option Example |
Denis Vlasenko | 059138f | 2009-01-19 16:32:23 +0000 | [diff] [blame] | 9 | * -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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 15 | * |
Denis Vlasenko | 059138f | 2009-01-19 16:32:23 +0000 | [diff] [blame] | 16 | * 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 Vlasenko | 059138f | 2009-01-19 16:32:23 +0000 | [diff] [blame] | 37 | /* 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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 46 | //config:config UNAME |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 47 | //config: bool "uname (3.7 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 48 | //config: default y |
| 49 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 50 | //config: uname is used to print system information. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 51 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 57 | //config: Sets the operating system name reported by uname -o. The |
| 58 | //config: default is "GNU/Linux". |
Denys Vlasenko | c49638b | 2017-07-16 16:43:41 +0200 | [diff] [blame] | 59 | //config: |
| 60 | //can't use "ARCH" for this applet, all hell breaks loose in build system :) |
Denys Vlasenko | 483405a | 2017-07-18 20:17:51 +0200 | [diff] [blame] | 61 | //config:config BB_ARCH |
Denys Vlasenko | ae178ce | 2017-07-19 14:32:54 +0200 | [diff] [blame] | 62 | //config: bool "arch (1.6 kb)" |
Denys Vlasenko | c49638b | 2017-07-16 16:43:41 +0200 | [diff] [blame] | 63 | //config: default y |
| 64 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 65 | //config: Same as uname -m. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 66 | |
Denys Vlasenko | 819b47a | 2017-08-03 03:29:32 +0200 | [diff] [blame] | 67 | // 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 Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 70 | |
Denys Vlasenko | 483405a | 2017-07-18 20:17:51 +0200 | [diff] [blame] | 71 | //kbuild:lib-$(CONFIG_UNAME) += uname.o |
| 72 | //kbuild:lib-$(CONFIG_BB_ARCH) += uname.o |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 73 | |
| 74 | /* BB_AUDIT SUSv3 compliant */ |
| 75 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 76 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 77 | //usage:#define uname_trivial_usage |
Mike Frysinger | 29ed580 | 2014-01-31 00:28:42 -0500 | [diff] [blame] | 78 | //usage: "[-amnrspvio]" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 79 | //usage:#define uname_full_usage "\n\n" |
| 80 | //usage: "Print system information\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 81 | //usage: "\n -a Print all" |
| 82 | //usage: "\n -m The machine (hardware) type" |
| 83 | //usage: "\n -n Hostname" |
Mike Frysinger | 29ed580 | 2014-01-31 00:28:42 -0500 | [diff] [blame] | 84 | //usage: "\n -r Kernel release" |
| 85 | //usage: "\n -s Kernel name (default)" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 86 | //usage: "\n -p Processor type" |
Mike Frysinger | 29ed580 | 2014-01-31 00:28:42 -0500 | [diff] [blame] | 87 | //usage: "\n -v Kernel version" |
| 88 | //usage: "\n -i The hardware platform" |
| 89 | //usage: "\n -o OS name" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 90 | //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 Vlasenko | c49638b | 2017-07-16 16:43:41 +0200 | [diff] [blame] | 95 | //usage:#define arch_trivial_usage |
| 96 | //usage: "" |
| 97 | //usage:#define arch_full_usage "\n\n" |
| 98 | //usage: "Print system architecture" |
| 99 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 100 | #include "libbb.h" |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 101 | /* After libbb.h, since it needs sys/types.h on some systems */ |
| 102 | #include <sys/utsname.h> |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 103 | |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 104 | typedef struct { |
| 105 | struct utsname name; |
Denis Vlasenko | 059138f | 2009-01-19 16:32:23 +0000 | [diff] [blame] | 106 | char processor[sizeof(((struct utsname*)NULL)->machine)]; |
| 107 | char platform[sizeof(((struct utsname*)NULL)->machine)]; |
Ron Yorston | 64ed5f0 | 2015-07-12 16:06:37 +0100 | [diff] [blame] | 108 | char os[sizeof(CONFIG_UNAME_OSNAME)]; |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 109 | } uname_info_t; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 110 | |
Denys Vlasenko | c49638b | 2017-07-16 16:43:41 +0200 | [diff] [blame] | 111 | #if ENABLE_UNAME |
| 112 | #define options "snrvmpioa" |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 113 | static const unsigned short utsname_offset[] = { |
Denis Vlasenko | 059138f | 2009-01-19 16:32:23 +0000 | [diff] [blame] | 114 | 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 | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 122 | }; |
Denys Vlasenko | c49638b | 2017-07-16 16:43:41 +0200 | [diff] [blame] | 123 | #endif |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 124 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 125 | int uname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | c49638b | 2017-07-16 16:43:41 +0200 | [diff] [blame] | 126 | int uname_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 127 | { |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 128 | uname_info_t uname_info; |
Denys Vlasenko | c49638b | 2017-07-16 16:43:41 +0200 | [diff] [blame] | 129 | IF_UNAME(const char *unknown_str = "unknown";) |
Denis Vlasenko | 038eec7 | 2009-01-19 16:38:30 +0000 | [diff] [blame] | 130 | unsigned toprint; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 131 | |
Denys Vlasenko | c49638b | 2017-07-16 16:43:41 +0200 | [diff] [blame] | 132 | toprint = (1 << 4); /* "arch" = "uname -m" */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 133 | |
Denys Vlasenko | c49638b | 2017-07-16 16:43:41 +0200 | [diff] [blame] | 134 | #if ENABLE_UNAME |
Denys Vlasenko | 483405a | 2017-07-18 20:17:51 +0200 | [diff] [blame] | 135 | if (!ENABLE_BB_ARCH || applet_name[0] == 'u') { |
Denys Vlasenko | c49638b | 2017-07-16 16:43:41 +0200 | [diff] [blame] | 136 | # if ENABLE_LONG_OPTS |
| 137 | static const char uname_longopts[] ALIGN1 = |
| 138 | /* name, has_arg, val */ |
| 139 | "all\0" No_argument "a" |
| 140 | "kernel-name\0" No_argument "s" |
| 141 | "nodename\0" No_argument "n" |
| 142 | "kernel-release\0" No_argument "r" |
| 143 | "release\0" No_argument "r" |
| 144 | "kernel-version\0" No_argument "v" |
| 145 | "machine\0" No_argument "m" |
| 146 | "processor\0" No_argument "p" |
| 147 | "hardware-platform\0" No_argument "i" |
| 148 | "operating-system\0" No_argument "o" |
| 149 | ; |
| 150 | # endif |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 151 | toprint = getopt32long(argv, options, uname_longopts); |
Denys Vlasenko | c49638b | 2017-07-16 16:43:41 +0200 | [diff] [blame] | 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 | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 164 | |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 165 | uname(&uname_info.name); /* never fails */ |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 166 | |
| 167 | #if defined(__sparc__) && defined(__linux__) |
Denys Vlasenko | c49638b | 2017-07-16 16:43:41 +0200 | [diff] [blame] | 168 | { |
| 169 | char *fake_sparc = getenv("FAKE_SPARC"); |
| 170 | if (fake_sparc && (fake_sparc[0] | 0x20) == 'y') { |
| 171 | strcpy(uname_info.name.machine, "sparc"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 172 | } |
Denys Vlasenko | c49638b | 2017-07-16 16:43:41 +0200 | [diff] [blame] | 173 | } |
| 174 | #endif |
Denys Vlasenko | 483405a | 2017-07-18 20:17:51 +0200 | [diff] [blame] | 175 | if (ENABLE_BB_ARCH && (!ENABLE_UNAME || applet_name[0] == 'a')) { |
Denys Vlasenko | c49638b | 2017-07-16 16:43:41 +0200 | [diff] [blame] | 176 | 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); |
Denys Vlasenko | 663ae52 | 2017-08-03 03:46:14 +0200 | [diff] [blame] | 186 | # if ENABLE_FEDORA_COMPAT |
Denys Vlasenko | c49638b | 2017-07-16 16:43:41 +0200 | [diff] [blame] | 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 Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 213 | |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 214 | fflush_stdout_and_exit(EXIT_SUCCESS); /* coreutils-6.9 compat */ |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 215 | } |