2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * osi_cred.c - Linux cred handling routines.
14 #include "../afs/param.h"
15 #include "../afs/sysincludes.h"
16 #include "../afs/afsincludes.h"
18 /* Setup a pool for creds. Allocate several at a time. */
19 #define CRED_ALLOC_STEP 29 /* at 140 bytes/cred = 4060 bytes. */
22 static cred_t *cred_pool = NULL;
26 /* Cred locking assumes current single threaded non-preemptive kernel.
27 * Also assuming a fast path through both down and up if no waiters. Otherwise,
28 * test if no creds in pool before grabbing lock in crfree().
30 #if defined(AFS_LINUX24_ENV)
31 static DECLARE_MUTEX(linux_cred_pool_lock);
33 static struct semaphore linux_cred_pool_lock = MUTEX;
35 #define CRED_LOCK() down(&linux_cred_pool_lock)
36 #define CRED_UNLOCK() up(&linux_cred_pool_lock)
46 cred_pool = (cred_t*)osi_Alloc(CRED_ALLOC_STEP * sizeof(cred_t));
48 osi_Panic("crget: No more memory for creds!\n");
50 for (i=0; i < CRED_ALLOC_STEP-1; i++)
51 cred_pool[i].cr_ref = (int)&cred_pool[i+1];
52 cred_pool[i].cr_ref = 0;
55 cred_pool = (cred_t*)tmp->cr_ref;
59 memset(tmp, 0, sizeof(cred_t));
64 void crfree(cred_t *cr)
72 cr->cr_ref = (int)cred_pool;
79 /* Return a duplicate of the cred. */
80 cred_t *crdup(cred_t *cr)
82 cred_t *tmp = crget();
91 cr->cr_uid = current->fsuid;
92 cr->cr_ruid = current->uid;
93 cr->cr_gid = current->fsgid;
94 cr->cr_rgid = current->gid;
95 memcpy(cr->cr_groups, current->groups, NGROUPS * sizeof(gid_t));
96 cr->cr_ngroups = current->ngroups;
101 /* Set the cred info into the current task */
102 void crset(cred_t *cr)
104 current->fsuid = cr->cr_uid;
105 current->uid = cr->cr_ruid;
106 current->fsgid = cr->cr_gid;
107 current->gid = cr->cr_rgid;
108 memcpy(current->groups, cr->cr_groups, NGROUPS * sizeof(gid_t));
109 current->ngroups = cr->cr_ngroups;