diff -r -C 2 bin/ps.orig/ps.1 bin/ps/ps.1 *** bin/ps.orig/ps.1 Thu Oct 17 09:37:48 2002 --- bin/ps/ps.1 Tue Oct 15 17:12:47 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 *************** *** 99,105 **** --- 100,110 ---- 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 -r -C 2 bin/ps.orig/ps.c bin/ps/ps.c *** bin/ps.orig/ps.c Thu Oct 17 09:37:48 2002 --- bin/ps/ps.c Tue Oct 15 17:15:00 2002 *************** *** 87,94 **** #if defined(LAZY_PS) static int forceuread=0; ! #define PS_ARGS "aCcefghjLlM:mN:O:o:p:rSTt:U:uvwx" #else static int forceuread=1; ! #define PS_ARGS "aCceghjLlM:mN:O:o:p:rSTt:U:uvwx" #endif --- 87,94 ---- #if defined(LAZY_PS) static int forceuread=0; ! #define PS_ARGS "aCcefgHhJ:jLlM:mN:O:o:p:rSTt:U:uvwx" #else static int forceuread=1; ! #define PS_ARGS "aCcegHhJ:jLlM:mN:O:o:p:rSTt:U:uvwx" #endif *************** *** 106,109 **** --- 106,113 ---- 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"; *************** *** 127,130 **** --- 131,136 ---- 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, nocludge, dropgid; int prtheader, wflag, what, xflg, uid, nuids; *************** *** 187,190 **** --- 193,199 ---- prtheader = ws.ws_row > 5 ? ws.ws_row : 22; break; + case 'H': + jailHost = 1; + break; case 'j': parsefmt(jfmt); *************** *** 192,195 **** --- 201,207 ---- jfmt[0] = '\0'; break; + case 'J': + jailName = optarg; + break; case 'L': showkey(); *************** *** 377,380 **** --- 389,415 ---- */ 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)) *************** *** 405,408 **** --- 440,487 ---- } + 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) *************** *** 651,655 **** (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]", " ps [-L]"); --- 730,734 ---- (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]", " ps [-L]");