Hi all,

I wished to propose a small diff in order to expose PID_MAX to the user
land via sysctl (readable/writable). It is actually fixed to the value
(SHRT_MAX-1) but with it we can set as will (with some boundaries...).
Might be also useful for app which rely on pid ...

Feel free to give your impressions.

Thanks in advance.
diff --git a/lib/libc/gen/sysctl.3 b/lib/libc/gen/sysctl.3
index 6ab7178..5ebf2f9 100644
--- a/lib/libc/gen/sysctl.3
+++ b/lib/libc/gen/sysctl.3
@@ -477,6 +477,7 @@ information.
 .It Dv KERN_VERSION Ta "string" Ta "no"
 .It Dv KERN_VNODE Ta "struct e_vnode" Ta "no"
 .It Dv KERN_WATCHDOG Ta "node" Ta "not applicable"
+.It Dv KERN_PID_MAX Ta "integer" Ta "yes"
 .El
 .Bl -tag -width "123456"
 .It Dv KERN_ARGMAX
@@ -1096,6 +1097,9 @@ variable.
 .It Dv KERN_WATCHDOG_PERIOD
 The period of the watchdog timer in seconds.
 Set to 0 to disable the watchdog timer.
+.It Dv KERN_PID_MAX
+The maximum process id.
+Between 1024 and 102400.
 .El
 .El
 .Ss CTL_MACHDEP
diff --git a/sbin/sysctl/sysctl.8 b/sbin/sysctl/sysctl.8
index 28809ee..d17dfd5 100644
--- a/sbin/sysctl/sysctl.8
+++ b/sbin/sysctl/sysctl.8
@@ -201,6 +201,7 @@ and a few require a kernel compiled with non-standard
 .It kern.bufcachepercent Ta integer Ta yes
 .It kern.consdev Ta string Ta no
 .It kern.chroot_mknod Ta integer Ta no
+.It kern.pid_max Ta integer Ta yes
 .It vm.vmmeter Ta struct Ta no
 .It vm.loadavg Ta struct Ta no
 .It vm.psstrings Ta struct Ta no
diff --git a/sys/conf/param.c b/sys/conf/param.c
index 623fca2..90a8f07 100644
--- a/sys/conf/param.c
+++ b/sys/conf/param.c
@@ -90,6 +90,8 @@ int   maxprocess = NPROCESS;
 int    maxthread = NPROCESS + 8 * MAXUSERS;
 int    maxfiles = 5 * (NPROCESS + MAXUSERS) + 80;
 int    nmbclust = NMBCLUSTERS;
+pid_t  pid_max = PID_MAX;
+pid_t  no_pid = NO_PID;
 
 #ifndef MBLOWAT
 #define MBLOWAT                16
diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c
index 3a1b54f..e23843d 100644
--- a/sys/kern/kern_fork.c
+++ b/sys/kern/kern_fork.c
@@ -583,7 +583,7 @@ allocpid(void)
                pid = ++lastpid;
        } else {
                do {
-                       pid = 1 + arc4random_uniform(PID_MAX);
+                       pid = 1 + arc4random_uniform(pid_max);
                } while (ispidtaken(pid));
        }
 
diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c
index 8038e23..9203913 100644
--- a/sys/kern/kern_sysctl.c
+++ b/sys/kern/kern_sysctl.c
@@ -614,6 +614,15 @@ kern_sysctl(int *name, u_int namelen, void *oldp, size_t 
*oldlenp, void *newp,
                        pool_reclaim_all();
                return (error);
        }
+       case KERN_PID_MAX:
+               if (newp && (*(pid_t*)newp < PID_SYSCTL_LW ||
+                       *(pid_t*)newp > PID_SYSCTL_UP))
+                       return (EINVAL);
+
+               error = sysctl_int(oldp, oldlenp, newp, newlen, &pid_max);
+               if (error == 0)
+                       no_pid = pid_max + 1;
+               return (error);
        default:
                return (EOPNOTSUPP);
        }
diff --git a/sys/kern/sys_pipe.c b/sys/kern/sys_pipe.c
index 4910354..525e06b 100644
--- a/sys/kern/sys_pipe.c
+++ b/sys/kern/sys_pipe.c
@@ -219,7 +219,7 @@ pipe_create(struct pipe *cpipe)
        getnanotime(&cpipe->pipe_ctime);
        cpipe->pipe_atime = cpipe->pipe_ctime;
        cpipe->pipe_mtime = cpipe->pipe_ctime;
-       cpipe->pipe_pgid = NO_PID;
+       cpipe->pipe_pgid = no_pid;
 
        return (0);
 }
