2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * ------+---------+---------+-------- + --------+---------+---------+---------*
29 * Copyright (c) 2004 - Garance Alistair Drosehn <gad@FreeBSD.org>.
30 * All rights reserved.
32 * Significant modifications made to bring `ps' options somewhat closer
33 * to the standard for `ps' as described in SingleUnixSpec-v3.
34 * ------+---------+---------+-------- + --------+---------+---------+---------*
38 static const char copyright[] =
39 "@(#) Copyright (c) 1990, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)ps.c 8.4 (Berkeley) 4/2/94";
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
52 #include <sys/param.h>
56 #include <sys/ioctl.h>
57 #include <sys/sysctl.h>
58 #include <sys/mount.h>
77 #define W_SEP " \t" /* "Whitespace" list separators */
78 #define T_SEP "," /* "Terminate-element" list separators */
82 #define OPT_LAZY_f "f"
84 #define DEF_UREAD 1 /* Always do the more-expensive read. */
85 #define OPT_LAZY_f /* I.e., the `-f' option is not added. */
89 * isdigit takes an `int', but expects values in the range of unsigned char.
90 * This wrapper ensures that values from a 'char' end up in the correct range.
92 #define isdigitch(Anychar) isdigit((u_char)(Anychar))
95 int eval; /* Exit value */
96 time_t now; /* Current time(3) value */
98 int sumrusage; /* -S */
99 int termwidth; /* Width of the screen (0 == infinity). */
100 int totwidth; /* Calculated-width of requested variables. */
102 struct velisthead varlist = STAILQ_HEAD_INITIALIZER(varlist);
104 static int forceuread = DEF_UREAD; /* Do extra work to get u-area. */
107 static int needcomm; /* -o "command" */
108 static int needenv; /* -e */
109 static int needuser; /* -o "user" */
110 static int optfatal; /* Fatal error parsing some list-option. */
112 static enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT;
115 typedef int addelem_rtn(struct listinfo *_inf, const char *_elem);
121 addelem_rtn *addelem;
132 static int check_procfs(void);
133 static int addelem_gid(struct listinfo *, const char *);
134 static int addelem_pid(struct listinfo *, const char *);
135 static int addelem_tty(struct listinfo *, const char *);
136 static int addelem_uid(struct listinfo *, const char *);
137 static void add_list(struct listinfo *, const char *);
138 static void dynsizevars(KINFO *);
139 static void *expand_list(struct listinfo *);
141 fmt(char **(*)(kvm_t *, const struct kinfo_proc *, int),
142 KINFO *, char *, int);
143 static void free_list(struct listinfo *);
144 static void init_list(struct listinfo *, addelem_rtn, int, const char *);
145 static char *kludge_oldps_options(const char *, char *, const char *);
146 static int pscomp(const void *, const void *);
147 static void saveuser(KINFO *);
148 static void scanvars(void);
149 static void sizevars(void);
150 static void usage(void);
152 static char dfmt[] = "pid,tt,state,time,command";
153 static char jfmt[] = "user,pid,ppid,pgid,sid,jobc,state,tt,time,command";
154 static char lfmt[] = "uid,pid,ppid,cpu,pri,nice,vsz,rss,mwchan,state,"
156 static char o1[] = "pid";
157 static char o2[] = "tt,state,time,command";
158 static char ufmt[] = "user,pid,%cpu,%mem,vsz,rss,tt,state,start,time,command";
159 static char vfmt[] = "pid,state,time,sl,re,pagein,vsz,rss,lim,tsiz,"
161 static char Zfmt[] = "label";
163 #define PS_ARGS "AaCce" OPT_LAZY_f "G:gHhjLlM:mN:O:o:p:rSTt:U:uvwXxZ"
166 main(int argc, char *argv[])
168 struct listinfo gidlist, pgrplist, pidlist;
169 struct listinfo ruidlist, sesslist, ttylist, uidlist;
170 struct kinfo_proc *kp;
174 const char *nlistf, *memf;
176 int all, ch, elem, flag, _fmt, i, lineno;
177 int nentries, nkept, nselectors;
178 int prtheader, showthreads, wflag, what, xkeep, xkeep_implied;
179 char errbuf[_POSIX2_LINE_MAX];
181 (void) setlocale(LC_ALL, "");
182 time(&now); /* Used by routines in print.c. */
184 if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0')
185 termwidth = atoi(cols);
186 else if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
187 ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
188 ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) ||
192 termwidth = ws.ws_col - 1;
195 * Hide a number of option-processing kludges in a separate routine,
196 * to support some historical BSD behaviors, such as `ps axu'.
199 argv[1] = kludge_oldps_options(PS_ARGS, argv[1], argv[2]);
201 all = _fmt = nselectors = optfatal = 0;
202 prtheader = showthreads = wflag = xkeep_implied = 0;
203 xkeep = -1; /* Neither -x nor -X. */
204 init_list(&gidlist, addelem_gid, sizeof(gid_t), "group");
205 init_list(&pgrplist, addelem_pid, sizeof(pid_t), "process group");
206 init_list(&pidlist, addelem_pid, sizeof(pid_t), "process id");
207 init_list(&ruidlist, addelem_uid, sizeof(uid_t), "ruser");
208 init_list(&sesslist, addelem_pid, sizeof(pid_t), "session id");
209 init_list(&ttylist, addelem_tty, sizeof(dev_t), "tty");
210 init_list(&uidlist, addelem_uid, sizeof(uid_t), "user");
211 memf = nlistf = _PATH_DEVNULL;
212 while ((ch = getopt(argc, argv, PS_ARGS)) != -1)
216 * Exactly the same as `-ax'. This has been
217 * added for compatability with SUSv3, but for
218 * now it will not be described in the man page.
233 case 'e': /* XXX set ufmt */
238 if (getuid() == 0 || getgid() == 0)
243 add_list(&gidlist, optarg);
250 * XXX - This SUSv3 behavior is still under debate
251 * since it conflicts with the (undocumented)
252 * `-g' option. So we skip it for now.
254 add_list(&pgrplist, optarg);
259 /* The historical BSD-ish (from SunOS) behavior. */
263 showthreads = KERN_PROC_INC_THREAD;
266 prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
294 o1[0] = o2[0] = '\0';
302 add_list(&pidlist, optarg);
304 * Note: `-p' does not *set* xkeep, but any values
305 * from pidlist are checked before xkeep is. That
306 * way they are always matched, even if the user
314 * XXX - This un-standard option is still under
315 * debate. This is what SUSv3 defines as
316 * the `-U' option, and while it would be
317 * nice to have, it could cause even more
318 * confusion to implement it as `-R'.
320 add_list(&ruidlist, optarg);
334 * XXX - This non-standard option is still under
335 * debate. This *is* supported on Solaris,
336 * Linux, and IRIX, but conflicts with `-s'
337 * on NetBSD and maybe some older BSD's.
339 add_list(&sesslist, optarg);
345 if ((optarg = ttyname(STDIN_FILENO)) == NULL)
346 errx(1, "stdin: not a terminal");
349 add_list(&ttylist, optarg);
354 /* This is what SUSv3 defines as the `-u' option. */
355 add_list(&uidlist, optarg);
373 termwidth = UNLIMITED;
374 else if (termwidth < 131)
380 * Note that `-X' and `-x' are not standard "selector"
381 * options. For most selector-options, we check *all*
382 * processes to see if any are matched by the given
383 * value(s). After we have a set of all the matched
384 * processes, then `-X' and `-x' govern whether we
385 * modify that *matched* set for processes which do
386 * not have a controlling terminal. `-X' causes
387 * those processes to be deleted from the matched
388 * set, while `-x' causes them to be kept.
407 * If the user specified ps -e then they want a copy of the process
408 * environment kvm_getenvv(3) attempts to open /proc/<pid>/mem.
409 * Check to make sure that procfs is mounted on /proc, otherwise
410 * print a warning informing the user that output will be incomplete.
412 if (needenv == 1 && check_procfs() == 0)
413 warnx("Process environment requires procfs(5)");
415 * If there arguments after processing all the options, attempt
416 * to treat them as a list of process ids.
419 if (!isdigitch(**argv))
421 add_list(&pidlist, *argv);
425 fprintf(stderr, "%s: illegal argument: %s\n",
426 getprogname(), *argv);
430 exit(1); /* Error messages already printed. */
431 if (xkeep < 0) /* Neither -X nor -x was specified. */
432 xkeep = xkeep_implied;
434 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
436 errx(1, "%s", errbuf);
441 if (nselectors == 0) {
442 uidlist.l.ptr = malloc(sizeof(uid_t));
443 if (uidlist.l.ptr == NULL)
444 errx(1, "malloc failed");
446 uidlist.count = uidlist.maxcount = 1;
447 *uidlist.l.uids = getuid();
451 * scan requested variables, noting what structures are needed,
452 * and adjusting header widths as appropriate.
457 * Get process list. If the user requested just one selector-
458 * option, then kvm_getprocs can be asked to return just those
459 * processes. Otherwise, have it return all processes, and
460 * then this routine will search that full list and select the
461 * processes which match any of the user's selector-options.
463 what = showthreads != 0 ? KERN_PROC_ALL : KERN_PROC_PROC;
465 if (nselectors == 1) {
466 if (gidlist.count == 1) {
467 what = KERN_PROC_RGID | showthreads;
468 flag = *gidlist.l.gids;
470 } else if (pgrplist.count == 1) {
471 what = KERN_PROC_PGRP | showthreads;
472 flag = *pgrplist.l.pids;
474 } else if (pidlist.count == 1) {
475 what = KERN_PROC_PID | showthreads;
476 flag = *pidlist.l.pids;
478 } else if (ruidlist.count == 1) {
479 what = KERN_PROC_RUID | showthreads;
480 flag = *ruidlist.l.uids;
482 } else if (sesslist.count == 1) {
483 what = KERN_PROC_SESSION | showthreads;
484 flag = *sesslist.l.pids;
486 } else if (ttylist.count == 1) {
487 what = KERN_PROC_TTY | showthreads;
488 flag = *ttylist.l.ttys;
490 } else if (uidlist.count == 1) {
491 what = KERN_PROC_UID | showthreads;
492 flag = *uidlist.l.uids;
495 /* No need for this routine to select processes. */
504 kp = kvm_getprocs(kd, what, flag, &nentries);
505 if ((kp == NULL && nentries > 0) || (kp != NULL && nentries < 0))
506 errx(1, "%s", kvm_geterr(kd));
509 if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL)
510 errx(1, "malloc failed");
511 for (i = nentries; --i >= 0; ++kp) {
513 * If the user specified multiple selection-criteria,
514 * then keep any process matched by the inclusive OR
515 * of all the selection-criteria given.
517 if (pidlist.count > 0) {
518 for (elem = 0; elem < pidlist.count; elem++)
519 if (kp->ki_pid == pidlist.l.pids[elem])
523 * Note that we had to process pidlist before
524 * filtering out processes which do not have
525 * a controlling terminal.
528 if ((kp->ki_tdev == NODEV ||
529 (kp->ki_flag & P_CONTROLT) == 0))
534 if (gidlist.count > 0) {
535 for (elem = 0; elem < gidlist.count; elem++)
536 if (kp->ki_rgid == gidlist.l.gids[elem])
539 if (pgrplist.count > 0) {
540 for (elem = 0; elem < pgrplist.count; elem++)
542 pgrplist.l.pids[elem])
545 if (ruidlist.count > 0) {
546 for (elem = 0; elem < ruidlist.count; elem++)
548 ruidlist.l.uids[elem])
551 if (sesslist.count > 0) {
552 for (elem = 0; elem < sesslist.count; elem++)
553 if (kp->ki_sid == sesslist.l.pids[elem])
556 if (ttylist.count > 0) {
557 for (elem = 0; elem < ttylist.count; elem++)
558 if (kp->ki_tdev == ttylist.l.ttys[elem])
561 if (uidlist.count > 0) {
562 for (elem = 0; elem < uidlist.count; elem++)
563 if (kp->ki_uid == uidlist.l.uids[elem])
567 * This process did not match any of the user's
568 * selector-options, so skip the process.
573 next_KINFO = &kinfo[nkept];
574 next_KINFO->ki_p = kp;
575 next_KINFO->ki_pcpu = getpcpu(next_KINFO);
576 if (sortby == SORTMEM)
577 next_KINFO->ki_memsize = kp->ki_tsize +
578 kp->ki_dsize + kp->ki_ssize;
580 saveuser(next_KINFO);
581 dynsizevars(next_KINFO);
598 qsort(kinfo, nkept, sizeof(KINFO), pscomp);
600 * For each process, call each variable output function.
602 for (i = lineno = 0; i < nkept; i++) {
603 STAILQ_FOREACH(vent, &varlist, next_ve) {
604 (vent->var->oproc)(&kinfo[i], vent);
605 if (STAILQ_NEXT(vent, next_ve) != NULL)
609 if (prtheader && lineno++ == prtheader - 4) {
617 free_list(&pgrplist);
618 free_list(&ruidlist);
619 free_list(&sesslist);
627 addelem_gid(struct listinfo *inf, const char *elem)
630 const char *nameorID;
634 if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) {
636 warnx("Invalid (zero-length) %s name", inf->lname);
638 warnx("%s name too long: %s", inf->lname, elem);
640 return (0); /* Do not add this value. */
644 * SUSv3 states that `ps -G grouplist' should match "real-group
645 * ID numbers", and does not mention group-names. I do want to
646 * also support group-names, so this tries for a group-id first,
647 * and only tries for a name if that doesn't work. This is the
648 * opposite order of what is done in addelem_uid(), but in
649 * practice the order would only matter for group-names which
655 bigtemp = strtoul(elem, &endp, 10);
656 if (errno == 0 && *endp == '\0' && bigtemp <= GID_MAX) {
657 nameorID = "name or ID matches";
658 grp = getgrgid((gid_t)bigtemp);
661 grp = getgrnam(elem);
663 warnx("No %s %s '%s'", inf->lname, nameorID, elem);
667 if (inf->count >= inf->maxcount)
669 inf->l.gids[(inf->count)++] = grp->gr_gid;
673 #define BSD_PID_MAX 99999 /* Copy of PID_MAX from sys/proc.h. */
675 addelem_pid(struct listinfo *inf, const char *elem)
681 warnx("Invalid (zero-length) process id");
683 return (0); /* Do not add this value. */
687 tempid = strtol(elem, &endp, 10);
688 if (*endp != '\0' || tempid < 0 || elem == endp) {
689 warnx("Invalid %s: %s", inf->lname, elem);
691 } else if (errno != 0 || tempid > BSD_PID_MAX) {
692 warnx("%s too large: %s", inf->lname, elem);
695 if (errno == ERANGE) {
699 if (inf->count >= inf->maxcount)
701 inf->l.pids[(inf->count)++] = tempid;
707 * The user can specify a device via one of three formats:
708 * 1) fully qualified, e.g.: /dev/ttyp0 /dev/console
709 * 2) missing "/dev", e.g.: ttyp0 console
710 * 3) two-letters, e.g.: p0 co
711 * (matching letters that would be seen in the "TT" column)
714 addelem_tty(struct listinfo *inf, const char *elem)
718 char pathbuf[PATH_MAX], pathbuf2[PATH_MAX];
727 if (strcmp(elem, "co") == 0) {
728 ttypath = _PATH_CONSOLE;
733 strlcpy(pathbuf, _PATH_DEV, sizeof(pathbuf));
734 strlcat(pathbuf, elem, sizeof(pathbuf));
736 if (strncmp(pathbuf, _PATH_TTY, strlen(_PATH_TTY)) == 0)
738 if (strcmp(pathbuf, _PATH_CONSOLE) == 0)
740 /* Check to see if /dev/tty${elem} exists */
741 strlcpy(pathbuf2, _PATH_TTY, sizeof(pathbuf2));
742 strlcat(pathbuf2, elem, sizeof(pathbuf2));
743 if (stat(pathbuf2, &sb) == 0 && S_ISCHR(sb.st_mode)) {
744 /* No need to repeat stat() && S_ISCHR() checks */
751 if (stat(ttypath, &sb) == -1) {
752 if (pathbuf2[0] != '\0')
753 warn("%s and %s", pathbuf2, ttypath);
759 if (!S_ISCHR(sb.st_mode)) {
760 if (pathbuf2[0] != '\0')
761 warnx("%s and %s: Not a terminal", pathbuf2,
764 warnx("%s: Not a terminal", ttypath);
769 if (inf->count >= inf->maxcount)
771 inf->l.ttys[(inf->count)++] = sb.st_rdev;
776 addelem_uid(struct listinfo *inf, const char *elem)
782 if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) {
784 warnx("Invalid (zero-length) %s name", inf->lname);
786 warnx("%s name too long: %s", inf->lname, elem);
788 return (0); /* Do not add this value. */
791 pwd = getpwnam(elem);
794 bigtemp = strtoul(elem, &endp, 10);
795 if (errno != 0 || *endp != '\0' || bigtemp > UID_MAX)
796 warnx("No %s named '%s'", inf->lname, elem);
798 /* The string is all digits, so it might be a userID. */
799 pwd = getpwuid((uid_t)bigtemp);
801 warnx("No %s name or ID matches '%s'",
807 * These used to be treated as minor warnings (and the
808 * option was simply ignored), but now they are fatal
809 * errors (and the command will be aborted).
814 if (inf->count >= inf->maxcount)
816 inf->l.uids[(inf->count)++] = pwd->pw_uid;
821 add_list(struct listinfo *inf, const char *argp)
826 char elemcopy[PATH_MAX];
829 inf->addelem(inf, elemcopy);
830 while (*argp != '\0') {
831 while (*argp != '\0' && strchr(W_SEP, *argp) != NULL)
836 if (strchr(T_SEP, *argp) == NULL) {
837 endp = elemcopy + sizeof(elemcopy) - 1;
838 while (*argp != '\0' && cp <= endp &&
839 strchr(W_SEP T_SEP, *argp) == NULL)
847 * Add this single element to the given list.
849 inf->addelem(inf, elemcopy);
852 * The string is too long to copy. Find the end
853 * of the string to print out the warning message.
855 while (*argp != '\0' && strchr(W_SEP T_SEP,
858 warnx("Value too long: %.*s", (int)(argp - savep),
863 * Skip over any number of trailing whitespace characters,
864 * but only one (at most) trailing element-terminating
867 while (*argp != '\0' && strchr(W_SEP, *argp) != NULL)
869 if (*argp != '\0' && strchr(T_SEP, *argp) != NULL) {
871 /* Catch case where string ended with a comma. */
873 inf->addelem(inf, argp);
879 expand_list(struct listinfo *inf)
884 newmax = (inf->maxcount + 1) << 1;
885 newlist = realloc(inf->l.ptr, newmax * inf->elemsize);
886 if (newlist == NULL) {
888 errx(1, "realloc to %d %ss failed", newmax, inf->lname);
890 inf->maxcount = newmax;
891 inf->l.ptr = newlist;
897 free_list(struct listinfo *inf)
900 inf->count = inf->elemsize = inf->maxcount = 0;
901 if (inf->l.ptr != NULL)
909 init_list(struct listinfo *inf, addelem_rtn artn, int elemsize,
913 inf->count = inf->maxcount = 0;
914 inf->elemsize = elemsize;
921 find_varentry(VAR *v)
925 STAILQ_FOREACH(vent, &varlist, next_ve) {
926 if (strcmp(vent->var->name, v->name) == 0)
938 STAILQ_FOREACH(vent, &varlist, next_ve) {
940 if (v->flag & DSIZ) {
941 v->dwidth = v->width;
952 dynsizevars(KINFO *ki)
958 STAILQ_FOREACH(vent, &varlist, next_ve) {
960 if (!(v->flag & DSIZ))
965 if (v->width > v->dwidth)
966 v->width = v->dwidth;
977 STAILQ_FOREACH(vent, &varlist, next_ve) {
979 i = strlen(vent->header);
982 totwidth += v->width + 1; /* +1 for space */
988 fmt(char **(*fn)(kvm_t *, const struct kinfo_proc *, int), KINFO *ki,
989 char *comm, int maxlen)
993 s = fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen);
997 #define UREADOK(ki) (forceuread || (ki->ki_p->ki_sflag & PS_INMEM))
1003 if (ki->ki_p->ki_sflag & PS_INMEM) {
1005 * The u-area might be swapped out, and we can't get
1006 * at it because we have a crashdump and no swap.
1007 * If it's here fill in these fields, otherwise, just
1014 * save arguments if needed
1017 if (ki->ki_p->ki_stat == SZOMB)
1018 ki->ki_args = strdup("<defunct>");
1019 else if (UREADOK(ki) || (ki->ki_p->ki_args != NULL))
1020 ki->ki_args = strdup(fmt(kvm_getargv, ki,
1021 ki->ki_p->ki_comm, MAXCOMLEN));
1023 asprintf(&ki->ki_args, "(%s)", ki->ki_p->ki_comm);
1024 if (ki->ki_args == NULL)
1025 errx(1, "malloc failed");
1031 ki->ki_env = strdup(fmt(kvm_getenvv, ki,
1034 ki->ki_env = strdup("()");
1035 if (ki->ki_env == NULL)
1036 errx(1, "malloc failed");
1042 /* A macro used to improve the readability of pscomp(). */
1043 #define DIFF_RETURN(a, b, field) do { \
1044 if ((a)->field != (b)->field) \
1045 return (((a)->field < (b)->field) ? -1 : 1); \
1049 pscomp(const void *a, const void *b)
1051 const KINFO *ka, *kb;
1055 /* SORTCPU and SORTMEM are sorted in descending order. */
1056 if (sortby == SORTCPU)
1057 DIFF_RETURN(kb, ka, ki_pcpu);
1058 if (sortby == SORTMEM)
1059 DIFF_RETURN(kb, ka, ki_memsize);
1061 * TTY's are sorted in ascending order, except that all NODEV
1062 * processes come before all processes with a device.
1064 if (ka->ki_p->ki_tdev != kb->ki_p->ki_tdev) {
1065 if (ka->ki_p->ki_tdev == NODEV)
1067 if (kb->ki_p->ki_tdev == NODEV)
1069 DIFF_RETURN(ka, kb, ki_p->ki_tdev);
1072 /* PID's and TID's (threads) are sorted in ascending order. */
1073 DIFF_RETURN(ka, kb, ki_p->ki_pid);
1074 DIFF_RETURN(ka, kb, ki_p->ki_tid);
1080 * ICK (all for getopt), would rather hide the ugliness
1081 * here than taint the main code.
1086 * The old convention that 't' with no trailing tty arg means the users
1087 * tty, is only supported if argv[1] doesn't begin with a '-'. This same
1088 * feature is available with the option 'T', which takes no argument.
1091 kludge_oldps_options(const char *optlist, char *origval, const char *nextarg)
1094 char *argp, *cp, *newopts, *ns, *optp, *pidp;
1097 * See if the original value includes any option which takes an
1098 * argument (and will thus use up the remainder of the string).
1101 if (optlist != NULL) {
1102 for (cp = origval; *cp != '\0'; cp++) {
1103 optp = strchr(optlist, *cp);
1104 if ((optp != NULL) && *(optp + 1) == ':') {
1110 if (argp != NULL && *origval == '-')
1114 * if last letter is a 't' flag with no argument (in the context
1115 * of the oldps options -- option string NOT starting with a '-' --
1116 * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
1118 * However, if a flag accepting a string argument is found earlier
1119 * in the option string (including a possible `t' flag), then the
1120 * remainder of the string must be the argument to that flag; so
1121 * do not modify that argument. Note that a trailing `t' would
1122 * cause argp to be set, if argp was not already set by some
1125 len = strlen(origval);
1126 cp = origval + len - 1;
1128 if (*cp == 't' && *origval != '-' && cp == argp) {
1129 if (nextarg == NULL || *nextarg == '-' || isdigitch(*nextarg))
1131 } else if (argp == NULL) {
1133 * The original value did not include any option which takes
1134 * an argument (and that would include `p' and `t'), so check
1135 * the value for trailing number, or comma-separated list of
1136 * numbers, which we will treat as a pid request.
1138 if (isdigitch(*cp)) {
1139 while (cp >= origval && (*cp == ',' || isdigitch(*cp)))
1146 * If nothing needs to be added to the string, then return
1147 * the "original" (although possibly modified) value.
1149 if (*origval == '-' && pidp == NULL)
1153 * Create a copy of the string to add '-' and/or 'p' to the
1156 if ((newopts = ns = malloc(len + 3)) == NULL)
1157 errx(1, "malloc failed");
1159 if (*origval != '-')
1160 *ns++ = '-'; /* add option flag */
1163 strcpy(ns, origval);
1166 * Copy everything before the pid string, add the `p',
1167 * and then copy the pid string.
1169 len = pidp - origval;
1170 memcpy(ns, origval, len);
1184 if (statfs("/proc", &mnt) < 0)
1186 if (strcmp(mnt.f_fstypename, "procfs") != 0)
1194 #define SINGLE_OPTS "[-aCce" OPT_LAZY_f "HhjlmrSTuvwXxZ]"
1196 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
1197 "usage: ps " SINGLE_OPTS " [-O fmt | -o fmt] [-G gid[,gid...]]",
1198 " [-M core] [-N system]",
1199 " [-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]]",