Merge with 2.3.99-pre9.
[linux-2.6/linux-mips.git] / ipc / util.c
blob29819efbb021d1cd8bed2b735b6ff55bfa2a956a
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 out = vmalloc(size);
164 else
165 out = kmalloc(size, GFP_KERNEL);
166 return out;
169 void ipc_free(void* ptr, int size)
171 if(size > PAGE_SIZE)
172 vfree(ptr);
173 else
174 kfree(ptr);
178 * Check user, group, other permissions for access
179 * to ipc resources. return 0 if allowed
181 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
182 { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
183 int requested_mode, granted_mode;
185 requested_mode = (flag >> 6) | (flag >> 3) | flag;
186 granted_mode = ipcp->mode;
187 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
188 granted_mode >>= 6;
189 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
190 granted_mode >>= 3;
191 /* is there some bit set in requested_mode but not in granted_mode? */
192 if ((requested_mode & ~granted_mode & 0007) &&
193 !capable(CAP_IPC_OWNER))
194 return -1;
196 return 0;
200 * Functions to convert between the kern_ipc_perm structure and the
201 * old/new ipc_perm structures
204 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
206 out->key = in->key;
207 out->uid = in->uid;
208 out->gid = in->gid;
209 out->cuid = in->cuid;
210 out->cgid = in->cgid;
211 out->mode = in->mode;
212 out->seq = in->seq;
215 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
217 out->key = in->key;
218 out->uid = NEW_TO_OLD_UID(in->uid);
219 out->gid = NEW_TO_OLD_GID(in->gid);
220 out->cuid = NEW_TO_OLD_UID(in->cuid);
221 out->cgid = NEW_TO_OLD_GID(in->cgid);
222 out->mode = in->mode;
223 out->seq = in->seq;
226 int ipc_parse_version (int *cmd)
228 if (*cmd & IPC_64) {
229 *cmd ^= IPC_64;
230 return IPC_64;
231 } else {
232 return IPC_OLD;
236 #else
238 * Dummy functions when SYSV IPC isn't configured
241 void sem_exit (void)
243 return;
246 int shm_swap (int prio, int gfp_mask)
248 return 0;
251 asmlinkage long sys_semget (key_t key, int nsems, int semflg)
253 return -ENOSYS;
256 asmlinkage long sys_semop (int semid, struct sembuf *sops, unsigned nsops)
258 return -ENOSYS;
261 asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
263 return -ENOSYS;
266 asmlinkage long sys_msgget (key_t key, int msgflg)
268 return -ENOSYS;
271 asmlinkage long sys_msgsnd (int msqid, struct msgbuf *msgp, size_t msgsz, int msgflg)
273 return -ENOSYS;
276 asmlinkage long sys_msgrcv (int msqid, struct msgbuf *msgp, size_t msgsz, long msgtyp,
277 int msgflg)
279 return -ENOSYS;
282 asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds *buf)
284 return -ENOSYS;
287 asmlinkage long sys_shmget (key_t key, size_t size, int shmflag)
289 return -ENOSYS;
292 asmlinkage long sys_shmat (int shmid, char *shmaddr, int shmflg, ulong *addr)
294 return -ENOSYS;
297 asmlinkage long sys_shmdt (char *shmaddr)
299 return -ENOSYS;
302 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds *buf)
304 return -ENOSYS;
307 void shm_unuse(swp_entry_t entry, struct page *page)
311 int map_zero_setup(struct vm_area_struct *vma)
313 return -EINVAL;
316 #endif /* CONFIG_SYSVIPC */