Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | ebe6d9d | 2017-10-05 14:40:24 +0200 | [diff] [blame] | 2 | /* |
| 3 | * 'time' utility to display resource usage of processes. |
| 4 | * Copyright (C) 1990, 91, 92, 93, 96 Free Software Foundation, Inc. |
| 5 | * |
| 6 | * Licensed under GPLv2, see file LICENSE in this source tree. |
| 7 | */ |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 8 | /* Originally written by David Keppel <pardo@cs.washington.edu>. |
Denys Vlasenko | ebe6d9d | 2017-10-05 14:40:24 +0200 | [diff] [blame] | 9 | * Heavily modified by David MacKenzie <djm@gnu.ai.mit.edu>. |
| 10 | * Heavily modified for busybox by Erik Andersen <andersen@codepoet.org> |
| 11 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 12 | //config:config TIME |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 13 | //config: bool "time (6.8 kb)" |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 14 | //config: default y |
| 15 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 16 | //config: The time command runs the specified program with the given arguments. |
| 17 | //config: When the command finishes, time writes a message to standard output |
| 18 | //config: giving timing statistics about this program run. |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 19 | |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 20 | //applet:IF_TIME(APPLET(time, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 21 | |
| 22 | //kbuild:lib-$(CONFIG_TIME) += time.o |
| 23 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 24 | //usage:#define time_trivial_usage |
Tommi Rantala | 5fe5be2 | 2017-04-28 17:54:14 +0200 | [diff] [blame] | 25 | //usage: "[-vpa] [-o FILE] PROG ARGS" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 26 | //usage:#define time_full_usage "\n\n" |
| 27 | //usage: "Run PROG, display resource usage when it exits\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 28 | //usage: "\n -v Verbose" |
Tommi Rantala | 854174f | 2017-04-24 19:08:53 +0300 | [diff] [blame] | 29 | //usage: "\n -p POSIX output format" |
Denys Vlasenko | a1a3b59 | 2017-04-28 18:01:18 +0200 | [diff] [blame] | 30 | //usage: "\n -f FMT Custom format" |
Tommi Rantala | 5fe5be2 | 2017-04-28 17:54:14 +0200 | [diff] [blame] | 31 | //usage: "\n -o FILE Write result to FILE" |
| 32 | //usage: "\n -a Append (else overwrite)" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 33 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 34 | #include "libbb.h" |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 35 | |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 36 | /* Information on the resources used by a child process. */ |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 37 | typedef struct { |
| 38 | int waitstatus; |
| 39 | struct rusage ru; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 40 | unsigned elapsed_ms; /* Wallclock time of process. */ |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 41 | } resource_t; |
| 42 | |
| 43 | /* msec = milliseconds = 1/1,000 (1*10e-3) second. |
| 44 | usec = microseconds = 1/1,000,000 (1*10e-6) second. */ |
| 45 | |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 46 | #define UL unsigned long |
| 47 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 48 | static const char default_format[] ALIGN1 = "real\t%E\nuser\t%u\nsys\t%T"; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 49 | |
| 50 | /* The output format for the -p option .*/ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 51 | static const char posix_format[] ALIGN1 = "real %e\nuser %U\nsys %S"; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 52 | |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 53 | /* Format string for printing all statistics verbosely. |
| 54 | Keep this output to 24 lines so users on terminals can see it all.*/ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 55 | static const char long_format[] ALIGN1 = |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 56 | "\tCommand being timed: \"%C\"\n" |
| 57 | "\tUser time (seconds): %U\n" |
| 58 | "\tSystem time (seconds): %S\n" |
| 59 | "\tPercent of CPU this job got: %P\n" |
| 60 | "\tElapsed (wall clock) time (h:mm:ss or m:ss): %E\n" |
| 61 | "\tAverage shared text size (kbytes): %X\n" |
| 62 | "\tAverage unshared data size (kbytes): %D\n" |
| 63 | "\tAverage stack size (kbytes): %p\n" |
| 64 | "\tAverage total size (kbytes): %K\n" |
| 65 | "\tMaximum resident set size (kbytes): %M\n" |
| 66 | "\tAverage resident set size (kbytes): %t\n" |
| 67 | "\tMajor (requiring I/O) page faults: %F\n" |
| 68 | "\tMinor (reclaiming a frame) page faults: %R\n" |
| 69 | "\tVoluntary context switches: %w\n" |
| 70 | "\tInvoluntary context switches: %c\n" |
| 71 | "\tSwaps: %W\n" |
| 72 | "\tFile system inputs: %I\n" |
| 73 | "\tFile system outputs: %O\n" |
| 74 | "\tSocket messages sent: %s\n" |
| 75 | "\tSocket messages received: %r\n" |
| 76 | "\tSignals delivered: %k\n" |
Denis Vlasenko | f93ab47 | 2006-12-22 12:36:13 +0000 | [diff] [blame] | 77 | "\tPage size (bytes): %Z\n" |
| 78 | "\tExit status: %x"; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 79 | |
Denis Vlasenko | f93ab47 | 2006-12-22 12:36:13 +0000 | [diff] [blame] | 80 | /* Wait for and fill in data on child process PID. |
| 81 | Return 0 on error, 1 if ok. */ |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 82 | /* pid_t is short on BSDI, so don't try to promote it. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 83 | static void resuse_end(pid_t pid, resource_t *resp) |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 84 | { |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 85 | pid_t caught; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 86 | |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 87 | /* Ignore signals, but don't ignore the children. When wait3 |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 88 | * returns the child process, set the time the command finished. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 89 | while ((caught = wait3(&resp->waitstatus, 0, &resp->ru)) != pid) { |
| 90 | if (caught == -1 && errno != EINTR) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 91 | bb_simple_perror_msg("wait"); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 92 | return; |
| 93 | } |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 94 | } |
Denys Vlasenko | f2c8aa6 | 2010-01-12 12:52:30 +0100 | [diff] [blame] | 95 | resp->elapsed_ms = monotonic_ms() - resp->elapsed_ms; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 98 | static void printargv(char *const *argv) |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 99 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 100 | const char *fmt = " %s" + 1; |
| 101 | do { |
| 102 | printf(fmt, *argv); |
| 103 | fmt = " %s"; |
| 104 | } while (*++argv); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* Return the number of kilobytes corresponding to a number of pages PAGES. |
| 108 | (Actually, we use it to convert pages*ticks into kilobytes*ticks.) |
| 109 | |
| 110 | Try to do arithmetic so that the risk of overflow errors is minimized. |
| 111 | This is funky since the pagesize could be less than 1K. |
| 112 | Note: Some machines express getrusage statistics in terms of K, |
| 113 | others in terms of pages. */ |
Denys Vlasenko | c7b858f | 2020-12-14 18:49:23 +0100 | [diff] [blame^] | 114 | #ifdef BB_ARCH_FIXED_PAGESIZE |
| 115 | # define pagesize BB_ARCH_FIXED_PAGESIZE |
| 116 | # define ptok(pagesize, pages) ptok(pages) |
| 117 | #endif |
Bernhard Reutner-Fischer | 0adf7f2 | 2009-02-23 16:51:25 +0000 | [diff] [blame] | 118 | static unsigned long ptok(const unsigned pagesize, const unsigned long pages) |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 119 | { |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 120 | unsigned long tmp; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 121 | |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 122 | /* Conversion. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 123 | if (pages > (LONG_MAX / pagesize)) { /* Could overflow. */ |
| 124 | tmp = pages / 1024; /* Smaller first, */ |
| 125 | return tmp * pagesize; /* then larger. */ |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 126 | } |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 127 | /* Could underflow. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 128 | tmp = pages * pagesize; /* Larger first, */ |
| 129 | return tmp / 1024; /* then smaller. */ |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 130 | } |
Denys Vlasenko | c7b858f | 2020-12-14 18:49:23 +0100 | [diff] [blame^] | 131 | #undef pagesize |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 132 | |
| 133 | /* summarize: Report on the system use of a command. |
| 134 | |
Denys Vlasenko | 95f7953 | 2017-08-02 14:26:33 +0200 | [diff] [blame] | 135 | Print the FMT argument except that '%' sequences |
| 136 | have special meaning, and '\n' and '\t' are translated into |
| 137 | newline and tab, respectively, and '\\' is translated into '\'. |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 138 | |
Denys Vlasenko | 95f7953 | 2017-08-02 14:26:33 +0200 | [diff] [blame] | 139 | The character following a '%' can be: |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 140 | (* means the tcsh time builtin also recognizes it) |
Denys Vlasenko | 95f7953 | 2017-08-02 14:26:33 +0200 | [diff] [blame] | 141 | % == a literal '%' |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 142 | C == command name and arguments |
| 143 | * D == average unshared data size in K (ru_idrss+ru_isrss) |
| 144 | * E == elapsed real (wall clock) time in [hour:]min:sec |
| 145 | * F == major page faults (required physical I/O) (ru_majflt) |
| 146 | * I == file system inputs (ru_inblock) |
| 147 | * K == average total mem usage (ru_idrss+ru_isrss+ru_ixrss) |
| 148 | * M == maximum resident set size in K (ru_maxrss) |
| 149 | * O == file system outputs (ru_oublock) |
| 150 | * P == percent of CPU this job got (total cpu time / elapsed time) |
| 151 | * R == minor page faults (reclaims; no physical I/O involved) (ru_minflt) |
| 152 | * S == system (kernel) time (seconds) (ru_stime) |
| 153 | * T == system time in [hour:]min:sec |
| 154 | * U == user time (seconds) (ru_utime) |
| 155 | * u == user time in [hour:]min:sec |
| 156 | * W == times swapped out (ru_nswap) |
| 157 | * X == average amount of shared text in K (ru_ixrss) |
| 158 | Z == page size |
| 159 | * c == involuntary context switches (ru_nivcsw) |
| 160 | e == elapsed real time in seconds |
| 161 | * k == signals delivered (ru_nsignals) |
| 162 | p == average unshared stack size in K (ru_isrss) |
| 163 | * r == socket messages received (ru_msgrcv) |
| 164 | * s == socket messages sent (ru_msgsnd) |
| 165 | t == average resident set size in K (ru_idrss) |
| 166 | * w == voluntary context switches (ru_nvcsw) |
| 167 | x == exit status of command |
| 168 | |
| 169 | Various memory usages are found by converting from page-seconds |
| 170 | to kbytes by multiplying by the page size, dividing by 1024, |
| 171 | and dividing by elapsed real time. |
| 172 | |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 173 | FMT is the format string, interpreted as described above. |
| 174 | COMMAND is the command and args that are being summarized. |
| 175 | RESP is resource information on the command. */ |
| 176 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 177 | #ifndef TICKS_PER_SEC |
| 178 | #define TICKS_PER_SEC 100 |
| 179 | #endif |
| 180 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 181 | static void summarize(const char *fmt, char **command, resource_t *resp) |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 182 | { |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 183 | unsigned vv_ms; /* Elapsed virtual (CPU) milliseconds */ |
| 184 | unsigned cpu_ticks; /* Same, in "CPU ticks" */ |
Denys Vlasenko | c7b858f | 2020-12-14 18:49:23 +0100 | [diff] [blame^] | 185 | unsigned pagesize = bb_getpagesize(); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 186 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 187 | /* Impossible: we do not use WUNTRACED flag in wait()... |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 188 | if (WIFSTOPPED(resp->waitstatus)) |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 189 | printf("Command stopped by signal %u\n", |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 190 | WSTOPSIG(resp->waitstatus)); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 191 | else */ |
| 192 | if (WIFSIGNALED(resp->waitstatus)) |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 193 | printf("Command terminated by signal %u\n", |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 194 | WTERMSIG(resp->waitstatus)); |
| 195 | else if (WIFEXITED(resp->waitstatus) && WEXITSTATUS(resp->waitstatus)) |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 196 | printf("Command exited with non-zero status %u\n", |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 197 | WEXITSTATUS(resp->waitstatus)); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 198 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 199 | vv_ms = (resp->ru.ru_utime.tv_sec + resp->ru.ru_stime.tv_sec) * 1000 |
| 200 | + (resp->ru.ru_utime.tv_usec + resp->ru.ru_stime.tv_usec) / 1000; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 201 | |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 202 | #if (1000 / TICKS_PER_SEC) * TICKS_PER_SEC == 1000 |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 203 | /* 1000 is exactly divisible by TICKS_PER_SEC (typical) */ |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 204 | cpu_ticks = vv_ms / (1000 / TICKS_PER_SEC); |
| 205 | #else |
| 206 | cpu_ticks = vv_ms * (unsigned long long)TICKS_PER_SEC / 1000; |
| 207 | #endif |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 208 | if (!cpu_ticks) cpu_ticks = 1; /* we divide by it, must be nonzero */ |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 209 | |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 210 | while (*fmt) { |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 211 | /* Handle leading literal part */ |
| 212 | int n = strcspn(fmt, "%\\"); |
| 213 | if (n) { |
| 214 | printf("%.*s", n, fmt); |
| 215 | fmt += n; |
| 216 | continue; |
| 217 | } |
| 218 | |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 219 | switch (*fmt) { |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 220 | #ifdef NOT_NEEDED |
| 221 | /* Handle literal char */ |
| 222 | /* Usually we optimize for size, but there is a limit |
| 223 | * for everything. With this we do a lot of 1-byte writes */ |
| 224 | default: |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 225 | bb_putchar(*fmt); |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 226 | break; |
| 227 | #endif |
| 228 | |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 229 | case '%': |
| 230 | switch (*++fmt) { |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 231 | #ifdef NOT_NEEDED_YET |
| 232 | /* Our format strings do not have these */ |
| 233 | /* and we do not take format str from user */ |
| 234 | default: |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 235 | bb_putchar('%'); |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 236 | /*FALLTHROUGH*/ |
| 237 | case '%': |
| 238 | if (!*fmt) goto ret; |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 239 | bb_putchar(*fmt); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 240 | break; |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 241 | #endif |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 242 | case 'C': /* The command that got timed. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 243 | printargv(command); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 244 | break; |
| 245 | case 'D': /* Average unshared data size. */ |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 246 | printf("%lu", |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 247 | (ptok(pagesize, (UL) resp->ru.ru_idrss) + |
| 248 | ptok(pagesize, (UL) resp->ru.ru_isrss)) / cpu_ticks); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 249 | break; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 250 | case 'E': { /* Elapsed real (wall clock) time. */ |
| 251 | unsigned seconds = resp->elapsed_ms / 1000; |
| 252 | if (seconds >= 3600) /* One hour -> h:m:s. */ |
| 253 | printf("%uh %um %02us", |
| 254 | seconds / 3600, |
| 255 | (seconds % 3600) / 60, |
| 256 | seconds % 60); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 257 | else |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 258 | printf("%um %u.%02us", /* -> m:s. */ |
| 259 | seconds / 60, |
| 260 | seconds % 60, |
| 261 | (unsigned)(resp->elapsed_ms / 10) % 100); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 262 | break; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 263 | } |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 264 | case 'F': /* Major page faults. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 265 | printf("%lu", resp->ru.ru_majflt); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 266 | break; |
| 267 | case 'I': /* Inputs. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 268 | printf("%lu", resp->ru.ru_inblock); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 269 | break; |
| 270 | case 'K': /* Average mem usage == data+stack+text. */ |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 271 | printf("%lu", |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 272 | (ptok(pagesize, (UL) resp->ru.ru_idrss) + |
| 273 | ptok(pagesize, (UL) resp->ru.ru_isrss) + |
| 274 | ptok(pagesize, (UL) resp->ru.ru_ixrss)) / cpu_ticks); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 275 | break; |
| 276 | case 'M': /* Maximum resident set size. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 277 | printf("%lu", ptok(pagesize, (UL) resp->ru.ru_maxrss)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 278 | break; |
| 279 | case 'O': /* Outputs. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 280 | printf("%lu", resp->ru.ru_oublock); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 281 | break; |
| 282 | case 'P': /* Percent of CPU this job got. */ |
| 283 | /* % cpu is (total cpu time)/(elapsed time). */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 284 | if (resp->elapsed_ms > 0) |
| 285 | printf("%u%%", (unsigned)(vv_ms * 100 / resp->elapsed_ms)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 286 | else |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 287 | printf("?%%"); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 288 | break; |
| 289 | case 'R': /* Minor page faults (reclaims). */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 290 | printf("%lu", resp->ru.ru_minflt); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 291 | break; |
| 292 | case 'S': /* System time. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 293 | printf("%u.%02u", |
| 294 | (unsigned)resp->ru.ru_stime.tv_sec, |
| 295 | (unsigned)(resp->ru.ru_stime.tv_usec / 10000)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 296 | break; |
| 297 | case 'T': /* System time. */ |
| 298 | if (resp->ru.ru_stime.tv_sec >= 3600) /* One hour -> h:m:s. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 299 | printf("%uh %um %02us", |
| 300 | (unsigned)(resp->ru.ru_stime.tv_sec / 3600), |
| 301 | (unsigned)(resp->ru.ru_stime.tv_sec % 3600) / 60, |
| 302 | (unsigned)(resp->ru.ru_stime.tv_sec % 60)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 303 | else |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 304 | printf("%um %u.%02us", /* -> m:s. */ |
| 305 | (unsigned)(resp->ru.ru_stime.tv_sec / 60), |
| 306 | (unsigned)(resp->ru.ru_stime.tv_sec % 60), |
| 307 | (unsigned)(resp->ru.ru_stime.tv_usec / 10000)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 308 | break; |
| 309 | case 'U': /* User time. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 310 | printf("%u.%02u", |
| 311 | (unsigned)resp->ru.ru_utime.tv_sec, |
| 312 | (unsigned)(resp->ru.ru_utime.tv_usec / 10000)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 313 | break; |
| 314 | case 'u': /* User time. */ |
| 315 | if (resp->ru.ru_utime.tv_sec >= 3600) /* One hour -> h:m:s. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 316 | printf("%uh %um %02us", |
| 317 | (unsigned)(resp->ru.ru_utime.tv_sec / 3600), |
| 318 | (unsigned)(resp->ru.ru_utime.tv_sec % 3600) / 60, |
| 319 | (unsigned)(resp->ru.ru_utime.tv_sec % 60)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 320 | else |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 321 | printf("%um %u.%02us", /* -> m:s. */ |
| 322 | (unsigned)(resp->ru.ru_utime.tv_sec / 60), |
| 323 | (unsigned)(resp->ru.ru_utime.tv_sec % 60), |
| 324 | (unsigned)(resp->ru.ru_utime.tv_usec / 10000)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 325 | break; |
| 326 | case 'W': /* Times swapped out. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 327 | printf("%lu", resp->ru.ru_nswap); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 328 | break; |
| 329 | case 'X': /* Average shared text size. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 330 | printf("%lu", ptok(pagesize, (UL) resp->ru.ru_ixrss) / cpu_ticks); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 331 | break; |
| 332 | case 'Z': /* Page size. */ |
Bernhard Reutner-Fischer | 0adf7f2 | 2009-02-23 16:51:25 +0000 | [diff] [blame] | 333 | printf("%u", pagesize); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 334 | break; |
| 335 | case 'c': /* Involuntary context switches. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 336 | printf("%lu", resp->ru.ru_nivcsw); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 337 | break; |
| 338 | case 'e': /* Elapsed real time in seconds. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 339 | printf("%u.%02u", |
| 340 | (unsigned)resp->elapsed_ms / 1000, |
| 341 | (unsigned)(resp->elapsed_ms / 10) % 100); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 342 | break; |
| 343 | case 'k': /* Signals delivered. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 344 | printf("%lu", resp->ru.ru_nsignals); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 345 | break; |
| 346 | case 'p': /* Average stack segment. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 347 | printf("%lu", ptok(pagesize, (UL) resp->ru.ru_isrss) / cpu_ticks); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 348 | break; |
| 349 | case 'r': /* Incoming socket messages received. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 350 | printf("%lu", resp->ru.ru_msgrcv); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 351 | break; |
| 352 | case 's': /* Outgoing socket messages sent. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 353 | printf("%lu", resp->ru.ru_msgsnd); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 354 | break; |
| 355 | case 't': /* Average resident set size. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 356 | printf("%lu", ptok(pagesize, (UL) resp->ru.ru_idrss) / cpu_ticks); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 357 | break; |
| 358 | case 'w': /* Voluntary context switches. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 359 | printf("%lu", resp->ru.ru_nvcsw); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 360 | break; |
| 361 | case 'x': /* Exit status. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 362 | printf("%u", WEXITSTATUS(resp->waitstatus)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 363 | break; |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 364 | } |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 365 | break; |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 366 | |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 367 | #ifdef NOT_NEEDED_YET |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 368 | case '\\': /* Format escape. */ |
| 369 | switch (*++fmt) { |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 370 | default: |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 371 | bb_putchar('\\'); |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 372 | /*FALLTHROUGH*/ |
| 373 | case '\\': |
| 374 | if (!*fmt) goto ret; |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 375 | bb_putchar(*fmt); |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 376 | break; |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 377 | case 't': |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 378 | bb_putchar('\t'); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 379 | break; |
| 380 | case 'n': |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 381 | bb_putchar('\n'); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 382 | break; |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 383 | } |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 384 | break; |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 385 | #endif |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 386 | } |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 387 | ++fmt; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 388 | } |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 389 | /* ret: */ |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 390 | bb_putchar('\n'); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /* Run command CMD and return statistics on it. |
| 394 | Put the statistics in *RESP. */ |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 395 | static void run_command(char *const *cmd, resource_t *resp) |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 396 | { |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 397 | pid_t pid; |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 398 | void (*interrupt_signal)(int); |
| 399 | void (*quit_signal)(int); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 400 | |
Denys Vlasenko | f2c8aa6 | 2010-01-12 12:52:30 +0100 | [diff] [blame] | 401 | resp->elapsed_ms = monotonic_ms(); |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 402 | pid = xvfork(); |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 403 | if (pid == 0) { |
| 404 | /* Child */ |
| 405 | BB_EXECVP_or_die((char**)cmd); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 406 | } |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 407 | |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 408 | /* Have signals kill the child but not self (if possible). */ |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 409 | //TODO: just block all sigs? and re-enable them in the very end in main? |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 410 | interrupt_signal = signal(SIGINT, SIG_IGN); |
| 411 | quit_signal = signal(SIGQUIT, SIG_IGN); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 412 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 413 | resuse_end(pid, resp); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 414 | |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 415 | /* Re-enable signals. */ |
| 416 | signal(SIGINT, interrupt_signal); |
| 417 | signal(SIGQUIT, quit_signal); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 420 | int time_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 421 | int time_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 422 | { |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 423 | resource_t res; |
Denys Vlasenko | a1a3b59 | 2017-04-28 18:01:18 +0200 | [diff] [blame] | 424 | /* $TIME has lowest prio (-v,-p,-f FMT overrride it) */ |
| 425 | const char *output_format = getenv("TIME") ? : default_format; |
Tommi Rantala | 5fe5be2 | 2017-04-28 17:54:14 +0200 | [diff] [blame] | 426 | char *output_filename; |
| 427 | int output_fd; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 428 | int opt; |
Tommi Rantala | 5fe5be2 | 2017-04-28 17:54:14 +0200 | [diff] [blame] | 429 | int ex; |
| 430 | enum { |
| 431 | OPT_v = (1 << 0), |
| 432 | OPT_p = (1 << 1), |
| 433 | OPT_a = (1 << 2), |
| 434 | OPT_o = (1 << 3), |
Denys Vlasenko | a1a3b59 | 2017-04-28 18:01:18 +0200 | [diff] [blame] | 435 | OPT_f = (1 << 4), |
Tommi Rantala | 5fe5be2 | 2017-04-28 17:54:14 +0200 | [diff] [blame] | 436 | }; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 437 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 438 | /* "+": stop on first non-option */ |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 439 | opt = getopt32(argv, "^+" "vpao:f:" "\0" "-1"/*at least one arg*/, |
| 440 | &output_filename, &output_format |
| 441 | ); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 442 | argv += optind; |
Tommi Rantala | 5fe5be2 | 2017-04-28 17:54:14 +0200 | [diff] [blame] | 443 | if (opt & OPT_v) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 444 | output_format = long_format; |
Tommi Rantala | 5fe5be2 | 2017-04-28 17:54:14 +0200 | [diff] [blame] | 445 | if (opt & OPT_p) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 446 | output_format = posix_format; |
Tommi Rantala | 5fe5be2 | 2017-04-28 17:54:14 +0200 | [diff] [blame] | 447 | output_fd = STDERR_FILENO; |
| 448 | if (opt & OPT_o) { |
Denys Vlasenko | d3a7e88 | 2017-10-27 19:05:00 +0200 | [diff] [blame] | 449 | #ifndef O_CLOEXEC |
| 450 | # define O_CLOEXEC 0 |
| 451 | #endif |
Tommi Rantala | 5fe5be2 | 2017-04-28 17:54:14 +0200 | [diff] [blame] | 452 | output_fd = xopen(output_filename, |
| 453 | (opt & OPT_a) /* append? */ |
| 454 | ? (O_CREAT | O_WRONLY | O_CLOEXEC | O_APPEND) |
| 455 | : (O_CREAT | O_WRONLY | O_CLOEXEC | O_TRUNC) |
| 456 | ); |
Denys Vlasenko | d3a7e88 | 2017-10-27 19:05:00 +0200 | [diff] [blame] | 457 | if (!O_CLOEXEC) |
| 458 | close_on_exec_on(output_fd); |
Tommi Rantala | 5fe5be2 | 2017-04-28 17:54:14 +0200 | [diff] [blame] | 459 | } |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 460 | |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 461 | run_command(argv, &res); |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 462 | |
| 463 | /* Cheat. printf's are shorter :) */ |
Tommi Rantala | 5fe5be2 | 2017-04-28 17:54:14 +0200 | [diff] [blame] | 464 | xdup2(output_fd, STDOUT_FILENO); |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 465 | summarize(output_format, argv, &res); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 466 | |
Tommi Rantala | 5fe5be2 | 2017-04-28 17:54:14 +0200 | [diff] [blame] | 467 | ex = WEXITSTATUS(res.waitstatus); |
| 468 | /* Impossible: we do not use WUNTRACED flag in wait()... |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 469 | if (WIFSTOPPED(res.waitstatus)) |
Tommi Rantala | 5fe5be2 | 2017-04-28 17:54:14 +0200 | [diff] [blame] | 470 | ex = WSTOPSIG(res.waitstatus); |
| 471 | */ |
Denis Vlasenko | f93ab47 | 2006-12-22 12:36:13 +0000 | [diff] [blame] | 472 | if (WIFSIGNALED(res.waitstatus)) |
Tommi Rantala | 5fe5be2 | 2017-04-28 17:54:14 +0200 | [diff] [blame] | 473 | ex = WTERMSIG(res.waitstatus); |
| 474 | |
| 475 | fflush_stdout_and_exit(ex); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 476 | } |