Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 2 | /* uname -- print system information |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 3 | * Copyright (C) 1989-1999 Free Software Foundation, Inc. |
| 4 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 5 | * 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] | 6 | */ |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 7 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 8 | /* BB_AUDIT SUSv3 compliant */ |
| 9 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */ |
| 10 | |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 11 | /* Option Example |
Denis Vlasenko | 059138f | 2009-01-19 16:32:23 +0000 | [diff] [blame] | 12 | * -s, --sysname SunOS |
| 13 | * -n, --nodename rocky8 |
| 14 | * -r, --release 4.0 |
| 15 | * -v, --version |
| 16 | * -m, --machine sun |
| 17 | * -a, --all SunOS rocky8 4.0 sun |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 18 | * |
Denis Vlasenko | 059138f | 2009-01-19 16:32:23 +0000 | [diff] [blame] | 19 | * The default behavior is equivalent to '-s'. |
| 20 | * |
| 21 | * David MacKenzie <djm@gnu.ai.mit.edu> |
| 22 | * |
| 23 | * GNU coreutils 6.10: |
| 24 | * Option: struct Example(s): |
| 25 | * utsname |
| 26 | * field: |
| 27 | * -s, --kernel-name sysname Linux |
| 28 | * -n, --nodename nodename localhost.localdomain |
| 29 | * -r, --kernel-release release 2.6.29 |
| 30 | * -v, --kernel-version version #1 SMP Sun Jan 11 20:52:37 EST 2009 |
| 31 | * -m, --machine machine x86_64 i686 |
| 32 | * -p, --processor (none) x86_64 i686 |
| 33 | * -i, --hardware-platform (none) x86_64 i386 |
| 34 | * NB: vanilla coreutils reports "unknown" -p and -i, |
| 35 | * x86_64 and i686/i386 shown above are Fedora's inventions. |
| 36 | * -o, --operating-system (none) GNU/Linux |
| 37 | * -a, --all: all of the above, in the order shown. |
| 38 | * If -p or -i is not known, don't show them |
| 39 | */ |
| 40 | |
| 41 | /* Busyboxed by Erik Andersen |
| 42 | * |
| 43 | * Before 2003: Glenn McGrath and Manuel Novoa III |
| 44 | * Further size reductions. |
| 45 | * Mar 16, 2003: Manuel Novoa III (mjn3@codepoet.org) |
| 46 | * Now does proper error checking on i/o. Plus some further space savings. |
| 47 | * Jan 2009: |
| 48 | * 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] | 49 | */ |
| 50 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 51 | //usage:#define uname_trivial_usage |
| 52 | //usage: "[-amnrspv]" |
| 53 | //usage:#define uname_full_usage "\n\n" |
| 54 | //usage: "Print system information\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 55 | //usage: "\n -a Print all" |
| 56 | //usage: "\n -m The machine (hardware) type" |
| 57 | //usage: "\n -n Hostname" |
| 58 | //usage: "\n -r OS release" |
| 59 | //usage: "\n -s OS name (default)" |
| 60 | //usage: "\n -p Processor type" |
| 61 | //usage: "\n -v OS version" |
| 62 | //usage: |
| 63 | //usage:#define uname_example_usage |
| 64 | //usage: "$ uname -a\n" |
| 65 | //usage: "Linux debian 2.4.23 #2 Tue Dec 23 17:09:10 MST 2003 i686 GNU/Linux\n" |
| 66 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 67 | #include "libbb.h" |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 68 | /* After libbb.h, since it needs sys/types.h on some systems */ |
| 69 | #include <sys/utsname.h> |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 70 | |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 71 | typedef struct { |
| 72 | struct utsname name; |
Denis Vlasenko | 059138f | 2009-01-19 16:32:23 +0000 | [diff] [blame] | 73 | char processor[sizeof(((struct utsname*)NULL)->machine)]; |
| 74 | char platform[sizeof(((struct utsname*)NULL)->machine)]; |
| 75 | char os[sizeof("GNU/Linux")]; |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 76 | } uname_info_t; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 77 | |
Denis Vlasenko | 059138f | 2009-01-19 16:32:23 +0000 | [diff] [blame] | 78 | static const char options[] ALIGN1 = "snrvmpioa"; |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 79 | static const unsigned short utsname_offset[] = { |
Denis Vlasenko | 059138f | 2009-01-19 16:32:23 +0000 | [diff] [blame] | 80 | offsetof(uname_info_t, name.sysname), /* -s */ |
| 81 | offsetof(uname_info_t, name.nodename), /* -n */ |
| 82 | offsetof(uname_info_t, name.release), /* -r */ |
| 83 | offsetof(uname_info_t, name.version), /* -v */ |
| 84 | offsetof(uname_info_t, name.machine), /* -m */ |
| 85 | offsetof(uname_info_t, processor), /* -p */ |
| 86 | offsetof(uname_info_t, platform), /* -i */ |
| 87 | offsetof(uname_info_t, os), /* -o */ |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 88 | }; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 89 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 90 | int uname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 91 | int uname_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 92 | { |
Denys Vlasenko | f3b92d3 | 2009-06-19 12:10:38 +0200 | [diff] [blame] | 93 | #if ENABLE_LONG_OPTS |
Denys Vlasenko | 7aca89a | 2009-07-18 03:41:29 +0200 | [diff] [blame] | 94 | static const char uname_longopts[] ALIGN1 = |
Denis Vlasenko | 447ab18 | 2009-03-14 18:41:19 +0000 | [diff] [blame] | 95 | /* name, has_arg, val */ |
| 96 | "all\0" No_argument "a" |
| 97 | "kernel-name\0" No_argument "s" |
| 98 | "nodename\0" No_argument "n" |
| 99 | "kernel-release\0" No_argument "r" |
| 100 | "release\0" No_argument "r" |
| 101 | "kernel-version\0" No_argument "v" |
| 102 | "machine\0" No_argument "m" |
| 103 | "processor\0" No_argument "p" |
| 104 | "hardware-platform\0" No_argument "i" |
| 105 | "operating-system\0" No_argument "o" |
| 106 | ; |
| 107 | #endif |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 108 | uname_info_t uname_info; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 109 | #if defined(__sparc__) && defined(__linux__) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 110 | char *fake_sparc = getenv("FAKE_SPARC"); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 111 | #endif |
Denis Vlasenko | 059138f | 2009-01-19 16:32:23 +0000 | [diff] [blame] | 112 | const char *unknown_str = "unknown"; |
| 113 | const char *fmt; |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 114 | const unsigned short *delta; |
Denis Vlasenko | 038eec7 | 2009-01-19 16:38:30 +0000 | [diff] [blame] | 115 | unsigned toprint; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 116 | |
Denys Vlasenko | 7aca89a | 2009-07-18 03:41:29 +0200 | [diff] [blame] | 117 | IF_LONG_OPTS(applet_long_options = uname_longopts); |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 118 | toprint = getopt32(argv, options); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 119 | |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 120 | if (argv[optind]) { /* coreutils-6.9 compat */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 121 | bb_show_usage(); |
| 122 | } |
| 123 | |
Denis Vlasenko | 059138f | 2009-01-19 16:32:23 +0000 | [diff] [blame] | 124 | if (toprint & (1 << 8)) { /* -a => all opts on */ |
| 125 | toprint = (1 << 8) - 1; |
| 126 | unknown_str = ""; /* -a does not print unknown fields */ |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 129 | if (toprint == 0) { /* no opts => -s (sysname) */ |
| 130 | toprint = 1; |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 133 | uname(&uname_info.name); /* never fails */ |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 134 | |
| 135 | #if defined(__sparc__) && defined(__linux__) |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 136 | if (fake_sparc && (fake_sparc[0] | 0x20) == 'y') { |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 137 | strcpy(uname_info.name.machine, "sparc"); |
| 138 | } |
| 139 | #endif |
Denis Vlasenko | 059138f | 2009-01-19 16:32:23 +0000 | [diff] [blame] | 140 | strcpy(uname_info.processor, unknown_str); |
| 141 | strcpy(uname_info.platform, unknown_str); |
| 142 | strcpy(uname_info.os, "GNU/Linux"); |
| 143 | #if 0 |
| 144 | /* Fedora does something like this */ |
| 145 | strcpy(uname_info.processor, uname_info.name.machine); |
| 146 | strcpy(uname_info.platform, uname_info.name.machine); |
| 147 | if (uname_info.platform[0] == 'i' |
| 148 | && uname_info.platform[1] |
| 149 | && uname_info.platform[2] == '8' |
| 150 | && uname_info.platform[3] == '6' |
| 151 | ) { |
| 152 | uname_info.platform[1] = '3'; |
| 153 | } |
| 154 | #endif |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 155 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 156 | delta = utsname_offset; |
Denis Vlasenko | 059138f | 2009-01-19 16:32:23 +0000 | [diff] [blame] | 157 | fmt = " %s" + 1; |
Manuel Novoa III | fa15f70 | 2002-03-25 02:37:20 +0000 | [diff] [blame] | 158 | do { |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 159 | if (toprint & 1) { |
Denis Vlasenko | 059138f | 2009-01-19 16:32:23 +0000 | [diff] [blame] | 160 | const char *p = (char *)(&uname_info) + *delta; |
| 161 | if (p[0]) { |
| 162 | printf(fmt, p); |
| 163 | fmt = " %s"; |
Manuel Novoa III | fa15f70 | 2002-03-25 02:37:20 +0000 | [diff] [blame] | 164 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 165 | } |
Manuel Novoa III | fa15f70 | 2002-03-25 02:37:20 +0000 | [diff] [blame] | 166 | ++delta; |
| 167 | } while (toprint >>= 1); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 168 | bb_putchar('\n'); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 169 | |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 170 | fflush_stdout_and_exit(EXIT_SUCCESS); /* coreutils-6.9 compat */ |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 171 | } |