@@ -262,7 +262,7 @@ pipeselwakeup(struct pipe *cpipe)
                selwakeup(&cpipe->pipe_sel);
        } else
                KNOTE(&cpipe->pipe_sel.si_note, 0);
-       if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_pgid != NO_PID)
+       if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_pgid != no_pid)
                gsignal(cpipe->pipe_pgid, SIGIO);
 }
 
diff --git a/sys/kern/tty.c b/sys/kern/tty.c
index d24522c..a0727d2 100644
--- a/sys/kern/tty.c
+++ b/sys/kern/tty.c
@@ -831,7 +831,7 @@ ttioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, 
struct proc *p)
        case TIOCGPGRP:                 /* get pgrp of tty */
                if (!isctty(pr, tp) && suser(p, 0))
                        return (ENOTTY);
-               *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
+               *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : no_pid;
                break;
        case TIOCGSID:                  /* get sid of tty */
                if (!isctty(pr, tp))
diff --git a/sys/net/if_pflog.c b/sys/net/if_pflog.c
index d992b95..468cc27 100644
--- a/sys/net/if_pflog.c
+++ b/sys/net/if_pflog.c
@@ -282,7 +282,7 @@ pflog_packet(struct pf_pdesc *pd, u_int8_t reason, struct 
pf_rule *rm,
                hdr.pid = pd->lookup.pid;
        } else {
                hdr.uid = UID_MAX;
-               hdr.pid = NO_PID;
+               hdr.pid = no_pid;
        }
        hdr.rule_uid = rm->cuid;
        hdr.rule_pid = rm->cpid;
diff --git a/sys/net/pf.c b/sys/net/pf.c
index a5d9aad..b569886 100644
--- a/sys/net/pf.c
+++ b/sys/net/pf.c
@@ -2794,7 +2794,7 @@ pf_socket_lookup(struct pf_pdesc *pd)
                return (-1);
        pd->lookup.uid = UID_MAX;
        pd->lookup.gid = GID_MAX;
-       pd->lookup.pid = NO_PID;
+       pd->lookup.pid = no_pid;
        switch (pd->proto) {
        case IPPROTO_TCP:
                if (pd->hdr.tcp == NULL)
diff --git a/sys/sys/proc.h b/sys/sys/proc.h
index 335bc14..7192dad 100644
--- a/sys/sys/proc.h
+++ b/sys/sys/proc.h
@@ -407,6 +407,12 @@ struct uidinfo *uid_find(uid_t);
 #define        PID_MAX         32766
 #define        NO_PID          (PID_MAX+1)
 
+/*
+ * Boundaries of sysctl possible values for pid_max
+ */
+#define        PID_SYSCTL_LW   1024
+#define PID_SYSCTL_UP  102400
+
 #define SESS_LEADER(pr)        ((pr)->ps_session->s_leader == (pr))
 #define        SESSHOLD(s)     ((s)->s_count++)
 #define        SESSRELE(s) do {                                                
\
@@ -447,6 +453,8 @@ extern struct process process0;             /* Process slot 
for kernel threads. */
 extern int nprocesses, maxprocess;     /* Cur and max number of processes. */
 extern int nthreads, maxthread;                /* Cur and max number of 
threads. */
 extern int randompid;                  /* fork() should create random pid's */
+extern pid_t pid_max;                  /* keeps track of pid max value */
+extern pid_t no_pid;                   /* keeps track of no pid value */
 
 LIST_HEAD(proclist, proc);
 LIST_HEAD(processlist, process);
diff --git a/sys/sys/sysctl.h b/sys/sys/sysctl.h
index 04a3483..660a096 100644
--- a/sys/sys/sysctl.h
+++ b/sys/sys/sysctl.h
@@ -181,7 +181,8 @@ struct ctlname {
 #define        KERN_PROC_CWD           78      /* node: proc cwd */
 #define        KERN_PROC_NOBROADCASTKILL 79    /* node: proc no broadcast kill 
*/
 #define        KERN_CHROOTMKNOD        80      /* allow mknod in chroot */
-#define        KERN_MAXID              81      /* number of valid kern ids */
+#define        KERN_PID_MAX            81      /* max process id */
+#define        KERN_MAXID              82      /* number of valid kern ids */
 
 #define        CTL_KERN_NAMES { \
        { 0, 0 }, \
@@ -265,6 +266,7 @@ struct ctlname {
        { "proc_cwd", CTLTYPE_NODE }, \
        { "proc_nobroadcastkill", CTLTYPE_NODE }, \
        { "chroot_mknod", CTLTYPE_INT }, \
+       { "pid_max", CTLTYPE_INT }, \
 }
 
 /*

Reply via email to