blob: 44e5135aee4785e2da26de92e0d3b52b02246842 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/ipc/util.c
3 * Copyright (C) 1992 Krishna Balasubramanian
4 *
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
Christian Kujau624dffc2006-01-15 02:43:54 +010010 * Manfred Spraul <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
12 * Mingming Cao <cmm@us.ibm.com>
Steve Grubb073115d2006-04-02 17:07:33 -040013 * Mar 2006 - support for audit of ipc object properties
14 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaev73ea4132006-10-02 02:18:20 -070015 * Jun 2006 - namespaces ssupport
16 * OpenVZ, SWsoft Inc.
17 * Pavel Emelianov <xemul@openvz.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/mm.h>
21#include <linux/shm.h>
22#include <linux/init.h>
23#include <linux/msg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/vmalloc.h>
25#include <linux/slab.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080026#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/highuid.h>
28#include <linux/security.h>
29#include <linux/rcupdate.h>
30#include <linux/workqueue.h>
Mike Waychisonae781772005-09-06 15:17:09 -070031#include <linux/seq_file.h>
32#include <linux/proc_fs.h>
Steve Grubb073115d2006-04-02 17:07:33 -040033#include <linux/audit.h>
Kirill Korotaev73ea4132006-10-02 02:18:20 -070034#include <linux/nsproxy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <asm/unistd.h>
37
38#include "util.h"
39
Mike Waychisonae781772005-09-06 15:17:09 -070040struct ipc_proc_iface {
41 const char *path;
42 const char *header;
Kirill Korotaev73ea4132006-10-02 02:18:20 -070043 int ids;
Mike Waychisonae781772005-09-06 15:17:09 -070044 int (*show)(struct seq_file *, void *);
45};
46
Kirill Korotaev73ea4132006-10-02 02:18:20 -070047struct ipc_namespace init_ipc_ns = {
48 .kref = {
49 .refcount = ATOMIC_INIT(2),
50 },
51};
52
Kirill Korotaev73ea4132006-10-02 02:18:20 -070053static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)
54{
55 int err;
56 struct ipc_namespace *ns;
57
58 err = -ENOMEM;
59 ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
60 if (ns == NULL)
61 goto err_mem;
62
63 err = sem_init_ns(ns);
64 if (err)
65 goto err_sem;
66 err = msg_init_ns(ns);
67 if (err)
68 goto err_msg;
69 err = shm_init_ns(ns);
70 if (err)
71 goto err_shm;
72
73 kref_init(&ns->kref);
74 return ns;
75
76err_shm:
77 msg_exit_ns(ns);
78err_msg:
79 sem_exit_ns(ns);
80err_sem:
81 kfree(ns);
82err_mem:
83 return ERR_PTR(err);
84}
85
Badari Pulavartye3222c42007-05-08 00:25:21 -070086struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
Kirill Korotaev73ea4132006-10-02 02:18:20 -070087{
Kirill Korotaev73ea4132006-10-02 02:18:20 -070088 struct ipc_namespace *new_ns;
Kirill Korotaev73ea4132006-10-02 02:18:20 -070089
Badari Pulavartye3222c42007-05-08 00:25:21 -070090 BUG_ON(!ns);
91 get_ipc_ns(ns);
Kirill Korotaev73ea4132006-10-02 02:18:20 -070092
93 if (!(flags & CLONE_NEWIPC))
Badari Pulavartye3222c42007-05-08 00:25:21 -070094 return ns;
Kirill Korotaev73ea4132006-10-02 02:18:20 -070095
Badari Pulavartye3222c42007-05-08 00:25:21 -070096 new_ns = clone_ipc_ns(ns);
Kirill Korotaev73ea4132006-10-02 02:18:20 -070097
Badari Pulavartye3222c42007-05-08 00:25:21 -070098 put_ipc_ns(ns);
99 return new_ns;
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700100}
101
102void free_ipc_ns(struct kref *kref)
103{
104 struct ipc_namespace *ns;
105
106 ns = container_of(kref, struct ipc_namespace, kref);
107 sem_exit_ns(ns);
108 msg_exit_ns(ns);
109 shm_exit_ns(ns);
110 kfree(ns);
111}
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/**
114 * ipc_init - initialise IPC subsystem
115 *
116 * The various system5 IPC resources (semaphores, messages and shared
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800117 * memory) are initialised
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
119
120static int __init ipc_init(void)
121{
122 sem_init();
123 msg_init();
124 shm_init();
125 return 0;
126}
127__initcall(ipc_init);
128
129/**
130 * ipc_init_ids - initialise IPC identifiers
131 * @ids: Identifier set
132 * @size: Number of identifiers
133 *
134 * Given a size for the ipc identifier range (limited below IPCMNI)
135 * set up the sequence range to use then allocate and initialise the
136 * array itself.
137 */
138
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -0700139void ipc_init_ids(struct ipc_ids* ids, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 int i;
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800142
143 mutex_init(&ids->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 if(size > IPCMNI)
146 size = IPCMNI;
147 ids->in_use = 0;
148 ids->max_id = -1;
149 ids->seq = 0;
150 {
151 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
152 if(seq_limit > USHRT_MAX)
153 ids->seq_max = USHRT_MAX;
154 else
155 ids->seq_max = seq_limit;
156 }
157
158 ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size +
159 sizeof(struct ipc_id_ary));
160
161 if(ids->entries == NULL) {
162 printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
163 size = 0;
164 ids->entries = &ids->nullentry;
165 }
166 ids->entries->size = size;
167 for(i=0;i<size;i++)
168 ids->entries->p[i] = NULL;
169}
170
Mike Waychisonae781772005-09-06 15:17:09 -0700171#ifdef CONFIG_PROC_FS
Arjan van de Ven9a321442007-02-12 00:55:35 -0800172static const struct file_operations sysvipc_proc_fops;
Mike Waychisonae781772005-09-06 15:17:09 -0700173/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800174 * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
Mike Waychisonae781772005-09-06 15:17:09 -0700175 * @path: Path in procfs
176 * @header: Banner to be printed at the beginning of the file.
177 * @ids: ipc id table to iterate.
178 * @show: show routine.
179 */
180void __init ipc_init_proc_interface(const char *path, const char *header,
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700181 int ids, int (*show)(struct seq_file *, void *))
Mike Waychisonae781772005-09-06 15:17:09 -0700182{
183 struct proc_dir_entry *pde;
184 struct ipc_proc_iface *iface;
185
186 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
187 if (!iface)
188 return;
189 iface->path = path;
190 iface->header = header;
191 iface->ids = ids;
192 iface->show = show;
193
194 pde = create_proc_entry(path,
195 S_IRUGO, /* world readable */
196 NULL /* parent dir */);
197 if (pde) {
198 pde->data = iface;
199 pde->proc_fops = &sysvipc_proc_fops;
200 } else {
201 kfree(iface);
202 }
203}
204#endif
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/**
207 * ipc_findkey - find a key in an ipc identifier set
208 * @ids: Identifier set
209 * @key: The key to find
210 *
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800211 * Requires ipc_ids.mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * Returns the identifier if found or -1 if not.
213 */
214
215int ipc_findkey(struct ipc_ids* ids, key_t key)
216{
217 int id;
218 struct kern_ipc_perm* p;
219 int max_id = ids->max_id;
220
221 /*
222 * rcu_dereference() is not needed here
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800223 * since ipc_ids.mutex is held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
225 for (id = 0; id <= max_id; id++) {
226 p = ids->entries->p[id];
227 if(p==NULL)
228 continue;
229 if (key == p->key)
230 return id;
231 }
232 return -1;
233}
234
235/*
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800236 * Requires ipc_ids.mutex locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
238static int grow_ary(struct ipc_ids* ids, int newsize)
239{
240 struct ipc_id_ary* new;
241 struct ipc_id_ary* old;
242 int i;
243 int size = ids->entries->size;
244
245 if(newsize > IPCMNI)
246 newsize = IPCMNI;
247 if(newsize <= size)
248 return newsize;
249
250 new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
251 sizeof(struct ipc_id_ary));
252 if(new == NULL)
253 return size;
254 new->size = newsize;
Alexey Kuznetsova9a5cd52006-04-17 15:39:23 +0400255 memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 for(i=size;i<newsize;i++) {
257 new->p[i] = NULL;
258 }
259 old = ids->entries;
260
261 /*
262 * Use rcu_assign_pointer() to make sure the memcpyed contents
263 * of the new array are visible before the new array becomes visible.
264 */
265 rcu_assign_pointer(ids->entries, new);
266
Pavel Emelianovc7e12b82006-11-02 22:07:03 -0800267 __ipc_fini_ids(ids, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return newsize;
269}
270
271/**
272 * ipc_addid - add an IPC identifier
273 * @ids: IPC identifier set
274 * @new: new IPC permission set
275 * @size: new size limit for the id array
276 *
277 * Add an entry 'new' to the IPC arrays. The permissions object is
278 * initialised and the first free entry is set up and the id assigned
279 * is returned. The list is returned in a locked state on success.
280 * On failure the list is not locked and -1 is returned.
281 *
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800282 * Called with ipc_ids.mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 */
284
285int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
286{
287 int id;
288
289 size = grow_ary(ids,size);
290
291 /*
292 * rcu_dereference()() is not needed here since
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800293 * ipc_ids.mutex is held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
295 for (id = 0; id < size; id++) {
296 if(ids->entries->p[id] == NULL)
297 goto found;
298 }
299 return -1;
300found:
301 ids->in_use++;
302 if (id > ids->max_id)
303 ids->max_id = id;
304
305 new->cuid = new->uid = current->euid;
306 new->gid = new->cgid = current->egid;
307
308 new->seq = ids->seq++;
309 if(ids->seq > ids->seq_max)
310 ids->seq = 0;
311
312 spin_lock_init(&new->lock);
313 new->deleted = 0;
314 rcu_read_lock();
315 spin_lock(&new->lock);
316 ids->entries->p[id] = new;
317 return id;
318}
319
320/**
321 * ipc_rmid - remove an IPC identifier
322 * @ids: identifier set
323 * @id: Identifier to remove
324 *
325 * The identifier must be valid, and in use. The kernel will panic if
326 * fed an invalid identifier. The entry is removed and internal
327 * variables recomputed. The object associated with the identifier
328 * is returned.
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800329 * ipc_ids.mutex and the spinlock for this ID is hold before this function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * is called, and remain locked on the exit.
331 */
332
333struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
334{
335 struct kern_ipc_perm* p;
336 int lid = id % SEQ_MULTIPLIER;
Eric Sesterhenn9bc98fc2006-04-01 01:20:23 +0200337 BUG_ON(lid >= ids->entries->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /*
340 * do not need a rcu_dereference()() here to force ordering
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800341 * on Alpha, since the ipc_ids.mutex is held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 */
343 p = ids->entries->p[lid];
344 ids->entries->p[lid] = NULL;
Eric Sesterhenn9bc98fc2006-04-01 01:20:23 +0200345 BUG_ON(p==NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 ids->in_use--;
347
348 if (lid == ids->max_id) {
349 do {
350 lid--;
351 if(lid == -1)
352 break;
353 } while (ids->entries->p[lid] == NULL);
354 ids->max_id = lid;
355 }
356 p->deleted = 1;
357 return p;
358}
359
360/**
361 * ipc_alloc - allocate ipc space
362 * @size: size desired
363 *
364 * Allocate memory from the appropriate pools and return a pointer to it.
365 * NULL is returned if the allocation fails
366 */
367
368void* ipc_alloc(int size)
369{
370 void* out;
371 if(size > PAGE_SIZE)
372 out = vmalloc(size);
373 else
374 out = kmalloc(size, GFP_KERNEL);
375 return out;
376}
377
378/**
379 * ipc_free - free ipc space
380 * @ptr: pointer returned by ipc_alloc
381 * @size: size of block
382 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800383 * Free a block created with ipc_alloc(). The caller must know the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 * used in the allocation call.
385 */
386
387void ipc_free(void* ptr, int size)
388{
389 if(size > PAGE_SIZE)
390 vfree(ptr);
391 else
392 kfree(ptr);
393}
394
395/*
396 * rcu allocations:
397 * There are three headers that are prepended to the actual allocation:
398 * - during use: ipc_rcu_hdr.
399 * - during the rcu grace period: ipc_rcu_grace.
400 * - [only if vmalloc]: ipc_rcu_sched.
401 * Their lifetime doesn't overlap, thus the headers share the same memory.
402 * Unlike a normal union, they are right-aligned, thus some container_of
403 * forward/backward casting is necessary:
404 */
405struct ipc_rcu_hdr
406{
407 int refcount;
408 int is_vmalloc;
409 void *data[0];
410};
411
412
413struct ipc_rcu_grace
414{
415 struct rcu_head rcu;
416 /* "void *" makes sure alignment of following data is sane. */
417 void *data[0];
418};
419
420struct ipc_rcu_sched
421{
422 struct work_struct work;
423 /* "void *" makes sure alignment of following data is sane. */
424 void *data[0];
425};
426
427#define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
428 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
429#define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
430 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
431
432static inline int rcu_use_vmalloc(int size)
433{
434 /* Too big for a single page? */
435 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
436 return 1;
437 return 0;
438}
439
440/**
441 * ipc_rcu_alloc - allocate ipc and rcu space
442 * @size: size desired
443 *
444 * Allocate memory for the rcu header structure + the object.
445 * Returns the pointer to the object.
446 * NULL is returned if the allocation fails.
447 */
448
449void* ipc_rcu_alloc(int size)
450{
451 void* out;
452 /*
453 * We prepend the allocation with the rcu struct, and
454 * workqueue if necessary (for vmalloc).
455 */
456 if (rcu_use_vmalloc(size)) {
457 out = vmalloc(HDRLEN_VMALLOC + size);
458 if (out) {
459 out += HDRLEN_VMALLOC;
460 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
461 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
462 }
463 } else {
464 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
465 if (out) {
466 out += HDRLEN_KMALLOC;
467 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
468 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
469 }
470 }
471
472 return out;
473}
474
475void ipc_rcu_getref(void *ptr)
476{
477 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
478}
479
David Howells65f27f32006-11-22 14:55:48 +0000480static void ipc_do_vfree(struct work_struct *work)
481{
482 vfree(container_of(work, struct ipc_rcu_sched, work));
483}
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/**
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800486 * ipc_schedule_free - free ipc + rcu space
487 * @head: RCU callback structure for queued work
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 *
489 * Since RCU callback function is called in bh,
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800490 * we need to defer the vfree to schedule_work().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 */
492static void ipc_schedule_free(struct rcu_head *head)
493{
494 struct ipc_rcu_grace *grace =
495 container_of(head, struct ipc_rcu_grace, rcu);
496 struct ipc_rcu_sched *sched =
497 container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]);
498
David Howells65f27f32006-11-22 14:55:48 +0000499 INIT_WORK(&sched->work, ipc_do_vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 schedule_work(&sched->work);
501}
502
503/**
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800504 * ipc_immediate_free - free ipc + rcu space
505 * @head: RCU callback structure that contains pointer to be freed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800507 * Free from the RCU callback context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 */
509static void ipc_immediate_free(struct rcu_head *head)
510{
511 struct ipc_rcu_grace *free =
512 container_of(head, struct ipc_rcu_grace, rcu);
513 kfree(free);
514}
515
516void ipc_rcu_putref(void *ptr)
517{
518 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
519 return;
520
521 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
522 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
523 ipc_schedule_free);
524 } else {
525 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
526 ipc_immediate_free);
527 }
528}
529
530/**
531 * ipcperms - check IPC permissions
532 * @ipcp: IPC permission set
533 * @flag: desired permission set.
534 *
535 * Check user, group, other permissions for access
536 * to ipc resources. return 0 if allowed
537 */
538
539int ipcperms (struct kern_ipc_perm *ipcp, short flag)
540{ /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
Steve Grubb073115d2006-04-02 17:07:33 -0400541 int requested_mode, granted_mode, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Steve Grubb073115d2006-04-02 17:07:33 -0400543 if (unlikely((err = audit_ipc_obj(ipcp))))
544 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 requested_mode = (flag >> 6) | (flag >> 3) | flag;
546 granted_mode = ipcp->mode;
547 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
548 granted_mode >>= 6;
549 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
550 granted_mode >>= 3;
551 /* is there some bit set in requested_mode but not in granted_mode? */
552 if ((requested_mode & ~granted_mode & 0007) &&
553 !capable(CAP_IPC_OWNER))
554 return -1;
555
556 return security_ipc_permission(ipcp, flag);
557}
558
559/*
560 * Functions to convert between the kern_ipc_perm structure and the
561 * old/new ipc_perm structures
562 */
563
564/**
565 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
566 * @in: kernel permissions
567 * @out: new style IPC permissions
568 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800569 * Turn the kernel object @in into a set of permissions descriptions
570 * for returning to userspace (@out).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 */
572
573
574void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
575{
576 out->key = in->key;
577 out->uid = in->uid;
578 out->gid = in->gid;
579 out->cuid = in->cuid;
580 out->cgid = in->cgid;
581 out->mode = in->mode;
582 out->seq = in->seq;
583}
584
585/**
586 * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
587 * @in: new style IPC permissions
588 * @out: old style IPC permissions
589 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800590 * Turn the new style permissions object @in into a compatibility
591 * object and store it into the @out pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 */
593
594void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
595{
596 out->key = in->key;
597 SET_UID(out->uid, in->uid);
598 SET_GID(out->gid, in->gid);
599 SET_UID(out->cuid, in->cuid);
600 SET_GID(out->cgid, in->cgid);
601 out->mode = in->mode;
602 out->seq = in->seq;
603}
604
605/*
606 * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800607 * is called with shm_ids.mutex locked. Since grow_ary() is also called with
608 * shm_ids.mutex down(for Shared Memory), there is no need to add read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 * barriers here to gurantee the writes in grow_ary() are seen in order
610 * here (for Alpha).
611 *
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800612 * However ipc_get() itself does not necessary require ipc_ids.mutex down. So
613 * if in the future ipc_get() is used by other places without ipc_ids.mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * down, then ipc_get() needs read memery barriers as ipc_lock() does.
615 */
616struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
617{
618 struct kern_ipc_perm* out;
619 int lid = id % SEQ_MULTIPLIER;
620 if(lid >= ids->entries->size)
621 return NULL;
622 out = ids->entries->p[lid];
623 return out;
624}
625
626struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
627{
628 struct kern_ipc_perm* out;
629 int lid = id % SEQ_MULTIPLIER;
630 struct ipc_id_ary* entries;
631
632 rcu_read_lock();
633 entries = rcu_dereference(ids->entries);
634 if(lid >= entries->size) {
635 rcu_read_unlock();
636 return NULL;
637 }
638 out = entries->p[lid];
639 if(out == NULL) {
640 rcu_read_unlock();
641 return NULL;
642 }
643 spin_lock(&out->lock);
644
645 /* ipc_rmid() may have already freed the ID while ipc_lock
646 * was spinning: here verify that the structure is still valid
647 */
648 if (out->deleted) {
649 spin_unlock(&out->lock);
650 rcu_read_unlock();
651 return NULL;
652 }
653 return out;
654}
655
656void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
657{
658 rcu_read_lock();
659 spin_lock(&perm->lock);
660}
661
662void ipc_unlock(struct kern_ipc_perm* perm)
663{
664 spin_unlock(&perm->lock);
665 rcu_read_unlock();
666}
667
668int ipc_buildid(struct ipc_ids* ids, int id, int seq)
669{
670 return SEQ_MULTIPLIER*seq + id;
671}
672
673int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
674{
675 if(uid/SEQ_MULTIPLIER != ipcp->seq)
676 return 1;
677 return 0;
678}
679
680#ifdef __ARCH_WANT_IPC_PARSE_VERSION
681
682
683/**
684 * ipc_parse_version - IPC call version
685 * @cmd: pointer to command
686 *
687 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800688 * The @cmd value is turned from an encoding command and version into
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 * just the command code.
690 */
691
692int ipc_parse_version (int *cmd)
693{
694 if (*cmd & IPC_64) {
695 *cmd ^= IPC_64;
696 return IPC_64;
697 } else {
698 return IPC_OLD;
699 }
700}
701
702#endif /* __ARCH_WANT_IPC_PARSE_VERSION */
Mike Waychisonae781772005-09-06 15:17:09 -0700703
704#ifdef CONFIG_PROC_FS
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800705struct ipc_proc_iter {
706 struct ipc_namespace *ns;
707 struct ipc_proc_iface *iface;
708};
709
Mike Waychisonae781772005-09-06 15:17:09 -0700710static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
711{
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800712 struct ipc_proc_iter *iter = s->private;
713 struct ipc_proc_iface *iface = iter->iface;
Mike Waychisonae781772005-09-06 15:17:09 -0700714 struct kern_ipc_perm *ipc = it;
715 loff_t p;
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700716 struct ipc_ids *ids;
717
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800718 ids = iter->ns->ids[iface->ids];
Mike Waychisonae781772005-09-06 15:17:09 -0700719
720 /* If we had an ipc id locked before, unlock it */
721 if (ipc && ipc != SEQ_START_TOKEN)
722 ipc_unlock(ipc);
723
724 /*
725 * p = *pos - 1 (because id 0 starts at position 1)
726 * + 1 (because we increment the position by one)
727 */
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700728 for (p = *pos; p <= ids->max_id; p++) {
729 if ((ipc = ipc_lock(ids, p)) != NULL) {
Mike Waychisonae781772005-09-06 15:17:09 -0700730 *pos = p + 1;
731 return ipc;
732 }
733 }
734
735 /* Out of range - return NULL to terminate iteration */
736 return NULL;
737}
738
739/*
740 * File positions: pos 0 -> header, pos n -> ipc id + 1.
741 * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START.
742 */
743static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
744{
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800745 struct ipc_proc_iter *iter = s->private;
746 struct ipc_proc_iface *iface = iter->iface;
Mike Waychisonae781772005-09-06 15:17:09 -0700747 struct kern_ipc_perm *ipc;
748 loff_t p;
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700749 struct ipc_ids *ids;
750
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800751 ids = iter->ns->ids[iface->ids];
Mike Waychisonae781772005-09-06 15:17:09 -0700752
753 /*
754 * Take the lock - this will be released by the corresponding
755 * call to stop().
756 */
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700757 mutex_lock(&ids->mutex);
Mike Waychisonae781772005-09-06 15:17:09 -0700758
759 /* pos < 0 is invalid */
760 if (*pos < 0)
761 return NULL;
762
763 /* pos == 0 means header */
764 if (*pos == 0)
765 return SEQ_START_TOKEN;
766
767 /* Find the (pos-1)th ipc */
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700768 for (p = *pos - 1; p <= ids->max_id; p++) {
769 if ((ipc = ipc_lock(ids, p)) != NULL) {
Mike Waychisonae781772005-09-06 15:17:09 -0700770 *pos = p + 1;
771 return ipc;
772 }
773 }
774 return NULL;
775}
776
777static void sysvipc_proc_stop(struct seq_file *s, void *it)
778{
779 struct kern_ipc_perm *ipc = it;
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800780 struct ipc_proc_iter *iter = s->private;
781 struct ipc_proc_iface *iface = iter->iface;
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700782 struct ipc_ids *ids;
Mike Waychisonae781772005-09-06 15:17:09 -0700783
784 /* If we had a locked segment, release it */
785 if (ipc && ipc != SEQ_START_TOKEN)
786 ipc_unlock(ipc);
787
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800788 ids = iter->ns->ids[iface->ids];
Mike Waychisonae781772005-09-06 15:17:09 -0700789 /* Release the lock we took in start() */
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700790 mutex_unlock(&ids->mutex);
Mike Waychisonae781772005-09-06 15:17:09 -0700791}
792
793static int sysvipc_proc_show(struct seq_file *s, void *it)
794{
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800795 struct ipc_proc_iter *iter = s->private;
796 struct ipc_proc_iface *iface = iter->iface;
Mike Waychisonae781772005-09-06 15:17:09 -0700797
798 if (it == SEQ_START_TOKEN)
799 return seq_puts(s, iface->header);
800
801 return iface->show(s, it);
802}
803
804static struct seq_operations sysvipc_proc_seqops = {
805 .start = sysvipc_proc_start,
806 .stop = sysvipc_proc_stop,
807 .next = sysvipc_proc_next,
808 .show = sysvipc_proc_show,
809};
810
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800811static int sysvipc_proc_open(struct inode *inode, struct file *file)
812{
Mike Waychisonae781772005-09-06 15:17:09 -0700813 int ret;
814 struct seq_file *seq;
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800815 struct ipc_proc_iter *iter;
816
817 ret = -ENOMEM;
818 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
819 if (!iter)
820 goto out;
Mike Waychisonae781772005-09-06 15:17:09 -0700821
822 ret = seq_open(file, &sysvipc_proc_seqops);
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800823 if (ret)
824 goto out_kfree;
825
826 seq = file->private_data;
827 seq->private = iter;
828
829 iter->iface = PDE(inode)->data;
830 iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
831out:
Mike Waychisonae781772005-09-06 15:17:09 -0700832 return ret;
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800833out_kfree:
834 kfree(iter);
835 goto out;
836}
837
838static int sysvipc_proc_release(struct inode *inode, struct file *file)
839{
840 struct seq_file *seq = file->private_data;
841 struct ipc_proc_iter *iter = seq->private;
842 put_ipc_ns(iter->ns);
843 return seq_release_private(inode, file);
Mike Waychisonae781772005-09-06 15:17:09 -0700844}
845
Arjan van de Ven9a321442007-02-12 00:55:35 -0800846static const struct file_operations sysvipc_proc_fops = {
Mike Waychisonae781772005-09-06 15:17:09 -0700847 .open = sysvipc_proc_open,
848 .read = seq_read,
849 .llseek = seq_lseek,
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800850 .release = sysvipc_proc_release,
Mike Waychisonae781772005-09-06 15:17:09 -0700851};
852#endif /* CONFIG_PROC_FS */