ipc: scale msgmni to the number of ipc namespaces
[deliverable/linux.git] / ipc / util.c
1 /*
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
10 * Manfred Spraul <manfred@colorfullife.com>
11 * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
12 * Mingming Cao <cmm@us.ibm.com>
13 * Mar 2006 - support for audit of ipc object properties
14 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
15 * Jun 2006 - namespaces ssupport
16 * OpenVZ, SWsoft Inc.
17 * Pavel Emelianov <xemul@openvz.org>
18 */
19
20 #include <linux/mm.h>
21 #include <linux/shm.h>
22 #include <linux/init.h>
23 #include <linux/msg.h>
24 #include <linux/vmalloc.h>
25 #include <linux/slab.h>
26 #include <linux/capability.h>
27 #include <linux/highuid.h>
28 #include <linux/security.h>
29 #include <linux/rcupdate.h>
30 #include <linux/workqueue.h>
31 #include <linux/seq_file.h>
32 #include <linux/proc_fs.h>
33 #include <linux/audit.h>
34 #include <linux/nsproxy.h>
35 #include <linux/rwsem.h>
36 #include <linux/ipc_namespace.h>
37
38 #include <asm/unistd.h>
39
40 #include "util.h"
41
42 struct ipc_proc_iface {
43 const char *path;
44 const char *header;
45 int ids;
46 int (*show)(struct seq_file *, void *);
47 };
48
49 struct ipc_namespace init_ipc_ns = {
50 .kref = {
51 .refcount = ATOMIC_INIT(2),
52 },
53 };
54
55 atomic_t nr_ipc_ns = ATOMIC_INIT(1);
56
57
58 /**
59 * ipc_init - initialise IPC subsystem
60 *
61 * The various system5 IPC resources (semaphores, messages and shared
62 * memory) are initialised
63 */
64
65 static int __init ipc_init(void)
66 {
67 sem_init();
68 msg_init();
69 shm_init();
70 return 0;
71 }
72 __initcall(ipc_init);
73
74 /**
75 * ipc_init_ids - initialise IPC identifiers
76 * @ids: Identifier set
77 *
78 * Set up the sequence range to use for the ipc identifier range (limited
79 * below IPCMNI) then initialise the ids idr.
80 */
81
82 void ipc_init_ids(struct ipc_ids *ids)
83 {
84 init_rwsem(&ids->rw_mutex);
85
86 ids->in_use = 0;
87 ids->seq = 0;
88 {
89 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
90 if(seq_limit > USHRT_MAX)
91 ids->seq_max = USHRT_MAX;
92 else
93 ids->seq_max = seq_limit;
94 }
95
96 idr_init(&ids->ipcs_idr);
97 }
98
99 #ifdef CONFIG_PROC_FS
100 static const struct file_operations sysvipc_proc_fops;
101 /**
102 * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
103 * @path: Path in procfs
104 * @header: Banner to be printed at the beginning of the file.
105 * @ids: ipc id table to iterate.
106 * @show: show routine.
107 */
108 void __init ipc_init_proc_interface(const char *path, const char *header,
109 int ids, int (*show)(struct seq_file *, void *))
110 {
111 struct proc_dir_entry *pde;
112 struct ipc_proc_iface *iface;
113
114 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
115 if (!iface)
116 return;
117 iface->path = path;
118 iface->header = header;
119 iface->ids = ids;
120 iface->show = show;
121
122 pde = create_proc_entry(path,
123 S_IRUGO, /* world readable */
124 NULL /* parent dir */);
125 if (pde) {
126 pde->data = iface;
127 pde->proc_fops = &sysvipc_proc_fops;
128 } else {
129 kfree(iface);
130 }
131 }
132 #endif
133
134 /**
135 * ipc_findkey - find a key in an ipc identifier set
136 * @ids: Identifier set
137 * @key: The key to find
138 *
139 * Requires ipc_ids.rw_mutex locked.
140 * Returns the LOCKED pointer to the ipc structure if found or NULL
141 * if not.
142 * If key is found ipc points to the owning ipc structure
143 */
144
145 static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
146 {
147 struct kern_ipc_perm *ipc;
148 int next_id;
149 int total;
150
151 for (total = 0, next_id = 0; total < ids->in_use; next_id++) {
152 ipc = idr_find(&ids->ipcs_idr, next_id);
153
154 if (ipc == NULL)
155 continue;
156
157 if (ipc->key != key) {
158 total++;
159 continue;
160 }
161
162 ipc_lock_by_ptr(ipc);
163 return ipc;
164 }
165
166 return NULL;
167 }
168
169 /**
170 * ipc_get_maxid - get the last assigned id
171 * @ids: IPC identifier set
172 *
173 * Called with ipc_ids.rw_mutex held.
174 */
175
176 int ipc_get_maxid(struct ipc_ids *ids)
177 {
178 struct kern_ipc_perm *ipc;
179 int max_id = -1;
180 int total, id;
181
182 if (ids->in_use == 0)
183 return -1;
184
185 if (ids->in_use == IPCMNI)
186 return IPCMNI - 1;
187
188 /* Look for the last assigned id */
189 total = 0;
190 for (id = 0; id < IPCMNI && total < ids->in_use; id++) {
191 ipc = idr_find(&ids->ipcs_idr, id);
192 if (ipc != NULL) {
193 max_id = id;
194 total++;
195 }
196 }
197 return max_id;
198 }
199
200 /**
201 * ipc_addid - add an IPC identifier
202 * @ids: IPC identifier set
203 * @new: new IPC permission set
204 * @size: limit for the number of used ids
205 *
206 * Add an entry 'new' to the IPC ids idr. The permissions object is
207 * initialised and the first free entry is set up and the id assigned
208 * is returned. The 'new' entry is returned in a locked state on success.
209 * On failure the entry is not locked and a negative err-code is returned.
210 *
211 * Called with ipc_ids.rw_mutex held as a writer.
212 */
213
214 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
215 {
216 int id, err;
217
218 if (size > IPCMNI)
219 size = IPCMNI;
220
221 if (ids->in_use >= size)
222 return -ENOSPC;
223
224 err = idr_get_new(&ids->ipcs_idr, new, &id);
225 if (err)
226 return err;
227
228 ids->in_use++;
229
230 new->cuid = new->uid = current->euid;
231 new->gid = new->cgid = current->egid;
232
233 new->seq = ids->seq++;
234 if(ids->seq > ids->seq_max)
235 ids->seq = 0;
236
237 new->id = ipc_buildid(id, new->seq);
238 spin_lock_init(&new->lock);
239 new->deleted = 0;
240 rcu_read_lock();
241 spin_lock(&new->lock);
242 return id;
243 }
244
245 /**
246 * ipcget_new - create a new ipc object
247 * @ns: namespace
248 * @ids: IPC identifer set
249 * @ops: the actual creation routine to call
250 * @params: its parameters
251 *
252 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
253 * when the key is IPC_PRIVATE.
254 */
255 static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
256 struct ipc_ops *ops, struct ipc_params *params)
257 {
258 int err;
259 retry:
260 err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
261
262 if (!err)
263 return -ENOMEM;
264
265 down_write(&ids->rw_mutex);
266 err = ops->getnew(ns, params);
267 up_write(&ids->rw_mutex);
268
269 if (err == -EAGAIN)
270 goto retry;
271
272 return err;
273 }
274
275 /**
276 * ipc_check_perms - check security and permissions for an IPC
277 * @ipcp: ipc permission set
278 * @ops: the actual security routine to call
279 * @params: its parameters
280 *
281 * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
282 * when the key is not IPC_PRIVATE and that key already exists in the
283 * ids IDR.
284 *
285 * On success, the IPC id is returned.
286 *
287 * It is called with ipc_ids.rw_mutex and ipcp->lock held.
288 */
289 static int ipc_check_perms(struct kern_ipc_perm *ipcp, struct ipc_ops *ops,
290 struct ipc_params *params)
291 {
292 int err;
293
294 if (ipcperms(ipcp, params->flg))
295 err = -EACCES;
296 else {
297 err = ops->associate(ipcp, params->flg);
298 if (!err)
299 err = ipcp->id;
300 }
301
302 return err;
303 }
304
305 /**
306 * ipcget_public - get an ipc object or create a new one
307 * @ns: namespace
308 * @ids: IPC identifer set
309 * @ops: the actual creation routine to call
310 * @params: its parameters
311 *
312 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
313 * when the key is not IPC_PRIVATE.
314 * It adds a new entry if the key is not found and does some permission
315 * / security checkings if the key is found.
316 *
317 * On success, the ipc id is returned.
318 */
319 static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
320 struct ipc_ops *ops, struct ipc_params *params)
321 {
322 struct kern_ipc_perm *ipcp;
323 int flg = params->flg;
324 int err;
325 retry:
326 err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
327
328 /*
329 * Take the lock as a writer since we are potentially going to add
330 * a new entry + read locks are not "upgradable"
331 */
332 down_write(&ids->rw_mutex);
333 ipcp = ipc_findkey(ids, params->key);
334 if (ipcp == NULL) {
335 /* key not used */
336 if (!(flg & IPC_CREAT))
337 err = -ENOENT;
338 else if (!err)
339 err = -ENOMEM;
340 else
341 err = ops->getnew(ns, params);
342 } else {
343 /* ipc object has been locked by ipc_findkey() */
344
345 if (flg & IPC_CREAT && flg & IPC_EXCL)
346 err = -EEXIST;
347 else {
348 err = 0;
349 if (ops->more_checks)
350 err = ops->more_checks(ipcp, params);
351 if (!err)
352 /*
353 * ipc_check_perms returns the IPC id on
354 * success
355 */
356 err = ipc_check_perms(ipcp, ops, params);
357 }
358 ipc_unlock(ipcp);
359 }
360 up_write(&ids->rw_mutex);
361
362 if (err == -EAGAIN)
363 goto retry;
364
365 return err;
366 }
367
368
369 /**
370 * ipc_rmid - remove an IPC identifier
371 * @ids: IPC identifier set
372 * @ipcp: ipc perm structure containing the identifier to remove
373 *
374 * ipc_ids.rw_mutex (as a writer) and the spinlock for this ID are held
375 * before this function is called, and remain locked on the exit.
376 */
377
378 void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
379 {
380 int lid = ipcid_to_idx(ipcp->id);
381
382 idr_remove(&ids->ipcs_idr, lid);
383
384 ids->in_use--;
385
386 ipcp->deleted = 1;
387
388 return;
389 }
390
391 /**
392 * ipc_alloc - allocate ipc space
393 * @size: size desired
394 *
395 * Allocate memory from the appropriate pools and return a pointer to it.
396 * NULL is returned if the allocation fails
397 */
398
399 void* ipc_alloc(int size)
400 {
401 void* out;
402 if(size > PAGE_SIZE)
403 out = vmalloc(size);
404 else
405 out = kmalloc(size, GFP_KERNEL);
406 return out;
407 }
408
409 /**
410 * ipc_free - free ipc space
411 * @ptr: pointer returned by ipc_alloc
412 * @size: size of block
413 *
414 * Free a block created with ipc_alloc(). The caller must know the size
415 * used in the allocation call.
416 */
417
418 void ipc_free(void* ptr, int size)
419 {
420 if(size > PAGE_SIZE)
421 vfree(ptr);
422 else
423 kfree(ptr);
424 }
425
426 /*
427 * rcu allocations:
428 * There are three headers that are prepended to the actual allocation:
429 * - during use: ipc_rcu_hdr.
430 * - during the rcu grace period: ipc_rcu_grace.
431 * - [only if vmalloc]: ipc_rcu_sched.
432 * Their lifetime doesn't overlap, thus the headers share the same memory.
433 * Unlike a normal union, they are right-aligned, thus some container_of
434 * forward/backward casting is necessary:
435 */
436 struct ipc_rcu_hdr
437 {
438 int refcount;
439 int is_vmalloc;
440 void *data[0];
441 };
442
443
444 struct ipc_rcu_grace
445 {
446 struct rcu_head rcu;
447 /* "void *" makes sure alignment of following data is sane. */
448 void *data[0];
449 };
450
451 struct ipc_rcu_sched
452 {
453 struct work_struct work;
454 /* "void *" makes sure alignment of following data is sane. */
455 void *data[0];
456 };
457
458 #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
459 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
460 #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
461 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
462
463 static inline int rcu_use_vmalloc(int size)
464 {
465 /* Too big for a single page? */
466 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
467 return 1;
468 return 0;
469 }
470
471 /**
472 * ipc_rcu_alloc - allocate ipc and rcu space
473 * @size: size desired
474 *
475 * Allocate memory for the rcu header structure + the object.
476 * Returns the pointer to the object.
477 * NULL is returned if the allocation fails.
478 */
479
480 void* ipc_rcu_alloc(int size)
481 {
482 void* out;
483 /*
484 * We prepend the allocation with the rcu struct, and
485 * workqueue if necessary (for vmalloc).
486 */
487 if (rcu_use_vmalloc(size)) {
488 out = vmalloc(HDRLEN_VMALLOC + size);
489 if (out) {
490 out += HDRLEN_VMALLOC;
491 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
492 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
493 }
494 } else {
495 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
496 if (out) {
497 out += HDRLEN_KMALLOC;
498 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
499 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
500 }
501 }
502
503 return out;
504 }
505
506 void ipc_rcu_getref(void *ptr)
507 {
508 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
509 }
510
511 static void ipc_do_vfree(struct work_struct *work)
512 {
513 vfree(container_of(work, struct ipc_rcu_sched, work));
514 }
515
516 /**
517 * ipc_schedule_free - free ipc + rcu space
518 * @head: RCU callback structure for queued work
519 *
520 * Since RCU callback function is called in bh,
521 * we need to defer the vfree to schedule_work().
522 */
523 static void ipc_schedule_free(struct rcu_head *head)
524 {
525 struct ipc_rcu_grace *grace;
526 struct ipc_rcu_sched *sched;
527
528 grace = container_of(head, struct ipc_rcu_grace, rcu);
529 sched = container_of(&(grace->data[0]), struct ipc_rcu_sched,
530 data[0]);
531
532 INIT_WORK(&sched->work, ipc_do_vfree);
533 schedule_work(&sched->work);
534 }
535
536 /**
537 * ipc_immediate_free - free ipc + rcu space
538 * @head: RCU callback structure that contains pointer to be freed
539 *
540 * Free from the RCU callback context.
541 */
542 static void ipc_immediate_free(struct rcu_head *head)
543 {
544 struct ipc_rcu_grace *free =
545 container_of(head, struct ipc_rcu_grace, rcu);
546 kfree(free);
547 }
548
549 void ipc_rcu_putref(void *ptr)
550 {
551 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
552 return;
553
554 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
555 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
556 ipc_schedule_free);
557 } else {
558 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
559 ipc_immediate_free);
560 }
561 }
562
563 /**
564 * ipcperms - check IPC permissions
565 * @ipcp: IPC permission set
566 * @flag: desired permission set.
567 *
568 * Check user, group, other permissions for access
569 * to ipc resources. return 0 if allowed
570 */
571
572 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
573 { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
574 int requested_mode, granted_mode, err;
575
576 if (unlikely((err = audit_ipc_obj(ipcp))))
577 return err;
578 requested_mode = (flag >> 6) | (flag >> 3) | flag;
579 granted_mode = ipcp->mode;
580 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
581 granted_mode >>= 6;
582 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
583 granted_mode >>= 3;
584 /* is there some bit set in requested_mode but not in granted_mode? */
585 if ((requested_mode & ~granted_mode & 0007) &&
586 !capable(CAP_IPC_OWNER))
587 return -1;
588
589 return security_ipc_permission(ipcp, flag);
590 }
591
592 /*
593 * Functions to convert between the kern_ipc_perm structure and the
594 * old/new ipc_perm structures
595 */
596
597 /**
598 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
599 * @in: kernel permissions
600 * @out: new style IPC permissions
601 *
602 * Turn the kernel object @in into a set of permissions descriptions
603 * for returning to userspace (@out).
604 */
605
606
607 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
608 {
609 out->key = in->key;
610 out->uid = in->uid;
611 out->gid = in->gid;
612 out->cuid = in->cuid;
613 out->cgid = in->cgid;
614 out->mode = in->mode;
615 out->seq = in->seq;
616 }
617
618 /**
619 * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
620 * @in: new style IPC permissions
621 * @out: old style IPC permissions
622 *
623 * Turn the new style permissions object @in into a compatibility
624 * object and store it into the @out pointer.
625 */
626
627 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
628 {
629 out->key = in->key;
630 SET_UID(out->uid, in->uid);
631 SET_GID(out->gid, in->gid);
632 SET_UID(out->cuid, in->cuid);
633 SET_GID(out->cgid, in->cgid);
634 out->mode = in->mode;
635 out->seq = in->seq;
636 }
637
638 /**
639 * ipc_lock - Lock an ipc structure without rw_mutex held
640 * @ids: IPC identifier set
641 * @id: ipc id to look for
642 *
643 * Look for an id in the ipc ids idr and lock the associated ipc object.
644 *
645 * The ipc object is locked on exit.
646 *
647 * This is the routine that should be called when the rw_mutex is not already
648 * held, i.e. idr tree not protected: it protects the idr tree in read mode
649 * during the idr_find().
650 */
651
652 struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
653 {
654 struct kern_ipc_perm *out;
655 int lid = ipcid_to_idx(id);
656
657 down_read(&ids->rw_mutex);
658
659 rcu_read_lock();
660 out = idr_find(&ids->ipcs_idr, lid);
661 if (out == NULL) {
662 rcu_read_unlock();
663 up_read(&ids->rw_mutex);
664 return ERR_PTR(-EINVAL);
665 }
666
667 up_read(&ids->rw_mutex);
668
669 spin_lock(&out->lock);
670
671 /* ipc_rmid() may have already freed the ID while ipc_lock
672 * was spinning: here verify that the structure is still valid
673 */
674 if (out->deleted) {
675 spin_unlock(&out->lock);
676 rcu_read_unlock();
677 return ERR_PTR(-EINVAL);
678 }
679
680 return out;
681 }
682
683 /**
684 * ipc_lock_down - Lock an ipc structure with rw_sem held
685 * @ids: IPC identifier set
686 * @id: ipc id to look for
687 *
688 * Look for an id in the ipc ids idr and lock the associated ipc object.
689 *
690 * The ipc object is locked on exit.
691 *
692 * This is the routine that should be called when the rw_mutex is already
693 * held, i.e. idr tree protected.
694 */
695
696 struct kern_ipc_perm *ipc_lock_down(struct ipc_ids *ids, int id)
697 {
698 struct kern_ipc_perm *out;
699 int lid = ipcid_to_idx(id);
700
701 rcu_read_lock();
702 out = idr_find(&ids->ipcs_idr, lid);
703 if (out == NULL) {
704 rcu_read_unlock();
705 return ERR_PTR(-EINVAL);
706 }
707
708 spin_lock(&out->lock);
709
710 /*
711 * No need to verify that the structure is still valid since the
712 * rw_mutex is held.
713 */
714 return out;
715 }
716
717 struct kern_ipc_perm *ipc_lock_check_down(struct ipc_ids *ids, int id)
718 {
719 struct kern_ipc_perm *out;
720
721 out = ipc_lock_down(ids, id);
722 if (IS_ERR(out))
723 return out;
724
725 if (ipc_checkid(out, id)) {
726 ipc_unlock(out);
727 return ERR_PTR(-EIDRM);
728 }
729
730 return out;
731 }
732
733 struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids, int id)
734 {
735 struct kern_ipc_perm *out;
736
737 out = ipc_lock(ids, id);
738 if (IS_ERR(out))
739 return out;
740
741 if (ipc_checkid(out, id)) {
742 ipc_unlock(out);
743 return ERR_PTR(-EIDRM);
744 }
745
746 return out;
747 }
748
749 /**
750 * ipcget - Common sys_*get() code
751 * @ns : namsepace
752 * @ids : IPC identifier set
753 * @ops : operations to be called on ipc object creation, permission checks
754 * and further checks
755 * @params : the parameters needed by the previous operations.
756 *
757 * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
758 */
759 int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
760 struct ipc_ops *ops, struct ipc_params *params)
761 {
762 if (params->key == IPC_PRIVATE)
763 return ipcget_new(ns, ids, ops, params);
764 else
765 return ipcget_public(ns, ids, ops, params);
766 }
767
768 #ifdef __ARCH_WANT_IPC_PARSE_VERSION
769
770
771 /**
772 * ipc_parse_version - IPC call version
773 * @cmd: pointer to command
774 *
775 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
776 * The @cmd value is turned from an encoding command and version into
777 * just the command code.
778 */
779
780 int ipc_parse_version (int *cmd)
781 {
782 if (*cmd & IPC_64) {
783 *cmd ^= IPC_64;
784 return IPC_64;
785 } else {
786 return IPC_OLD;
787 }
788 }
789
790 #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
791
792 #ifdef CONFIG_PROC_FS
793 struct ipc_proc_iter {
794 struct ipc_namespace *ns;
795 struct ipc_proc_iface *iface;
796 };
797
798 /*
799 * This routine locks the ipc structure found at least at position pos.
800 */
801 static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
802 loff_t *new_pos)
803 {
804 struct kern_ipc_perm *ipc;
805 int total, id;
806
807 total = 0;
808 for (id = 0; id < pos && total < ids->in_use; id++) {
809 ipc = idr_find(&ids->ipcs_idr, id);
810 if (ipc != NULL)
811 total++;
812 }
813
814 if (total >= ids->in_use)
815 return NULL;
816
817 for ( ; pos < IPCMNI; pos++) {
818 ipc = idr_find(&ids->ipcs_idr, pos);
819 if (ipc != NULL) {
820 *new_pos = pos + 1;
821 ipc_lock_by_ptr(ipc);
822 return ipc;
823 }
824 }
825
826 /* Out of range - return NULL to terminate iteration */
827 return NULL;
828 }
829
830 static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
831 {
832 struct ipc_proc_iter *iter = s->private;
833 struct ipc_proc_iface *iface = iter->iface;
834 struct kern_ipc_perm *ipc = it;
835
836 /* If we had an ipc id locked before, unlock it */
837 if (ipc && ipc != SEQ_START_TOKEN)
838 ipc_unlock(ipc);
839
840 return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
841 }
842
843 /*
844 * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
845 * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
846 */
847 static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
848 {
849 struct ipc_proc_iter *iter = s->private;
850 struct ipc_proc_iface *iface = iter->iface;
851 struct ipc_ids *ids;
852
853 ids = &iter->ns->ids[iface->ids];
854
855 /*
856 * Take the lock - this will be released by the corresponding
857 * call to stop().
858 */
859 down_read(&ids->rw_mutex);
860
861 /* pos < 0 is invalid */
862 if (*pos < 0)
863 return NULL;
864
865 /* pos == 0 means header */
866 if (*pos == 0)
867 return SEQ_START_TOKEN;
868
869 /* Find the (pos-1)th ipc */
870 return sysvipc_find_ipc(ids, *pos - 1, pos);
871 }
872
873 static void sysvipc_proc_stop(struct seq_file *s, void *it)
874 {
875 struct kern_ipc_perm *ipc = it;
876 struct ipc_proc_iter *iter = s->private;
877 struct ipc_proc_iface *iface = iter->iface;
878 struct ipc_ids *ids;
879
880 /* If we had a locked structure, release it */
881 if (ipc && ipc != SEQ_START_TOKEN)
882 ipc_unlock(ipc);
883
884 ids = &iter->ns->ids[iface->ids];
885 /* Release the lock we took in start() */
886 up_read(&ids->rw_mutex);
887 }
888
889 static int sysvipc_proc_show(struct seq_file *s, void *it)
890 {
891 struct ipc_proc_iter *iter = s->private;
892 struct ipc_proc_iface *iface = iter->iface;
893
894 if (it == SEQ_START_TOKEN)
895 return seq_puts(s, iface->header);
896
897 return iface->show(s, it);
898 }
899
900 static struct seq_operations sysvipc_proc_seqops = {
901 .start = sysvipc_proc_start,
902 .stop = sysvipc_proc_stop,
903 .next = sysvipc_proc_next,
904 .show = sysvipc_proc_show,
905 };
906
907 static int sysvipc_proc_open(struct inode *inode, struct file *file)
908 {
909 int ret;
910 struct seq_file *seq;
911 struct ipc_proc_iter *iter;
912
913 ret = -ENOMEM;
914 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
915 if (!iter)
916 goto out;
917
918 ret = seq_open(file, &sysvipc_proc_seqops);
919 if (ret)
920 goto out_kfree;
921
922 seq = file->private_data;
923 seq->private = iter;
924
925 iter->iface = PDE(inode)->data;
926 iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
927 out:
928 return ret;
929 out_kfree:
930 kfree(iter);
931 goto out;
932 }
933
934 static int sysvipc_proc_release(struct inode *inode, struct file *file)
935 {
936 struct seq_file *seq = file->private_data;
937 struct ipc_proc_iter *iter = seq->private;
938 put_ipc_ns(iter->ns);
939 return seq_release_private(inode, file);
940 }
941
942 static const struct file_operations sysvipc_proc_fops = {
943 .open = sysvipc_proc_open,
944 .read = seq_read,
945 .llseek = seq_lseek,
946 .release = sysvipc_proc_release,
947 };
948 #endif /* CONFIG_PROC_FS */
This page took 0.094818 seconds and 5 git commands to generate.