2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * 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
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This module implements variable storage and management for variant
37 * symlinks. These variables may also be used for general purposes.
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/ucred.h>
44 #include <sys/resourcevar.h>
48 #include <sys/queue.h>
49 #include <sys/sysctl.h>
50 #include <sys/malloc.h>
51 #include <sys/varsym.h>
52 #include <sys/sysmsg.h>
54 MALLOC_DEFINE(M_VARSYM, "varsym", "variable sets for variant symlinks");
56 struct varsymset varsymset_sys;
59 * Initialize the variant symlink subsystem
62 varsym_sysinit(void *dummy)
64 varsymset_init(&varsymset_sys, NULL);
66 SYSINIT(announce, SI_BOOT2_MACHDEP, SI_ORDER_FIRST, varsym_sysinit, NULL);
69 * varsymreplace() - called from namei
71 * Do variant symlink variable substitution
74 varsymreplace(char *cp, int linklen, int maxlen)
84 if (cp[0] == '$' && cp[1] == '{') {
85 for (i = 2; i < linklen; ++i) {
90 (var = varsymfind(VARSYM_ALL_MASK, cp + 2, i - 2)) != NULL
92 xlen = i + 1; /* bytes to strike */
93 nlen = strlen(var->vs_data); /* bytes to add */
94 if (linklen + nlen - xlen >= maxlen) {
98 KKASSERT(linklen >= xlen);
100 bcopy(cp + xlen, cp + nlen, linklen - xlen);
101 bcopy(var->vs_data, cp, nlen);
102 linklen += nlen - xlen; /* new relative length */
103 rlen += nlen - xlen; /* returned total length */
104 cp += nlen; /* adjust past replacement */
105 linklen -= nlen; /* adjust past replacement */
106 maxlen -= nlen; /* adjust past replacement */
109 * It's ok if i points to the '}', it will simply be
110 * skipped. i could also have hit linklen.
126 * varsym_set() system call
128 * (int level, const char *name, const char *data)
131 sys_varsym_set(struct sysmsg *sysmsg, const struct varsym_set_args *uap)
133 char name[MAXVARSYM_NAME];
138 int level = uap->level;
143 if ((error = copyinstr(uap->name, name, sizeof(name), NULL)) != 0)
145 buf = kmalloc(MAXVARSYM_DATA, M_TEMP, M_WAITOK);
147 (error = copyinstr(uap->data, buf, MAXVARSYM_DATA, NULL)) != 0)
154 if (lp != NULL && td->td_ucred->cr_prison != NULL)
155 level = VARSYM_PRISON;
159 (error = priv_check_cred(td->td_ucred, PRIV_VARSYM_SYS, 0)) != 0)
163 /* XXX check jail / implement per-jail user */
167 (void)varsymmake(level, name, NULL);
168 error = varsymmake(level, name, buf);
170 error = varsymmake(level, name, NULL);
181 * varsym_get() system call
183 * (int mask, const char *wild, char *buf, int bufsize)
188 sys_varsym_get(struct sysmsg *sysmsg, const struct varsym_get_args *uap)
190 char wild[MAXVARSYM_NAME];
195 if ((error = copyinstr(uap->wild, wild, sizeof(wild), NULL)) != 0)
197 sym = varsymfind(uap->mask, wild, strlen(wild));
202 dlen = strlen(sym->vs_data);
203 if (dlen < uap->bufsize) {
204 copyout(sym->vs_data, uap->buf, dlen + 1);
205 } else if (uap->bufsize) {
206 copyout("", uap->buf, 1);
208 sysmsg->sysmsg_result = dlen + 1;
216 * varsym_list() system call
218 * (int level, char *buf, int maxsize, int *marker)
223 sys_varsym_list(struct sysmsg *sysmsg, const struct varsym_list_args *uap)
225 struct varsymset *vss;
226 struct varsyment *ve;
237 * Get the marker from userspace.
239 if ((error = copyin(uap->marker, &marker, sizeof(marker))) != 0)
243 * Figure out the varsym set.
247 p = lp ? lp->lwp_proc : NULL;
251 switch (uap->level) {
254 vss = &p->p_varsymset;
258 vss = &td->td_ucred->cr_uidinfo->ui_varsymset;
261 vss = &varsymset_sys;
264 if (lp && td->td_ucred->cr_prison)
265 vss = &td->td_ucred->cr_prison->pr_varsymset;
274 * Loop through the variables and dump them to uap->buf
280 lockmgr(&vss->vx_lock, LK_SHARED);
281 TAILQ_FOREACH(ve, &vss->vx_queue, ve_entry) {
282 varsym_t sym = ve->ve_sym;
283 int namelen = strlen(sym->vs_name);
284 int datalen = strlen(sym->vs_data);
285 int totlen = namelen + datalen + 2;
288 * Skip to our index point
296 * Stop if there is insufficient space in the user buffer.
297 * If we haven't stored anything yet return EOVERFLOW.
298 * Note that the marker index (i) does not change.
300 if (bytes + totlen > uap->maxsize) {
307 error = copyout(sym->vs_name, uap->buf + bytes, namelen + 1);
309 bytes += namelen + 1;
310 error = copyout(sym->vs_data, uap->buf + bytes, datalen + 1);
312 bytes += datalen + 1;
314 bytes -= namelen + 1; /* revert if error */
322 lockmgr(&vss->vx_lock, LK_RELEASE);
325 * Save the marker back. If no error occured and earlyterm is clear
326 * the marker is set to -1 indicating that the variable list has been
327 * exhausted. If no error occured the number of bytes loaded into
328 * the buffer will be returned, otherwise the syscall code returns -1.
330 if (error == 0 && earlyterm == 0)
335 error = copyout(&marker, uap->marker, sizeof(marker));
336 sysmsg->sysmsg_result = bytes;
342 * Lookup a variant symlink. XXX use a hash table.
346 varsymlookup(struct varsymset *vss, const char *name, int namelen)
348 struct varsyment *ve;
350 KKASSERT(lockstatus(&vss->vx_lock, curthread) != 0);
351 TAILQ_FOREACH(ve, &vss->vx_queue, ve_entry) {
352 varsym_t var = ve->ve_sym;
353 if (var->vs_namelen == namelen &&
354 bcmp(name, var->vs_name, namelen) == 0
364 vsslock(struct varsymset **vss, struct varsymset *n)
367 lockmgr(&(*vss)->vx_lock, LK_RELEASE);
369 lockmgr(&n->vx_lock, LK_SHARED);
374 varsymfind(int mask, const char *name, int namelen)
376 struct varsyment *ve = NULL;
377 struct varsymset *vss = NULL;
385 p = lp ? lp->lwp_proc : NULL;
387 if ((mask & (VARSYM_PROC_MASK|VARSYM_USER_MASK)) && lp != NULL) {
388 if (mask & VARSYM_PROC_MASK) {
389 vsslock(&vss, &p->p_varsymset);
390 ve = varsymlookup(vss, name, namelen);
392 if (ve == NULL && (mask & VARSYM_USER_MASK)) {
393 vsslock(&vss, &td->td_ucred->cr_uidinfo->ui_varsymset);
394 ve = varsymlookup(vss, name, namelen);
397 if (ve == NULL && (mask & VARSYM_SYS_MASK)) {
398 if (lp != NULL && td->td_ucred->cr_prison) {
399 vsslock(&vss, &td->td_ucred->cr_prison->pr_varsymset);
400 ve = varsymlookup(vss, name, namelen);
402 vsslock(&vss, &varsymset_sys);
403 ve = varsymlookup(vss, name, namelen);
408 atomic_add_int(&sym->vs_refs, 1);
413 lockmgr(&vss->vx_lock, LK_RELEASE);
418 varsymmake(int level, const char *name, const char *data)
420 struct varsymset *vss = NULL;
421 struct varsyment *ve;
426 int namelen = strlen(name);
432 p = lp ? lp->lwp_proc : NULL;
437 vss = &p->p_varsymset;
441 vss = &td->td_ucred->cr_uidinfo->ui_varsymset;
444 vss = &varsymset_sys;
447 if (lp && td->td_ucred->cr_prison)
448 vss = &td->td_ucred->cr_prison->pr_varsymset;
454 lockmgr(&vss->vx_lock, LK_EXCLUSIVE);
455 if (data && vss->vx_setsize >= MAXVARSYM_SET) {
458 datalen = strlen(data);
459 ve = kmalloc(sizeof(struct varsyment), M_VARSYM, M_WAITOK|M_ZERO);
460 sym = kmalloc(sizeof(struct varsym) + namelen + datalen + 2, M_VARSYM, M_WAITOK);
463 sym->vs_namelen = namelen;
464 sym->vs_name = (char *)(sym + 1);
465 sym->vs_data = sym->vs_name + namelen + 1;
466 strcpy(sym->vs_name, name);
467 strcpy(sym->vs_data, data);
468 TAILQ_INSERT_TAIL(&vss->vx_queue, ve, ve_entry);
469 vss->vx_setsize += sizeof(struct varsyment) + sizeof(struct varsym) + namelen + datalen + 8;
472 if ((ve = varsymlookup(vss, name, namelen)) != NULL) {
473 TAILQ_REMOVE(&vss->vx_queue, ve, ve_entry);
474 vss->vx_setsize -= sizeof(struct varsyment) + sizeof(struct varsym) + namelen + strlen(ve->ve_sym->vs_data) + 8;
475 varsymdrop(ve->ve_sym);
482 lockmgr(&vss->vx_lock, LK_RELEASE);
487 varsymdrop(varsym_t sym)
489 KKASSERT(sym->vs_refs > 0);
490 if (atomic_fetchadd_int(&sym->vs_refs, -1) == 1) {
491 kfree(sym, M_VARSYM);
496 * Insert a duplicate of ve in vss. Does not do any locking,
497 * so it is the callers responsibility to make sure nobody
498 * else can mess with the TAILQ in vss at the same time.
501 varsymdup(struct varsymset *vss, struct varsyment *ve)
503 struct varsyment *nve;
505 nve = kmalloc(sizeof(struct varsyment), M_VARSYM, M_WAITOK|M_ZERO);
506 nve->ve_sym = ve->ve_sym;
507 ++nve->ve_sym->vs_refs; /* can't be reached, no need for atomic add */
509 * We're only called through varsymset_init() so vss is not yet reachable,
512 TAILQ_INSERT_TAIL(&vss->vx_queue, nve, ve_entry);
516 varsymset_init(struct varsymset *vss, struct varsymset *copy)
518 struct varsyment *ve;
520 TAILQ_INIT(&vss->vx_queue);
521 lockinit(&vss->vx_lock, "vx", 0, 0);
523 TAILQ_FOREACH(ve, ©->vx_queue, ve_entry) {
526 vss->vx_setsize = copy->vx_setsize;
531 varsymset_clean(struct varsymset *vss)
533 struct varsyment *ve;
535 lockmgr(&vss->vx_lock, LK_EXCLUSIVE);
536 while ((ve = TAILQ_FIRST(&vss->vx_queue)) != NULL) {
537 TAILQ_REMOVE(&vss->vx_queue, ve, ve_entry);
538 varsymdrop(ve->ve_sym);
542 lockmgr(&vss->vx_lock, LK_RELEASE);