diff -C 2 -r -N ./bin/ps.orig/ps.1 ./bin/ps/ps.1 *** ./bin/ps.orig/ps.1 Thu May 23 15:26:34 2002 --- ./bin/ps/ps.1 Mon May 27 18:48:48 2002 *************** *** 41,49 **** .Sh SYNOPSIS .Nm ! .Op Fl aCcefhjlmrSTuvwx .Op Fl M Ar core .Op Fl N Ar system .Op Fl O Ar fmt .Op Fl o Ar fmt .Op Fl p Ar pid .Op Fl t Ar tty --- 41,50 ---- .Sh SYNOPSIS .Nm ! .Op Fl aCcefhHjlmrSTuvwx .Op Fl M Ar core .Op Fl N Ar system .Op Fl O Ar fmt .Op Fl o Ar fmt + .Op Fl J Ar jail .Op Fl p Ar pid .Op Fl t Ar tty *************** *** 98,104 **** --- 99,109 ---- Repeat the information header as often as necessary to guarantee one header per page of information. + .It Fl H + When running jails only displays processes belonging to the host system. .It Fl j Print information associated with the following keywords: user, pid, ppid, pgid, sess, jobc, state, tt, time and command. + .It Fl J + Limit processes displayed to the jail specified. .It Fl L List the set of available keywords. diff -C 2 -r -N ./bin/ps.orig/ps.c ./bin/ps/ps.c *** ./bin/ps.orig/ps.c Thu May 23 15:26:34 2002 --- ./bin/ps/ps.c Mon May 27 18:46:18 2002 *************** *** 104,107 **** --- 104,111 ---- static uid_t *getuids(const char *, int *); + static int getpidjail(pid_t, char*); + #define JAIL_BUFF_SIZE MAXHOSTNAMELEN + 2 + #define PROC_STATUS_PATH "/proc/%d/status" + char dfmt[] = "pid tt state time command"; char jfmt[] = "user pid ppid pgid sess jobc state tt time command"; *************** *** 125,128 **** --- 129,134 ---- pid_t pid; uid_t *uids; + char *jailName = NULL; /* Only show processes in this jail */ + int jailHost = 0; /* Only show processes in host system */ int all, ch, flag, i, fmt, lineno, nentries, dropgid; int prtheader, wflag, what, xflg, uid, nuids; *************** *** 151,157 **** while ((ch = getopt(argc, argv, #if defined(LAZY_PS) ! "aCcefghjLlM:mN:O:o:p:rSTt:U:uvW:wx")) != -1) #else ! "aCceghjLlM:mN:O:o:p:rSTt:U:uvW:wx")) != -1) #endif switch((char)ch) { --- 157,163 ---- while ((ch = getopt(argc, argv, #if defined(LAZY_PS) ! "aCcefghHjJ:LlM:mN:O:o:p:rSTt:U:uvW:wx")) != -1) #else ! "aCceghHjJ:LlM:mN:O:o:p:rSTt:U:uvW:wx")) != -1) #endif switch((char)ch) { *************** *** 173,176 **** --- 179,185 ---- prtheader = ws.ws_row > 5 ? ws.ws_row : 22; break; + case 'H': + jailHost = 1; + break; case 'j': parsefmt(jfmt); *************** *** 178,181 **** --- 187,193 ---- jfmt[0] = '\0'; break; + case 'J': + jailName = optarg; + break; case 'L': showkey(); *************** *** 369,372 **** --- 381,407 ---- */ for (i = lineno = 0; i < nentries; i++) { + + /* Nate: If limiting to jails in the host ... */ + if (jailHost) { + struct proc *p = KI_PROC(&kinfo[i]); + if(!p || p->p_prison || p->p_flag & P_JAILED) + continue; + } + + /* Nate: If limiting to a certain jail then check */ + else if (jailName) { + struct proc *p = KI_PROC(&kinfo[i]); + char pidjail[JAIL_BUFF_SIZE]; + + if(!p || !p->p_prison || !(p->p_flag & P_JAILED)) + continue; + + if(getpidjail(p->p_pid, pidjail) < 0) + continue; + + if(strcmp(jailName, pidjail)) + continue; + } + if (xflg == 0 && (KI_EPROC(&kinfo[i])->e_tdev == NODEV || (KI_PROC(&kinfo[i])->p_flag & P_CONTROLT ) == 0)) *************** *** 397,400 **** --- 432,479 ---- } + int + getpidjail(pid_t pid, char* buff) + { + int fd; + size_t bytes; + off_t off; + + /* Format the file name */ + if(snprintf(buff, JAIL_BUFF_SIZE, PROC_STATUS_PATH, pid)) + { + /* Open the file */ + if((fd = open(buff, O_RDONLY)) >= 0) + { + /* Seek to the last bit */ + off = lseek(fd, SEEK_END, 0); + + if(off != -1) + { + off -= JAIL_BUFF_SIZE; + if(off < 0) off = 0; + lseek(fd, SEEK_SET, off); + + if((bytes = read(fd, buff, JAIL_BUFF_SIZE - 1)) > 0) + { + /* Okay now jailname should be the last token */ + while(isspace(buff[bytes - 1])) + bytes--; + + buff[bytes] = 0; + + while(!isspace(buff[bytes - 1])) + bytes--; + + memmove(buff, buff + bytes, JAIL_BUFF_SIZE - bytes); + return 0; + + } + } + } + } + + return -1; + } + uid_t * getuids(const char *arg, int *nuids) *************** *** 643,647 **** (void)fprintf(stderr, "%s\n%s\n%s\n", ! "usage: ps [-aChjlmrSTuvwx] [-O|o fmt] [-p pid] [-t tty] [-U user]", " [-M core] [-N system] [-W swap]", " ps [-L]"); --- 722,726 ---- (void)fprintf(stderr, "%s\n%s\n%s\n", ! "usage: ps [-aChHjlmrSTuvwx] [-J jail] [-O|o fmt] [-p pid] [-t tty] [-U user]", " [-M core] [-N system] [-W swap]", " ps [-L]");