xref: /dragonfly/sys/kern/kern_varsym.c (revision 2b3f93ea)
198a7f915SMatthew Dillon /*
28c10bfcfSMatthew Dillon  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
38c10bfcfSMatthew Dillon  *
48c10bfcfSMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
58c10bfcfSMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
698a7f915SMatthew Dillon  *
798a7f915SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
898a7f915SMatthew Dillon  * modification, are permitted provided that the following conditions
998a7f915SMatthew Dillon  * are met:
108c10bfcfSMatthew Dillon  *
1198a7f915SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
1298a7f915SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
1398a7f915SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
148c10bfcfSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
158c10bfcfSMatthew Dillon  *    the documentation and/or other materials provided with the
168c10bfcfSMatthew Dillon  *    distribution.
178c10bfcfSMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
188c10bfcfSMatthew Dillon  *    contributors may be used to endorse or promote products derived
198c10bfcfSMatthew Dillon  *    from this software without specific, prior written permission.
2098a7f915SMatthew Dillon  *
218c10bfcfSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
228c10bfcfSMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
238c10bfcfSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
248c10bfcfSMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
258c10bfcfSMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
268c10bfcfSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
278c10bfcfSMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
288c10bfcfSMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
298c10bfcfSMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
308c10bfcfSMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
318c10bfcfSMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3298a7f915SMatthew Dillon  * SUCH DAMAGE.
3398a7f915SMatthew Dillon  */
3498a7f915SMatthew Dillon 
3598a7f915SMatthew Dillon /*
3698a7f915SMatthew Dillon  * This module implements variable storage and management for variant
3798a7f915SMatthew Dillon  * symlinks.  These variables may also be used for general purposes.
3898a7f915SMatthew Dillon  */
3998a7f915SMatthew Dillon 
4098a7f915SMatthew Dillon #include <sys/param.h>
4198a7f915SMatthew Dillon #include <sys/systm.h>
4298a7f915SMatthew Dillon #include <sys/kernel.h>
4398a7f915SMatthew Dillon #include <sys/ucred.h>
4498a7f915SMatthew Dillon #include <sys/resourcevar.h>
4598a7f915SMatthew Dillon #include <sys/proc.h>
46*2b3f93eaSMatthew Dillon #include <sys/caps.h>
47dbc282dfSJoerg Sonnenberger #include <sys/jail.h>
4898a7f915SMatthew Dillon #include <sys/queue.h>
4998a7f915SMatthew Dillon #include <sys/sysctl.h>
5098a7f915SMatthew Dillon #include <sys/malloc.h>
5198a7f915SMatthew Dillon #include <sys/varsym.h>
5280d831e1SMatthew Dillon #include <sys/sysmsg.h>
5398a7f915SMatthew Dillon 
5498a7f915SMatthew Dillon MALLOC_DEFINE(M_VARSYM, "varsym", "variable sets for variant symlinks");
5598a7f915SMatthew Dillon 
56f4e72adaSSascha Wildner static struct varsymset varsymset_sys;
5798a7f915SMatthew Dillon 
5898a7f915SMatthew Dillon /*
5998a7f915SMatthew Dillon  * Initialize the variant symlink subsystem
6098a7f915SMatthew Dillon  */
6198a7f915SMatthew Dillon static void
varsym_sysinit(void * dummy)6298a7f915SMatthew Dillon varsym_sysinit(void *dummy)
6398a7f915SMatthew Dillon {
6498a7f915SMatthew Dillon     varsymset_init(&varsymset_sys, NULL);
6598a7f915SMatthew Dillon }
66ba39e2e0SMatthew Dillon SYSINIT(announce, SI_BOOT2_MACHDEP, SI_ORDER_FIRST, varsym_sysinit, NULL);
6798a7f915SMatthew Dillon 
6898a7f915SMatthew Dillon /*
69063147e5SMatthew Dillon  * varsymreplace() - called from namei
70063147e5SMatthew Dillon  *
71063147e5SMatthew Dillon  *	Do variant symlink variable substitution
72063147e5SMatthew Dillon  */
73063147e5SMatthew Dillon int
varsymreplace(char * cp,int linklen,int maxlen)74063147e5SMatthew Dillon varsymreplace(char *cp, int linklen, int maxlen)
75063147e5SMatthew Dillon {
76063147e5SMatthew Dillon     int rlen;
77063147e5SMatthew Dillon     int xlen;
78063147e5SMatthew Dillon     int nlen;
79063147e5SMatthew Dillon     int i;
80063147e5SMatthew Dillon     varsym_t var;
81063147e5SMatthew Dillon 
82063147e5SMatthew Dillon     rlen = linklen;
83063147e5SMatthew Dillon     while (linklen > 1) {
84063147e5SMatthew Dillon 	if (cp[0] == '$' && cp[1] == '{') {
85063147e5SMatthew Dillon 	    for (i = 2; i < linklen; ++i) {
86063147e5SMatthew Dillon 		if (cp[i] == '}')
87063147e5SMatthew Dillon 		    break;
88063147e5SMatthew Dillon 	    }
89063147e5SMatthew Dillon 	    if (i < linklen &&
90063147e5SMatthew Dillon 		(var = varsymfind(VARSYM_ALL_MASK, cp + 2, i - 2)) != NULL
91063147e5SMatthew Dillon 	    ) {
92063147e5SMatthew Dillon 		xlen = i + 1;			/* bytes to strike */
93063147e5SMatthew Dillon 		nlen = strlen(var->vs_data);	/* bytes to add */
94063147e5SMatthew Dillon 		if (linklen + nlen - xlen >= maxlen) {
95063147e5SMatthew Dillon 		    varsymdrop(var);
96063147e5SMatthew Dillon 		    return(-1);
97063147e5SMatthew Dillon 		}
98063147e5SMatthew Dillon 		KKASSERT(linklen >= xlen);
99063147e5SMatthew Dillon 		if (linklen != xlen)
100063147e5SMatthew Dillon 		    bcopy(cp + xlen, cp + nlen, linklen - xlen);
101063147e5SMatthew Dillon 		bcopy(var->vs_data, cp, nlen);
102063147e5SMatthew Dillon 		linklen += nlen - xlen;	/* new relative length */
103063147e5SMatthew Dillon 		rlen += nlen - xlen;	/* returned total length */
104063147e5SMatthew Dillon 		cp += nlen;		/* adjust past replacement */
105063147e5SMatthew Dillon 		linklen -= nlen;	/* adjust past replacement */
106063147e5SMatthew Dillon 		maxlen -= nlen;		/* adjust past replacement */
107063147e5SMatthew Dillon 	    } else {
108063147e5SMatthew Dillon 		/*
109063147e5SMatthew Dillon 		 * It's ok if i points to the '}', it will simply be
110063147e5SMatthew Dillon 		 * skipped.  i could also have hit linklen.
111063147e5SMatthew Dillon 		 */
112063147e5SMatthew Dillon 		cp += i;
113063147e5SMatthew Dillon 		linklen -= i;
114063147e5SMatthew Dillon 		maxlen -= i;
115063147e5SMatthew Dillon 	    }
116063147e5SMatthew Dillon 	} else {
117063147e5SMatthew Dillon 	    ++cp;
118063147e5SMatthew Dillon 	    --linklen;
119063147e5SMatthew Dillon 	    --maxlen;
120063147e5SMatthew Dillon 	}
121063147e5SMatthew Dillon     }
122063147e5SMatthew Dillon     return(rlen);
123063147e5SMatthew Dillon }
124063147e5SMatthew Dillon 
125063147e5SMatthew Dillon /*
12698a7f915SMatthew Dillon  * varsym_set() system call
12798a7f915SMatthew Dillon  *
12898a7f915SMatthew Dillon  * (int level, const char *name, const char *data)
12998a7f915SMatthew Dillon  */
13098a7f915SMatthew Dillon int
sys_varsym_set(struct sysmsg * sysmsg,const struct varsym_set_args * uap)13180d831e1SMatthew Dillon sys_varsym_set(struct sysmsg *sysmsg, const struct varsym_set_args *uap)
13298a7f915SMatthew Dillon {
13398a7f915SMatthew Dillon     char name[MAXVARSYM_NAME];
13498a7f915SMatthew Dillon     char *buf;
1359910d07bSMatthew Dillon     struct thread *td;
1369910d07bSMatthew Dillon     struct lwp *lp;
13798a7f915SMatthew Dillon     int error;
13880d831e1SMatthew Dillon     int level = uap->level;
13998a7f915SMatthew Dillon 
1409910d07bSMatthew Dillon     td = curthread;
1419910d07bSMatthew Dillon     lp = td->td_lwp;
142e61b3df7SMichael Neumann 
14398a7f915SMatthew Dillon     if ((error = copyinstr(uap->name, name, sizeof(name), NULL)) != 0)
14498a7f915SMatthew Dillon 	goto done2;
145efda3bd0SMatthew Dillon     buf = kmalloc(MAXVARSYM_DATA, M_TEMP, M_WAITOK);
14698a7f915SMatthew Dillon     if (uap->data &&
14798a7f915SMatthew Dillon 	(error = copyinstr(uap->data, buf, MAXVARSYM_DATA, NULL)) != 0)
14898a7f915SMatthew Dillon     {
14998a7f915SMatthew Dillon 	goto done1;
15098a7f915SMatthew Dillon     }
1513919ced0SMatthew Dillon 
15280d831e1SMatthew Dillon     switch(level) {
15398a7f915SMatthew Dillon     case VARSYM_SYS:
1549910d07bSMatthew Dillon 	if (lp != NULL && td->td_ucred->cr_prison != NULL)
15580d831e1SMatthew Dillon 	    level = VARSYM_PRISON;
156f8bfbc51SMatthew Dillon 	/* fall through */
157dbc282dfSJoerg Sonnenberger     case VARSYM_PRISON:
1589910d07bSMatthew Dillon 	if (lp != NULL &&
159*2b3f93eaSMatthew Dillon 	    (error = caps_priv_check_td(td, SYSCAP_NOVARSYM_SYS)) != 0)
160*2b3f93eaSMatthew Dillon 	{
16198a7f915SMatthew Dillon 	    break;
162*2b3f93eaSMatthew Dillon 	}
16398a7f915SMatthew Dillon 	/* fall through */
16498a7f915SMatthew Dillon     case VARSYM_USER:
16598a7f915SMatthew Dillon 	/* XXX check jail / implement per-jail user */
16698a7f915SMatthew Dillon 	/* fall through */
16798a7f915SMatthew Dillon     case VARSYM_PROC:
16898a7f915SMatthew Dillon 	if (uap->data) {
16980d831e1SMatthew Dillon 	    (void)varsymmake(level, name, NULL);
17080d831e1SMatthew Dillon 	    error = varsymmake(level, name, buf);
17198a7f915SMatthew Dillon 	} else {
17280d831e1SMatthew Dillon 	    error = varsymmake(level, name, NULL);
17398a7f915SMatthew Dillon 	}
17498a7f915SMatthew Dillon 	break;
17598a7f915SMatthew Dillon     }
17698a7f915SMatthew Dillon done1:
177efda3bd0SMatthew Dillon     kfree(buf, M_TEMP);
17898a7f915SMatthew Dillon done2:
17998a7f915SMatthew Dillon     return(error);
18098a7f915SMatthew Dillon }
18198a7f915SMatthew Dillon 
18298a7f915SMatthew Dillon /*
18398a7f915SMatthew Dillon  * varsym_get() system call
18498a7f915SMatthew Dillon  *
18598a7f915SMatthew Dillon  * (int mask, const char *wild, char *buf, int bufsize)
1863919ced0SMatthew Dillon  *
1873919ced0SMatthew Dillon  * MPALMOSTSAFE
18898a7f915SMatthew Dillon  */
18998a7f915SMatthew Dillon int
sys_varsym_get(struct sysmsg * sysmsg,const struct varsym_get_args * uap)19080d831e1SMatthew Dillon sys_varsym_get(struct sysmsg *sysmsg, const struct varsym_get_args *uap)
19198a7f915SMatthew Dillon {
19298a7f915SMatthew Dillon     char wild[MAXVARSYM_NAME];
19398a7f915SMatthew Dillon     varsym_t sym;
19498a7f915SMatthew Dillon     int error;
19598a7f915SMatthew Dillon     int dlen;
19698a7f915SMatthew Dillon 
19798a7f915SMatthew Dillon     if ((error = copyinstr(uap->wild, wild, sizeof(wild), NULL)) != 0)
19898a7f915SMatthew Dillon 	goto done;
19998a7f915SMatthew Dillon     sym = varsymfind(uap->mask, wild, strlen(wild));
20098a7f915SMatthew Dillon     if (sym == NULL) {
20198a7f915SMatthew Dillon 	error = ENOENT;
20298a7f915SMatthew Dillon 	goto done;
20398a7f915SMatthew Dillon     }
20498a7f915SMatthew Dillon     dlen = strlen(sym->vs_data);
20598a7f915SMatthew Dillon     if (dlen < uap->bufsize) {
206631c21f2SMatthew Dillon 	error = copyout(sym->vs_data, uap->buf, dlen + 1);
20798a7f915SMatthew Dillon     } else if (uap->bufsize) {
20898a7f915SMatthew Dillon 	copyout("", uap->buf, 1);
209631c21f2SMatthew Dillon 	error = EOVERFLOW;
21098a7f915SMatthew Dillon     }
211631c21f2SMatthew Dillon     sysmsg->sysmsg_result = 0;
21298a7f915SMatthew Dillon     varsymdrop(sym);
21398a7f915SMatthew Dillon done:
214f8bfbc51SMatthew Dillon 
21598a7f915SMatthew Dillon     return(error);
21698a7f915SMatthew Dillon }
21798a7f915SMatthew Dillon 
21898a7f915SMatthew Dillon /*
2190a004aa2SMatthew Dillon  * varsym_list() system call
2200a004aa2SMatthew Dillon  *
2210a004aa2SMatthew Dillon  * (int level, char *buf, int maxsize, int *marker)
2223919ced0SMatthew Dillon  *
2233919ced0SMatthew Dillon  * MPALMOSTSAFE
2240a004aa2SMatthew Dillon  */
2250a004aa2SMatthew Dillon int
sys_varsym_list(struct sysmsg * sysmsg,const struct varsym_list_args * uap)22680d831e1SMatthew Dillon sys_varsym_list(struct sysmsg *sysmsg, const struct varsym_list_args *uap)
2270a004aa2SMatthew Dillon {
2280a004aa2SMatthew Dillon 	struct varsymset *vss;
2290a004aa2SMatthew Dillon 	struct varsyment *ve;
2309910d07bSMatthew Dillon 	struct thread *td;
2310a004aa2SMatthew Dillon 	struct proc *p;
2329910d07bSMatthew Dillon 	struct lwp *lp;
2330a004aa2SMatthew Dillon 	int i;
2340a004aa2SMatthew Dillon 	int error;
2350a004aa2SMatthew Dillon 	int bytes;
2360a004aa2SMatthew Dillon 	int earlyterm;
2370a004aa2SMatthew Dillon 	int marker;
2380a004aa2SMatthew Dillon 
2390a004aa2SMatthew Dillon 	/*
2400a004aa2SMatthew Dillon 	 * Get the marker from userspace.
2410a004aa2SMatthew Dillon 	 */
2420a004aa2SMatthew Dillon 	if ((error = copyin(uap->marker, &marker, sizeof(marker))) != 0)
2430a004aa2SMatthew Dillon 		goto done;
2440a004aa2SMatthew Dillon 
2450a004aa2SMatthew Dillon 	/*
2460a004aa2SMatthew Dillon 	 * Figure out the varsym set.
2470a004aa2SMatthew Dillon 	 */
2489910d07bSMatthew Dillon 	td = curthread;
2499910d07bSMatthew Dillon 	lp = td->td_lwp;
2509910d07bSMatthew Dillon 	p = lp ? lp->lwp_proc : NULL;
2519910d07bSMatthew Dillon 
2520a004aa2SMatthew Dillon 	vss = NULL;
2530a004aa2SMatthew Dillon 
2540a004aa2SMatthew Dillon 	switch (uap->level) {
2550a004aa2SMatthew Dillon 	case VARSYM_PROC:
2560a004aa2SMatthew Dillon 		if (p)
2570a004aa2SMatthew Dillon 			vss = &p->p_varsymset;
2580a004aa2SMatthew Dillon 		break;
2590a004aa2SMatthew Dillon 	case VARSYM_USER:
2609910d07bSMatthew Dillon 		if (lp)
2619910d07bSMatthew Dillon 			vss = &td->td_ucred->cr_uidinfo->ui_varsymset;
2620a004aa2SMatthew Dillon 		break;
2630a004aa2SMatthew Dillon 	case VARSYM_SYS:
2640a004aa2SMatthew Dillon 		vss = &varsymset_sys;
2650a004aa2SMatthew Dillon 		break;
266dbc282dfSJoerg Sonnenberger 	case VARSYM_PRISON:
2679910d07bSMatthew Dillon 		if (lp && td->td_ucred->cr_prison)
2689910d07bSMatthew Dillon 			vss = &td->td_ucred->cr_prison->pr_varsymset;
269dbc282dfSJoerg Sonnenberger 		break;
2700a004aa2SMatthew Dillon 	}
2710a004aa2SMatthew Dillon 	if (vss == NULL) {
2720a004aa2SMatthew Dillon 		error = EINVAL;
2730a004aa2SMatthew Dillon 		goto done;
2740a004aa2SMatthew Dillon 	}
2750a004aa2SMatthew Dillon 
2760a004aa2SMatthew Dillon 	/*
2770a004aa2SMatthew Dillon 	 * Loop through the variables and dump them to uap->buf
2780a004aa2SMatthew Dillon 	 */
2790a004aa2SMatthew Dillon 	i = 0;
2800a004aa2SMatthew Dillon 	bytes = 0;
2810a004aa2SMatthew Dillon 	earlyterm = 0;
2820a004aa2SMatthew Dillon 
2839d7a637eSAggelos Economopoulos 	lockmgr(&vss->vx_lock, LK_SHARED);
2840a004aa2SMatthew Dillon 	TAILQ_FOREACH(ve, &vss->vx_queue, ve_entry) {
2850a004aa2SMatthew Dillon 		varsym_t sym = ve->ve_sym;
2860a004aa2SMatthew Dillon 		int namelen = strlen(sym->vs_name);
2870a004aa2SMatthew Dillon 		int datalen = strlen(sym->vs_data);
2880a004aa2SMatthew Dillon 		int totlen = namelen + datalen + 2;
2890a004aa2SMatthew Dillon 
2900a004aa2SMatthew Dillon 		/*
2910a004aa2SMatthew Dillon 		 * Skip to our index point
2920a004aa2SMatthew Dillon 		 */
2930a004aa2SMatthew Dillon 		if (i < marker) {
2940a004aa2SMatthew Dillon 			++i;
2950a004aa2SMatthew Dillon 			continue;
2960a004aa2SMatthew Dillon 		}
2970a004aa2SMatthew Dillon 
2980a004aa2SMatthew Dillon 		/*
2990a004aa2SMatthew Dillon 		 * Stop if there is insufficient space in the user buffer.
3000a004aa2SMatthew Dillon 		 * If we haven't stored anything yet return EOVERFLOW.
3010a004aa2SMatthew Dillon 		 * Note that the marker index (i) does not change.
3020a004aa2SMatthew Dillon 		 */
3030a004aa2SMatthew Dillon 		if (bytes + totlen > uap->maxsize) {
3040a004aa2SMatthew Dillon 			if (bytes == 0)
3050a004aa2SMatthew Dillon 				error = EOVERFLOW;
3060a004aa2SMatthew Dillon 			earlyterm = 1;
3070a004aa2SMatthew Dillon 			break;
3080a004aa2SMatthew Dillon 		}
3090a004aa2SMatthew Dillon 
3100a004aa2SMatthew Dillon 		error = copyout(sym->vs_name, uap->buf + bytes, namelen + 1);
3110a004aa2SMatthew Dillon 		if (error == 0) {
3120a004aa2SMatthew Dillon 			bytes += namelen + 1;
3130a004aa2SMatthew Dillon 			error = copyout(sym->vs_data, uap->buf + bytes, datalen + 1);
3140a004aa2SMatthew Dillon 			if (error == 0)
3150a004aa2SMatthew Dillon 				bytes += datalen + 1;
3160a004aa2SMatthew Dillon 			else
3170a004aa2SMatthew Dillon 				bytes -= namelen + 1;	/* revert if error */
3180a004aa2SMatthew Dillon 		}
3190a004aa2SMatthew Dillon 		if (error) {
3200a004aa2SMatthew Dillon 			earlyterm = 1;
3210a004aa2SMatthew Dillon 			break;
3220a004aa2SMatthew Dillon 		}
3230a004aa2SMatthew Dillon 		++i;
3240a004aa2SMatthew Dillon 	}
3259d7a637eSAggelos Economopoulos 	lockmgr(&vss->vx_lock, LK_RELEASE);
3260a004aa2SMatthew Dillon 
3270a004aa2SMatthew Dillon 	/*
3280a004aa2SMatthew Dillon 	 * Save the marker back.  If no error occured and earlyterm is clear
3290a004aa2SMatthew Dillon 	 * the marker is set to -1 indicating that the variable list has been
3300a004aa2SMatthew Dillon 	 * exhausted.  If no error occured the number of bytes loaded into
3310a004aa2SMatthew Dillon 	 * the buffer will be returned, otherwise the syscall code returns -1.
3320a004aa2SMatthew Dillon 	 */
3330a004aa2SMatthew Dillon 	if (error == 0 && earlyterm == 0)
3340a004aa2SMatthew Dillon 		marker = -1;
3350a004aa2SMatthew Dillon 	else
3360a004aa2SMatthew Dillon 		marker = i;
3370a004aa2SMatthew Dillon 	if (error == 0)
3380a004aa2SMatthew Dillon 		error = copyout(&marker, uap->marker, sizeof(marker));
33980d831e1SMatthew Dillon 	sysmsg->sysmsg_result = bytes;
3400a004aa2SMatthew Dillon done:
3410a004aa2SMatthew Dillon 	return(error);
3420a004aa2SMatthew Dillon }
3430a004aa2SMatthew Dillon 
3440a004aa2SMatthew Dillon /*
34598a7f915SMatthew Dillon  * Lookup a variant symlink.  XXX use a hash table.
34698a7f915SMatthew Dillon  */
34798a7f915SMatthew Dillon static
34898a7f915SMatthew Dillon struct varsyment *
varsymlookup(struct varsymset * vss,const char * name,int namelen)34998a7f915SMatthew Dillon varsymlookup(struct varsymset *vss, const char *name, int namelen)
35098a7f915SMatthew Dillon {
35198a7f915SMatthew Dillon     struct varsyment *ve;
35298a7f915SMatthew Dillon 
3539d7a637eSAggelos Economopoulos     KKASSERT(lockstatus(&vss->vx_lock, curthread) != 0);
35498a7f915SMatthew Dillon     TAILQ_FOREACH(ve, &vss->vx_queue, ve_entry) {
35598a7f915SMatthew Dillon 	varsym_t var = ve->ve_sym;
35698a7f915SMatthew Dillon 	if (var->vs_namelen == namelen &&
35798a7f915SMatthew Dillon 	    bcmp(name, var->vs_name, namelen) == 0
35898a7f915SMatthew Dillon 	) {
35998a7f915SMatthew Dillon 	    return(ve);
36098a7f915SMatthew Dillon 	}
36198a7f915SMatthew Dillon     }
36298a7f915SMatthew Dillon     return(NULL);
36398a7f915SMatthew Dillon }
36498a7f915SMatthew Dillon 
3659d7a637eSAggelos Economopoulos static
3669d7a637eSAggelos Economopoulos void
vsslock(struct varsymset ** vss,struct varsymset * n)3679d7a637eSAggelos Economopoulos vsslock(struct varsymset **vss, struct varsymset *n)
3689d7a637eSAggelos Economopoulos {
3699d7a637eSAggelos Economopoulos 	if (*vss) {
3709d7a637eSAggelos Economopoulos 		lockmgr(&(*vss)->vx_lock, LK_RELEASE);
3719d7a637eSAggelos Economopoulos 	}
3729d7a637eSAggelos Economopoulos 	lockmgr(&n->vx_lock, LK_SHARED);
3739d7a637eSAggelos Economopoulos 	*vss = n;
3749d7a637eSAggelos Economopoulos }
3759d7a637eSAggelos Economopoulos 
37698a7f915SMatthew Dillon varsym_t
varsymfind(int mask,const char * name,int namelen)37798a7f915SMatthew Dillon varsymfind(int mask, const char *name, int namelen)
37898a7f915SMatthew Dillon {
37998a7f915SMatthew Dillon     struct varsyment *ve = NULL;
3809d7a637eSAggelos Economopoulos     struct varsymset *vss = NULL;
3819910d07bSMatthew Dillon     struct thread *td;
3829910d07bSMatthew Dillon     struct lwp *lp;
3839910d07bSMatthew Dillon     struct proc *p;
38498a7f915SMatthew Dillon     varsym_t sym;
38598a7f915SMatthew Dillon 
3869910d07bSMatthew Dillon     td = curthread;
3879910d07bSMatthew Dillon     lp = td->td_lwp;
3889910d07bSMatthew Dillon     p = lp ? lp->lwp_proc : NULL;
3899910d07bSMatthew Dillon 
3909910d07bSMatthew Dillon     if ((mask & (VARSYM_PROC_MASK|VARSYM_USER_MASK)) && lp != NULL) {
3919d7a637eSAggelos Economopoulos 	if (mask & VARSYM_PROC_MASK) {
3929d7a637eSAggelos Economopoulos 	    vsslock(&vss, &p->p_varsymset);
3939d7a637eSAggelos Economopoulos 	    ve = varsymlookup(vss, name, namelen);
3949d7a637eSAggelos Economopoulos 	}
3959d7a637eSAggelos Economopoulos 	if (ve == NULL && (mask & VARSYM_USER_MASK)) {
3969910d07bSMatthew Dillon 	    vsslock(&vss, &td->td_ucred->cr_uidinfo->ui_varsymset);
3979d7a637eSAggelos Economopoulos 	    ve = varsymlookup(vss, name, namelen);
3989d7a637eSAggelos Economopoulos 	}
39998a7f915SMatthew Dillon     }
400dbc282dfSJoerg Sonnenberger     if (ve == NULL && (mask & VARSYM_SYS_MASK)) {
4019910d07bSMatthew Dillon 	if (lp != NULL && td->td_ucred->cr_prison) {
4029910d07bSMatthew Dillon 	    vsslock(&vss, &td->td_ucred->cr_prison->pr_varsymset);
4039d7a637eSAggelos Economopoulos 	    ve = varsymlookup(vss, name, namelen);
4049d7a637eSAggelos Economopoulos 	} else {
4059d7a637eSAggelos Economopoulos 	    vsslock(&vss, &varsymset_sys);
4069d7a637eSAggelos Economopoulos 	    ve = varsymlookup(vss, name, namelen);
4079d7a637eSAggelos Economopoulos 	}
408dbc282dfSJoerg Sonnenberger     }
40998a7f915SMatthew Dillon     if (ve) {
41098a7f915SMatthew Dillon 	sym = ve->ve_sym;
4119d7a637eSAggelos Economopoulos 	atomic_add_int(&sym->vs_refs, 1);
41298a7f915SMatthew Dillon     } else {
4139d7a637eSAggelos Economopoulos 	sym = NULL;
41498a7f915SMatthew Dillon     }
415548d73efSSascha Wildner     if (vss)
4169d7a637eSAggelos Economopoulos 	lockmgr(&vss->vx_lock, LK_RELEASE);
4179d7a637eSAggelos Economopoulos     return sym;
41898a7f915SMatthew Dillon }
41998a7f915SMatthew Dillon 
42098a7f915SMatthew Dillon int
varsymmake(int level,const char * name,const char * data)42198a7f915SMatthew Dillon varsymmake(int level, const char *name, const char *data)
42298a7f915SMatthew Dillon {
42398a7f915SMatthew Dillon     struct varsymset *vss = NULL;
42498a7f915SMatthew Dillon     struct varsyment *ve;
4259910d07bSMatthew Dillon     struct thread *td;
4269910d07bSMatthew Dillon     struct proc *p;
4279910d07bSMatthew Dillon     struct lwp *lp;
42898a7f915SMatthew Dillon     varsym_t sym;
42998a7f915SMatthew Dillon     int namelen = strlen(name);
43098a7f915SMatthew Dillon     int datalen;
43198a7f915SMatthew Dillon     int error;
43298a7f915SMatthew Dillon 
4339910d07bSMatthew Dillon     td = curthread;
4349910d07bSMatthew Dillon     lp = td->td_lwp;
4359910d07bSMatthew Dillon     p = lp ? lp->lwp_proc : NULL;
4369910d07bSMatthew Dillon 
43798a7f915SMatthew Dillon     switch(level) {
43898a7f915SMatthew Dillon     case VARSYM_PROC:
43998a7f915SMatthew Dillon 	if (p)
44098a7f915SMatthew Dillon 	    vss = &p->p_varsymset;
44198a7f915SMatthew Dillon 	break;
44298a7f915SMatthew Dillon     case VARSYM_USER:
4439910d07bSMatthew Dillon 	if (lp)
4449910d07bSMatthew Dillon 	    vss = &td->td_ucred->cr_uidinfo->ui_varsymset;
44598a7f915SMatthew Dillon 	break;
44698a7f915SMatthew Dillon     case VARSYM_SYS:
44798a7f915SMatthew Dillon 	vss = &varsymset_sys;
44898a7f915SMatthew Dillon 	break;
449dbc282dfSJoerg Sonnenberger     case VARSYM_PRISON:
4509910d07bSMatthew Dillon 	if (lp && td->td_ucred->cr_prison)
4519910d07bSMatthew Dillon 	    vss = &td->td_ucred->cr_prison->pr_varsymset;
452dbc282dfSJoerg Sonnenberger 	break;
45398a7f915SMatthew Dillon     }
45498a7f915SMatthew Dillon     if (vss == NULL) {
4559d7a637eSAggelos Economopoulos 	return EINVAL;
4569d7a637eSAggelos Economopoulos     }
4579d7a637eSAggelos Economopoulos     lockmgr(&vss->vx_lock, LK_EXCLUSIVE);
4589d7a637eSAggelos Economopoulos     if (data && vss->vx_setsize >= MAXVARSYM_SET) {
45998a7f915SMatthew Dillon 	error = E2BIG;
46098a7f915SMatthew Dillon     } else if (data) {
46198a7f915SMatthew Dillon 	datalen = strlen(data);
462efda3bd0SMatthew Dillon 	ve = kmalloc(sizeof(struct varsyment), M_VARSYM, M_WAITOK|M_ZERO);
463efda3bd0SMatthew Dillon 	sym = kmalloc(sizeof(struct varsym) + namelen + datalen + 2, M_VARSYM, M_WAITOK);
46498a7f915SMatthew Dillon 	ve->ve_sym = sym;
46598a7f915SMatthew Dillon 	sym->vs_refs = 1;
46698a7f915SMatthew Dillon 	sym->vs_namelen = namelen;
46798a7f915SMatthew Dillon 	sym->vs_name = (char *)(sym + 1);
46898a7f915SMatthew Dillon 	sym->vs_data = sym->vs_name + namelen + 1;
46998a7f915SMatthew Dillon 	strcpy(sym->vs_name, name);
47098a7f915SMatthew Dillon 	strcpy(sym->vs_data, data);
47198a7f915SMatthew Dillon 	TAILQ_INSERT_TAIL(&vss->vx_queue, ve, ve_entry);
47298a7f915SMatthew Dillon 	vss->vx_setsize += sizeof(struct varsyment) + sizeof(struct varsym) + namelen + datalen + 8;
47398a7f915SMatthew Dillon 	error = 0;
47498a7f915SMatthew Dillon     } else {
47598a7f915SMatthew Dillon 	if ((ve = varsymlookup(vss, name, namelen)) != NULL) {
47698a7f915SMatthew Dillon 	    TAILQ_REMOVE(&vss->vx_queue, ve, ve_entry);
47798a7f915SMatthew Dillon 	    vss->vx_setsize -= sizeof(struct varsyment) + sizeof(struct varsym) + namelen + strlen(ve->ve_sym->vs_data) + 8;
47898a7f915SMatthew Dillon 	    varsymdrop(ve->ve_sym);
479efda3bd0SMatthew Dillon 	    kfree(ve, M_VARSYM);
48098a7f915SMatthew Dillon 	    error = 0;
48198a7f915SMatthew Dillon 	} else {
48298a7f915SMatthew Dillon 	    error = ENOENT;
48398a7f915SMatthew Dillon 	}
48498a7f915SMatthew Dillon     }
4859d7a637eSAggelos Economopoulos     lockmgr(&vss->vx_lock, LK_RELEASE);
48698a7f915SMatthew Dillon     return(error);
48798a7f915SMatthew Dillon }
48898a7f915SMatthew Dillon 
48998a7f915SMatthew Dillon void
varsymdrop(varsym_t sym)49098a7f915SMatthew Dillon varsymdrop(varsym_t sym)
49198a7f915SMatthew Dillon {
49298a7f915SMatthew Dillon     KKASSERT(sym->vs_refs > 0);
4939d7a637eSAggelos Economopoulos     if (atomic_fetchadd_int(&sym->vs_refs, -1) == 1) {
494efda3bd0SMatthew Dillon 	kfree(sym, M_VARSYM);
49598a7f915SMatthew Dillon     }
49698a7f915SMatthew Dillon }
49798a7f915SMatthew Dillon 
4989d7a637eSAggelos Economopoulos /*
4999d7a637eSAggelos Economopoulos  * Insert a duplicate of ve in vss. Does not do any locking,
5009d7a637eSAggelos Economopoulos  * so it is the callers responsibility to make sure nobody
5019d7a637eSAggelos Economopoulos  * else can mess with the TAILQ in vss at the same time.
5029d7a637eSAggelos Economopoulos  */
50398a7f915SMatthew Dillon static void
varsymdup(struct varsymset * vss,struct varsyment * ve)50498a7f915SMatthew Dillon varsymdup(struct varsymset *vss, struct varsyment *ve)
50598a7f915SMatthew Dillon {
50698a7f915SMatthew Dillon     struct varsyment *nve;
50798a7f915SMatthew Dillon 
508efda3bd0SMatthew Dillon     nve = kmalloc(sizeof(struct varsyment), M_VARSYM, M_WAITOK|M_ZERO);
50998a7f915SMatthew Dillon     nve->ve_sym = ve->ve_sym;
5109d7a637eSAggelos Economopoulos     ++nve->ve_sym->vs_refs;	/* can't be reached, no need for atomic add */
5119d7a637eSAggelos Economopoulos     /*
5129d7a637eSAggelos Economopoulos      * We're only called through varsymset_init() so vss is not yet reachable,
5139d7a637eSAggelos Economopoulos      * no need to lock.
5149d7a637eSAggelos Economopoulos      */
51598a7f915SMatthew Dillon     TAILQ_INSERT_TAIL(&vss->vx_queue, nve, ve_entry);
51698a7f915SMatthew Dillon }
51798a7f915SMatthew Dillon 
51898a7f915SMatthew Dillon void
varsymset_init(struct varsymset * vss,struct varsymset * copy)51998a7f915SMatthew Dillon varsymset_init(struct varsymset *vss, struct varsymset *copy)
52098a7f915SMatthew Dillon {
52198a7f915SMatthew Dillon     struct varsyment *ve;
52298a7f915SMatthew Dillon 
52398a7f915SMatthew Dillon     TAILQ_INIT(&vss->vx_queue);
5249d7a637eSAggelos Economopoulos     lockinit(&vss->vx_lock, "vx", 0, 0);
52598a7f915SMatthew Dillon     if (copy) {
52698a7f915SMatthew Dillon 	TAILQ_FOREACH(ve, &copy->vx_queue, ve_entry) {
52798a7f915SMatthew Dillon 	    varsymdup(vss, ve);
52898a7f915SMatthew Dillon 	}
52998a7f915SMatthew Dillon 	vss->vx_setsize = copy->vx_setsize;
53098a7f915SMatthew Dillon     }
53198a7f915SMatthew Dillon }
53298a7f915SMatthew Dillon 
53398a7f915SMatthew Dillon void
varsymset_clean(struct varsymset * vss)53498a7f915SMatthew Dillon varsymset_clean(struct varsymset *vss)
53598a7f915SMatthew Dillon {
53698a7f915SMatthew Dillon     struct varsyment *ve;
53798a7f915SMatthew Dillon 
5389d7a637eSAggelos Economopoulos     lockmgr(&vss->vx_lock, LK_EXCLUSIVE);
53998a7f915SMatthew Dillon     while ((ve = TAILQ_FIRST(&vss->vx_queue)) != NULL) {
54098a7f915SMatthew Dillon 	TAILQ_REMOVE(&vss->vx_queue, ve, ve_entry);
54198a7f915SMatthew Dillon 	varsymdrop(ve->ve_sym);
542efda3bd0SMatthew Dillon 	kfree(ve, M_VARSYM);
54398a7f915SMatthew Dillon     }
54498a7f915SMatthew Dillon     vss->vx_setsize = 0;
5459d7a637eSAggelos Economopoulos     lockmgr(&vss->vx_lock, LK_RELEASE);
54698a7f915SMatthew Dillon }
54798a7f915SMatthew Dillon 
548