-
-#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 | CTLFLAG_LOCKED,
- 0, 0, sysctl_kern_lctx, "S,lctx",
- "Return entire login context table");
-SYSCTL_NODE(_kern_lctx, KERN_LCTX_LCID, lcid, CTLFLAG_RD | CTLFLAG_LOCKED,
- sysctl_kern_lctx, "Login Context Table");
-SYSCTL_INT(_kern_lctx, OID_AUTO, last, CTLFLAG_RD | CTLFLAG_LOCKED, &lastlcid, 0, "");
-SYSCTL_INT(_kern_lctx, OID_AUTO, count, CTLFLAG_RD | CTLFLAG_LOCKED, &alllctx_cnt, 0, "");
-SYSCTL_INT(_kern_lctx, OID_AUTO, max, CTLFLAG_RW | CTLFLAG_LOCKED, &maxlcid, 0, "");
-
-#endif /* LCTX */