2 * Copyright (c) 2011 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Processes may set login class name using setloginclass(2). This
34 * is usually done through call to setusercontext(3), by programs
35 * such as login(1), based on information from master.passwd(5). Kernel
36 * uses this information to enforce per-class resource limits. Current
37 * login class can be determined using id(1). Login class is inherited
38 * from the parent process during fork(2). If not set, it defaults
41 * Code in this file implements setloginclass(2) and getloginclass(2)
42 * system calls, and maintains class name storage and retrieval.
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/eventhandler.h>
50 #include <sys/kernel.h>
52 #include <sys/loginclass.h>
53 #include <sys/malloc.h>
54 #include <sys/mutex.h>
55 #include <sys/types.h>
58 #include <sys/queue.h>
59 #include <sys/racct.h>
60 #include <sys/refcount.h>
61 #include <sys/sysproto.h>
62 #include <sys/systm.h>
64 static MALLOC_DEFINE(M_LOGINCLASS, "loginclass", "loginclass structures");
66 LIST_HEAD(, loginclass) loginclasses;
69 * Lock protecting loginclasses list.
71 static struct mtx loginclasses_lock;
73 static void lc_init(void);
74 SYSINIT(loginclass, SI_SUB_CPU, SI_ORDER_FIRST, lc_init, NULL);
77 loginclass_hold(struct loginclass *lc)
80 refcount_acquire(&lc->lc_refcount);
84 loginclass_free(struct loginclass *lc)
88 old = lc->lc_refcount;
89 if (old > 1 && atomic_cmpset_int(&lc->lc_refcount, old, old - 1))
92 mtx_lock(&loginclasses_lock);
93 if (refcount_release(&lc->lc_refcount)) {
94 racct_destroy(&lc->lc_racct);
95 LIST_REMOVE(lc, lc_next);
96 mtx_unlock(&loginclasses_lock);
97 free(lc, M_LOGINCLASS);
101 mtx_unlock(&loginclasses_lock);
105 * Return loginclass structure with a corresponding name. Not
106 * performance critical, as it's used mainly by setloginclass(2),
107 * which happens once per login session. Caller has to use
108 * loginclass_free() on the returned value when it's no longer
112 loginclass_find(const char *name)
114 struct loginclass *lc, *newlc;
116 if (name[0] == '\0' || strlen(name) >= MAXLOGNAME)
119 newlc = malloc(sizeof(*newlc), M_LOGINCLASS, M_ZERO | M_WAITOK);
120 racct_create(&newlc->lc_racct);
122 mtx_lock(&loginclasses_lock);
123 LIST_FOREACH(lc, &loginclasses, lc_next) {
124 if (strcmp(name, lc->lc_name) != 0)
127 /* Found loginclass with a matching name? */
129 mtx_unlock(&loginclasses_lock);
130 racct_destroy(&newlc->lc_racct);
131 free(newlc, M_LOGINCLASS);
135 /* Add new loginclass. */
136 strcpy(newlc->lc_name, name);
137 refcount_init(&newlc->lc_refcount, 1);
138 LIST_INSERT_HEAD(&loginclasses, newlc, lc_next);
139 mtx_unlock(&loginclasses_lock);
145 * Get login class name.
147 #ifndef _SYS_SYSPROTO_H_
148 struct getloginclass_args {
155 sys_getloginclass(struct thread *td, struct getloginclass_args *uap)
160 struct loginclass *lc;
164 lc = p->p_ucred->cr_loginclass;
168 lcnamelen = strlen(lc->lc_name) + 1;
169 if (lcnamelen > uap->namelen)
172 error = copyout(lc->lc_name, uap->namebuf, lcnamelen);
178 * Set login class name.
180 #ifndef _SYS_SYSPROTO_H_
181 struct setloginclass_args {
187 sys_setloginclass(struct thread *td, struct setloginclass_args *uap)
189 struct proc *p = td->td_proc;
191 char lcname[MAXLOGNAME];
192 struct loginclass *newlc;
193 struct ucred *newcred, *oldcred;
195 error = priv_check(td, PRIV_PROC_SETLOGINCLASS);
198 error = copyinstr(uap->namebuf, lcname, sizeof(lcname), NULL);
202 newlc = loginclass_find(lcname);
208 oldcred = crcopysafe(p, newcred);
209 newcred->cr_loginclass = newlc;
210 p->p_ucred = newcred;
213 racct_proc_ucred_changed(p, oldcred, newcred);
215 loginclass_free(oldcred->cr_loginclass);
222 loginclass_racct_foreach(void (*callback)(struct racct *racct,
223 void *arg2, void *arg3), void *arg2, void *arg3)
225 struct loginclass *lc;
227 mtx_lock(&loginclasses_lock);
228 LIST_FOREACH(lc, &loginclasses, lc_next)
229 (callback)(lc->lc_racct, arg2, arg3);
230 mtx_unlock(&loginclasses_lock);
237 mtx_init(&loginclasses_lock, "loginclasses lock", NULL, MTX_DEF);