Import 2.3.99pre7-7
[davej-history.git] / ipc / util.c
blob8771f73d11aece8d2257dbeb615c7b2ecc21b75f
1 /*
2 * linux/ipc/util.c
3 * Copyright (C) 1992 Krishna Balasubramanian
5 * Sep 1997 - Call suser() last after "normal" permission checks so we
6 * get BSD style process accounting right.
7 * Occurs in several places in the IPC code.
8 * Chris Evans, <chris@ferret.lmh.ox.ac.uk>
9 * Nov 1999 - ipc helper functions, unified SMP locking
10 * Manfred Spraul <manfreds@colorfullife.com>
13 #include <linux/config.h>
14 #include <linux/mm.h>
15 #include <linux/shm.h>
16 #include <linux/init.h>
17 #include <linux/msg.h>
18 #include <linux/smp_lock.h>
19 #include <linux/vmalloc.h>
20 #include <linux/malloc.h>
21 #include <linux/highuid.h>
23 #if defined(CONFIG_SYSVIPC)
25 #include "util.h"
27 void __init ipc_init (void)
29 sem_init();
30 msg_init();
31 shm_init();
32 return;
35 void __init ipc_init_ids(struct ipc_ids* ids, int size)
37 int i;
38 sema_init(&ids->sem,1);
40 if(size > IPCMNI)
41 size = IPCMNI;
42 ids->size = size;
43 ids->in_use = 0;
44 ids->max_id = -1;
45 ids->seq = 0;
47 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
48 if(seq_limit > USHRT_MAX)
49 ids->seq_max = USHRT_MAX;
50 else
51 ids->seq_max = seq_limit;
54 ids->entries = ipc_alloc(sizeof(struct ipc_id)*size);
56 if(ids->entries == NULL) {
57 printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
58 ids->size = 0;
60 ids->ary = SPIN_LOCK_UNLOCKED;
61 for(i=0;i<size;i++)
62 ids->entries[i].p = NULL;
65 int ipc_findkey(struct ipc_ids* ids, key_t key)
67 int id;
68 struct kern_ipc_perm* p;
70 for (id = 0; id <= ids->max_id; id++) {
71 p = ids->entries[id].p;
72 if(p==NULL)
73 continue;
74 if (key == p->key)
75 return id;
77 return -1;
80 static int grow_ary(struct ipc_ids* ids, int newsize)
82 struct ipc_id* new;
83 struct ipc_id* old;
84 int i;
86 if(newsize > IPCMNI)
87 newsize = IPCMNI;
88 if(newsize <= ids->size)
89 return newsize;
91 new = ipc_alloc(sizeof(struct ipc_id)*newsize);
92 if(new == NULL)
93 return ids->size;
94 memcpy(new, ids->entries, sizeof(struct ipc_id)*ids->size);
95 for(i=ids->size;i<newsize;i++) {
96 new[i].p = NULL;
98 spin_lock(&ids->ary);
100 old = ids->entries;
101 ids->entries = new;
102 i = ids->size;
103 ids->size = newsize;
104 spin_unlock(&ids->ary);
105 ipc_free(old, sizeof(struct ipc_id)*i);
106 return ids->size;
109 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
111 int id;
113 size = grow_ary(ids,size);
114 for (id = 0; id < size; id++) {
115 if(ids->entries[id].p == NULL)
116 goto found;
118 return -1;
119 found:
120 ids->in_use++;
121 if (id > ids->max_id)
122 ids->max_id = id;
124 new->cuid = new->uid = current->euid;
125 new->gid = new->cgid = current->egid;
127 new->seq = ids->seq++;
128 if(ids->seq > ids->seq_max)
129 ids->seq = 0;
131 spin_lock(&ids->ary);
132 ids->entries[id].p = new;
133 return id;
136 struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
138 struct kern_ipc_perm* p;
139 int lid = id % SEQ_MULTIPLIER;
140 if(lid > ids->size)
141 BUG();
142 p = ids->entries[lid].p;
143 ids->entries[lid].p = NULL;
144 if(p==NULL)
145 BUG();
146 ids->in_use--;
148 if (lid == ids->max_id) {
149 do {
150 lid--;
151 if(lid == -1)
152 break;
153 } while (ids->entries[lid].p == NULL);
154 ids->max_id = lid;
156 return p;
159 void* ipc_alloc(int size)
161 void* out;
162 if(size > PAGE_SIZE) {
163 lock_kernel();
164 out = vmalloc(size);
165 unlock_kernel();
166 } else {
167 out = kmalloc(size, GFP_KERNEL);
169 return out;
172 void ipc_free(void* ptr, int size)
174 if(size > PAGE_SIZE) {
175 lock_kernel();
176 vfree(ptr);
177 unlock_kernel();
178 } else {
179 kfree(ptr);
184 * Check user, group, other permissions for access
185 * to ipc resources. return 0 if allowed
187 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
188 { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
189 int requested_mode, granted_mode;
191 requested_mode = (flag >> 6) | (flag >> 3) | flag;
192 granted_mode = ipcp->mode;
193 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
194 granted_mode >>= 6;
195 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
196 granted_mode >>= 3;
197 /* is there some bit set in requested_mode but not in granted_mode? */
198 if ((requested_mode & ~granted_mode & 0007) &&
199 !capable(CAP_IPC_OWNER))
200 return -1;
202 return 0;
206 * Functions to convert between the kern_ipc_perm structure and the
207 * old/new ipc_perm structures
210 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
212 out->key = in->key;
213 out->uid = in->uid;
214 out->gid = in->gid;
215 out->cuid = in->cuid;
216 out->cgid = in->cgid;
217 out->mode = in->mode;
218 out->seq = in->seq;
221 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
223 out->key = in->key;
224 out->uid = NEW_TO_OLD_UID(in->uid);
225 out->gid = NEW_TO_OLD_GID(in->gid);
226 out->cuid = NEW_TO_OLD_UID(in->cuid);
227 out->cgid = NEW_TO_OLD_GID(in->cgid);
228 out->mode = in->mode;
229 out->seq = in->seq;
232 int ipc_parse_version (int *cmd)
234 if (*cmd & IPC_64) {
235 *cmd ^= IPC_64;
236 return IPC_64;
237 } else {
238 return IPC_OLD;
242 #else
244 * Dummy functions when SYSV IPC isn't configured
247 void sem_exit (void)
249 return;
252 int shm_swap (int prio, int gfp_mask)
254 return 0;
257 asmlinkage long sys_semget (key_t key, int nsems, int semflg)
259 return -ENOSYS;
262 asmlinkage long sys_semop (int semid, struct sembuf *sops, unsigned nsops)
264 return -ENOSYS;
267 asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
269 return -ENOSYS;
272 asmlinkage long sys_msgget (key_t key, int msgflg)
274 return -ENOSYS;
277 asmlinkage long sys_msgsnd (int msqid, struct msgbuf *msgp, size_t msgsz, int msgflg)
279 return -ENOSYS;
282 asmlinkage long sys_msgrcv (int msqid, struct msgbuf *msgp, size_t msgsz, long msgtyp,
283 int msgflg)
285 return -ENOSYS;
288 asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds *buf)
290 return -ENOSYS;
293 asmlinkage long sys_shmget (key_t key, size_t size, int shmflag)
295 return -ENOSYS;
298 asmlinkage long sys_shmat (int shmid, char *shmaddr, int shmflg, ulong *addr)
300 return -ENOSYS;
303 asmlinkage long sys_shmdt (char *shmaddr)
305 return -ENOSYS;
308 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds *buf)
310 return -ENOSYS;
313 void shm_unuse(swp_entry_t entry, struct page *page)
317 int map_zero_setup(struct vm_area_struct *vma)
319 return -EINVAL;
322 #endif /* CONFIG_SYSVIPC */