diff --git a/bin/pkill/pkill.c b/bin/pkill/pkill.c --- a/bin/pkill/pkill.c +++ b/bin/pkill/pkill.c @@ -67,7 +67,6 @@ #define STATUS_ERROR 3 #define MIN_PID 5 -#define MAX_PID 99999 /* Ignore system-processes (if '-S' flag is not specified) and myself. */ #define PSKIP(kp) ((kp)->ki_pid == mypid || \ @@ -828,6 +827,20 @@ usage(); } +static int +pidmax(void) +{ + int val; + size_t size; + + size = sizeof(val); + if (sysctlbyname("kern.pid_max", &val, &size, NULL, 0) < 0) { + err(STATUS_ERROR, "Unable to read kern.pid_max sysctl"); + } + + return (val); +} + static int takepid(const char *pidfile, int pidfilelock) { @@ -868,7 +881,7 @@ rval = strtol(line, &endp, 10); if (*endp != '\0' && !isspace((unsigned char)*endp)) errx(STATUS_ERROR, "Invalid pid in file `%s'", pidfile); - else if (rval < MIN_PID || rval > MAX_PID) + else if (rval < MIN_PID || rval > pidmax()) errx(STATUS_ERROR, "Invalid pid in file `%s'", pidfile); return (rval); } diff --git a/bin/ps/ps.c b/bin/ps/ps.c --- a/bin/ps/ps.c +++ b/bin/ps/ps.c @@ -1468,8 +1468,7 @@ intsize = sizeof(pid_max); if (sysctlbyname("kern.pid_max", &pid_max, &intsize, NULL, 0) < 0) { - xo_warn("unable to read kern.pid_max"); - pid_max = 99999; + xo_err(1, "unable to read kern.pid_max"); } } diff --git a/sys/sys/proc.h b/sys/sys/proc.h --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -955,7 +955,7 @@ * in a pid_t, as it is used to represent "no process group". */ #define PID_MAX 99999 -#define NO_PID 100000 +#define NO_PID (PID_MAX + 1) #define THREAD0_TID NO_PID extern pid_t pid_max; diff --git a/usr.bin/top/display.c b/usr.bin/top/display.c --- a/usr.bin/top/display.c +++ b/usr.bin/top/display.c @@ -245,7 +245,7 @@ /* mpid == -1 implies this system doesn't have an _mpid */ if (mpid != -1) { - printf("last pid: %5d; ", mpid); + printf("last pid: %*d; ", maxpidlen, mpid); } printf("load averages"); @@ -270,7 +270,7 @@ if (mpid != lmpid) { Move_to(x_lastpid, y_lastpid); - printf("%5d", mpid); + printf("%*d", maxpidlen, mpid); lmpid = mpid; } diff --git a/usr.bin/top/machine.h b/usr.bin/top/machine.h --- a/usr.bin/top/machine.h +++ b/usr.bin/top/machine.h @@ -88,6 +88,8 @@ void get_system_info(struct system_info *si); int machine_init(struct statics *statics); +extern int maxpidlen; + /* non-int routines typically used by the machine dependent module */ extern struct process_select ps; diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c --- a/usr.bin/top/machine.c +++ b/usr.bin/top/machine.c @@ -182,6 +182,9 @@ static int carc_enabled; static int pageshift; /* log base 2 of the pagesize */ +static int maxcpulen; +int maxpidlen; + /* define pagetok in terms of pageshift */ #define pagetok(size) ((size) << pageshift) @@ -273,6 +276,22 @@ } } +static int +numlen(const char *name) +{ + int len, val; + + GETSYSCTL(name, val); + + len = 0; + do { + val /= 10; + len++; + } while (val > 0); + + return (len); +} + int machine_init(struct statics *statics) { @@ -307,6 +326,12 @@ GETSYSCTL("kern.ccpu", ccpu); + if (smpmode) + maxcpulen = numlen("hw.ncpu"); + else + maxcpulen = 1; + maxpidlen = numlen("kern.pid_max"); + /* this is used in calculating WCPU -- calculate it ahead of time */ logcpu = log(loaddouble(ccpu)); @@ -398,9 +423,11 @@ switch (displaymode) { case DISP_CPU: { - sbuf_printf(header, " %s", ps.thread_id ? " THR" : "PID"); + sbuf_printf(header, "%*s", + ps.thread_id ? maxpidlen + 1 : maxpidlen, + ps.thread_id ? "THR" : "PID"); sbuf_printf(header, "%*s", ps.jail ? TOP_JID_LEN : 0, - ps.jail ? " JID" : ""); + ps.jail ? "JID" : ""); sbuf_printf(header, " %-*.*s ", namelength, namelength, uname_field); if (!ps.thread) { sbuf_cat(header, "THR "); @@ -409,9 +436,9 @@ if (ps.swap) { sbuf_printf(header, "%*s ", TOP_SWAP_LEN - 1, "SWAP"); } - sbuf_cat(header, "STATE "); + sbuf_cat(header, "STATE "); if (smpmode) { - sbuf_cat(header, "C "); + sbuf_printf(header, "%*s ", maxcpulen, "C"); } sbuf_cat(header, "TIME "); sbuf_printf(header, " %6s ", ps.wcpu ? "WCPU" : "CPU"); @@ -420,9 +447,10 @@ break; } case DISP_IO: { - sbuf_printf(header, " %s%*s %-*.*s", - ps.thread_id ? " THR" : "PID", - ps.jail ? TOP_JID_LEN : 0, ps.jail ? " JID" : "", + sbuf_printf(header, "%*s%*s %-*.*s", + ps.thread_id ? maxpidlen + 1 : maxpidlen, + ps.thread_id ? "THR" : "PID", + ps.jail ? TOP_JID_LEN : 0, ps.jail ? "JID" : "", namelength, namelength, uname_field); sbuf_cat(header, " VCSW IVCSW READ WRITE FAULT TOTAL PERCENT COMMAND"); sbuf_finish(header); @@ -1080,6 +1108,13 @@ } } + sbuf_printf(procbuf, "%*d ", (ps.thread_id) ? maxpidlen + 1 : maxpidlen, + (ps.thread_id) ? pp->ki_tid : pp->ki_pid); + if (ps.jail) { + sbuf_printf(procbuf, "%*d ", TOP_JID_LEN - 1, pp->ki_jid); + } + sbuf_printf(procbuf, "%-*.*s", namelength, namelength, (*get_userid)(pp->ki_ruid)); + if (displaymode == DISP_IO) { oldp = get_old_proc(pp); if (oldp != NULL) { @@ -1097,12 +1132,6 @@ p_tot = rup->ru_inblock + rup->ru_oublock + rup->ru_majflt; s_tot = total_inblock + total_oublock + total_majflt; - sbuf_printf(procbuf, "%5d ", (ps.thread_id) ? pp->ki_tid : pp->ki_pid); - - if (ps.jail) { - sbuf_printf(procbuf, "%*d ", TOP_JID_LEN - 1, pp->ki_jid); - } - sbuf_printf(procbuf, "%-*.*s", namelength, namelength, (*get_userid)(pp->ki_ruid)); sbuf_printf(procbuf, "%6ld ", rup->ru_nvcsw); sbuf_printf(procbuf, "%6ld ", rup->ru_nivcsw); sbuf_printf(procbuf, "%6ld ", rup->ru_inblock); @@ -1112,16 +1141,10 @@ sbuf_printf(procbuf, "%6.2f%% ", s_tot == 0 ? 0.0 : (p_tot * 100.0 / s_tot)); } else { - sbuf_printf(procbuf, "%5d ", (ps.thread_id) ? pp->ki_tid : pp->ki_pid); - if (ps.jail) { - sbuf_printf(procbuf, "%*d ", TOP_JID_LEN - 1, pp->ki_jid); - } - sbuf_printf(procbuf, "%-*.*s ", namelength, namelength, (*get_userid)(pp->ki_ruid)); - if (!ps.thread) { - sbuf_printf(procbuf, "%4d ", pp->ki_numthreads); + sbuf_printf(procbuf, " %4d ", pp->ki_numthreads); } else { - sbuf_printf(procbuf, " "); + sbuf_printf(procbuf, " "); } sbuf_printf(procbuf, "%3d ", pp->ki_pri.pri_level - PZERO); @@ -1141,7 +1164,7 @@ } else { cpu = pp->ki_lastcpu; } - sbuf_printf(procbuf, "%3d ", cpu); + sbuf_printf(procbuf, "%*d ", maxcpulen, cpu); } sbuf_printf(procbuf, "%6s ", format_time(cputime)); sbuf_printf(procbuf, "%6.2f%% ", ps.wcpu ? 100.0 * weighted_cpu(PCTCPU(pp), pp) : 100.0 * PCTCPU(pp));