]> git.saurik.com Git - apple/shell_cmds.git/blame - w/w.c
shell_cmds-81.1.tar.gz
[apple/shell_cmds.git] / w / w.c
CommitLineData
44bd5ea7
A
1/*-
2 * Copyright (c) 1980, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
9bafe280 35
44bd5ea7 36#ifndef lint
9bafe280
A
37static const char copyright[] =
38"@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n";
40#endif
44bd5ea7
A
41
42#ifndef lint
9bafe280 43static const char sccsid[] = "@(#)w.c 8.4 (Berkeley) 4/16/94";
44bd5ea7 44#endif
44bd5ea7
A
45
46/*
47 * w - print system status (who and what)
48 *
49 * This program is similar to the systat command on Tenex/Tops 10/20
50 *
51 */
52#include <sys/param.h>
53#include <sys/time.h>
54#include <sys/stat.h>
55#include <sys/sysctl.h>
56#include <sys/proc.h>
57#include <sys/user.h>
58#include <sys/ioctl.h>
59#include <sys/socket.h>
9bafe280 60#include <sys/tty.h>
44bd5ea7 61
44bd5ea7
A
62#include <netinet/in.h>
63#include <arpa/inet.h>
9bafe280 64#include <arpa/nameser.h>
44bd5ea7
A
65
66#include <ctype.h>
67#include <err.h>
68#include <errno.h>
69#include <fcntl.h>
70#include <kvm.h>
71#include <limits.h>
9bafe280 72#include <locale.h>
44bd5ea7
A
73#include <netdb.h>
74#include <nlist.h>
75#include <paths.h>
9bafe280 76#include <resolv.h>
44bd5ea7
A
77#include <stdio.h>
78#include <stdlib.h>
79#include <string.h>
44bd5ea7
A
80#include <unistd.h>
81#include <utmp.h>
82#include <vis.h>
83
84#include "extern.h"
85
86struct timeval boottime;
87struct utmp utmp;
88struct winsize ws;
89kvm_t *kd;
90time_t now; /* the current time of day */
44bd5ea7
A
91int ttywidth; /* width of tty */
92int argwidth; /* width of tty */
93int header = 1; /* true if -h flag: don't print heading */
94int nflag; /* true if -n flag: don't convert addrs */
9bafe280
A
95int dflag; /* true if -d flag: output debug info */
96int sortidle; /* sort by idle time */
97int use_ampm; /* use AM/PM time */
98int use_comma; /* use comma as floats separator */
99char **sel_users; /* login array of particular users selected */
44bd5ea7
A
100
101/*
102 * One of these per active utmp entry.
103 */
104struct entry {
105 struct entry *next;
106 struct utmp utmp;
9bafe280
A
107 dev_t tdev; /* dev_t of terminal */
108 time_t idle; /* idle time of terminal in seconds */
109 struct kinfo_proc *kp; /* `most interesting' proc */
110 char *args; /* arg list of interesting process */
111 struct kinfo_proc *dkp; /* debug option proc list */
44bd5ea7
A
112} *ep, *ehead = NULL, **nextp = &ehead;
113
9bafe280
A
114#define debugproc(p) *((struct kinfo_proc **)&(p)->ki_spare[0])
115
116/* W_DISPHOSTSIZE should not be greater than UT_HOSTSIZE */
117#define W_DISPHOSTSIZE 16
118
119static void pr_header(time_t *, int);
120static struct stat *ttystat(char *, int);
121static void usage(int);
122static int this_is_uptime(const char *s);
123static void w_getargv(void);
124
125char *fmt_argv(char **, char *, int); /* ../../bin/ps/fmt.c */
44bd5ea7
A
126
127int
128main(argc, argv)
129 int argc;
130 char **argv;
131{
9bafe280
A
132 struct kinfo_proc *kp, *kprocbuf;
133 struct kinfo_proc *dkp;
44bd5ea7
A
134 struct stat *stp;
135 FILE *ut;
9bafe280
A
136 time_t touched;
137 int ch, i, nentries, nusers, wcmd, longidle, dropgid;
138 const char *memf, *nlistf, *p;
139 char *x_suffix;
44bd5ea7 140 char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
9bafe280
A
141 char fn[MAXHOSTNAMELEN];
142 char *dot;
143 int local_error = 0, retry_count = 0;
144 size_t bufSize = 0;
145 size_t orig_bufSize = 0;
146 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
147
148 (void)setlocale(LC_ALL, "");
149 /*
150 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
151 use_comma = (*nl_langinfo(RADIXCHAR) != ',');
152 */
44bd5ea7
A
153
154 /* Are we w(1) or uptime(1)? */
9bafe280 155 if (this_is_uptime(argv[0]) == 0) {
44bd5ea7
A
156 wcmd = 0;
157 p = "";
158 } else {
159 wcmd = 1;
9bafe280 160 p = "dhiflM:N:nsuw";
44bd5ea7
A
161 }
162
9bafe280 163 dropgid = 0;
44bd5ea7
A
164 memf = nlistf = NULL;
165 while ((ch = getopt(argc, argv, p)) != -1)
166 switch (ch) {
9bafe280
A
167 case 'd':
168 dflag = 1;
169 break;
44bd5ea7
A
170 case 'h':
171 header = 0;
172 break;
173 case 'i':
174 sortidle = 1;
175 break;
176 case 'M':
177 header = 0;
178 memf = optarg;
9bafe280 179 dropgid = 1;
44bd5ea7
A
180 break;
181 case 'N':
182 nlistf = optarg;
9bafe280 183 dropgid = 1;
44bd5ea7
A
184 break;
185 case 'n':
186 nflag = 1;
187 break;
188 case 'f': case 'l': case 's': case 'u': case 'w':
189 warnx("[-flsuw] no longer supported");
190 /* FALLTHROUGH */
191 case '?':
192 default:
193 usage(wcmd);
194 }
195 argc -= optind;
196 argv += optind;
197
9bafe280
A
198 if (!(_res.options & RES_INIT))
199 res_init();
200 _res.retrans = 2; /* resolver timeout to 2 seconds per try */
201 _res.retry = 1; /* only try once.. */
202
44bd5ea7 203 /*
9bafe280
A
204 * Discard setgid privileges if not the running kernel so that bad
205 * guys can't print interesting stuff from kernel memory.
44bd5ea7 206 */
9bafe280
A
207 if (dropgid)
208 setgid(getgid());
44bd5ea7 209
9bafe280 210#ifdef OLD_PROC
c0fcf4e1 211 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
44bd5ea7 212 errx(1, "%s", errbuf);
9bafe280 213#endif
44bd5ea7
A
214
215 (void)time(&now);
216 if ((ut = fopen(_PATH_UTMP, "r")) == NULL)
217 err(1, "%s", _PATH_UTMP);
218
219 if (*argv)
9bafe280 220 sel_users = argv;
44bd5ea7
A
221
222 for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) {
223 if (utmp.ut_name[0] == '\0')
224 continue;
9bafe280
A
225 if (!(stp = ttystat(utmp.ut_line, UT_LINESIZE)))
226 continue; /* corrupted record */
44bd5ea7 227 ++nusers;
9bafe280 228 if (wcmd == 0)
44bd5ea7 229 continue;
9bafe280
A
230 if (sel_users) {
231 int usermatch;
232 char **user;
233
234 usermatch = 0;
235 for (user = sel_users; !usermatch && *user; user++)
236 if (!strncmp(utmp.ut_name, *user, UT_NAMESIZE))
237 usermatch = 1;
238 if (!usermatch)
239 continue;
240 }
44bd5ea7 241 if ((ep = calloc(1, sizeof(struct entry))) == NULL)
9bafe280 242 errx(1, "calloc");
44bd5ea7 243 *nextp = ep;
9bafe280
A
244 nextp = &ep->next;
245 memmove(&ep->utmp, &utmp, sizeof(struct utmp));
44bd5ea7
A
246 ep->tdev = stp->st_rdev;
247#ifdef CPU_CONSDEV
248 /*
249 * If this is the console device, attempt to ascertain
250 * the true console device dev_t.
251 */
252 if (ep->tdev == 0) {
253 int mib[2];
254 size_t size;
255
256 mib[0] = CTL_MACHDEP;
257 mib[1] = CPU_CONSDEV;
258 size = sizeof(dev_t);
9bafe280 259 (void)sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
44bd5ea7
A
260 }
261#endif
9bafe280
A
262 touched = stp->st_atime;
263 if (touched < ep->utmp.ut_time) {
264 /* tty untouched since before login */
265 touched = ep->utmp.ut_time;
266 }
267 if ((ep->idle = now - touched) < 0)
44bd5ea7
A
268 ep->idle = 0;
269 }
270 (void)fclose(ut);
271
272 if (header || wcmd == 0) {
273 pr_header(&now, nusers);
9bafe280
A
274 if (wcmd == 0) {
275#ifdef OLD_PROC
276 (void)kvm_close(kd);
277#endif
278 exit(0);
279 }
44bd5ea7 280
9bafe280
A
281#define HEADER_USER "USER"
282#define HEADER_TTY "TTY"
283#define HEADER_FROM "FROM"
284#define HEADER_LOGIN_IDLE "LOGIN@ IDLE "
285#define HEADER_WHAT "WHAT\n"
286#define WUSED (UT_NAMESIZE + UT_LINESIZE + W_DISPHOSTSIZE + \
287 sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */
288 (void)printf("%-*.*s %-*.*s %-*.*s %s",
289 UT_NAMESIZE, UT_NAMESIZE, HEADER_USER,
290 UT_LINESIZE, UT_LINESIZE, HEADER_TTY,
291 W_DISPHOSTSIZE, W_DISPHOSTSIZE, HEADER_FROM,
292 HEADER_LOGIN_IDLE HEADER_WHAT);
293 }
44bd5ea7 294
9bafe280 295#ifdef OLD_PROC
44bd5ea7 296 if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
9bafe280 297 err(1, "%s", kvm_geterr(kd));
44bd5ea7 298#else
9bafe280
A
299 mib[0] = CTL_KERN;
300 mib[1] = KERN_PROC;
301 mib[2] = KERN_PROC_ALL;
302 mib[3] = 0;
303
304 if (sysctl(mib, 4, NULL, &bufSize, NULL, 0) < 0) {
305 perror("Failure calling sysctl");
306 exit(1);
307 }
44bd5ea7 308
9bafe280
A
309 kprocbuf = kp = (struct kinfo_proc *)malloc(bufSize);
310
311 retry_count = 0;
312 orig_bufSize = bufSize;
313 for (retry_count = 0; ; retry_count++) {
314 local_error = 0;
315 bufSize = orig_bufSize;
316 if ((local_error = sysctl(mib, 4, kp, &bufSize, NULL, 0)) < 0) {
317 if (retry_count < 1000) {
318 sleep(1);
319 continue;
320 }
321 perror("Failure calling sysctl");
322 exit(1);
323 } else if (local_error == 0) {
324 break;
325 }
326 sleep(1);
327 }
328 nentries = bufSize / sizeof(struct kinfo_proc);
329#endif
330 for (i = 0; i < nentries; i++, kp++) {
331 if (kp->kp_proc.p_stat == SIDL || kp->kp_proc.p_stat == SZOMB)
44bd5ea7 332 continue;
44bd5ea7 333 for (ep = ehead; ep != NULL; ep = ep->next) {
9bafe280 334 if (ep->tdev == kp->kp_eproc.e_tdev) {
44bd5ea7 335 /*
9bafe280 336 * proc is associated with this terminal
44bd5ea7 337 */
9bafe280
A
338 if (ep->kp == NULL && kp->kp_eproc.e_pgid == kp->kp_eproc.e_tpgid) {
339 /*
340 * Proc is 'most interesting'
341 */
342 if (proc_compare(&ep->kp->kp_proc, &kp->kp_proc))
343 ep->kp = kp;
344 }
345 /*
346 * Proc debug option info; add to debug
347 * list using kinfo_proc ki_spare[0]
348 * as next pointer; ptr to ptr avoids the
349 * ptr = long assumption.
350 */
351 dkp = ep->dkp;
352 ep->dkp = kp;
353/* --bbraun
354 debugproc(kp) = dkp;
355*/
44bd5ea7
A
356 }
357 }
358 }
359 if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
360 ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
361 ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
362 ttywidth = 79;
363 else
364 ttywidth = ws.ws_col - 1;
365 argwidth = ttywidth - WUSED;
366 if (argwidth < 4)
367 argwidth = 8;
9bafe280
A
368 for (ep = ehead; ep != NULL; ep = ep->next) {
369 if (ep->kp == NULL) {
370 ep->args = strdup("-");
371 continue;
372 }
373#ifdef OLD_PROC
374 ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth),
375 ep->kp->kp_proc.p_comm, MAXCOMLEN);
376#else
377 w_getargv();
378#endif
379 if (ep->args == NULL)
380 err(1, NULL);
381 }
44bd5ea7
A
382 /* sort by idle time */
383 if (sortidle && ehead != NULL) {
9bafe280
A
384 struct entry *from, *save;
385
386 from = ehead;
44bd5ea7
A
387 ehead = NULL;
388 while (from != NULL) {
389 for (nextp = &ehead;
390 (*nextp) && from->idle >= (*nextp)->idle;
391 nextp = &(*nextp)->next)
392 continue;
393 save = from;
394 from = from->next;
395 save->next = *nextp;
396 *nextp = save;
397 }
398 }
44bd5ea7
A
399
400 for (ep = ehead; ep != NULL; ep = ep->next) {
9bafe280
A
401 char host_buf[UT_HOSTSIZE + 1];
402 struct sockaddr_storage ss;
403 struct sockaddr *sa = (struct sockaddr *)&ss;
404 struct sockaddr_in *lsin = (struct sockaddr_in *)&ss;
405#ifdef SUCKAGE
406 struct hostent *hp;
407#else
408 struct sockaddr_in6 *lsin6 = (struct sockaddr_in6 *)&ss;
409#endif
410 int isaddr;
411
412 host_buf[UT_HOSTSIZE] = '\0';
413 strncpy(host_buf, ep->utmp.ut_host, UT_HOSTSIZE);
414 p = *host_buf ? host_buf : "-";
415 if ((x_suffix = strrchr(p, ':')) != NULL) {
416 if ((dot = strchr(x_suffix, '.')) != NULL &&
417 strchr(dot+1, '.') == NULL)
418 *x_suffix++ = '\0';
419 else
420 x_suffix = NULL;
421 }
422 if (!nflag) {
423 /* Attempt to change an IP address into a name */
424 isaddr = 0;
425 memset(&ss, '\0', sizeof(ss));
426#ifdef SUCKAGE
427 if (inet_aton(p, &lsin->sin_addr) ) {
428 lsin->sin_len = sizeof(*lsin);
429 lsin->sin_family = AF_INET;
430 isaddr = 1;
44bd5ea7 431 }
9bafe280
A
432
433 hp = gethostbyaddr((char *)&lsin->sin_addr, sizeof(lsin->sin_addr), AF_INET);
434 if( hp ) {
435 p = hp->h_name;
436 }
437#else
438 if (inet_pton(AF_INET6, p, &lsin6->sin6_addr) == 1) {
439 lsin6->sin6_len = sizeof(*lsin6);
440 lsin6->sin6_family = AF_INET6;
441 isaddr = 1;
442 } else if (inet_pton(AF_INET, p, &lsin->sin_addr) == 1) {
443 lsin->sin_len = sizeof(*lsin);
444 lsin->sin_family = AF_INET;
445 isaddr = 1;
446 }
447 if (isaddr && realhostname_sa(fn, sizeof(fn), sa,
448 sa->sa_len) == HOSTNAME_FOUND)
449 p = fn;
450#endif
44bd5ea7 451 }
9bafe280
A
452 if (x_suffix) {
453 (void)snprintf(buf, sizeof(buf), "%s:%s", p, x_suffix);
44bd5ea7
A
454 p = buf;
455 }
9bafe280
A
456#ifndef SUCKAGE
457 if (dflag) {
458 for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) {
459 const char *ptr;
460
461 ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth),
462 dkp->ki_comm, MAXCOMLEN);
463 if (ptr == NULL)
464 ptr = "-";
465 (void)printf("\t\t%-9d %s\n",
466 dkp->ki_pid, ptr);
467 }
468 }
469#endif
470 (void)printf("%-*.*s %-*.*s %-*.*s ",
44bd5ea7 471 UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
9bafe280
A
472 UT_LINESIZE, UT_LINESIZE,
473 strncmp(ep->utmp.ut_line, "tty", 3) &&
474 strncmp(ep->utmp.ut_line, "cua", 3) ?
44bd5ea7 475 ep->utmp.ut_line : ep->utmp.ut_line + 3,
9bafe280 476 W_DISPHOSTSIZE, W_DISPHOSTSIZE, *p ? p : "-");
44bd5ea7 477 pr_attime(&ep->utmp.ut_time, &now);
9bafe280
A
478 longidle = pr_idle(ep->idle);
479 (void)printf("%.*s\n", argwidth - longidle, ep->args);
480#ifndef OLD_PROC
481 free(ep->args);
482#endif
44bd5ea7 483 }
9bafe280
A
484#ifdef OLD_PROC
485 (void)kvm_close(kd);
486#else
487 free(kprocbuf);
488#endif
44bd5ea7
A
489 exit(0);
490}
491
44bd5ea7
A
492static void
493pr_header(nowp, nusers)
494 time_t *nowp;
495 int nusers;
496{
497 double avenrun[3];
498 time_t uptime;
9bafe280 499 int days, hrs, i, mins, secs;
44bd5ea7
A
500 int mib[2];
501 size_t size;
502 char buf[256];
503
504 /*
505 * Print time of day.
44bd5ea7 506 */
9bafe280
A
507 (void)strftime(buf, sizeof(buf) - 1,
508 use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp));
44bd5ea7
A
509 buf[sizeof(buf) - 1] = '\0';
510 (void)printf("%s ", buf);
511
512 /*
513 * Print how long system has been up.
514 * (Found by looking getting "boottime" from the kernel)
515 */
516 mib[0] = CTL_KERN;
517 mib[1] = KERN_BOOTTIME;
518 size = sizeof(boottime);
519 if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
520 boottime.tv_sec != 0) {
521 uptime = now - boottime.tv_sec;
9bafe280
A
522 if (uptime > 60)
523 uptime += 30;
524 days = uptime / 86400;
525 uptime %= 86400;
526 hrs = uptime / 3600;
527 uptime %= 3600;
528 mins = uptime / 60;
529 secs = uptime % 60;
530 (void)printf(" up");
531 if (days > 0)
532 (void)printf(" %d day%s,", days, days > 1 ? "s" : "");
533 if (hrs > 0 && mins > 0)
534 (void)printf(" %2d:%02d,", hrs, mins);
535 else if (hrs > 0)
536 (void)printf(" %d hr%s,", hrs, hrs > 1 ? "s" : "");
537 else if (mins > 0)
538 (void)printf(" %d min%s,", mins, mins > 1 ? "s" : "");
539 else
540 (void)printf(" %d sec%s,", secs, secs > 1 ? "s" : "");
44bd5ea7
A
541 }
542
543 /* Print number of users logged in to system */
9bafe280 544 (void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s");
44bd5ea7
A
545
546 /*
547 * Print 1, 5, and 15 minute load averages.
548 */
549 if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
550 (void)printf(", no load average information available\n");
551 else {
552 (void)printf(", load averages:");
9bafe280
A
553 for (i = 0; i < (int)(sizeof(avenrun) / sizeof(avenrun[0])); i++) {
554 if (use_comma && i > 0)
44bd5ea7
A
555 (void)printf(",");
556 (void)printf(" %.2f", avenrun[i]);
557 }
558 (void)printf("\n");
559 }
560}
561
562static struct stat *
9bafe280 563ttystat(line, sz)
44bd5ea7 564 char *line;
9bafe280 565 int sz;
44bd5ea7
A
566{
567 static struct stat sb;
568 char ttybuf[MAXPATHLEN];
569
9bafe280
A
570 (void)snprintf(ttybuf, sizeof(ttybuf), "%s%.*s", _PATH_DEV, sz, line);
571 if (stat(ttybuf, &sb)) {
572 warn("%s", ttybuf);
44bd5ea7 573 return (NULL);
9bafe280 574 }
44bd5ea7
A
575 return (&sb);
576}
577
578static void
579usage(wcmd)
580 int wcmd;
581{
582 if (wcmd)
583 (void)fprintf(stderr,
9bafe280 584 "usage: w [-dhin] [-M core] [-N system] [user ...]\n");
44bd5ea7 585 else
9bafe280 586 (void)fprintf(stderr, "usage: uptime\n");
44bd5ea7
A
587 exit(1);
588}
9bafe280
A
589
590static int
591this_is_uptime(s)
592 const char *s;
593{
594 const char *u;
595
596 if ((u = strrchr(s, '/')) != NULL)
597 ++u;
598 else
599 u = s;
600 if (strcmp(u, "uptime") == 0)
601 return (0);
602 return (-1);
603}
604
605static void
606w_getargv(void)
607{
608 int mib[3], argmax;
609 size_t size;
610 char *procargs, *sp, *np, *cp;
611
612 mib[0] = CTL_KERN;
613 mib[1] = KERN_ARGMAX;
614
615 size = sizeof(argmax);
616 if (sysctl(mib, 2, &argmax, &size, NULL, 0) == -1) {
617 goto ERROR;
618 }
619
620 procargs = malloc(argmax);
621 if (procargs == NULL) {
622 goto ERROR;
623 }
624
625 mib[0] = CTL_KERN;
626 mib[1] = KERN_PROCARGS;
627 mib[2] = KI_PROC(ep)->p_pid;
628
629 size = (size_t)argmax;
630 if (sysctl(mib, 3, procargs, &size, NULL, 0) == -1) {
631 goto ERROR_FREE;
632 }
633
634 for (cp = procargs; cp < &procargs[size]; cp++) {
635 if (*cp == '\0') {
636 break;
637 }
638 }
639 if (cp == &procargs[size]) {
640 goto ERROR_FREE;
641 }
642
643 sp = cp;
644
645 for (np = NULL; cp < &procargs[size]; cp++) {
646 if (*cp == '\0') {
647 if (np != NULL) {
648 *np = ' ';
649 }
650 np = cp;
651 } else if (*cp == '=') {
652 break;
653 }
654 }
655
656 for (np = sp; (np < &procargs[size]) && (*np == ' '); np++);
657
658 ep->args = strdup(np);
659 free(procargs);
660 return;
661
662ERROR_FREE:
663 free(procargs);
664ERROR:
665/*
666 ep->args = malloc(2);
667 ep->args[0] = '-';
668 ep->args[1] = '\0';
669*/
670 asprintf(&ep->args, "%s", KI_PROC(ep)->p_comm);
671 return;
672}