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
34 * $DragonFly: src/sys/kern/kern_varsym.c,v 1.9 2007/04/30 07:18:54 dillon Exp $
38 * This module implements variable storage and management for variant
39 * symlinks. These variables may also be used for general purposes.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/ucred.h>
46 #include <sys/resourcevar.h>
50 #include <sys/queue.h>
51 #include <sys/sysctl.h>
52 #include <sys/malloc.h>
53 #include <sys/varsym.h>
54 #include <sys/sysproto.h>
56 MALLOC_DEFINE(M_VARSYM
, "varsym", "variable sets for variant symlinks");
58 struct varsymset varsymset_sys
;
61 * Initialize the variant symlink subsystem
64 varsym_sysinit(void *dummy
)
66 varsymset_init(&varsymset_sys
, NULL
);
68 SYSINIT(announce
, SI_BOOT2_MACHDEP
, SI_ORDER_FIRST
, varsym_sysinit
, NULL
);
71 * varsymreplace() - called from namei
73 * Do variant symlink variable substitution
76 varsymreplace(char *cp
, int linklen
, int maxlen
)
86 if (cp
[0] == '$' && cp
[1] == '{') {
87 for (i
= 2; i
< linklen
; ++i
) {
92 (var
= varsymfind(VARSYM_ALL_MASK
, cp
+ 2, i
- 2)) != NULL
94 xlen
= i
+ 1; /* bytes to strike */
95 nlen
= strlen(var
->vs_data
); /* bytes to add */
96 if (linklen
+ nlen
- xlen
>= maxlen
) {
100 KKASSERT(linklen
>= xlen
);
102 bcopy(cp
+ xlen
, cp
+ nlen
, linklen
- xlen
);
103 bcopy(var
->vs_data
, cp
, nlen
);
104 linklen
+= nlen
- xlen
; /* new relative length */
105 rlen
+= nlen
- xlen
; /* returned total length */
106 cp
+= nlen
; /* adjust past replacement */
107 linklen
-= nlen
; /* adjust past replacement */
108 maxlen
-= nlen
; /* adjust past replacement */
111 * It's ok if i points to the '}', it will simply be
112 * skipped. i could also have hit linklen.
128 * varsym_set() system call
130 * (int level, const char *name, const char *data)
133 sys_varsym_set(struct varsym_set_args
*uap
)
135 char name
[MAXVARSYM_NAME
];
139 if ((error
= copyinstr(uap
->name
, name
, sizeof(name
), NULL
)) != 0)
141 buf
= kmalloc(MAXVARSYM_DATA
, M_TEMP
, M_WAITOK
);
143 (error
= copyinstr(uap
->data
, buf
, MAXVARSYM_DATA
, NULL
)) != 0)
149 if (curthread
->td_proc
!= NULL
&& curthread
->td_proc
->p_ucred
->cr_prison
!= NULL
)
150 uap
->level
= VARSYM_PRISON
;
152 if (curthread
->td_proc
!= NULL
&&
153 (error
= priv_check_cred(curthread
->td_proc
->p_ucred
, PRIV_ROOT
, PRISON_ROOT
)) != 0)
157 /* XXX check jail / implement per-jail user */
161 (void)varsymmake(uap
->level
, name
, NULL
);
162 error
= varsymmake(uap
->level
, name
, buf
);
164 error
= varsymmake(uap
->level
, name
, NULL
);
175 * varsym_get() system call
177 * (int mask, const char *wild, char *buf, int bufsize)
180 sys_varsym_get(struct varsym_get_args
*uap
)
182 char wild
[MAXVARSYM_NAME
];
187 if ((error
= copyinstr(uap
->wild
, wild
, sizeof(wild
), NULL
)) != 0)
189 sym
= varsymfind(uap
->mask
, wild
, strlen(wild
));
194 dlen
= strlen(sym
->vs_data
);
195 if (dlen
< uap
->bufsize
) {
196 copyout(sym
->vs_data
, uap
->buf
, dlen
+ 1);
197 } else if (uap
->bufsize
) {
198 copyout("", uap
->buf
, 1);
200 uap
->sysmsg_result
= dlen
+ 1;
207 * varsym_list() system call
209 * (int level, char *buf, int maxsize, int *marker)
212 sys_varsym_list(struct varsym_list_args
*uap
)
214 struct varsymset
*vss
;
215 struct varsyment
*ve
;
224 * Get the marker from userspace.
226 if ((error
= copyin(uap
->marker
, &marker
, sizeof(marker
))) != 0)
230 * Figure out the varsym set.
235 switch (uap
->level
) {
238 vss
= &p
->p_varsymset
;
242 vss
= &p
->p_ucred
->cr_uidinfo
->ui_varsymset
;
245 vss
= &varsymset_sys
;
248 if (p
!= NULL
&& p
->p_ucred
->cr_prison
!= NULL
)
249 vss
= &p
->p_ucred
->cr_prison
->pr_varsymset
;
258 * Loop through the variables and dump them to uap->buf
264 TAILQ_FOREACH(ve
, &vss
->vx_queue
, ve_entry
) {
265 varsym_t sym
= ve
->ve_sym
;
266 int namelen
= strlen(sym
->vs_name
);
267 int datalen
= strlen(sym
->vs_data
);
268 int totlen
= namelen
+ datalen
+ 2;
271 * Skip to our index point
279 * Stop if there is insufficient space in the user buffer.
280 * If we haven't stored anything yet return EOVERFLOW.
281 * Note that the marker index (i) does not change.
283 if (bytes
+ totlen
> uap
->maxsize
) {
290 error
= copyout(sym
->vs_name
, uap
->buf
+ bytes
, namelen
+ 1);
292 bytes
+= namelen
+ 1;
293 error
= copyout(sym
->vs_data
, uap
->buf
+ bytes
, datalen
+ 1);
295 bytes
+= datalen
+ 1;
297 bytes
-= namelen
+ 1; /* revert if error */
307 * Save the marker back. If no error occured and earlyterm is clear
308 * the marker is set to -1 indicating that the variable list has been
309 * exhausted. If no error occured the number of bytes loaded into
310 * the buffer will be returned, otherwise the syscall code returns -1.
312 if (error
== 0 && earlyterm
== 0)
317 error
= copyout(&marker
, uap
->marker
, sizeof(marker
));
318 uap
->sysmsg_result
= bytes
;
324 * Lookup a variant symlink. XXX use a hash table.
328 varsymlookup(struct varsymset
*vss
, const char *name
, int namelen
)
330 struct varsyment
*ve
;
332 TAILQ_FOREACH(ve
, &vss
->vx_queue
, ve_entry
) {
333 varsym_t var
= ve
->ve_sym
;
334 if (var
->vs_namelen
== namelen
&&
335 bcmp(name
, var
->vs_name
, namelen
) == 0
344 varsymfind(int mask
, const char *name
, int namelen
)
346 struct proc
*p
= curproc
;
347 struct varsyment
*ve
= NULL
;
350 if ((mask
& (VARSYM_PROC_MASK
|VARSYM_USER_MASK
)) && p
!= NULL
) {
351 if (mask
& VARSYM_PROC_MASK
)
352 ve
= varsymlookup(&p
->p_varsymset
, name
, namelen
);
353 if (ve
== NULL
&& (mask
& VARSYM_USER_MASK
))
354 ve
= varsymlookup(&p
->p_ucred
->cr_uidinfo
->ui_varsymset
, name
, namelen
);
356 if (ve
== NULL
&& (mask
& VARSYM_SYS_MASK
)) {
357 if (p
!= NULL
&& p
->p_ucred
->cr_prison
)
358 ve
= varsymlookup(&p
->p_ucred
->cr_prison
->pr_varsymset
, name
, namelen
);
360 ve
= varsymlookup(&varsymset_sys
, name
, namelen
);
372 varsymmake(int level
, const char *name
, const char *data
)
374 struct varsymset
*vss
= NULL
;
375 struct varsyment
*ve
;
376 struct proc
*p
= curproc
;
378 int namelen
= strlen(name
);
385 vss
= &p
->p_varsymset
;
389 vss
= &p
->p_ucred
->cr_uidinfo
->ui_varsymset
;
392 vss
= &varsymset_sys
;
395 if (p
!= NULL
&& p
->p_ucred
->cr_prison
!= NULL
)
396 vss
= &p
->p_ucred
->cr_prison
->pr_varsymset
;
401 } else if (data
&& vss
->vx_setsize
>= MAXVARSYM_SET
) {
404 datalen
= strlen(data
);
405 ve
= kmalloc(sizeof(struct varsyment
), M_VARSYM
, M_WAITOK
|M_ZERO
);
406 sym
= kmalloc(sizeof(struct varsym
) + namelen
+ datalen
+ 2, M_VARSYM
, M_WAITOK
);
409 sym
->vs_namelen
= namelen
;
410 sym
->vs_name
= (char *)(sym
+ 1);
411 sym
->vs_data
= sym
->vs_name
+ namelen
+ 1;
412 strcpy(sym
->vs_name
, name
);
413 strcpy(sym
->vs_data
, data
);
414 TAILQ_INSERT_TAIL(&vss
->vx_queue
, ve
, ve_entry
);
415 vss
->vx_setsize
+= sizeof(struct varsyment
) + sizeof(struct varsym
) + namelen
+ datalen
+ 8;
418 if ((ve
= varsymlookup(vss
, name
, namelen
)) != NULL
) {
419 TAILQ_REMOVE(&vss
->vx_queue
, ve
, ve_entry
);
420 vss
->vx_setsize
-= sizeof(struct varsyment
) + sizeof(struct varsym
) + namelen
+ strlen(ve
->ve_sym
->vs_data
) + 8;
421 varsymdrop(ve
->ve_sym
);
432 varsymdrop(varsym_t sym
)
434 KKASSERT(sym
->vs_refs
> 0);
435 if (--sym
->vs_refs
== 0) {
436 kfree(sym
, M_VARSYM
);
441 varsymdup(struct varsymset
*vss
, struct varsyment
*ve
)
443 struct varsyment
*nve
;
445 nve
= kmalloc(sizeof(struct varsyment
), M_VARSYM
, M_WAITOK
|M_ZERO
);
446 nve
->ve_sym
= ve
->ve_sym
;
447 ++nve
->ve_sym
->vs_refs
;
448 TAILQ_INSERT_TAIL(&vss
->vx_queue
, nve
, ve_entry
);
452 varsymset_init(struct varsymset
*vss
, struct varsymset
*copy
)
454 struct varsyment
*ve
;
456 TAILQ_INIT(&vss
->vx_queue
);
458 TAILQ_FOREACH(ve
, ©
->vx_queue
, ve_entry
) {
461 vss
->vx_setsize
= copy
->vx_setsize
;
466 varsymset_clean(struct varsymset
*vss
)
468 struct varsyment
*ve
;
470 while ((ve
= TAILQ_FIRST(&vss
->vx_queue
)) != NULL
) {
471 TAILQ_REMOVE(&vss
->vx_queue
, ve
, ve_entry
);
472 varsymdrop(ve
->ve_sym
);