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 || \ @@ -868,7 +867,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) 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 @@ -99,7 +99,7 @@ static int needenv; /* -e */ static int needuser; /* -o "user" */ static int optfatal; /* Fatal error parsing some list-option. */ -static int pid_max; /* kern.pid_max */ +static int pid_max; /* kern.pid_max_limit */ static enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT; @@ -1464,11 +1464,11 @@ static void pidmax_init(void) { - size_t intsize; + size_t size; - intsize = sizeof(pid_max); - if (sysctlbyname("kern.pid_max", &pid_max, &intsize, NULL, 0) < 0) { - xo_warn("unable to read kern.pid_max"); + size = sizeof(pid_max); + if (sysctlbyname("kern.pid_max_limit", &pid_max, &size, NULL, 0) < 0) { + xo_warn("unable to read kern.pid_max_limit"); pid_max = 99999; } } diff --git a/lib/libc/gen/sched_getaffinity.c b/lib/libc/gen/sched_getaffinity.c --- a/lib/libc/gen/sched_getaffinity.c +++ b/lib/libc/gen/sched_getaffinity.c @@ -28,6 +28,7 @@ #define _WANT_P_OSREL #include +#include #include #include #include @@ -37,11 +38,20 @@ int sched_getaffinity(pid_t pid, size_t cpusetsz, cpuset_t *cpuset) { + static int pidmax; cpuwhich_t which; + size_t sz; int error; if (__getosreldate() < P_OSREL_TIDPID) { - if (pid == 0 || pid > _PID_MAX) + if (pidmax == 0) { + sz = sizeof(pidmax); + if (sysctlbyname("kern.pid_max_limit", &pidmax, &sz, + NULL, 0) == -1) { + pidmax = _PID_MAX; + } + } + if (pid == 0 || pid > pidmax) which = CPU_WHICH_TID; else which = CPU_WHICH_PID; diff --git a/lib/libc/gen/sched_setaffinity.c b/lib/libc/gen/sched_setaffinity.c --- a/lib/libc/gen/sched_setaffinity.c +++ b/lib/libc/gen/sched_setaffinity.c @@ -38,14 +38,21 @@ int sched_setaffinity(pid_t pid, size_t cpusetsz, const cpuset_t *cpuset) { - static int mp_maxid; + static int mp_maxid, pidmax; cpuwhich_t which; cpuset_t c; int error, lbs, cpu; - size_t len, sz; + size_t sz; if (__getosreldate() < P_OSREL_TIDPID) { - if (pid == 0 || pid > _PID_MAX) + if (pidmax == 0) { + sz = sizeof(pidmax); + if (sysctlbyname("kern.pid_max_limit", &pidmax, &sz, + NULL, 0) == -1) { + pidmax = _PID_MAX; + } + } + if (pid == 0 || pid > pidmax) which = CPU_WHICH_TID; else which = CPU_WHICH_PID; @@ -58,8 +65,8 @@ /* Linux ignores high bits */ if (mp_maxid == 0) { - len = sizeof(mp_maxid); - error = sysctlbyname("kern.smp.maxid", &mp_maxid, &len, + sz = sizeof(mp_maxid); + error = sysctlbyname("kern.smp.maxid", &mp_maxid, &sz, NULL, 0); if (error == -1) return (error); diff --git a/lib/libc/include/libc_private.h b/lib/libc/include/libc_private.h --- a/lib/libc/include/libc_private.h +++ b/lib/libc/include/libc_private.h @@ -41,7 +41,8 @@ /* * The kernel doesn't expose PID_MAX to the user space. Save it here - * to allow to run a newer world on a pre-1400079 kernel. + * to allow to run a newer world on an older kernel missing kern.pid_max_limit + * sysctl. */ #define _PID_MAX 99999 diff --git a/sys/kern/kern_mib.c b/sys/kern/kern_mib.c --- a/sys/kern/kern_mib.c +++ b/sys/kern/kern_mib.c @@ -754,6 +754,10 @@ CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, 0, 0, sysctl_kern_pid_max, "I", "Maximum allowed pid"); +SYSCTL_INT(_kern, OID_AUTO, pid_max_limit, CTLFLAG_RD, + SYSCTL_NULL_INT_PTR, PID_MAX, + "Maximum allowed pid (kern.pid_max) top limit"); + #include #include SYSCTL_INT(_debug_sizeof, OID_AUTO, bio, CTLFLAG_RD, 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/tests/sys/kern/sysctl_kern_proc.c b/tests/sys/kern/sysctl_kern_proc.c --- a/tests/sys/kern/sysctl_kern_proc.c +++ b/tests/sys/kern/sysctl_kern_proc.c @@ -35,7 +35,8 @@ size_t sz; sz = sizeof(pid_max); - ATF_REQUIRE(sysctlbyname("kern.pid_max", &pid_max, &sz, NULL, 0) == 0); + ATF_REQUIRE(sysctlbyname("kern.pid_max_limit", &pid_max, &sz, NULL, + 0) == 0); mib[0] = CTL_KERN; mib[1] = KERN_PROC; 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_limit"); + /* 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)); diff --git a/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_swrun_tbl.c b/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_swrun_tbl.c --- a/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_swrun_tbl.c +++ b/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_swrun_tbl.c @@ -45,10 +45,7 @@ #include "hostres_oid.h" #include "hostres_tree.h" -/* - * Ugly thing: PID_MAX, NO_PID defined only in kernel - */ -#define NO_PID 100000 +static int no_pid; enum SWRunType { SRT_UNKNOWN = 1, @@ -361,6 +358,8 @@ struct swrun_entry *entry; struct kld_file_stat stat; + assert(no_pid > 0); + for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { stat.version = sizeof(struct kld_file_stat); if (kldstat(fileid, &stat) < 0) { @@ -373,10 +372,10 @@ * NO_PID + 1; NO_PID is PID_MAX + 1 thus it will be no risk to * overlap with real PIDs which are in range of 1 .. NO_PID */ - entry = swrun_entry_find_by_index(NO_PID + 1 + stat.id); + entry = swrun_entry_find_by_index(no_pid + 1 + stat.id); if (entry == NULL) { /* new entry - get memory for it */ - entry = swrun_entry_create(NO_PID + 1 + stat.id); + entry = swrun_entry_create(no_pid + 1 + stat.id); if (entry == NULL) continue; } @@ -429,9 +428,10 @@ int nproc; struct kld_file_stat stat; - assert(entry != NULL); + assert(entry != NULL); + assert(no_pid > 0); - if (entry->index >= NO_PID + 1) { + if (entry->index >= no_pid + 1) { /* * kernel and kernel files (*.ko) will be indexed * starting with NO_PID + 1; NO_PID is PID_MAX + 1 @@ -439,13 +439,13 @@ * which are in range of 1 .. NO_PID */ stat.version = sizeof(stat); - if (kldstat(entry->index - NO_PID - 1, &stat) == -1) { + if (kldstat(entry->index - no_pid - 1, &stat) == -1) { /* * not found, it's gone. Mark it as invalid for now, it * will be removed from the list at next global refersh */ HRDBG("missing item with kid=%d", - entry->index - NO_PID - 1); + entry->index - no_pid - 1); entry->status = (int32_t)SRS_INVALID; } else kld_file_stat_to_swrun(&stat, entry); @@ -473,12 +473,12 @@ int nproc; struct kld_file_stat stat; - assert(entry != NULL); + assert(entry != NULL); + assert(no_pid > 0); - if (entry->index >= NO_PID + 1) { + if (entry->index >= no_pid + 1) { /* this is a kernel item */ - HRDBG("attempt to unload KLD %d", - entry->index - NO_PID - 1); + HRDBG("attempt to unload KLD %d", entry->index - no_pid - 1); if (entry->index == SWOSIndex) { /* can't invalidate the kernel itself */ @@ -486,14 +486,14 @@ } stat.version = sizeof(stat); - if (kldstat(entry->index - NO_PID - 1, &stat) == -1) { + if (kldstat(entry->index - no_pid - 1, &stat) == -1) { /* * not found, it's gone. Mark it as invalid for now, it * will be removed from the list at next global * refresh */ HRDBG("missing item with kid=%d", - entry->index - NO_PID - 1); + entry->index - no_pid - 1); entry->status = (int32_t)SRS_INVALID; return (SNMP_ERR_NOERROR); } @@ -551,6 +551,20 @@ void init_swrun_tbl(void) { + size_t size; + + if (no_pid == 0) { + size = sizeof(no_pid); + if (sysctlbyname("kern.pid_max_limit", &no_pid, &size, NULL, + 0) == 0) { + no_pid++; + } else { + /* + * Fallback when running an older kernel. + */ + no_pid = 100000; + } + } refresh_swrun_tbl(); HRDBG("done");