198a7f915SMatthew Dillon /* 2*8c10bfcfSMatthew Dillon * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. 3*8c10bfcfSMatthew Dillon * 4*8c10bfcfSMatthew Dillon * This code is derived from software contributed to The DragonFly Project 5*8c10bfcfSMatthew 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: 10*8c10bfcfSMatthew 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 14*8c10bfcfSMatthew Dillon * notice, this list of conditions and the following disclaimer in 15*8c10bfcfSMatthew Dillon * the documentation and/or other materials provided with the 16*8c10bfcfSMatthew Dillon * distribution. 17*8c10bfcfSMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its 18*8c10bfcfSMatthew Dillon * contributors may be used to endorse or promote products derived 19*8c10bfcfSMatthew Dillon * from this software without specific, prior written permission. 2098a7f915SMatthew Dillon * 21*8c10bfcfSMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22*8c10bfcfSMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23*8c10bfcfSMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24*8c10bfcfSMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25*8c10bfcfSMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26*8c10bfcfSMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27*8c10bfcfSMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28*8c10bfcfSMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29*8c10bfcfSMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30*8c10bfcfSMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31*8c10bfcfSMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3298a7f915SMatthew Dillon * SUCH DAMAGE. 3398a7f915SMatthew Dillon * 34*8c10bfcfSMatthew Dillon * $DragonFly: src/sys/kern/kern_varsym.c,v 1.5 2004/07/16 05:51:10 dillon Exp $ 3598a7f915SMatthew Dillon */ 3698a7f915SMatthew Dillon 3798a7f915SMatthew Dillon /* 3898a7f915SMatthew Dillon * This module implements variable storage and management for variant 3998a7f915SMatthew Dillon * symlinks. These variables may also be used for general purposes. 4098a7f915SMatthew Dillon */ 4198a7f915SMatthew Dillon 4298a7f915SMatthew Dillon #include <sys/param.h> 4398a7f915SMatthew Dillon #include <sys/systm.h> 4498a7f915SMatthew Dillon #include <sys/kernel.h> 4598a7f915SMatthew Dillon #include <sys/ucred.h> 4698a7f915SMatthew Dillon #include <sys/resourcevar.h> 4798a7f915SMatthew Dillon #include <sys/proc.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> 5298a7f915SMatthew Dillon #include <sys/sysproto.h> 5398a7f915SMatthew Dillon 5498a7f915SMatthew Dillon MALLOC_DEFINE(M_VARSYM, "varsym", "variable sets for variant symlinks"); 5598a7f915SMatthew Dillon 5698a7f915SMatthew Dillon struct varsymset varsymset_sys; 5798a7f915SMatthew Dillon 5898a7f915SMatthew Dillon /* 5998a7f915SMatthew Dillon * Initialize the variant symlink subsystem 6098a7f915SMatthew Dillon */ 6198a7f915SMatthew Dillon static void 6298a7f915SMatthew Dillon varsym_sysinit(void *dummy) 6398a7f915SMatthew Dillon { 6498a7f915SMatthew Dillon varsymset_init(&varsymset_sys, NULL); 6598a7f915SMatthew Dillon } 6698a7f915SMatthew Dillon SYSINIT(announce, SI_SUB_INTRINSIC, 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 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 13198a7f915SMatthew Dillon varsym_set(struct varsym_set_args *uap) 13298a7f915SMatthew Dillon { 13398a7f915SMatthew Dillon char name[MAXVARSYM_NAME]; 13498a7f915SMatthew Dillon char *buf; 13598a7f915SMatthew Dillon int error; 13698a7f915SMatthew Dillon 13798a7f915SMatthew Dillon if ((error = copyinstr(uap->name, name, sizeof(name), NULL)) != 0) 13898a7f915SMatthew Dillon goto done2; 139dc1fd4b3SMatthew Dillon buf = malloc(MAXVARSYM_DATA, M_TEMP, M_WAITOK); 14098a7f915SMatthew Dillon if (uap->data && 14198a7f915SMatthew Dillon (error = copyinstr(uap->data, buf, MAXVARSYM_DATA, NULL)) != 0) 14298a7f915SMatthew Dillon { 14398a7f915SMatthew Dillon goto done1; 14498a7f915SMatthew Dillon } 14598a7f915SMatthew Dillon switch(uap->level) { 14698a7f915SMatthew Dillon case VARSYM_SYS: 14798a7f915SMatthew Dillon if ((error = suser(curthread)) != 0) 14898a7f915SMatthew Dillon break; 14998a7f915SMatthew Dillon /* XXX implement per-jail sys */ 15098a7f915SMatthew Dillon /* fall through */ 15198a7f915SMatthew Dillon case VARSYM_USER: 15298a7f915SMatthew Dillon /* XXX check jail / implement per-jail user */ 15398a7f915SMatthew Dillon /* fall through */ 15498a7f915SMatthew Dillon case VARSYM_PROC: 15598a7f915SMatthew Dillon if (uap->data) { 15698a7f915SMatthew Dillon (void)varsymmake(uap->level, name, NULL); 15798a7f915SMatthew Dillon error = varsymmake(uap->level, name, buf); 15898a7f915SMatthew Dillon } else { 15998a7f915SMatthew Dillon error = varsymmake(uap->level, name, NULL); 16098a7f915SMatthew Dillon } 16198a7f915SMatthew Dillon break; 16298a7f915SMatthew Dillon } 16398a7f915SMatthew Dillon done1: 16498a7f915SMatthew Dillon free(buf, M_TEMP); 16598a7f915SMatthew Dillon done2: 16698a7f915SMatthew Dillon return(error); 16798a7f915SMatthew Dillon } 16898a7f915SMatthew Dillon 16998a7f915SMatthew Dillon /* 17098a7f915SMatthew Dillon * varsym_get() system call 17198a7f915SMatthew Dillon * 17298a7f915SMatthew Dillon * (int mask, const char *wild, char *buf, int bufsize) 17398a7f915SMatthew Dillon */ 17498a7f915SMatthew Dillon int 17598a7f915SMatthew Dillon varsym_get(struct varsym_get_args *uap) 17698a7f915SMatthew Dillon { 17798a7f915SMatthew Dillon char wild[MAXVARSYM_NAME]; 17898a7f915SMatthew Dillon varsym_t sym; 17998a7f915SMatthew Dillon int error; 18098a7f915SMatthew Dillon int dlen; 18198a7f915SMatthew Dillon 18298a7f915SMatthew Dillon if ((error = copyinstr(uap->wild, wild, sizeof(wild), NULL)) != 0) 18398a7f915SMatthew Dillon goto done; 18498a7f915SMatthew Dillon sym = varsymfind(uap->mask, wild, strlen(wild)); 18598a7f915SMatthew Dillon if (sym == NULL) { 18698a7f915SMatthew Dillon error = ENOENT; 18798a7f915SMatthew Dillon goto done; 18898a7f915SMatthew Dillon } 18998a7f915SMatthew Dillon dlen = strlen(sym->vs_data); 19098a7f915SMatthew Dillon if (dlen < uap->bufsize) { 19198a7f915SMatthew Dillon copyout(sym->vs_data, uap->buf, dlen + 1); 19298a7f915SMatthew Dillon } else if (uap->bufsize) { 19398a7f915SMatthew Dillon copyout("", uap->buf, 1); 19498a7f915SMatthew Dillon } 19598a7f915SMatthew Dillon uap->sysmsg_result = dlen + 1; 19698a7f915SMatthew Dillon varsymdrop(sym); 19798a7f915SMatthew Dillon done: 19898a7f915SMatthew Dillon return(error); 19998a7f915SMatthew Dillon } 20098a7f915SMatthew Dillon 20198a7f915SMatthew Dillon /* 2020a004aa2SMatthew Dillon * varsym_list() system call 2030a004aa2SMatthew Dillon * 2040a004aa2SMatthew Dillon * (int level, char *buf, int maxsize, int *marker) 2050a004aa2SMatthew Dillon */ 2060a004aa2SMatthew Dillon int 2070a004aa2SMatthew Dillon varsym_list(struct varsym_list_args *uap) 2080a004aa2SMatthew Dillon { 2090a004aa2SMatthew Dillon struct varsymset *vss; 2100a004aa2SMatthew Dillon struct varsyment *ve; 2110a004aa2SMatthew Dillon struct proc *p; 2120a004aa2SMatthew Dillon int i; 2130a004aa2SMatthew Dillon int error; 2140a004aa2SMatthew Dillon int bytes; 2150a004aa2SMatthew Dillon int earlyterm; 2160a004aa2SMatthew Dillon int marker; 2170a004aa2SMatthew Dillon 2180a004aa2SMatthew Dillon /* 2190a004aa2SMatthew Dillon * Get the marker from userspace. 2200a004aa2SMatthew Dillon */ 2210a004aa2SMatthew Dillon if ((error = copyin(uap->marker, &marker, sizeof(marker))) != 0) 2220a004aa2SMatthew Dillon goto done; 2230a004aa2SMatthew Dillon 2240a004aa2SMatthew Dillon /* 2250a004aa2SMatthew Dillon * Figure out the varsym set. 2260a004aa2SMatthew Dillon */ 2270a004aa2SMatthew Dillon p = curproc; 2280a004aa2SMatthew Dillon vss = NULL; 2290a004aa2SMatthew Dillon 2300a004aa2SMatthew Dillon switch (uap->level) { 2310a004aa2SMatthew Dillon case VARSYM_PROC: 2320a004aa2SMatthew Dillon if (p) 2330a004aa2SMatthew Dillon vss = &p->p_varsymset; 2340a004aa2SMatthew Dillon break; 2350a004aa2SMatthew Dillon case VARSYM_USER: 2360a004aa2SMatthew Dillon if (p) 2370a004aa2SMatthew Dillon vss = &p->p_ucred->cr_uidinfo->ui_varsymset; 2380a004aa2SMatthew Dillon break; 2390a004aa2SMatthew Dillon case VARSYM_SYS: 2400a004aa2SMatthew Dillon vss = &varsymset_sys; 2410a004aa2SMatthew Dillon break; 2420a004aa2SMatthew Dillon } 2430a004aa2SMatthew Dillon if (vss == NULL) { 2440a004aa2SMatthew Dillon error = EINVAL; 2450a004aa2SMatthew Dillon goto done; 2460a004aa2SMatthew Dillon } 2470a004aa2SMatthew Dillon 2480a004aa2SMatthew Dillon /* 2490a004aa2SMatthew Dillon * Loop through the variables and dump them to uap->buf 2500a004aa2SMatthew Dillon */ 2510a004aa2SMatthew Dillon i = 0; 2520a004aa2SMatthew Dillon bytes = 0; 2530a004aa2SMatthew Dillon earlyterm = 0; 2540a004aa2SMatthew Dillon 2550a004aa2SMatthew Dillon TAILQ_FOREACH(ve, &vss->vx_queue, ve_entry) { 2560a004aa2SMatthew Dillon varsym_t sym = ve->ve_sym; 2570a004aa2SMatthew Dillon int namelen = strlen(sym->vs_name); 2580a004aa2SMatthew Dillon int datalen = strlen(sym->vs_data); 2590a004aa2SMatthew Dillon int totlen = namelen + datalen + 2; 2600a004aa2SMatthew Dillon 2610a004aa2SMatthew Dillon /* 2620a004aa2SMatthew Dillon * Skip to our index point 2630a004aa2SMatthew Dillon */ 2640a004aa2SMatthew Dillon if (i < marker) { 2650a004aa2SMatthew Dillon ++i; 2660a004aa2SMatthew Dillon continue; 2670a004aa2SMatthew Dillon } 2680a004aa2SMatthew Dillon 2690a004aa2SMatthew Dillon /* 2700a004aa2SMatthew Dillon * Stop if there is insufficient space in the user buffer. 2710a004aa2SMatthew Dillon * If we haven't stored anything yet return EOVERFLOW. 2720a004aa2SMatthew Dillon * Note that the marker index (i) does not change. 2730a004aa2SMatthew Dillon */ 2740a004aa2SMatthew Dillon if (bytes + totlen > uap->maxsize) { 2750a004aa2SMatthew Dillon if (bytes == 0) 2760a004aa2SMatthew Dillon error = EOVERFLOW; 2770a004aa2SMatthew Dillon earlyterm = 1; 2780a004aa2SMatthew Dillon break; 2790a004aa2SMatthew Dillon } 2800a004aa2SMatthew Dillon 2810a004aa2SMatthew Dillon error = copyout(sym->vs_name, uap->buf + bytes, namelen + 1); 2820a004aa2SMatthew Dillon if (error == 0) { 2830a004aa2SMatthew Dillon bytes += namelen + 1; 2840a004aa2SMatthew Dillon error = copyout(sym->vs_data, uap->buf + bytes, datalen + 1); 2850a004aa2SMatthew Dillon if (error == 0) 2860a004aa2SMatthew Dillon bytes += datalen + 1; 2870a004aa2SMatthew Dillon else 2880a004aa2SMatthew Dillon bytes -= namelen + 1; /* revert if error */ 2890a004aa2SMatthew Dillon } 2900a004aa2SMatthew Dillon if (error) { 2910a004aa2SMatthew Dillon earlyterm = 1; 2920a004aa2SMatthew Dillon break; 2930a004aa2SMatthew Dillon } 2940a004aa2SMatthew Dillon ++i; 2950a004aa2SMatthew Dillon } 2960a004aa2SMatthew Dillon 2970a004aa2SMatthew Dillon /* 2980a004aa2SMatthew Dillon * Save the marker back. If no error occured and earlyterm is clear 2990a004aa2SMatthew Dillon * the marker is set to -1 indicating that the variable list has been 3000a004aa2SMatthew Dillon * exhausted. If no error occured the number of bytes loaded into 3010a004aa2SMatthew Dillon * the buffer will be returned, otherwise the syscall code returns -1. 3020a004aa2SMatthew Dillon */ 3030a004aa2SMatthew Dillon if (error == 0 && earlyterm == 0) 3040a004aa2SMatthew Dillon marker = -1; 3050a004aa2SMatthew Dillon else 3060a004aa2SMatthew Dillon marker = i; 3070a004aa2SMatthew Dillon if (error == 0) 3080a004aa2SMatthew Dillon error = copyout(&marker, uap->marker, sizeof(marker)); 3090a004aa2SMatthew Dillon uap->sysmsg_result = bytes; 3100a004aa2SMatthew Dillon done: 3110a004aa2SMatthew Dillon return(error); 3120a004aa2SMatthew Dillon } 3130a004aa2SMatthew Dillon 3140a004aa2SMatthew Dillon /* 31598a7f915SMatthew Dillon * Lookup a variant symlink. XXX use a hash table. 31698a7f915SMatthew Dillon */ 31798a7f915SMatthew Dillon static 31898a7f915SMatthew Dillon struct varsyment * 31998a7f915SMatthew Dillon varsymlookup(struct varsymset *vss, const char *name, int namelen) 32098a7f915SMatthew Dillon { 32198a7f915SMatthew Dillon struct varsyment *ve; 32298a7f915SMatthew Dillon 32398a7f915SMatthew Dillon TAILQ_FOREACH(ve, &vss->vx_queue, ve_entry) { 32498a7f915SMatthew Dillon varsym_t var = ve->ve_sym; 32598a7f915SMatthew Dillon if (var->vs_namelen == namelen && 32698a7f915SMatthew Dillon bcmp(name, var->vs_name, namelen) == 0 32798a7f915SMatthew Dillon ) { 32898a7f915SMatthew Dillon return(ve); 32998a7f915SMatthew Dillon } 33098a7f915SMatthew Dillon } 33198a7f915SMatthew Dillon return(NULL); 33298a7f915SMatthew Dillon } 33398a7f915SMatthew Dillon 33498a7f915SMatthew Dillon varsym_t 33598a7f915SMatthew Dillon varsymfind(int mask, const char *name, int namelen) 33698a7f915SMatthew Dillon { 33798a7f915SMatthew Dillon struct proc *p; 33898a7f915SMatthew Dillon struct varsyment *ve = NULL; 33998a7f915SMatthew Dillon varsym_t sym; 34098a7f915SMatthew Dillon 34198a7f915SMatthew Dillon if ((mask & (VARSYM_PROC_MASK|VARSYM_USER_MASK)) && (p = curproc) != NULL) { 34298a7f915SMatthew Dillon if (mask & VARSYM_PROC_MASK) 34398a7f915SMatthew Dillon ve = varsymlookup(&p->p_varsymset, name, namelen); 34498a7f915SMatthew Dillon if (ve == NULL && (mask & VARSYM_USER_MASK)) 34598a7f915SMatthew Dillon ve = varsymlookup(&p->p_ucred->cr_uidinfo->ui_varsymset, name, namelen); 34698a7f915SMatthew Dillon } 34798a7f915SMatthew Dillon if (ve == NULL && (mask & VARSYM_SYS_MASK)) 34898a7f915SMatthew Dillon ve = varsymlookup(&varsymset_sys, name, namelen); 34998a7f915SMatthew Dillon if (ve) { 35098a7f915SMatthew Dillon sym = ve->ve_sym; 35198a7f915SMatthew Dillon ++sym->vs_refs; 35298a7f915SMatthew Dillon return(sym); 35398a7f915SMatthew Dillon } else { 35498a7f915SMatthew Dillon return(NULL); 35598a7f915SMatthew Dillon } 35698a7f915SMatthew Dillon } 35798a7f915SMatthew Dillon 35898a7f915SMatthew Dillon int 35998a7f915SMatthew Dillon varsymmake(int level, const char *name, const char *data) 36098a7f915SMatthew Dillon { 36198a7f915SMatthew Dillon struct varsymset *vss = NULL; 36298a7f915SMatthew Dillon struct varsyment *ve; 36398a7f915SMatthew Dillon struct proc *p = curproc; 36498a7f915SMatthew Dillon varsym_t sym; 36598a7f915SMatthew Dillon int namelen = strlen(name); 36698a7f915SMatthew Dillon int datalen; 36798a7f915SMatthew Dillon int error; 36898a7f915SMatthew Dillon 36998a7f915SMatthew Dillon switch(level) { 37098a7f915SMatthew Dillon case VARSYM_PROC: 37198a7f915SMatthew Dillon if (p) 37298a7f915SMatthew Dillon vss = &p->p_varsymset; 37398a7f915SMatthew Dillon break; 37498a7f915SMatthew Dillon case VARSYM_USER: 37598a7f915SMatthew Dillon if (p) 37698a7f915SMatthew Dillon vss = &p->p_ucred->cr_uidinfo->ui_varsymset; 37798a7f915SMatthew Dillon break; 37898a7f915SMatthew Dillon case VARSYM_SYS: 37998a7f915SMatthew Dillon vss = &varsymset_sys; 38098a7f915SMatthew Dillon break; 38198a7f915SMatthew Dillon } 38298a7f915SMatthew Dillon if (vss == NULL) { 38398a7f915SMatthew Dillon error = EINVAL; 38498a7f915SMatthew Dillon } else if (data && vss->vx_setsize >= MAXVARSYM_SET) { 38598a7f915SMatthew Dillon error = E2BIG; 38698a7f915SMatthew Dillon } else if (data) { 38798a7f915SMatthew Dillon datalen = strlen(data); 388dc1fd4b3SMatthew Dillon ve = malloc(sizeof(struct varsyment), M_VARSYM, M_WAITOK|M_ZERO); 389dc1fd4b3SMatthew Dillon sym = malloc(sizeof(struct varsym) + namelen + datalen + 2, M_VARSYM, M_WAITOK); 39098a7f915SMatthew Dillon ve->ve_sym = sym; 39198a7f915SMatthew Dillon sym->vs_refs = 1; 39298a7f915SMatthew Dillon sym->vs_namelen = namelen; 39398a7f915SMatthew Dillon sym->vs_name = (char *)(sym + 1); 39498a7f915SMatthew Dillon sym->vs_data = sym->vs_name + namelen + 1; 39598a7f915SMatthew Dillon strcpy(sym->vs_name, name); 39698a7f915SMatthew Dillon strcpy(sym->vs_data, data); 39798a7f915SMatthew Dillon TAILQ_INSERT_TAIL(&vss->vx_queue, ve, ve_entry); 39898a7f915SMatthew Dillon vss->vx_setsize += sizeof(struct varsyment) + sizeof(struct varsym) + namelen + datalen + 8; 39998a7f915SMatthew Dillon error = 0; 40098a7f915SMatthew Dillon } else { 40198a7f915SMatthew Dillon if ((ve = varsymlookup(vss, name, namelen)) != NULL) { 40298a7f915SMatthew Dillon TAILQ_REMOVE(&vss->vx_queue, ve, ve_entry); 40398a7f915SMatthew Dillon vss->vx_setsize -= sizeof(struct varsyment) + sizeof(struct varsym) + namelen + strlen(ve->ve_sym->vs_data) + 8; 40498a7f915SMatthew Dillon varsymdrop(ve->ve_sym); 40598a7f915SMatthew Dillon free(ve, M_VARSYM); 40698a7f915SMatthew Dillon error = 0; 40798a7f915SMatthew Dillon } else { 40898a7f915SMatthew Dillon error = ENOENT; 40998a7f915SMatthew Dillon } 41098a7f915SMatthew Dillon } 41198a7f915SMatthew Dillon return(error); 41298a7f915SMatthew Dillon } 41398a7f915SMatthew Dillon 41498a7f915SMatthew Dillon void 41598a7f915SMatthew Dillon varsymdrop(varsym_t sym) 41698a7f915SMatthew Dillon { 41798a7f915SMatthew Dillon KKASSERT(sym->vs_refs > 0); 41898a7f915SMatthew Dillon if (--sym->vs_refs == 0) { 41998a7f915SMatthew Dillon free(sym, M_VARSYM); 42098a7f915SMatthew Dillon } 42198a7f915SMatthew Dillon } 42298a7f915SMatthew Dillon 42398a7f915SMatthew Dillon static void 42498a7f915SMatthew Dillon varsymdup(struct varsymset *vss, struct varsyment *ve) 42598a7f915SMatthew Dillon { 42698a7f915SMatthew Dillon struct varsyment *nve; 42798a7f915SMatthew Dillon 428dc1fd4b3SMatthew Dillon nve = malloc(sizeof(struct varsyment), M_VARSYM, M_WAITOK|M_ZERO); 42998a7f915SMatthew Dillon nve->ve_sym = ve->ve_sym; 43098a7f915SMatthew Dillon ++nve->ve_sym->vs_refs; 43198a7f915SMatthew Dillon TAILQ_INSERT_TAIL(&vss->vx_queue, nve, ve_entry); 43298a7f915SMatthew Dillon } 43398a7f915SMatthew Dillon 43498a7f915SMatthew Dillon void 43598a7f915SMatthew Dillon varsymset_init(struct varsymset *vss, struct varsymset *copy) 43698a7f915SMatthew Dillon { 43798a7f915SMatthew Dillon struct varsyment *ve; 43898a7f915SMatthew Dillon 43998a7f915SMatthew Dillon TAILQ_INIT(&vss->vx_queue); 44098a7f915SMatthew Dillon if (copy) { 44198a7f915SMatthew Dillon TAILQ_FOREACH(ve, ©->vx_queue, ve_entry) { 44298a7f915SMatthew Dillon varsymdup(vss, ve); 44398a7f915SMatthew Dillon } 44498a7f915SMatthew Dillon vss->vx_setsize = copy->vx_setsize; 44598a7f915SMatthew Dillon } 44698a7f915SMatthew Dillon } 44798a7f915SMatthew Dillon 44898a7f915SMatthew Dillon void 44998a7f915SMatthew Dillon varsymset_clean(struct varsymset *vss) 45098a7f915SMatthew Dillon { 45198a7f915SMatthew Dillon struct varsyment *ve; 45298a7f915SMatthew Dillon 45398a7f915SMatthew Dillon while ((ve = TAILQ_FIRST(&vss->vx_queue)) != NULL) { 45498a7f915SMatthew Dillon TAILQ_REMOVE(&vss->vx_queue, ve, ve_entry); 45598a7f915SMatthew Dillon varsymdrop(ve->ve_sym); 45698a7f915SMatthew Dillon free(ve, M_VARSYM); 45798a7f915SMatthew Dillon } 45898a7f915SMatthew Dillon vss->vx_setsize = 0; 45998a7f915SMatthew Dillon } 46098a7f915SMatthew Dillon 461