2 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $DragonFly: src/sys/kern/kern_varsym.c,v 1.4 2004/01/20 05:04:06 dillon Exp $
30 * This module implements variable storage and management for variant
31 * symlinks. These variables may also be used for general purposes.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/ucred.h>
38 #include <sys/resourcevar.h>
40 #include <sys/queue.h>
41 #include <sys/sysctl.h>
42 #include <sys/malloc.h>
43 #include <sys/varsym.h>
44 #include <sys/sysproto.h>
46 MALLOC_DEFINE(M_VARSYM, "varsym", "variable sets for variant symlinks");
48 struct varsymset varsymset_sys;
51 * Initialize the variant symlink subsystem
54 varsym_sysinit(void *dummy)
56 varsymset_init(&varsymset_sys, NULL);
58 SYSINIT(announce, SI_SUB_INTRINSIC, SI_ORDER_FIRST, varsym_sysinit, NULL);
61 * varsymreplace() - called from namei
63 * Do variant symlink variable substitution
66 varsymreplace(char *cp, int linklen, int maxlen)
76 if (cp[0] == '$' && cp[1] == '{') {
77 for (i = 2; i < linklen; ++i) {
82 (var = varsymfind(VARSYM_ALL_MASK, cp + 2, i - 2)) != NULL
84 xlen = i + 1; /* bytes to strike */
85 nlen = strlen(var->vs_data); /* bytes to add */
86 if (linklen + nlen - xlen >= maxlen) {
90 KKASSERT(linklen >= xlen);
92 bcopy(cp + xlen, cp + nlen, linklen - xlen);
93 bcopy(var->vs_data, cp, nlen);
94 linklen += nlen - xlen; /* new relative length */
95 rlen += nlen - xlen; /* returned total length */
96 cp += nlen; /* adjust past replacement */
97 linklen -= nlen; /* adjust past replacement */
98 maxlen -= nlen; /* adjust past replacement */
101 * It's ok if i points to the '}', it will simply be
102 * skipped. i could also have hit linklen.
118 * varsym_set() system call
120 * (int level, const char *name, const char *data)
123 varsym_set(struct varsym_set_args *uap)
125 char name[MAXVARSYM_NAME];
129 if ((error = copyinstr(uap->name, name, sizeof(name), NULL)) != 0)
131 buf = malloc(MAXVARSYM_DATA, M_TEMP, M_WAITOK);
133 (error = copyinstr(uap->data, buf, MAXVARSYM_DATA, NULL)) != 0)
139 if ((error = suser(curthread)) != 0)
141 /* XXX implement per-jail sys */
144 /* XXX check jail / implement per-jail user */
148 (void)varsymmake(uap->level, name, NULL);
149 error = varsymmake(uap->level, name, buf);
151 error = varsymmake(uap->level, name, NULL);
162 * varsym_get() system call
164 * (int mask, const char *wild, char *buf, int bufsize)
167 varsym_get(struct varsym_get_args *uap)
169 char wild[MAXVARSYM_NAME];
174 if ((error = copyinstr(uap->wild, wild, sizeof(wild), NULL)) != 0)
176 sym = varsymfind(uap->mask, wild, strlen(wild));
181 dlen = strlen(sym->vs_data);
182 if (dlen < uap->bufsize) {
183 copyout(sym->vs_data, uap->buf, dlen + 1);
184 } else if (uap->bufsize) {
185 copyout("", uap->buf, 1);
187 uap->sysmsg_result = dlen + 1;
194 * varsym_list() system call
196 * (int level, char *buf, int maxsize, int *marker)
199 varsym_list(struct varsym_list_args *uap)
201 struct varsymset *vss;
202 struct varsyment *ve;
211 * Get the marker from userspace.
213 if ((error = copyin(uap->marker, &marker, sizeof(marker))) != 0)
217 * Figure out the varsym set.
222 switch (uap->level) {
225 vss = &p->p_varsymset;
229 vss = &p->p_ucred->cr_uidinfo->ui_varsymset;
232 vss = &varsymset_sys;
241 * Loop through the variables and dump them to uap->buf
247 TAILQ_FOREACH(ve, &vss->vx_queue, ve_entry) {
248 varsym_t sym = ve->ve_sym;
249 int namelen = strlen(sym->vs_name);
250 int datalen = strlen(sym->vs_data);
251 int totlen = namelen + datalen + 2;
254 * Skip to our index point
262 * Stop if there is insufficient space in the user buffer.
263 * If we haven't stored anything yet return EOVERFLOW.
264 * Note that the marker index (i) does not change.
266 if (bytes + totlen > uap->maxsize) {
273 error = copyout(sym->vs_name, uap->buf + bytes, namelen + 1);
275 bytes += namelen + 1;
276 error = copyout(sym->vs_data, uap->buf + bytes, datalen + 1);
278 bytes += datalen + 1;
280 bytes -= namelen + 1; /* revert if error */
290 * Save the marker back. If no error occured and earlyterm is clear
291 * the marker is set to -1 indicating that the variable list has been
292 * exhausted. If no error occured the number of bytes loaded into
293 * the buffer will be returned, otherwise the syscall code returns -1.
295 if (error == 0 && earlyterm == 0)
300 error = copyout(&marker, uap->marker, sizeof(marker));
301 uap->sysmsg_result = bytes;
307 * Lookup a variant symlink. XXX use a hash table.
311 varsymlookup(struct varsymset *vss, const char *name, int namelen)
313 struct varsyment *ve;
315 TAILQ_FOREACH(ve, &vss->vx_queue, ve_entry) {
316 varsym_t var = ve->ve_sym;
317 if (var->vs_namelen == namelen &&
318 bcmp(name, var->vs_name, namelen) == 0
327 varsymfind(int mask, const char *name, int namelen)
330 struct varsyment *ve = NULL;
333 if ((mask & (VARSYM_PROC_MASK|VARSYM_USER_MASK)) && (p = curproc) != NULL) {
334 if (mask & VARSYM_PROC_MASK)
335 ve = varsymlookup(&p->p_varsymset, name, namelen);
336 if (ve == NULL && (mask & VARSYM_USER_MASK))
337 ve = varsymlookup(&p->p_ucred->cr_uidinfo->ui_varsymset, name, namelen);
339 if (ve == NULL && (mask & VARSYM_SYS_MASK))
340 ve = varsymlookup(&varsymset_sys, name, namelen);
351 varsymmake(int level, const char *name, const char *data)
353 struct varsymset *vss = NULL;
354 struct varsyment *ve;
355 struct proc *p = curproc;
357 int namelen = strlen(name);
364 vss = &p->p_varsymset;
368 vss = &p->p_ucred->cr_uidinfo->ui_varsymset;
371 vss = &varsymset_sys;
376 } else if (data && vss->vx_setsize >= MAXVARSYM_SET) {
379 datalen = strlen(data);
380 ve = malloc(sizeof(struct varsyment), M_VARSYM, M_WAITOK|M_ZERO);
381 sym = malloc(sizeof(struct varsym) + namelen + datalen + 2, M_VARSYM, M_WAITOK);
384 sym->vs_namelen = namelen;
385 sym->vs_name = (char *)(sym + 1);
386 sym->vs_data = sym->vs_name + namelen + 1;
387 strcpy(sym->vs_name, name);
388 strcpy(sym->vs_data, data);
389 TAILQ_INSERT_TAIL(&vss->vx_queue, ve, ve_entry);
390 vss->vx_setsize += sizeof(struct varsyment) + sizeof(struct varsym) + namelen + datalen + 8;
393 if ((ve = varsymlookup(vss, name, namelen)) != NULL) {
394 TAILQ_REMOVE(&vss->vx_queue, ve, ve_entry);
395 vss->vx_setsize -= sizeof(struct varsyment) + sizeof(struct varsym) + namelen + strlen(ve->ve_sym->vs_data) + 8;
396 varsymdrop(ve->ve_sym);
407 varsymdrop(varsym_t sym)
409 KKASSERT(sym->vs_refs > 0);
410 if (--sym->vs_refs == 0) {
416 varsymdup(struct varsymset *vss, struct varsyment *ve)
418 struct varsyment *nve;
420 nve = malloc(sizeof(struct varsyment), M_VARSYM, M_WAITOK|M_ZERO);
421 nve->ve_sym = ve->ve_sym;
422 ++nve->ve_sym->vs_refs;
423 TAILQ_INSERT_TAIL(&vss->vx_queue, nve, ve_entry);
427 varsymset_init(struct varsymset *vss, struct varsymset *copy)
429 struct varsyment *ve;
431 TAILQ_INIT(&vss->vx_queue);
433 TAILQ_FOREACH(ve, ©->vx_queue, ve_entry) {
436 vss->vx_setsize = copy->vx_setsize;
441 varsymset_clean(struct varsymset *vss)
443 struct varsyment *ve;
445 while ((ve = TAILQ_FIRST(&vss->vx_queue)) != NULL) {
446 TAILQ_REMOVE(&vss->vx_queue, ve, ve_entry);
447 varsymdrop(ve->ve_sym);