24 *
25 * Portions Copyright 2008 Chad Mynhier
26 */
27 /*
28 * Copyright 2016 Joyent, Inc.
29 */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <math.h>
38 #include <wait.h>
39 #include <signal.h>
40 #include <sys/types.h>
41 #include <sys/time.h>
42 #include <signal.h>
43 #include <libproc.h>
44
45 static int look(pid_t);
46 static void hr_min_sec(char *, long);
47 static void prtime(char *, timestruc_t *);
48 static int perr(const char *);
49
50 static void tsadd(timestruc_t *result, timestruc_t *a, timestruc_t *b);
51 static void tssub(timestruc_t *result, timestruc_t *a, timestruc_t *b);
52 static void hrt2ts(hrtime_t hrt, timestruc_t *tsp);
53
54 static char *command;
55 static char *pidarg;
56 static char procname[64];
57
58 static int Fflag;
59 static int mflag;
60 static int errflg;
61 static int pflag;
62 static int pfirst;
63
172 (void) waitpid(pid, &status, 0);
173
174 if (WIFEXITED(status))
175 return (WEXITSTATUS(status));
176
177 if (WIFSIGNALED(status)) {
178 int sig = WTERMSIG(status);
179 char name[SIG2STR_MAX];
180
181 (void) fprintf(stderr, "%s: command terminated "
182 "abnormally by %s\n", command,
183 proc_signame(sig, name, sizeof (name)));
184 }
185
186 return (status | WCOREFLG); /* see time(1) */
187 }
188
189 static int
190 look(pid_t pid)
191 {
192 char pathname[100];
193 int rval = 0;
194 int fd;
195 psinfo_t psinfo;
196 prusage_t prusage;
197 timestruc_t real, user, sys;
198 hrtime_t hrtime;
199 prusage_t *pup = &prusage;
200
201 pfirst++;
202
203 if (proc_get_psinfo(pid, &psinfo) < 0)
204 return (perr("read psinfo"));
205
206 (void) sprintf(pathname, "/proc/%d/usage", (int)pid);
207 if ((fd = open(pathname, O_RDONLY)) < 0)
208 return (perr("open usage"));
209
210 if (read(fd, &prusage, sizeof (prusage)) != sizeof (prusage))
211 rval = perr("read usage");
212 else {
213 if (pidarg) {
214 hrtime = gethrtime();
215 hrt2ts(hrtime, &real);
216 } else {
217 real = pup->pr_term;
218 }
219 tssub(&real, &real, &pup->pr_create);
220 user = pup->pr_utime;
221 sys = pup->pr_stime;
222 if (!mflag)
223 tsadd(&sys, &sys, &pup->pr_ttime);
224
225 if (!pflag || pfirst > 1)
226 (void) fprintf(stderr, "\n");
|
24 *
25 * Portions Copyright 2008 Chad Mynhier
26 */
27 /*
28 * Copyright 2016 Joyent, Inc.
29 */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <math.h>
38 #include <wait.h>
39 #include <signal.h>
40 #include <sys/types.h>
41 #include <sys/time.h>
42 #include <signal.h>
43 #include <libproc.h>
44 #include <limits.h>
45 #include "ptools_common.h"
46
47 static int look(pid_t);
48 static void hr_min_sec(char *, long);
49 static void prtime(char *, timestruc_t *);
50 static int perr(const char *);
51
52 static void tsadd(timestruc_t *result, timestruc_t *a, timestruc_t *b);
53 static void tssub(timestruc_t *result, timestruc_t *a, timestruc_t *b);
54 static void hrt2ts(hrtime_t hrt, timestruc_t *tsp);
55
56 static char *command;
57 static char *pidarg;
58 static char procname[64];
59
60 static int Fflag;
61 static int mflag;
62 static int errflg;
63 static int pflag;
64 static int pfirst;
65
174 (void) waitpid(pid, &status, 0);
175
176 if (WIFEXITED(status))
177 return (WEXITSTATUS(status));
178
179 if (WIFSIGNALED(status)) {
180 int sig = WTERMSIG(status);
181 char name[SIG2STR_MAX];
182
183 (void) fprintf(stderr, "%s: command terminated "
184 "abnormally by %s\n", command,
185 proc_signame(sig, name, sizeof (name)));
186 }
187
188 return (status | WCOREFLG); /* see time(1) */
189 }
190
191 static int
192 look(pid_t pid)
193 {
194 char pathname[PATH_MAX];
195 int rval = 0;
196 int fd;
197 psinfo_t psinfo;
198 prusage_t prusage;
199 timestruc_t real, user, sys;
200 hrtime_t hrtime;
201 prusage_t *pup = &prusage;
202
203 pfirst++;
204
205 if (proc_get_psinfo(pid, &psinfo) < 0)
206 return (perr("read psinfo"));
207
208 (void) proc_snprintf(pathname, sizeof (pathname), "/proc/%d/usage",
209 (int)pid);
210 if ((fd = open(pathname, O_RDONLY)) < 0)
211 return (perr("open usage"));
212
213 if (read(fd, &prusage, sizeof (prusage)) != sizeof (prusage))
214 rval = perr("read usage");
215 else {
216 if (pidarg) {
217 hrtime = gethrtime();
218 hrt2ts(hrtime, &real);
219 } else {
220 real = pup->pr_term;
221 }
222 tssub(&real, &real, &pup->pr_create);
223 user = pup->pr_utime;
224 sys = pup->pr_stime;
225 if (!mflag)
226 tsadd(&sys, &sys, &pup->pr_ttime);
227
228 if (!pflag || pfirst > 1)
229 (void) fprintf(stderr, "\n");
|