- Linus: drop support for old-style Makefiles entirely. Big.
[davej-history.git] / ipc / util.c
blob66eae7215c30a014309f641b9daf8242efb4b91a
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 /**
28 * ipc_init - initialise IPC subsystem
30 * The various system5 IPC resources (semaphores, messages and shared
31 * memory are initialised
34 void __init ipc_init (void)
36 sem_init();
37 msg_init();
38 shm_init();
39 return;
42 /**
43 * ipc_init_ids - initialise IPC identifiers
44 * @ids: Identifier set
45 * @size: Number of identifiers
47 * Given a size for the ipc identifier range (limited below IPCMNI)
48 * set up the sequence range to use then allocate and initialise the
49 * array itself.
52 void __init ipc_init_ids(struct ipc_ids* ids, int size)
54 int i;
55 sema_init(&ids->sem,1);
57 if(size > IPCMNI)
58 size = IPCMNI;
59 ids->size = size;
60 ids->in_use = 0;
61 ids->max_id = -1;
62 ids->seq = 0;
64 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
65 if(seq_limit > USHRT_MAX)
66 ids->seq_max = USHRT_MAX;
67 else
68 ids->seq_max = seq_limit;
71 ids->entries = ipc_alloc(sizeof(struct ipc_id)*size);
73 if(ids->entries == NULL) {
74 printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
75 ids->size = 0;
77 ids->ary = SPIN_LOCK_UNLOCKED;
78 for(i=0;i<size;i++)
79 ids->entries[i].p = NULL;
82 /**
83 * ipc_findkey - find a key in an ipc identifier set
84 * @ids: Identifier set
85 * @key: The key to find
87 * Returns the identifier if found or -1 if not.
90 int ipc_findkey(struct ipc_ids* ids, key_t key)
92 int id;
93 struct kern_ipc_perm* p;
95 for (id = 0; id <= ids->max_id; id++) {
96 p = ids->entries[id].p;
97 if(p==NULL)
98 continue;
99 if (key == p->key)
100 return id;
102 return -1;
105 static int grow_ary(struct ipc_ids* ids, int newsize)
107 struct ipc_id* new;
108 struct ipc_id* old;
109 int i;
111 if(newsize > IPCMNI)
112 newsize = IPCMNI;
113 if(newsize <= ids->size)
114 return newsize;
116 new = ipc_alloc(sizeof(struct ipc_id)*newsize);
117 if(new == NULL)
118 return ids->size;
119 memcpy(new, ids->entries, sizeof(struct ipc_id)*ids->size);
120 for(i=ids->size;i<newsize;i++) {
121 new[i].p = NULL;
123 spin_lock(&ids->ary);
125 old = ids->entries;
126 ids->entries = new;
127 i = ids->size;
128 ids->size = newsize;
129 spin_unlock(&ids->ary);
130 ipc_free(old, sizeof(struct ipc_id)*i);
131 return ids->size;
135 * ipc_addid - add an IPC identifier
136 * @ids: IPC identifier set
137 * @new: new IPC permission set
138 * @size: new size limit for the id array
140 * Add an entry 'new' to the IPC arrays. The permissions object is
141 * initialised and the first free entry is set up and the id assigned
142 * is returned. The list is returned in a locked state on success.
143 * On failure the list is not locked and -1 is returned.
146 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
148 int id;
150 size = grow_ary(ids,size);
151 for (id = 0; id < size; id++) {
152 if(ids->entries[id].p == NULL)
153 goto found;
155 return -1;
156 found:
157 ids->in_use++;
158 if (id > ids->max_id)
159 ids->max_id = id;
161 new->cuid = new->uid = current->euid;
162 new->gid = new->cgid = current->egid;
164 new->seq = ids->seq++;
165 if(ids->seq > ids->seq_max)
166 ids->seq = 0;
168 spin_lock(&ids->ary);
169 ids->entries[id].p = new;
170 return id;
174 * ipc_rmid - remove an IPC identifier
175 * @ids: identifier set
176 * @id: Identifier to remove
178 * The identifier must be valid, and in use. The kernel will panic if
179 * fed an invalid identifier. The entry is removed and internal
180 * variables recomputed. The object associated with the identifier
181 * is returned.
184 struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
186 struct kern_ipc_perm* p;
187 int lid = id % SEQ_MULTIPLIER;
188 if(lid > ids->size)
189 BUG();
190 p = ids->entries[lid].p;
191 ids->entries[lid].p = NULL;
192 if(p==NULL)
193 BUG();
194 ids->in_use--;
196 if (lid == ids->max_id) {
197 do {
198 lid--;
199 if(lid == -1)
200 break;
201 } while (ids->entries[lid].p == NULL);
202 ids->max_id = lid;
204 return p;
208 * ipc_alloc - allocate ipc space
209 * @size: size desired
211 * Allocate memory from the appropriate pools and return a pointer to it.
212 * NULL is returned if the allocation fails
215 void* ipc_alloc(int size)
217 void* out;
218 if(size > PAGE_SIZE)
219 out = vmalloc(size);
220 else
221 out = kmalloc(size, GFP_KERNEL);
222 return out;
226 * ipc_free - free ipc space
227 * @ptr: pointer returned by ipc_alloc
228 * @size: size of block
230 * Free a block created with ipc_alloc. The caller must know the size
231 * used in the allocation call.
234 void ipc_free(void* ptr, int size)
236 if(size > PAGE_SIZE)
237 vfree(ptr);
238 else
239 kfree(ptr);
243 * ipcperms - check IPC permissions
244 * @ipcp: IPC permission set
245 * @flag: desired permission set.
247 * Check user, group, other permissions for access
248 * to ipc resources. return 0 if allowed
251 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
252 { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
253 int requested_mode, granted_mode;
255 requested_mode = (flag >> 6) | (flag >> 3) | flag;
256 granted_mode = ipcp->mode;
257 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
258 granted_mode >>= 6;
259 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
260 granted_mode >>= 3;
261 /* is there some bit set in requested_mode but not in granted_mode? */
262 if ((requested_mode & ~granted_mode & 0007) &&
263 !capable(CAP_IPC_OWNER))
264 return -1;
266 return 0;
270 * Functions to convert between the kern_ipc_perm structure and the
271 * old/new ipc_perm structures
275 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
276 * @in: kernel permissions
277 * @out: new style IPC permissions
279 * Turn the kernel object 'in' into a set of permissions descriptions
280 * for returning to userspace (out).
284 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
286 out->key = in->key;
287 out->uid = in->uid;
288 out->gid = in->gid;
289 out->cuid = in->cuid;
290 out->cgid = in->cgid;
291 out->mode = in->mode;
292 out->seq = in->seq;
296 * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
297 * @in: new style IPC permissions
298 * @out: old style IPC permissions
300 * Turn the new style permissions object in into a compatibility
301 * object and store it into the 'out' pointer.
304 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
306 out->key = in->key;
307 out->uid = NEW_TO_OLD_UID(in->uid);
308 out->gid = NEW_TO_OLD_GID(in->gid);
309 out->cuid = NEW_TO_OLD_UID(in->cuid);
310 out->cgid = NEW_TO_OLD_GID(in->cgid);
311 out->mode = in->mode;
312 out->seq = in->seq;
315 #ifndef __ia64__
318 * ipc_parse_version - IPC call version
319 * @cmd: pointer to command
321 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
322 * The cmd value is turned from an encoding command and version into
323 * just the command code.
326 int ipc_parse_version (int *cmd)
328 if (*cmd & IPC_64) {
329 *cmd ^= IPC_64;
330 return IPC_64;
331 } else {
332 return IPC_OLD;
336 #endif /* __ia64__ */
338 #else
340 * Dummy functions when SYSV IPC isn't configured
343 void sem_exit (void)
345 return;
348 int shm_swap (int prio, int gfp_mask)
350 return 0;
353 asmlinkage long sys_semget (key_t key, int nsems, int semflg)
355 return -ENOSYS;
358 asmlinkage long sys_semop (int semid, struct sembuf *sops, unsigned nsops)
360 return -ENOSYS;
363 asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
365 return -ENOSYS;
368 asmlinkage long sys_msgget (key_t key, int msgflg)
370 return -ENOSYS;
373 asmlinkage long sys_msgsnd (int msqid, struct msgbuf *msgp, size_t msgsz, int msgflg)
375 return -ENOSYS;
378 asmlinkage long sys_msgrcv (int msqid, struct msgbuf *msgp, size_t msgsz, long msgtyp,
379 int msgflg)
381 return -ENOSYS;
384 asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds *buf)
386 return -ENOSYS;
389 asmlinkage long sys_shmget (key_t key, size_t size, int shmflag)
391 return -ENOSYS;
394 asmlinkage long sys_shmat (int shmid, char *shmaddr, int shmflg, ulong *addr)
396 return -ENOSYS;
399 asmlinkage long sys_shmdt (char *shmaddr)
401 return -ENOSYS;
404 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds *buf)
406 return -ENOSYS;
409 void shm_unuse(swp_entry_t entry, struct page *page)
413 int map_zero_setup(struct vm_area_struct *vma)
415 return -EINVAL;
418 #endif /* CONFIG_SYSVIPC */