Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/uptime.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /* GNU's uptime. | - | ||||||||||||
2 | Copyright (C) 1992-2018 Free Software Foundation, Inc. | - | ||||||||||||
3 | - | |||||||||||||
4 | This program is free software: you can redistribute it and/or modify | - | ||||||||||||
5 | it under the terms of the GNU General Public License as published by | - | ||||||||||||
6 | the Free Software Foundation, either version 3 of the License, or | - | ||||||||||||
7 | (at your option) any later version. | - | ||||||||||||
8 | - | |||||||||||||
9 | This program is distributed in the hope that it will be useful, | - | ||||||||||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | - | ||||||||||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | - | ||||||||||||
12 | GNU General Public License for more details. | - | ||||||||||||
13 | - | |||||||||||||
14 | You should have received a copy of the GNU General Public License | - | ||||||||||||
15 | along with this program. If not, see <https://www.gnu.org/licenses/>. */ | - | ||||||||||||
16 | - | |||||||||||||
17 | /* Created by hacking who.c by Kaveh Ghazi ghazi@caip.rutgers.edu. */ | - | ||||||||||||
18 | - | |||||||||||||
19 | #include <config.h> | - | ||||||||||||
20 | #include <getopt.h> | - | ||||||||||||
21 | #include <stdio.h> | - | ||||||||||||
22 | - | |||||||||||||
23 | #include <sys/types.h> | - | ||||||||||||
24 | #include "system.h" | - | ||||||||||||
25 | - | |||||||||||||
26 | #if HAVE_SYSCTL && HAVE_SYS_SYSCTL_H | - | ||||||||||||
27 | # include <sys/sysctl.h> | - | ||||||||||||
28 | #endif | - | ||||||||||||
29 | - | |||||||||||||
30 | #if HAVE_OS_H | - | ||||||||||||
31 | # include <OS.h> | - | ||||||||||||
32 | #endif | - | ||||||||||||
33 | - | |||||||||||||
34 | #include "c-strtod.h" | - | ||||||||||||
35 | #include "die.h" | - | ||||||||||||
36 | #include "error.h" | - | ||||||||||||
37 | #include "long-options.h" | - | ||||||||||||
38 | #include "quote.h" | - | ||||||||||||
39 | #include "readutmp.h" | - | ||||||||||||
40 | #include "fprintftime.h" | - | ||||||||||||
41 | - | |||||||||||||
42 | /* The official name of this program (e.g., no 'g' prefix). */ | - | ||||||||||||
43 | #define PROGRAM_NAME "uptime" | - | ||||||||||||
44 | - | |||||||||||||
45 | #define AUTHORS \ | - | ||||||||||||
46 | proper_name ("Joseph Arceneaux"), \ | - | ||||||||||||
47 | proper_name ("David MacKenzie"), \ | - | ||||||||||||
48 | proper_name ("Kaveh Ghazi") | - | ||||||||||||
49 | - | |||||||||||||
50 | static struct option const long_options[] = | - | ||||||||||||
51 | { | - | ||||||||||||
52 | {NULL, 0, NULL, 0} | - | ||||||||||||
53 | }; | - | ||||||||||||
54 | - | |||||||||||||
55 | static void | - | ||||||||||||
56 | print_uptime (size_t n, const STRUCT_UTMP *this) | - | ||||||||||||
57 | { | - | ||||||||||||
58 | size_t entries = 0; | - | ||||||||||||
59 | time_t boot_time = 0; | - | ||||||||||||
60 | time_t time_now; | - | ||||||||||||
61 | time_t uptime = 0; | - | ||||||||||||
62 | long int updays; | - | ||||||||||||
63 | int uphours; | - | ||||||||||||
64 | int upmins; | - | ||||||||||||
65 | struct tm *tmn; | - | ||||||||||||
66 | double avg[3]; | - | ||||||||||||
67 | int loads; | - | ||||||||||||
68 | #ifdef HAVE_PROC_UPTIME | - | ||||||||||||
69 | FILE *fp; | - | ||||||||||||
70 | - | |||||||||||||
71 | fp = fopen ("/proc/uptime", "r"); | - | ||||||||||||
72 | if (fp != NULL)
| 0 | ||||||||||||
73 | { | - | ||||||||||||
74 | char buf[BUFSIZ]; | - | ||||||||||||
75 | char *b = fgets (buf, BUFSIZ, fp); | - | ||||||||||||
76 | if (b == buf)
| 0 | ||||||||||||
77 | { | - | ||||||||||||
78 | char *end_ptr; | - | ||||||||||||
79 | double upsecs = c_strtod (buf, &end_ptr); | - | ||||||||||||
80 | if (buf != end_ptr)
| 0 | ||||||||||||
81 | uptime = (0 <= upsecs && upsecs < TYPE_MAXIMUM (time_t) never executed: uptime = (0 <= upsecs && upsecs < ((time_t) (! (! ((time_t) 0 < (time_t) -1)) ? (time_t) -1 : ((((time_t) 1 << ((sizeof (time_t) * 8) - 2)) - 1) * 2 + 1))) ? upsecs : -1);
| 0 | ||||||||||||
82 | ? upsecs : -1); never executed: uptime = (0 <= upsecs && upsecs < ((time_t) (! (! ((time_t) 0 < (time_t) -1)) ? (time_t) -1 : ((((time_t) 1 << ((sizeof (time_t) * 8) - 2)) - 1) * 2 + 1))) ? upsecs : -1); | 0 | ||||||||||||
83 | } never executed: end of block | 0 | ||||||||||||
84 | - | |||||||||||||
85 | fclose (fp); | - | ||||||||||||
86 | } never executed: end of block | 0 | ||||||||||||
87 | #endif /* HAVE_PROC_UPTIME */ | - | ||||||||||||
88 | - | |||||||||||||
89 | #if HAVE_SYSCTL && defined CTL_KERN && defined KERN_BOOTTIME | - | ||||||||||||
90 | { | - | ||||||||||||
91 | /* FreeBSD specific: fetch sysctl "kern.boottime". */ | - | ||||||||||||
92 | static int request[2] = { CTL_KERN, KERN_BOOTTIME }; | - | ||||||||||||
93 | struct timeval result; | - | ||||||||||||
94 | size_t result_len = sizeof result; | - | ||||||||||||
95 | - | |||||||||||||
96 | if (sysctl (request, 2, &result, &result_len, NULL, 0) >= 0) | - | ||||||||||||
97 | boot_time = result.tv_sec; | - | ||||||||||||
98 | } | - | ||||||||||||
99 | #endif | - | ||||||||||||
100 | - | |||||||||||||
101 | #if HAVE_OS_H /* BeOS */ | - | ||||||||||||
102 | { | - | ||||||||||||
103 | system_info si; | - | ||||||||||||
104 | - | |||||||||||||
105 | get_system_info (&si); | - | ||||||||||||
106 | boot_time = si.boot_time / 1000000; | - | ||||||||||||
107 | } | - | ||||||||||||
108 | #endif | - | ||||||||||||
109 | - | |||||||||||||
110 | #if HAVE_UTMPX_H || HAVE_UTMP_H | - | ||||||||||||
111 | /* Loop through all the utmp entries we just read and count up the valid | - | ||||||||||||
112 | ones, also in the process possibly gleaning boottime. */ | - | ||||||||||||
113 | while (n--)
| 0 | ||||||||||||
114 | { | - | ||||||||||||
115 | entries += IS_USER_PROCESS (this); dead code: ((this)->ut_tv.tv_sec) != 0
| - | ||||||||||||
116 | if (UT_TYPE_BOOT_TIME (this))
| 0 | ||||||||||||
117 | boot_time = UT_TIME_MEMBER (this); never executed: boot_time = ((this)->ut_tv.tv_sec); | 0 | ||||||||||||
118 | ++this; | - | ||||||||||||
119 | } never executed: end of block | 0 | ||||||||||||
120 | #else | - | ||||||||||||
121 | (void) n; | - | ||||||||||||
122 | (void) this; | - | ||||||||||||
123 | #endif | - | ||||||||||||
124 | - | |||||||||||||
125 | time_now = time (NULL); | - | ||||||||||||
126 | #if defined HAVE_PROC_UPTIME | - | ||||||||||||
127 | if (uptime == 0)
| 0 | ||||||||||||
128 | #endif | - | ||||||||||||
129 | { | - | ||||||||||||
130 | if (boot_time == 0)
| 0 | ||||||||||||
131 | die (EXIT_FAILURE, errno, _("couldn't get boot time")); never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), dcgettext (((void *)0), \"couldn't get boot time\", 5)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "couldn't get boot time" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "couldn't get boot time" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ())))); | 0 | ||||||||||||
132 | uptime = time_now - boot_time; | - | ||||||||||||
133 | } never executed: end of block | 0 | ||||||||||||
134 | updays = uptime / 86400; | - | ||||||||||||
135 | uphours = (uptime - (updays * 86400)) / 3600; | - | ||||||||||||
136 | upmins = (uptime - (updays * 86400) - (uphours * 3600)) / 60; | - | ||||||||||||
137 | tmn = localtime (&time_now); | - | ||||||||||||
138 | /* procps' version of uptime also prints the seconds field, but | - | ||||||||||||
139 | previous versions of coreutils don't. */ | - | ||||||||||||
140 | if (tmn)
| 0 | ||||||||||||
141 | /* TRANSLATORS: This prints the current clock time. */ | - | ||||||||||||
142 | fprintftime (stdout, _(" %H:%M:%S "), tmn, 0, 0); never executed: fprintftime ( stdout , dcgettext (((void *)0), " %H:%M:%S " , 5) , tmn, 0, 0); | 0 | ||||||||||||
143 | else | - | ||||||||||||
144 | printf (_(" ??:???? ")); never executed: printf ( dcgettext (((void *)0), " ??:???? " , 5) ); | 0 | ||||||||||||
145 | if (uptime == (time_t) -1)
| 0 | ||||||||||||
146 | printf (_("up ???? days ??:??, ")); never executed: printf ( dcgettext (((void *)0), "up ???? days ??:??, " , 5) ); | 0 | ||||||||||||
147 | else | - | ||||||||||||
148 | { | - | ||||||||||||
149 | if (0 < updays)
| 0 | ||||||||||||
150 | printf (ngettext ("up %ld day %2d:%02d, ", never executed: printf ( dcngettext (((void *)0), "up %ld day %2d:%02d, " , "up %ld days %2d:%02d, " , select_plural (updays) , 5) , updays, uphours, upmins); | 0 | ||||||||||||
151 | "up %ld days %2d:%02d, ", never executed: printf ( dcngettext (((void *)0), "up %ld day %2d:%02d, " , "up %ld days %2d:%02d, " , select_plural (updays) , 5) , updays, uphours, upmins); | 0 | ||||||||||||
152 | select_plural (updays)), never executed: printf ( dcngettext (((void *)0), "up %ld day %2d:%02d, " , "up %ld days %2d:%02d, " , select_plural (updays) , 5) , updays, uphours, upmins); | 0 | ||||||||||||
153 | updays, uphours, upmins); never executed: printf ( dcngettext (((void *)0), "up %ld day %2d:%02d, " , "up %ld days %2d:%02d, " , select_plural (updays) , 5) , updays, uphours, upmins); | 0 | ||||||||||||
154 | else | - | ||||||||||||
155 | printf (_("up %2d:%02d, "), uphours, upmins); never executed: printf ( dcgettext (((void *)0), "up %2d:%02d, " , 5) , uphours, upmins); | 0 | ||||||||||||
156 | } | - | ||||||||||||
157 | printf (ngettext ("%lu user", "%lu users", select_plural (entries)), | - | ||||||||||||
158 | (unsigned long int) entries); | - | ||||||||||||
159 | - | |||||||||||||
160 | loads = getloadavg (avg, 3); | - | ||||||||||||
161 | - | |||||||||||||
162 | if (loads == -1)
| 0 | ||||||||||||
163 | putchar ('\n'); never executed: putchar_unlocked ('\n'); | 0 | ||||||||||||
164 | else | - | ||||||||||||
165 | { | - | ||||||||||||
166 | if (loads > 0)
| 0 | ||||||||||||
167 | printf (_(", load average: %.2f"), avg[0]); never executed: printf ( dcgettext (((void *)0), ", load average: %.2f" , 5) , avg[0]); | 0 | ||||||||||||
168 | if (loads > 1)
| 0 | ||||||||||||
169 | printf (", %.2f", avg[1]); never executed: printf (", %.2f", avg[1]); | 0 | ||||||||||||
170 | if (loads > 2)
| 0 | ||||||||||||
171 | printf (", %.2f", avg[2]); never executed: printf (", %.2f", avg[2]); | 0 | ||||||||||||
172 | if (loads > 0)
| 0 | ||||||||||||
173 | putchar ('\n'); never executed: putchar_unlocked ('\n'); | 0 | ||||||||||||
174 | } never executed: end of block | 0 | ||||||||||||
175 | } | - | ||||||||||||
176 | - | |||||||||||||
177 | /* Display the system uptime and the number of users on the system, | - | ||||||||||||
178 | according to utmp file FILENAME. Use read_utmp OPTIONS to read the | - | ||||||||||||
179 | utmp file. */ | - | ||||||||||||
180 | - | |||||||||||||
181 | static void | - | ||||||||||||
182 | uptime (const char *filename, int options) | - | ||||||||||||
183 | { | - | ||||||||||||
184 | size_t n_users; | - | ||||||||||||
185 | STRUCT_UTMP *utmp_buf = NULL; | - | ||||||||||||
186 | - | |||||||||||||
187 | #if HAVE_UTMPX_H || HAVE_UTMP_H | - | ||||||||||||
188 | if (read_utmp (filename, &n_users, &utmp_buf, options) != 0)
| 0 | ||||||||||||
189 | die (EXIT_FAILURE, errno, "%s", quotef (filename)); never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), \"%s\", quotearg_n_style_colon (0, shell_escape_quoting_style, filename)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , (*__errno_location ...(0, shell_escape_quoting_style, filename)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , "%s", quotearg_n_style_colon (0, shell_escape_quoting_style, filename)), (( 0 ) ? (void) 0 : __builtin_unreachable ())))); | 0 | ||||||||||||
190 | #endif | - | ||||||||||||
191 | - | |||||||||||||
192 | print_uptime (n_users, utmp_buf); | - | ||||||||||||
193 | - | |||||||||||||
194 | IF_LINT (free (utmp_buf)); | - | ||||||||||||
195 | } never executed: end of block | 0 | ||||||||||||
196 | - | |||||||||||||
197 | void | - | ||||||||||||
198 | usage (int status) | - | ||||||||||||
199 | { | - | ||||||||||||
200 | if (status != EXIT_SUCCESS)
| 3 | ||||||||||||
201 | emit_try_help (); executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||
202 | else | - | ||||||||||||
203 | { | - | ||||||||||||
204 | printf (_("Usage: %s [OPTION]... [FILE]\n"), program_name); | - | ||||||||||||
205 | printf (_("\ | - | ||||||||||||
206 | Print the current time, the length of time the system has been up,\n\ | - | ||||||||||||
207 | the number of users on the system, and the average number of jobs\n\ | - | ||||||||||||
208 | in the run queue over the last 1, 5 and 15 minutes.")); | - | ||||||||||||
209 | #ifdef __linux__ | - | ||||||||||||
210 | /* It would be better to introduce a configure test for this, | - | ||||||||||||
211 | but such a test is hard to write. For the moment then, we | - | ||||||||||||
212 | have a hack which depends on the preprocessor used at compile | - | ||||||||||||
213 | time to tell us what the running kernel is. Ugh. */ | - | ||||||||||||
214 | printf (_(" \ | - | ||||||||||||
215 | Processes in\n\ | - | ||||||||||||
216 | an uninterruptible sleep state also contribute to the load average.\n")); | - | ||||||||||||
217 | #else | - | ||||||||||||
218 | printf (_("\n")); | - | ||||||||||||
219 | #endif | - | ||||||||||||
220 | printf (_("\ | - | ||||||||||||
221 | If FILE is not specified, use %s. %s as FILE is common.\n\ | - | ||||||||||||
222 | \n"), | - | ||||||||||||
223 | UTMP_FILE, WTMP_FILE); | - | ||||||||||||
224 | fputs (HELP_OPTION_DESCRIPTION, stdout); | - | ||||||||||||
225 | fputs (VERSION_OPTION_DESCRIPTION, stdout); | - | ||||||||||||
226 | emit_ancillary_info (PROGRAM_NAME); | - | ||||||||||||
227 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||
228 | exit (status); executed 6 times by 1 test: exit (status); Executed by:
| 6 | ||||||||||||
229 | } | - | ||||||||||||
230 | - | |||||||||||||
231 | int | - | ||||||||||||
232 | main (int argc, char **argv) | - | ||||||||||||
233 | { | - | ||||||||||||
234 | initialize_main (&argc, &argv); | - | ||||||||||||
235 | set_program_name (argv[0]); | - | ||||||||||||
236 | setlocale (LC_ALL, ""); | - | ||||||||||||
237 | bindtextdomain (PACKAGE, LOCALEDIR); | - | ||||||||||||
238 | textdomain (PACKAGE); | - | ||||||||||||
239 | - | |||||||||||||
240 | atexit (close_stdout); | - | ||||||||||||
241 | - | |||||||||||||
242 | parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, | - | ||||||||||||
243 | usage, AUTHORS, (char const *) NULL); | - | ||||||||||||
244 | if (getopt_long (argc, argv, "", long_options, NULL) != -1)
| 0-3 | ||||||||||||
245 | usage (EXIT_FAILURE); executed 3 times by 1 test: usage ( 1 ); Executed by:
| 3 | ||||||||||||
246 | - | |||||||||||||
247 | switch (argc - optind) | - | ||||||||||||
248 | { | - | ||||||||||||
249 | case 0: /* uptime */ never executed: case 0: | 0 | ||||||||||||
250 | uptime (UTMP_FILE, READ_UTMP_CHECK_PIDS); | - | ||||||||||||
251 | break; never executed: break; | 0 | ||||||||||||
252 | - | |||||||||||||
253 | case 1: /* uptime <utmp file> */ never executed: case 1: | 0 | ||||||||||||
254 | uptime (argv[optind], 0); | - | ||||||||||||
255 | break; never executed: break; | 0 | ||||||||||||
256 | - | |||||||||||||
257 | default: /* lose */ never executed: default: | 0 | ||||||||||||
258 | error (0, 0, _("extra operand %s"), quote (argv[optind + 1])); | - | ||||||||||||
259 | usage (EXIT_FAILURE); | - | ||||||||||||
260 | } never executed: end of block | 0 | ||||||||||||
261 | - | |||||||||||||
262 | return EXIT_SUCCESS; never executed: return 0 ; | 0 | ||||||||||||
263 | } | - | ||||||||||||
Source code | Switch to Preprocessed file |