+ return (1);
+}
+
+#if CONFIG_LCTX
+
+static void
+lctxinit(void)
+{
+ LIST_INIT(&alllctx);
+ alllctx_cnt = 0;
+
+ /* allocate lctx lock group attribute and group */
+ lctx_lck_grp_attr = lck_grp_attr_alloc_init();
+ lck_grp_attr_setstat(lctx_lck_grp_attr);
+
+ lctx_lck_grp = lck_grp_alloc_init("lctx", lctx_lck_grp_attr);
+ /* Allocate lctx lock attribute */
+ lctx_lck_attr = lck_attr_alloc_init();
+
+ lck_mtx_init(&alllctx_lock, lctx_lck_grp, lctx_lck_attr);
+}
+
+/*
+ * Locate login context by number.
+ */
+struct lctx *
+lcfind(pid_t lcid)
+{
+ struct lctx *l;
+
+ ALLLCTX_LOCK;
+ LIST_FOREACH(l, &alllctx, lc_list) {
+ if (l->lc_id == lcid) {
+ LCTX_LOCK(l);
+ break;
+ }
+ }
+ ALLLCTX_UNLOCK;
+ return (l);
+}
+
+#define LCID_INC \
+ do { \
+ lastlcid++; \
+ if (lastlcid > maxlcid) \
+ lastlcid = 1; \
+ } while (0) \
+
+struct lctx *
+lccreate(void)
+{
+ struct lctx *l;
+ pid_t newlcid;
+
+ /* Not very efficient but this isn't a common operation. */
+ while ((l = lcfind(lastlcid)) != NULL) {
+ LCTX_UNLOCK(l);
+ LCID_INC;
+ }
+ newlcid = lastlcid;
+ LCID_INC;
+
+ MALLOC(l, struct lctx *, sizeof(struct lctx), M_LCTX, M_WAITOK|M_ZERO);
+ l->lc_id = newlcid;
+ LIST_INIT(&l->lc_members);
+ lck_mtx_init(&l->lc_mtx, lctx_lck_grp, lctx_lck_attr);
+#if CONFIG_MACF
+ l->lc_label = mac_lctx_label_alloc();
+#endif
+ ALLLCTX_LOCK;
+ LIST_INSERT_HEAD(&alllctx, l, lc_list);
+ alllctx_cnt++;
+ ALLLCTX_UNLOCK;
+
+ return (l);
+}
+
+/*
+ * Call with proc protected (either by being invisible
+ * or by having the all-login-context lock held) and
+ * the lctx locked.
+ *
+ * Will unlock lctx on return.
+ */
+void
+enterlctx (proc_t p, struct lctx *l, __unused int create)
+{
+ if (l == NULL)
+ return;
+
+ p->p_lctx = l;
+ LIST_INSERT_HEAD(&l->lc_members, p, p_lclist);
+ l->lc_mc++;
+
+#if CONFIG_MACF
+ if (create)
+ mac_lctx_notify_create(p, l);
+ else
+ mac_lctx_notify_join(p, l);
+#endif
+ LCTX_UNLOCK(l);
+
+ return;
+}
+
+/*
+ * Remove process from login context (if any). Called with p protected by
+ * the alllctx lock.
+ */
+void
+leavelctx (proc_t p)
+{
+ struct lctx *l;
+
+ if (p->p_lctx == NULL) {
+ return;
+ }
+
+ LCTX_LOCK(p->p_lctx);
+ l = p->p_lctx;
+ p->p_lctx = NULL;
+ LIST_REMOVE(p, p_lclist);
+ l->lc_mc--;
+#if CONFIG_MACF
+ mac_lctx_notify_leave(p, l);
+#endif
+ if (LIST_EMPTY(&l->lc_members)) {
+ LIST_REMOVE(l, lc_list);
+ alllctx_cnt--;
+ LCTX_UNLOCK(l);
+ lck_mtx_destroy(&l->lc_mtx, lctx_lck_grp);
+#if CONFIG_MACF
+ mac_lctx_label_free(l->lc_label);
+ l->lc_label = NULL;
+#endif
+ FREE(l, M_LCTX);
+ } else {
+ LCTX_UNLOCK(l);
+ }
+ return;
+}
+
+static int
+sysctl_kern_lctx SYSCTL_HANDLER_ARGS
+{
+ int *name = (int*) arg1;
+ u_int namelen = arg2;
+ struct kinfo_lctx kil;
+ struct lctx *l;
+ int error;
+
+ error = 0;
+
+ switch (oidp->oid_number) {
+ case KERN_LCTX_ALL:
+ ALLLCTX_LOCK;
+ /* Request for size. */
+ if (!req->oldptr) {
+ error = SYSCTL_OUT(req, 0,
+ sizeof(struct kinfo_lctx) * (alllctx_cnt + 1));
+ goto out;
+ }
+ break;
+
+ case KERN_LCTX_LCID:
+ /* No space */
+ if (req->oldlen < sizeof(struct kinfo_lctx))
+ return (ENOMEM);
+ /* No argument */
+ if (namelen != 1)
+ return (EINVAL);
+ /* No login context */
+ l = lcfind((pid_t)name[0]);
+ if (l == NULL)
+ return (ENOENT);
+ kil.id = l->lc_id;
+ kil.mc = l->lc_mc;
+ LCTX_UNLOCK(l);
+ return (SYSCTL_OUT(req, (caddr_t)&kil, sizeof(kil)));
+
+ default:
+ return (EINVAL);
+ }
+
+ /* Provided buffer is too small. */
+ if (req->oldlen < (sizeof(struct kinfo_lctx) * alllctx_cnt)) {
+ error = ENOMEM;
+ goto out;
+ }
+
+ LIST_FOREACH(l, &alllctx, lc_list) {
+ LCTX_LOCK(l);
+ kil.id = l->lc_id;
+ kil.mc = l->lc_mc;
+ LCTX_UNLOCK(l);
+ error = SYSCTL_OUT(req, (caddr_t)&kil, sizeof(kil));
+ if (error)
+ break;
+ }
+out:
+ ALLLCTX_UNLOCK;
+
+ return (error);
+}
+
+SYSCTL_NODE(_kern, KERN_LCTX, lctx, CTLFLAG_RW|CTLFLAG_LOCKED, 0, "Login Context");
+
+SYSCTL_PROC(_kern_lctx, KERN_LCTX_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
+ 0, 0, sysctl_kern_lctx, "S,lctx",
+ "Return entire login context table");
+SYSCTL_NODE(_kern_lctx, KERN_LCTX_LCID, lcid, CTLFLAG_RD,
+ sysctl_kern_lctx, "Login Context Table");
+SYSCTL_INT(_kern_lctx, OID_AUTO, last, CTLFLAG_RD, &lastlcid, 0, "");
+SYSCTL_INT(_kern_lctx, OID_AUTO, count, CTLFLAG_RD, &alllctx_cnt, 0, "");
+SYSCTL_INT(_kern_lctx, OID_AUTO, max, CTLFLAG_RW, &maxlcid, 0, "");
+
+#endif /* LCTX */
+
+/* Code Signing related routines */
+
+int
+csops(__unused proc_t p, struct csops_args *uap, __unused register_t *retval)
+{
+ int ops = uap->ops;
+ pid_t pid = uap->pid;
+ user_addr_t uaddr = uap->useraddr;
+ size_t usize = (size_t)CAST_DOWN(size_t, uap->usersize);
+ proc_t pt;
+ uint32_t retflags;
+ int vid, forself;
+ int error;
+ vnode_t tvp;
+ off_t toff;
+ char * buf;
+ unsigned char cdhash[SHA1_RESULTLEN];
+
+ forself = error = 0;
+
+ if (pid == 0)
+ pid = proc_selfpid();
+ if (pid == proc_selfpid())
+ forself = 1;
+
+
+ /* Pre flight checks for CS_OPS_PIDPATH */
+ if (ops == CS_OPS_PIDPATH) {
+ /* usize is unsigned.. */
+ if (usize > 4 * PATH_MAX)
+ return(EOVERFLOW);
+ if (kauth_cred_issuser(kauth_cred_get()) != TRUE)
+ return(EPERM);
+ } else if ((forself == 0) && ((ops != CS_OPS_STATUS) && (ops != CS_OPS_CDHASH) && (kauth_cred_issuser(kauth_cred_get()) != TRUE))) {
+ return(EPERM);
+ }
+
+ pt = proc_find(pid);
+ if (pt == PROC_NULL)
+ return(ESRCH);
+
+
+
+ switch (ops) {
+
+ case CS_OPS_STATUS:
+ retflags = pt->p_csflags;
+ if (uaddr != USER_ADDR_NULL)
+ error = copyout(&retflags, uaddr, sizeof(uint32_t));
+ break;
+
+ case CS_OPS_MARKINVALID:
+ proc_lock(pt);
+ if ((pt->p_csflags & CS_VALID) == CS_VALID) { /* is currently valid */
+ pt->p_csflags &= ~CS_VALID; /* set invalid */
+ if ((pt->p_csflags & CS_KILL) == CS_KILL) {
+ proc_unlock(pt);
+ psignal(pt, SIGKILL);
+ } else
+ proc_unlock(pt);
+ } else
+ proc_unlock(pt);
+
+ break;
+
+ case CS_OPS_MARKHARD:
+ proc_lock(pt);
+ pt->p_csflags |= CS_HARD;
+ if ((pt->p_csflags & CS_VALID) == 0) {
+ /* @@@ allow? reject? kill? @@@ */
+ proc_unlock(pt);
+ error = EINVAL;
+ goto out;
+ } else
+ proc_unlock(pt);
+ break;
+
+ case CS_OPS_MARKKILL:
+ proc_lock(pt);
+ pt->p_csflags |= CS_KILL;
+ if ((pt->p_csflags & CS_VALID) == 0) {
+ proc_unlock(pt);
+ psignal(pt, SIGKILL);
+ } else
+ proc_unlock(pt);
+ break;
+
+ case CS_OPS_PIDPATH:
+ tvp = pt->p_textvp;
+ vid = vnode_vid(tvp);
+
+ proc_rele(pt);
+
+ buf = (char *)kalloc(usize);
+ if (buf == NULL)
+ return(ENOMEM);
+ bzero(buf, usize);
+
+ error = vnode_getwithvid(tvp, vid);
+ if (error == 0) {
+ int len;
+ len = usize;
+ error = vn_getpath(tvp, buf, &len);
+ vnode_put(tvp);
+ if (error == 0) {
+ error = copyout(buf, uaddr, usize);
+ }
+ kfree(buf, usize);
+ }
+ return(error);
+
+ case CS_OPS_CDHASH:
+ if (usize != SHA1_RESULTLEN) {
+ proc_rele(pt);
+ return EINVAL;
+ }
+
+ /* pt already holds a reference on its p_textvp */
+ tvp = pt->p_textvp;
+ toff = pt->p_textoff;
+
+ error = vn_getcdhash(tvp, toff, cdhash);
+ proc_rele(pt);
+
+ if (error == 0) {
+ error = copyout(cdhash, uaddr, sizeof (cdhash));
+ }
+
+ return error;
+
+ default:
+ error = EINVAL;
+ break;
+ }
+out:
+ proc_rele(pt);
+ return(error);
+}
+
+
+int
+proc_iterate(flags, callout, arg, filterfn, filterarg)
+ int flags;
+ int (*callout)(proc_t, void *);
+ void * arg;
+ int (*filterfn)(proc_t, void *);
+ void * filterarg;
+{
+ proc_t p;
+ pid_t * pid_list;
+ int count, pidcount, alloc_count, i, retval;
+
+ count = nprocs+ 10;
+ if (count > hard_maxproc)
+ count = hard_maxproc;
+ alloc_count = count * sizeof(pid_t);
+ pid_list = (pid_t *)kalloc(alloc_count);
+ bzero(pid_list, alloc_count);
+
+
+ proc_list_lock();
+
+
+ pidcount = 0;
+ if (flags & PROC_ALLPROCLIST) {
+ for (p = allproc.lh_first; (p != 0); p = p->p_list.le_next) {
+ if (p->p_stat == SIDL)
+ continue;
+ if ( (filterfn == 0 ) || (filterfn(p, filterarg) != 0)) {
+ pid_list[pidcount] = p->p_pid;
+ pidcount++;
+ if (pidcount >= count)
+ break;
+ }
+ }
+ }
+ if ((pidcount < count ) && (flags & PROC_ZOMBPROCLIST)) {
+ for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next) {
+ if ( (filterfn == 0 ) || (filterfn(p, filterarg) != 0)) {
+ pid_list[pidcount] = p->p_pid;
+ pidcount++;
+ if (pidcount >= count)
+ break;
+ }
+ }
+ }
+
+
+ proc_list_unlock();
+
+
+ for (i = 0; i< pidcount; i++) {
+ p = proc_find(pid_list[i]);
+ if (p) {
+ if ((flags & PROC_NOWAITTRANS) == 0)
+ proc_transwait(p, 0);
+ retval = callout(p, arg);
+
+ switch (retval) {
+ case PROC_RETURNED:
+ case PROC_RETURNED_DONE:
+ proc_rele(p);
+ if (retval == PROC_RETURNED_DONE) {
+ goto out;
+ }
+ break;
+
+ case PROC_CLAIMED_DONE:
+ goto out;
+ case PROC_CLAIMED:
+ default:
+ break;
+ }
+ } else if (flags & PROC_ZOMBPROCLIST) {
+ p = proc_find_zombref(pid_list[i]);
+ if (p != PROC_NULL) {
+ retval = callout(p, arg);
+
+ switch (retval) {
+ case PROC_RETURNED:
+ case PROC_RETURNED_DONE:
+ proc_drop_zombref(p);
+ if (retval == PROC_RETURNED_DONE) {
+ goto out;
+ }
+ break;
+
+ case PROC_CLAIMED_DONE:
+ goto out;
+ case PROC_CLAIMED:
+ default:
+ break;
+ }
+ }
+ }
+ }
+
+out:
+ kfree(pid_list, alloc_count);
+ return(0);
+
+}
+
+
+#if 0
+/* This is for iteration in case of trivial non blocking callouts */
+int
+proc_scanall(flags, callout, arg)
+ int flags;
+ int (*callout)(proc_t, void *);
+ void * arg;
+{
+ proc_t p;
+ int retval;
+
+
+ proc_list_lock();
+
+
+ if (flags & PROC_ALLPROCLIST) {
+ for (p = allproc.lh_first; (p != 0); p = p->p_list.le_next) {
+ retval = callout(p, arg);
+ if (retval == PROC_RETURNED_DONE)
+ goto out;
+ }
+ }
+ if (flags & PROC_ZOMBPROCLIST) {
+ for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next) {
+ retval = callout(p, arg);
+ if (retval == PROC_RETURNED_DONE)
+ goto out;
+ }
+ }
+out:
+
+ proc_list_unlock();
+
+ return(0);
+}
+#endif
+
+
+int
+proc_rebootscan(callout, arg, filterfn, filterarg)
+ int (*callout)(proc_t, void *);
+ void * arg;
+ int (*filterfn)(proc_t, void *);
+ void * filterarg;
+{
+ proc_t p;
+ int lockheld = 0, retval;
+
+ps_allprocscan:
+
+ proc_list_lock();
+ lockheld = 1;
+
+ for (p = allproc.lh_first; (p != 0); p = p->p_list.le_next) {
+ if ( (filterfn == 0 ) || (filterfn(p, filterarg) != 0)) {
+ p = proc_refinternal_locked(p);
+
+ proc_list_unlock();
+ lockheld = 0;
+
+ if (p) {
+ proc_transwait(p, 0);
+ retval = callout(p, arg);
+ proc_rele(p);
+
+ switch (retval) {
+ case PROC_RETURNED_DONE:
+ case PROC_CLAIMED_DONE:
+ goto out;
+ }
+ }
+ goto ps_allprocscan;
+ } /* filter pass */
+ } /* allproc walk thru */
+
+ if (lockheld == 1) {
+ proc_list_unlock();
+ lockheld = 0;
+ }
+
+out:
+ return(0);
+
+}
+
+
+int
+proc_childrenwalk(parent, callout, arg)
+ struct proc * parent;
+ int (*callout)(proc_t, void *);
+ void * arg;
+{
+ register struct proc *p;
+ pid_t * pid_list;
+ int count, pidcount, alloc_count, i, retval;
+
+ count = nprocs+ 10;
+ if (count > hard_maxproc)
+ count = hard_maxproc;
+ alloc_count = count * sizeof(pid_t);
+ pid_list = (pid_t *)kalloc(alloc_count);
+ bzero(pid_list, alloc_count);
+
+
+ proc_list_lock();
+
+
+ pidcount = 0;
+ for (p = parent->p_children.lh_first; (p != 0); p = p->p_sibling.le_next) {
+ if (p->p_stat == SIDL)
+ continue;
+ pid_list[pidcount] = p->p_pid;
+ pidcount++;
+ if (pidcount >= count)
+ break;
+ }
+ proc_list_unlock();
+
+
+ for (i = 0; i< pidcount; i++) {
+ p = proc_find(pid_list[i]);
+ if (p) {
+ proc_transwait(p, 0);
+ retval = callout(p, arg);
+
+ switch (retval) {
+ case PROC_RETURNED:
+ case PROC_RETURNED_DONE:
+ proc_rele(p);
+ if (retval == PROC_RETURNED_DONE) {
+ goto out;
+ }
+ break;
+
+ case PROC_CLAIMED_DONE:
+ goto out;
+ case PROC_CLAIMED:
+ default:
+ break;
+ }
+ }
+ }
+
+out:
+ kfree(pid_list, alloc_count);
+ return(0);
+
+}
+
+/*
+ */
+/* PGRP_BLOCKITERATE is not implemented yet */
+int
+pgrp_iterate(pgrp, flags, callout, arg, filterfn, filterarg)
+ struct pgrp *pgrp;
+ int flags;
+ int (*callout)(proc_t, void *);
+ void * arg;
+ int (*filterfn)(proc_t, void *);
+ void * filterarg;
+{
+ proc_t p;
+ pid_t * pid_list;
+ int count, pidcount, i, alloc_count;
+ int retval;
+ pid_t pgid;
+ int dropref = flags & PGRP_DROPREF;
+#if 0
+ int serialize = flags & PGRP_BLOCKITERATE;
+#else
+ int serialize = 0;
+#endif
+
+ if (pgrp == 0)
+ return(0);
+ count = pgrp->pg_membercnt + 10;
+ if (count > hard_maxproc)
+ count = hard_maxproc;
+ alloc_count = count * sizeof(pid_t);
+ pid_list = (pid_t *)kalloc(alloc_count);
+ bzero(pid_list, alloc_count);
+
+ pgrp_lock(pgrp);
+ if (serialize != 0) {
+ while ((pgrp->pg_listflags & PGRP_FLAG_ITERABEGIN) == PGRP_FLAG_ITERABEGIN) {
+ pgrp->pg_listflags |= PGRP_FLAG_ITERWAIT;
+ msleep(&pgrp->pg_listflags, &pgrp->pg_mlock, 0, "pgrp_iterate", 0);
+ }
+ pgrp->pg_listflags |= PGRP_FLAG_ITERABEGIN;
+ }
+
+ pgid = pgrp->pg_id;
+
+ pidcount = 0;
+ for (p = pgrp->pg_members.lh_first; p != 0;
+ p = p->p_pglist.le_next) {
+ if ( (filterfn == 0 ) || (filterfn(p, filterarg) != 0)) {
+ pid_list[pidcount] = p->p_pid;
+ pidcount++;
+ if (pidcount >= count)
+ break;
+ }
+ }
+
+
+ pgrp_unlock(pgrp);
+ if ((serialize == 0) && (dropref != 0))
+ pg_rele(pgrp);
+
+
+ for (i = 0; i< pidcount; i++) {
+ /* No handling or proc0 */
+ if (pid_list[i] == 0)
+ continue;
+ p = proc_find(pid_list[i]);
+ if (p) {
+ if (p->p_pgrpid != pgid) {
+ proc_rele(p);
+ continue;
+ }
+ proc_transwait(p, 0);
+ retval = callout(p, arg);
+
+ switch (retval) {
+ case PROC_RETURNED:
+ case PROC_RETURNED_DONE:
+ proc_rele(p);
+ if (retval == PROC_RETURNED_DONE) {
+ goto out;
+ }
+ break;
+
+ case PROC_CLAIMED_DONE:
+ goto out;
+ case PROC_CLAIMED:
+ default:
+ break;
+ }
+ }
+ }
+out:
+ if (serialize != 0) {
+ pgrp_lock(pgrp);
+ pgrp->pg_listflags &= ~PGRP_FLAG_ITERABEGIN;
+ if ((pgrp->pg_listflags & PGRP_FLAG_ITERWAIT) == PGRP_FLAG_ITERWAIT) {
+ pgrp->pg_listflags &= ~PGRP_FLAG_ITERWAIT;
+ wakeup(&pgrp->pg_listflags);
+ }
+ pgrp_unlock(pgrp);
+ if (dropref != 0)
+ pg_rele(pgrp);
+ }
+ kfree(pid_list, alloc_count);
+ return(0);
+}
+
+static void
+pgrp_add(struct pgrp * pgrp, struct proc * parent, struct proc * child)
+{
+ proc_list_lock();
+ child->p_pgrp = pgrp;
+ child->p_pgrpid = pgrp->pg_id;
+ child->p_listflag |= P_LIST_INPGRP;
+ /*
+ * When pgrp is being freed , a process can still
+ * request addition using setpgid from bash when
+ * login is terminated (login cycler) return ESRCH
+ * Safe to hold lock due to refcount on pgrp
+ */
+ if ((pgrp->pg_listflags & (PGRP_FLAG_TERMINATE | PGRP_FLAG_DEAD)) == PGRP_FLAG_TERMINATE) {
+ pgrp->pg_listflags &= ~PGRP_FLAG_TERMINATE;
+ }
+
+ if ((pgrp->pg_listflags & PGRP_FLAG_DEAD) == PGRP_FLAG_DEAD)
+ panic("pgrp_add : pgrp is dead adding process");
+ proc_list_unlock();
+
+ pgrp_lock(pgrp);
+ pgrp->pg_membercnt++;
+ if ( parent != PROC_NULL) {
+ LIST_INSERT_AFTER(parent, child, p_pglist);
+ }else {
+ LIST_INSERT_HEAD(&pgrp->pg_members, child, p_pglist);
+ }
+ pgrp_unlock(pgrp);
+
+ proc_list_lock();
+ if (((pgrp->pg_listflags & (PGRP_FLAG_TERMINATE | PGRP_FLAG_DEAD)) == PGRP_FLAG_TERMINATE) && (pgrp->pg_membercnt != 0)) {
+ pgrp->pg_listflags &= ~PGRP_FLAG_TERMINATE;
+ }
+ proc_list_unlock();
+}
+
+static void
+pgrp_remove(struct proc * p)
+{
+ struct pgrp * pg;
+
+ pg = proc_pgrp(p);
+
+ proc_list_lock();
+#if __PROC_INTERNAL_DEBUG
+ if ((p->p_listflag & P_LIST_INPGRP) == 0)
+ panic("removing from pglist but no named ref\n");
+#endif
+ p->p_pgrpid = PGRPID_DEAD;
+ p->p_listflag &= ~P_LIST_INPGRP;
+ p->p_pgrp = NULL;
+ proc_list_unlock();
+
+ if (pg == PGRP_NULL)
+ panic("pgrp_remove: pg is NULL");
+ pgrp_lock(pg);
+ pg->pg_membercnt--;
+
+ if (pg->pg_membercnt < 0)
+ panic("pgprp: -ve membercnt pgprp:%x p:%x\n",(unsigned int)pg, (unsigned int)p);
+
+ LIST_REMOVE(p, p_pglist);
+ if (pg->pg_members.lh_first == 0) {
+ pgrp_unlock(pg);
+ pgdelete_dropref(pg);
+ } else {
+ pgrp_unlock(pg);
+ pg_rele(pg);
+ }
+}
+
+
+/* cannot use proc_pgrp as it maybe stalled */
+static void
+pgrp_replace(struct proc * p, struct pgrp * newpg)
+{
+ struct pgrp * oldpg;
+
+
+
+ proc_list_lock();
+
+ while ((p->p_listflag & P_LIST_PGRPTRANS) == P_LIST_PGRPTRANS) {
+ p->p_listflag |= P_LIST_PGRPTRWAIT;
+ (void)msleep(&p->p_pgrpid, proc_list_mlock, 0, "proc_pgrp", 0);
+ }
+
+ p->p_listflag |= P_LIST_PGRPTRANS;
+
+ oldpg = p->p_pgrp;
+ if (oldpg == PGRP_NULL)
+ panic("pgrp_replace: oldpg NULL");
+ oldpg->pg_refcount++;
+#if __PROC_INTERNAL_DEBUG
+ if ((p->p_listflag & P_LIST_INPGRP) == 0)
+ panic("removing from pglist but no named ref\n");
+#endif
+ p->p_pgrpid = PGRPID_DEAD;
+ p->p_listflag &= ~P_LIST_INPGRP;
+ p->p_pgrp = NULL;
+
+ proc_list_unlock();
+
+ pgrp_lock(oldpg);
+ oldpg->pg_membercnt--;
+ if (oldpg->pg_membercnt < 0)
+ panic("pgprp: -ve membercnt pgprp:%x p:%x\n",(unsigned int)oldpg, (unsigned int)p);
+ LIST_REMOVE(p, p_pglist);
+ if (oldpg->pg_members.lh_first == 0) {
+ pgrp_unlock(oldpg);
+ pgdelete_dropref(oldpg);
+ } else {
+ pgrp_unlock(oldpg);
+ pg_rele(oldpg);
+ }
+
+ proc_list_lock();
+ p->p_pgrp = newpg;
+ p->p_pgrpid = newpg->pg_id;
+ p->p_listflag |= P_LIST_INPGRP;
+ /*
+ * When pgrp is being freed , a process can still
+ * request addition using setpgid from bash when
+ * login is terminated (login cycler) return ESRCH
+ * Safe to hold lock due to refcount on pgrp
+ */
+ if ((newpg->pg_listflags & (PGRP_FLAG_TERMINATE | PGRP_FLAG_DEAD)) == PGRP_FLAG_TERMINATE) {
+ newpg->pg_listflags &= ~PGRP_FLAG_TERMINATE;
+ }
+
+ if ((newpg->pg_listflags & PGRP_FLAG_DEAD) == PGRP_FLAG_DEAD)
+ panic("pgrp_add : pgrp is dead adding process");
+ proc_list_unlock();
+
+ pgrp_lock(newpg);
+ newpg->pg_membercnt++;
+ LIST_INSERT_HEAD(&newpg->pg_members, p, p_pglist);
+ pgrp_unlock(newpg);
+
+ proc_list_lock();
+ if (((newpg->pg_listflags & (PGRP_FLAG_TERMINATE | PGRP_FLAG_DEAD)) == PGRP_FLAG_TERMINATE) && (newpg->pg_membercnt != 0)) {
+ newpg->pg_listflags &= ~PGRP_FLAG_TERMINATE;
+ }
+
+ p->p_listflag &= ~P_LIST_PGRPTRANS;
+ if ((p->p_listflag & P_LIST_PGRPTRWAIT) == P_LIST_PGRPTRWAIT) {
+ p->p_listflag &= ~P_LIST_PGRPTRWAIT;
+ wakeup(&p->p_pgrpid);
+
+ }
+ proc_list_unlock();
+}
+
+void
+pgrp_lock(struct pgrp * pgrp)
+{
+ lck_mtx_lock(&pgrp->pg_mlock);