kernel - use new td_ucred in numerous places
[dragonfly.git] / sys / kern / kern_varsym.c
CommitLineData
98a7f915 1/*
8c10bfcf
MD
2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
98a7f915
MD
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
8c10bfcf 10 *
98a7f915
MD
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
8c10bfcf
MD
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
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.
20 *
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
98a7f915 32 * SUCH DAMAGE.
8c10bfcf 33 *
ba39e2e0 34 * $DragonFly: src/sys/kern/kern_varsym.c,v 1.9 2007/04/30 07:18:54 dillon Exp $
98a7f915
MD
35 */
36
37/*
38 * This module implements variable storage and management for variant
39 * symlinks. These variables may also be used for general purposes.
40 */
41
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>
47#include <sys/proc.h>
895c1f85 48#include <sys/priv.h>
dbc282df 49#include <sys/jail.h>
98a7f915
MD
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>
55
56MALLOC_DEFINE(M_VARSYM, "varsym", "variable sets for variant symlinks");
57
58struct varsymset varsymset_sys;
59
60/*
61 * Initialize the variant symlink subsystem
62 */
63static void
64varsym_sysinit(void *dummy)
65{
66 varsymset_init(&varsymset_sys, NULL);
67}
ba39e2e0 68SYSINIT(announce, SI_BOOT2_MACHDEP, SI_ORDER_FIRST, varsym_sysinit, NULL);
98a7f915
MD
69
70/*
063147e5
MD
71 * varsymreplace() - called from namei
72 *
73 * Do variant symlink variable substitution
74 */
75int
76varsymreplace(char *cp, int linklen, int maxlen)
77{
78 int rlen;
79 int xlen;
80 int nlen;
81 int i;
82 varsym_t var;
83
84 rlen = linklen;
85 while (linklen > 1) {
86 if (cp[0] == '$' && cp[1] == '{') {
87 for (i = 2; i < linklen; ++i) {
88 if (cp[i] == '}')
89 break;
90 }
91 if (i < linklen &&
92 (var = varsymfind(VARSYM_ALL_MASK, cp + 2, i - 2)) != NULL
93 ) {
94 xlen = i + 1; /* bytes to strike */
95 nlen = strlen(var->vs_data); /* bytes to add */
96 if (linklen + nlen - xlen >= maxlen) {
97 varsymdrop(var);
98 return(-1);
99 }
100 KKASSERT(linklen >= xlen);
101 if (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 */
109 } else {
110 /*
111 * It's ok if i points to the '}', it will simply be
112 * skipped. i could also have hit linklen.
113 */
114 cp += i;
115 linklen -= i;
116 maxlen -= i;
117 }
118 } else {
119 ++cp;
120 --linklen;
121 --maxlen;
122 }
123 }
124 return(rlen);
125}
126
127/*
98a7f915
MD
128 * varsym_set() system call
129 *
130 * (int level, const char *name, const char *data)
3919ced0
MD
131 *
132 * MPALMOSTSAFE
98a7f915
MD
133 */
134int
753fd850 135sys_varsym_set(struct varsym_set_args *uap)
98a7f915
MD
136{
137 char name[MAXVARSYM_NAME];
138 char *buf;
9910d07b 139 struct thread *td;
e61b3df7 140 struct proc *p;
9910d07b 141 struct lwp *lp;
98a7f915
MD
142 int error;
143
9910d07b
MD
144 td = curthread;
145 lp = td->td_lwp;
146 p = lp ? lp->lwp_proc : NULL;
e61b3df7 147
98a7f915
MD
148 if ((error = copyinstr(uap->name, name, sizeof(name), NULL)) != 0)
149 goto done2;
efda3bd0 150 buf = kmalloc(MAXVARSYM_DATA, M_TEMP, M_WAITOK);
98a7f915
MD
151 if (uap->data &&
152 (error = copyinstr(uap->data, buf, MAXVARSYM_DATA, NULL)) != 0)
153 {
154 goto done1;
155 }
3919ced0
MD
156
157 get_mplock();
158
98a7f915
MD
159 switch(uap->level) {
160 case VARSYM_SYS:
9910d07b 161 if (lp != NULL && td->td_ucred->cr_prison != NULL)
dbc282df
JS
162 uap->level = VARSYM_PRISON;
163 case VARSYM_PRISON:
9910d07b
MD
164 if (lp != NULL &&
165 (error = priv_check_cred(td->td_ucred, PRIV_VARSYM_SYS, 0)) != 0)
98a7f915 166 break;
98a7f915
MD
167 /* fall through */
168 case VARSYM_USER:
169 /* XXX check jail / implement per-jail user */
170 /* fall through */
171 case VARSYM_PROC:
172 if (uap->data) {
173 (void)varsymmake(uap->level, name, NULL);
174 error = varsymmake(uap->level, name, buf);
175 } else {
176 error = varsymmake(uap->level, name, NULL);
177 }
178 break;
179 }
3919ced0 180 rel_mplock();
98a7f915 181done1:
efda3bd0 182 kfree(buf, M_TEMP);
98a7f915
MD
183done2:
184 return(error);
185}
186
187/*
188 * varsym_get() system call
189 *
190 * (int mask, const char *wild, char *buf, int bufsize)
3919ced0
MD
191 *
192 * MPALMOSTSAFE
98a7f915
MD
193 */
194int
753fd850 195sys_varsym_get(struct varsym_get_args *uap)
98a7f915
MD
196{
197 char wild[MAXVARSYM_NAME];
198 varsym_t sym;
199 int error;
200 int dlen;
201
3919ced0 202 get_mplock();
98a7f915
MD
203 if ((error = copyinstr(uap->wild, wild, sizeof(wild), NULL)) != 0)
204 goto done;
205 sym = varsymfind(uap->mask, wild, strlen(wild));
206 if (sym == NULL) {
207 error = ENOENT;
208 goto done;
209 }
210 dlen = strlen(sym->vs_data);
211 if (dlen < uap->bufsize) {
212 copyout(sym->vs_data, uap->buf, dlen + 1);
213 } else if (uap->bufsize) {
214 copyout("", uap->buf, 1);
215 }
216 uap->sysmsg_result = dlen + 1;
217 varsymdrop(sym);
218done:
3919ced0 219 rel_mplock();
98a7f915
MD
220 return(error);
221}
222
223/*
0a004aa2
MD
224 * varsym_list() system call
225 *
226 * (int level, char *buf, int maxsize, int *marker)
3919ced0
MD
227 *
228 * MPALMOSTSAFE
0a004aa2
MD
229 */
230int
753fd850 231sys_varsym_list(struct varsym_list_args *uap)
0a004aa2
MD
232{
233 struct varsymset *vss;
234 struct varsyment *ve;
9910d07b 235 struct thread *td;
0a004aa2 236 struct proc *p;
9910d07b 237 struct lwp *lp;
0a004aa2
MD
238 int i;
239 int error;
240 int bytes;
241 int earlyterm;
242 int marker;
243
244 /*
245 * Get the marker from userspace.
246 */
3919ced0 247 get_mplock();
0a004aa2
MD
248 if ((error = copyin(uap->marker, &marker, sizeof(marker))) != 0)
249 goto done;
250
251 /*
252 * Figure out the varsym set.
253 */
9910d07b
MD
254 td = curthread;
255 lp = td->td_lwp;
256 p = lp ? lp->lwp_proc : NULL;
257
0a004aa2
MD
258 vss = NULL;
259
260 switch (uap->level) {
261 case VARSYM_PROC:
262 if (p)
263 vss = &p->p_varsymset;
264 break;
265 case VARSYM_USER:
9910d07b
MD
266 if (lp)
267 vss = &td->td_ucred->cr_uidinfo->ui_varsymset;
0a004aa2
MD
268 break;
269 case VARSYM_SYS:
270 vss = &varsymset_sys;
271 break;
dbc282df 272 case VARSYM_PRISON:
9910d07b
MD
273 if (lp && td->td_ucred->cr_prison)
274 vss = &td->td_ucred->cr_prison->pr_varsymset;
dbc282df 275 break;
0a004aa2
MD
276 }
277 if (vss == NULL) {
278 error = EINVAL;
279 goto done;
280 }
281
282 /*
283 * Loop through the variables and dump them to uap->buf
284 */
285 i = 0;
286 bytes = 0;
287 earlyterm = 0;
288
9d7a637e 289 lockmgr(&vss->vx_lock, LK_SHARED);
0a004aa2
MD
290 TAILQ_FOREACH(ve, &vss->vx_queue, ve_entry) {
291 varsym_t sym = ve->ve_sym;
292 int namelen = strlen(sym->vs_name);
293 int datalen = strlen(sym->vs_data);
294 int totlen = namelen + datalen + 2;
295
296 /*
297 * Skip to our index point
298 */
299 if (i < marker) {
300 ++i;
301 continue;
302 }
303
304 /*
305 * Stop if there is insufficient space in the user buffer.
306 * If we haven't stored anything yet return EOVERFLOW.
307 * Note that the marker index (i) does not change.
308 */
309 if (bytes + totlen > uap->maxsize) {
310 if (bytes == 0)
311 error = EOVERFLOW;
312 earlyterm = 1;
313 break;
314 }
315
316 error = copyout(sym->vs_name, uap->buf + bytes, namelen + 1);
317 if (error == 0) {
318 bytes += namelen + 1;
319 error = copyout(sym->vs_data, uap->buf + bytes, datalen + 1);
320 if (error == 0)
321 bytes += datalen + 1;
322 else
323 bytes -= namelen + 1; /* revert if error */
324 }
325 if (error) {
326 earlyterm = 1;
327 break;
328 }
329 ++i;
330 }
9d7a637e 331 lockmgr(&vss->vx_lock, LK_RELEASE);
0a004aa2
MD
332
333 /*
334 * Save the marker back. If no error occured and earlyterm is clear
335 * the marker is set to -1 indicating that the variable list has been
336 * exhausted. If no error occured the number of bytes loaded into
337 * the buffer will be returned, otherwise the syscall code returns -1.
338 */
339 if (error == 0 && earlyterm == 0)
340 marker = -1;
341 else
342 marker = i;
343 if (error == 0)
344 error = copyout(&marker, uap->marker, sizeof(marker));
345 uap->sysmsg_result = bytes;
346done:
3919ced0 347 rel_mplock();
0a004aa2
MD
348 return(error);
349}
350
351/*
98a7f915
MD
352 * Lookup a variant symlink. XXX use a hash table.
353 */
354static
355struct varsyment *
356varsymlookup(struct varsymset *vss, const char *name, int namelen)
357{
358 struct varsyment *ve;
359
9d7a637e 360 KKASSERT(lockstatus(&vss->vx_lock, curthread) != 0);
98a7f915
MD
361 TAILQ_FOREACH(ve, &vss->vx_queue, ve_entry) {
362 varsym_t var = ve->ve_sym;
363 if (var->vs_namelen == namelen &&
364 bcmp(name, var->vs_name, namelen) == 0
365 ) {
366 return(ve);
367 }
368 }
369 return(NULL);
370}
9d7a637e
AE
371
372static
373void
374vsslock(struct varsymset **vss, struct varsymset *n)
375{
376 if (*vss) {
377 lockmgr(&(*vss)->vx_lock, LK_RELEASE);
378 }
379 lockmgr(&n->vx_lock, LK_SHARED);
380 *vss = n;
381}
98a7f915
MD
382
383varsym_t
384varsymfind(int mask, const char *name, int namelen)
385{
98a7f915 386 struct varsyment *ve = NULL;
9d7a637e 387 struct varsymset *vss = NULL;
9910d07b
MD
388 struct thread *td;
389 struct lwp *lp;
390 struct proc *p;
98a7f915
MD
391 varsym_t sym;
392
9910d07b
MD
393 td = curthread;
394 lp = td->td_lwp;
395 p = lp ? lp->lwp_proc : NULL;
396
397 if ((mask & (VARSYM_PROC_MASK|VARSYM_USER_MASK)) && lp != NULL) {
9d7a637e
AE
398 if (mask & VARSYM_PROC_MASK) {
399 vsslock(&vss, &p->p_varsymset);
400 ve = varsymlookup(vss, name, namelen);
401 }
402 if (ve == NULL && (mask & VARSYM_USER_MASK)) {
9910d07b 403 vsslock(&vss, &td->td_ucred->cr_uidinfo->ui_varsymset);
9d7a637e
AE
404 ve = varsymlookup(vss, name, namelen);
405 }
98a7f915 406 }
dbc282df 407 if (ve == NULL && (mask & VARSYM_SYS_MASK)) {
9910d07b
MD
408 if (lp != NULL && td->td_ucred->cr_prison) {
409 vsslock(&vss, &td->td_ucred->cr_prison->pr_varsymset);
9d7a637e
AE
410 ve = varsymlookup(vss, name, namelen);
411 } else {
412 vsslock(&vss, &varsymset_sys);
413 ve = varsymlookup(vss, name, namelen);
414 }
dbc282df 415 }
98a7f915
MD
416 if (ve) {
417 sym = ve->ve_sym;
9d7a637e 418 atomic_add_int(&sym->vs_refs, 1);
98a7f915 419 } else {
9d7a637e 420 sym = NULL;
98a7f915 421 }
9d7a637e
AE
422 lockmgr(&vss->vx_lock, LK_RELEASE);
423 return sym;
98a7f915
MD
424}
425
426int
427varsymmake(int level, const char *name, const char *data)
428{
429 struct varsymset *vss = NULL;
430 struct varsyment *ve;
9910d07b
MD
431 struct thread *td;
432 struct proc *p;
433 struct lwp *lp;
98a7f915
MD
434 varsym_t sym;
435 int namelen = strlen(name);
436 int datalen;
437 int error;
438
9910d07b
MD
439 td = curthread;
440 lp = td->td_lwp;
441 p = lp ? lp->lwp_proc : NULL;
442
98a7f915
MD
443 switch(level) {
444 case VARSYM_PROC:
445 if (p)
446 vss = &p->p_varsymset;
447 break;
448 case VARSYM_USER:
9910d07b
MD
449 if (lp)
450 vss = &td->td_ucred->cr_uidinfo->ui_varsymset;
98a7f915
MD
451 break;
452 case VARSYM_SYS:
453 vss = &varsymset_sys;
454 break;
dbc282df 455 case VARSYM_PRISON:
9910d07b
MD
456 if (lp && td->td_ucred->cr_prison)
457 vss = &td->td_ucred->cr_prison->pr_varsymset;
dbc282df 458 break;
98a7f915
MD
459 }
460 if (vss == NULL) {
9d7a637e
AE
461 return EINVAL;
462 }
463 lockmgr(&vss->vx_lock, LK_EXCLUSIVE);
464 if (data && vss->vx_setsize >= MAXVARSYM_SET) {
98a7f915
MD
465 error = E2BIG;
466 } else if (data) {
467 datalen = strlen(data);
efda3bd0
MD
468 ve = kmalloc(sizeof(struct varsyment), M_VARSYM, M_WAITOK|M_ZERO);
469 sym = kmalloc(sizeof(struct varsym) + namelen + datalen + 2, M_VARSYM, M_WAITOK);
98a7f915
MD
470 ve->ve_sym = sym;
471 sym->vs_refs = 1;
472 sym->vs_namelen = namelen;
473 sym->vs_name = (char *)(sym + 1);
474 sym->vs_data = sym->vs_name + namelen + 1;
475 strcpy(sym->vs_name, name);
476 strcpy(sym->vs_data, data);
477 TAILQ_INSERT_TAIL(&vss->vx_queue, ve, ve_entry);
478 vss->vx_setsize += sizeof(struct varsyment) + sizeof(struct varsym) + namelen + datalen + 8;
479 error = 0;
480 } else {
481 if ((ve = varsymlookup(vss, name, namelen)) != NULL) {
482 TAILQ_REMOVE(&vss->vx_queue, ve, ve_entry);
483 vss->vx_setsize -= sizeof(struct varsyment) + sizeof(struct varsym) + namelen + strlen(ve->ve_sym->vs_data) + 8;
484 varsymdrop(ve->ve_sym);
efda3bd0 485 kfree(ve, M_VARSYM);
98a7f915
MD
486 error = 0;
487 } else {
488 error = ENOENT;
489 }
490 }
9d7a637e 491 lockmgr(&vss->vx_lock, LK_RELEASE);
98a7f915
MD
492 return(error);
493}
494
495void
496varsymdrop(varsym_t sym)
497{
498 KKASSERT(sym->vs_refs > 0);
9d7a637e 499 if (atomic_fetchadd_int(&sym->vs_refs, -1) == 1) {
efda3bd0 500 kfree(sym, M_VARSYM);
98a7f915
MD
501 }
502}
503
9d7a637e
AE
504/*
505 * Insert a duplicate of ve in vss. Does not do any locking,
506 * so it is the callers responsibility to make sure nobody
507 * else can mess with the TAILQ in vss at the same time.
508 */
98a7f915
MD
509static void
510varsymdup(struct varsymset *vss, struct varsyment *ve)
511{
512 struct varsyment *nve;
513
efda3bd0 514 nve = kmalloc(sizeof(struct varsyment), M_VARSYM, M_WAITOK|M_ZERO);
98a7f915 515 nve->ve_sym = ve->ve_sym;
9d7a637e
AE
516 ++nve->ve_sym->vs_refs; /* can't be reached, no need for atomic add */
517 /*
518 * We're only called through varsymset_init() so vss is not yet reachable,
519 * no need to lock.
520 */
98a7f915
MD
521 TAILQ_INSERT_TAIL(&vss->vx_queue, nve, ve_entry);
522}
523
524void
525varsymset_init(struct varsymset *vss, struct varsymset *copy)
526{
527 struct varsyment *ve;
528
529 TAILQ_INIT(&vss->vx_queue);
9d7a637e 530 lockinit(&vss->vx_lock, "vx", 0, 0);
98a7f915
MD
531 if (copy) {
532 TAILQ_FOREACH(ve, &copy->vx_queue, ve_entry) {
533 varsymdup(vss, ve);
534 }
535 vss->vx_setsize = copy->vx_setsize;
536 }
537}
538
539void
540varsymset_clean(struct varsymset *vss)
541{
542 struct varsyment *ve;
543
9d7a637e 544 lockmgr(&vss->vx_lock, LK_EXCLUSIVE);
98a7f915
MD
545 while ((ve = TAILQ_FIRST(&vss->vx_queue)) != NULL) {
546 TAILQ_REMOVE(&vss->vx_queue, ve, ve_entry);
547 varsymdrop(ve->ve_sym);
efda3bd0 548 kfree(ve, M_VARSYM);
98a7f915
MD
549 }
550 vss->vx_setsize = 0;
9d7a637e 551 lockmgr(&vss->vx_lock, LK_RELEASE);
98a7f915
MD
552}
553