sysvipc: duplicate lock comments wrt ipc_addid()
[cris-mirror.git] / ipc / util.c
blob429c06bdb8ef7936365fd255dccd09cfb84d5ecc
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/ipc/util.c
4 * Copyright (C) 1992 Krishna Balasubramanian
6 * Sep 1997 - Call suser() last after "normal" permission checks so we
7 * get BSD style process accounting right.
8 * Occurs in several places in the IPC code.
9 * Chris Evans, <chris@ferret.lmh.ox.ac.uk>
10 * Nov 1999 - ipc helper functions, unified SMP locking
11 * Manfred Spraul <manfred@colorfullife.com>
12 * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
13 * Mingming Cao <cmm@us.ibm.com>
14 * Mar 2006 - support for audit of ipc object properties
15 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
16 * Jun 2006 - namespaces ssupport
17 * OpenVZ, SWsoft Inc.
18 * Pavel Emelianov <xemul@openvz.org>
20 * General sysv ipc locking scheme:
21 * rcu_read_lock()
22 * obtain the ipc object (kern_ipc_perm) by looking up the id in an idr
23 * tree.
24 * - perform initial checks (capabilities, auditing and permission,
25 * etc).
26 * - perform read-only operations, such as STAT, INFO commands.
27 * acquire the ipc lock (kern_ipc_perm.lock) through
28 * ipc_lock_object()
29 * - perform data updates, such as SET, RMID commands and
30 * mechanism-specific operations (semop/semtimedop,
31 * msgsnd/msgrcv, shmat/shmdt).
32 * drop the ipc lock, through ipc_unlock_object().
33 * rcu_read_unlock()
35 * The ids->rwsem must be taken when:
36 * - creating, removing and iterating the existing entries in ipc
37 * identifier sets.
38 * - iterating through files under /proc/sysvipc/
40 * Note that sems have a special fast path that avoids kern_ipc_perm.lock -
41 * see sem_lock().
44 #include <linux/mm.h>
45 #include <linux/shm.h>
46 #include <linux/init.h>
47 #include <linux/msg.h>
48 #include <linux/vmalloc.h>
49 #include <linux/slab.h>
50 #include <linux/notifier.h>
51 #include <linux/capability.h>
52 #include <linux/highuid.h>
53 #include <linux/security.h>
54 #include <linux/rcupdate.h>
55 #include <linux/workqueue.h>
56 #include <linux/seq_file.h>
57 #include <linux/proc_fs.h>
58 #include <linux/audit.h>
59 #include <linux/nsproxy.h>
60 #include <linux/rwsem.h>
61 #include <linux/memory.h>
62 #include <linux/ipc_namespace.h>
64 #include <asm/unistd.h>
66 #include "util.h"
68 struct ipc_proc_iface {
69 const char *path;
70 const char *header;
71 int ids;
72 int (*show)(struct seq_file *, void *);
75 /**
76 * ipc_init - initialise ipc subsystem
78 * The various sysv ipc resources (semaphores, messages and shared
79 * memory) are initialised.
81 * A callback routine is registered into the memory hotplug notifier
82 * chain: since msgmni scales to lowmem this callback routine will be
83 * called upon successful memory add / remove to recompute msmgni.
85 static int __init ipc_init(void)
87 int err_sem, err_msg;
89 err_sem = sem_init();
90 WARN(err_sem, "ipc: sysv sem_init failed: %d\n", err_sem);
91 err_msg = msg_init();
92 WARN(err_msg, "ipc: sysv msg_init failed: %d\n", err_msg);
93 shm_init();
95 return err_msg ? err_msg : err_sem;
97 device_initcall(ipc_init);
99 static const struct rhashtable_params ipc_kht_params = {
100 .head_offset = offsetof(struct kern_ipc_perm, khtnode),
101 .key_offset = offsetof(struct kern_ipc_perm, key),
102 .key_len = FIELD_SIZEOF(struct kern_ipc_perm, key),
103 .locks_mul = 1,
104 .automatic_shrinking = true,
108 * ipc_init_ids - initialise ipc identifiers
109 * @ids: ipc identifier set
111 * Set up the sequence range to use for the ipc identifier range (limited
112 * below IPCMNI) then initialise the keys hashtable and ids idr.
114 int ipc_init_ids(struct ipc_ids *ids)
116 int err;
117 ids->in_use = 0;
118 ids->seq = 0;
119 init_rwsem(&ids->rwsem);
120 err = rhashtable_init(&ids->key_ht, &ipc_kht_params);
121 if (err)
122 return err;
123 idr_init(&ids->ipcs_idr);
124 ids->tables_initialized = true;
125 #ifdef CONFIG_CHECKPOINT_RESTORE
126 ids->next_id = -1;
127 #endif
128 return 0;
131 #ifdef CONFIG_PROC_FS
132 static const struct file_operations sysvipc_proc_fops;
134 * ipc_init_proc_interface - create a proc interface for sysipc types using a seq_file interface.
135 * @path: Path in procfs
136 * @header: Banner to be printed at the beginning of the file.
137 * @ids: ipc id table to iterate.
138 * @show: show routine.
140 void __init ipc_init_proc_interface(const char *path, const char *header,
141 int ids, int (*show)(struct seq_file *, void *))
143 struct proc_dir_entry *pde;
144 struct ipc_proc_iface *iface;
146 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
147 if (!iface)
148 return;
149 iface->path = path;
150 iface->header = header;
151 iface->ids = ids;
152 iface->show = show;
154 pde = proc_create_data(path,
155 S_IRUGO, /* world readable */
156 NULL, /* parent dir */
157 &sysvipc_proc_fops,
158 iface);
159 if (!pde)
160 kfree(iface);
162 #endif
165 * ipc_findkey - find a key in an ipc identifier set
166 * @ids: ipc identifier set
167 * @key: key to find
169 * Returns the locked pointer to the ipc structure if found or NULL
170 * otherwise. If key is found ipc points to the owning ipc structure
172 * Called with writer ipc_ids.rwsem held.
174 static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
176 struct kern_ipc_perm *ipcp = NULL;
178 if (likely(ids->tables_initialized))
179 ipcp = rhashtable_lookup_fast(&ids->key_ht, &key,
180 ipc_kht_params);
182 if (ipcp) {
183 rcu_read_lock();
184 ipc_lock_object(ipcp);
185 return ipcp;
188 return NULL;
192 * ipc_get_maxid - get the last assigned id
193 * @ids: ipc identifier set
195 * Called with ipc_ids.rwsem held.
197 int ipc_get_maxid(struct ipc_ids *ids)
199 struct kern_ipc_perm *ipc;
200 int max_id = -1;
201 int total, id;
203 if (ids->in_use == 0)
204 return -1;
206 if (ids->in_use == IPCMNI)
207 return IPCMNI - 1;
209 /* Look for the last assigned id */
210 total = 0;
211 for (id = 0; id < IPCMNI && total < ids->in_use; id++) {
212 ipc = idr_find(&ids->ipcs_idr, id);
213 if (ipc != NULL) {
214 max_id = id;
215 total++;
218 return max_id;
221 #ifdef CONFIG_CHECKPOINT_RESTORE
223 * Specify desired id for next allocated IPC object.
225 #define ipc_idr_alloc(ids, new) \
226 idr_alloc(&(ids)->ipcs_idr, (new), \
227 (ids)->next_id < 0 ? 0 : ipcid_to_idx((ids)->next_id),\
228 0, GFP_NOWAIT)
230 static inline int ipc_buildid(int id, struct ipc_ids *ids,
231 struct kern_ipc_perm *new)
233 if (ids->next_id < 0) { /* default, behave as !CHECKPOINT_RESTORE */
234 new->seq = ids->seq++;
235 if (ids->seq > IPCID_SEQ_MAX)
236 ids->seq = 0;
237 } else {
238 new->seq = ipcid_to_seqx(ids->next_id);
239 ids->next_id = -1;
242 return SEQ_MULTIPLIER * new->seq + id;
245 #else
246 #define ipc_idr_alloc(ids, new) \
247 idr_alloc(&(ids)->ipcs_idr, (new), 0, 0, GFP_NOWAIT)
249 static inline int ipc_buildid(int id, struct ipc_ids *ids,
250 struct kern_ipc_perm *new)
252 new->seq = ids->seq++;
253 if (ids->seq > IPCID_SEQ_MAX)
254 ids->seq = 0;
256 return SEQ_MULTIPLIER * new->seq + id;
259 #endif /* CONFIG_CHECKPOINT_RESTORE */
262 * ipc_addid - add an ipc identifier
263 * @ids: ipc identifier set
264 * @new: new ipc permission set
265 * @size: limit for the number of used ids
267 * Add an entry 'new' to the ipc ids idr. The permissions object is
268 * initialised and the first free entry is set up and the id assigned
269 * is returned. The 'new' entry is returned in a locked state on success.
270 * On failure the entry is not locked and a negative err-code is returned.
272 * Called with writer ipc_ids.rwsem held.
274 int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int size)
276 kuid_t euid;
277 kgid_t egid;
278 int id, err;
280 if (size > IPCMNI)
281 size = IPCMNI;
283 if (!ids->tables_initialized || ids->in_use >= size)
284 return -ENOSPC;
286 idr_preload(GFP_KERNEL);
288 refcount_set(&new->refcount, 1);
289 spin_lock_init(&new->lock);
290 new->deleted = false;
291 rcu_read_lock();
292 spin_lock(&new->lock);
294 current_euid_egid(&euid, &egid);
295 new->cuid = new->uid = euid;
296 new->gid = new->cgid = egid;
298 id = ipc_idr_alloc(ids, new);
299 idr_preload_end();
301 if (id >= 0 && new->key != IPC_PRIVATE) {
302 err = rhashtable_insert_fast(&ids->key_ht, &new->khtnode,
303 ipc_kht_params);
304 if (err < 0) {
305 idr_remove(&ids->ipcs_idr, id);
306 id = err;
309 if (id < 0) {
310 spin_unlock(&new->lock);
311 rcu_read_unlock();
312 return id;
315 ids->in_use++;
316 new->id = ipc_buildid(id, ids, new);
318 return id;
322 * ipcget_new - create a new ipc object
323 * @ns: ipc namespace
324 * @ids: ipc identifier set
325 * @ops: the actual creation routine to call
326 * @params: its parameters
328 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
329 * when the key is IPC_PRIVATE.
331 static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
332 const struct ipc_ops *ops, struct ipc_params *params)
334 int err;
336 down_write(&ids->rwsem);
337 err = ops->getnew(ns, params);
338 up_write(&ids->rwsem);
339 return err;
343 * ipc_check_perms - check security and permissions for an ipc object
344 * @ns: ipc namespace
345 * @ipcp: ipc permission set
346 * @ops: the actual security routine to call
347 * @params: its parameters
349 * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
350 * when the key is not IPC_PRIVATE and that key already exists in the
351 * ds IDR.
353 * On success, the ipc id is returned.
355 * It is called with ipc_ids.rwsem and ipcp->lock held.
357 static int ipc_check_perms(struct ipc_namespace *ns,
358 struct kern_ipc_perm *ipcp,
359 const struct ipc_ops *ops,
360 struct ipc_params *params)
362 int err;
364 if (ipcperms(ns, ipcp, params->flg))
365 err = -EACCES;
366 else {
367 err = ops->associate(ipcp, params->flg);
368 if (!err)
369 err = ipcp->id;
372 return err;
376 * ipcget_public - get an ipc object or create a new one
377 * @ns: ipc namespace
378 * @ids: ipc identifier set
379 * @ops: the actual creation routine to call
380 * @params: its parameters
382 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
383 * when the key is not IPC_PRIVATE.
384 * It adds a new entry if the key is not found and does some permission
385 * / security checkings if the key is found.
387 * On success, the ipc id is returned.
389 static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
390 const struct ipc_ops *ops, struct ipc_params *params)
392 struct kern_ipc_perm *ipcp;
393 int flg = params->flg;
394 int err;
397 * Take the lock as a writer since we are potentially going to add
398 * a new entry + read locks are not "upgradable"
400 down_write(&ids->rwsem);
401 ipcp = ipc_findkey(ids, params->key);
402 if (ipcp == NULL) {
403 /* key not used */
404 if (!(flg & IPC_CREAT))
405 err = -ENOENT;
406 else
407 err = ops->getnew(ns, params);
408 } else {
409 /* ipc object has been locked by ipc_findkey() */
411 if (flg & IPC_CREAT && flg & IPC_EXCL)
412 err = -EEXIST;
413 else {
414 err = 0;
415 if (ops->more_checks)
416 err = ops->more_checks(ipcp, params);
417 if (!err)
419 * ipc_check_perms returns the IPC id on
420 * success
422 err = ipc_check_perms(ns, ipcp, ops, params);
424 ipc_unlock(ipcp);
426 up_write(&ids->rwsem);
428 return err;
432 * ipc_kht_remove - remove an ipc from the key hashtable
433 * @ids: ipc identifier set
434 * @ipcp: ipc perm structure containing the key to remove
436 * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
437 * before this function is called, and remain locked on the exit.
439 static void ipc_kht_remove(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
441 if (ipcp->key != IPC_PRIVATE)
442 rhashtable_remove_fast(&ids->key_ht, &ipcp->khtnode,
443 ipc_kht_params);
447 * ipc_rmid - remove an ipc identifier
448 * @ids: ipc identifier set
449 * @ipcp: ipc perm structure containing the identifier to remove
451 * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
452 * before this function is called, and remain locked on the exit.
454 void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
456 int lid = ipcid_to_idx(ipcp->id);
458 idr_remove(&ids->ipcs_idr, lid);
459 ipc_kht_remove(ids, ipcp);
460 ids->in_use--;
461 ipcp->deleted = true;
465 * ipc_set_key_private - switch the key of an existing ipc to IPC_PRIVATE
466 * @ids: ipc identifier set
467 * @ipcp: ipc perm structure containing the key to modify
469 * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
470 * before this function is called, and remain locked on the exit.
472 void ipc_set_key_private(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
474 ipc_kht_remove(ids, ipcp);
475 ipcp->key = IPC_PRIVATE;
478 int ipc_rcu_getref(struct kern_ipc_perm *ptr)
480 return refcount_inc_not_zero(&ptr->refcount);
483 void ipc_rcu_putref(struct kern_ipc_perm *ptr,
484 void (*func)(struct rcu_head *head))
486 if (!refcount_dec_and_test(&ptr->refcount))
487 return;
489 call_rcu(&ptr->rcu, func);
493 * ipcperms - check ipc permissions
494 * @ns: ipc namespace
495 * @ipcp: ipc permission set
496 * @flag: desired permission set
498 * Check user, group, other permissions for access
499 * to ipc resources. return 0 if allowed
501 * @flag will most probably be 0 or ``S_...UGO`` from <linux/stat.h>
503 int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flag)
505 kuid_t euid = current_euid();
506 int requested_mode, granted_mode;
508 audit_ipc_obj(ipcp);
509 requested_mode = (flag >> 6) | (flag >> 3) | flag;
510 granted_mode = ipcp->mode;
511 if (uid_eq(euid, ipcp->cuid) ||
512 uid_eq(euid, ipcp->uid))
513 granted_mode >>= 6;
514 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
515 granted_mode >>= 3;
516 /* is there some bit set in requested_mode but not in granted_mode? */
517 if ((requested_mode & ~granted_mode & 0007) &&
518 !ns_capable(ns->user_ns, CAP_IPC_OWNER))
519 return -1;
521 return security_ipc_permission(ipcp, flag);
525 * Functions to convert between the kern_ipc_perm structure and the
526 * old/new ipc_perm structures
530 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
531 * @in: kernel permissions
532 * @out: new style ipc permissions
534 * Turn the kernel object @in into a set of permissions descriptions
535 * for returning to userspace (@out).
537 void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out)
539 out->key = in->key;
540 out->uid = from_kuid_munged(current_user_ns(), in->uid);
541 out->gid = from_kgid_munged(current_user_ns(), in->gid);
542 out->cuid = from_kuid_munged(current_user_ns(), in->cuid);
543 out->cgid = from_kgid_munged(current_user_ns(), in->cgid);
544 out->mode = in->mode;
545 out->seq = in->seq;
549 * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
550 * @in: new style ipc permissions
551 * @out: old style ipc permissions
553 * Turn the new style permissions object @in into a compatibility
554 * object and store it into the @out pointer.
556 void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out)
558 out->key = in->key;
559 SET_UID(out->uid, in->uid);
560 SET_GID(out->gid, in->gid);
561 SET_UID(out->cuid, in->cuid);
562 SET_GID(out->cgid, in->cgid);
563 out->mode = in->mode;
564 out->seq = in->seq;
568 * ipc_obtain_object_idr
569 * @ids: ipc identifier set
570 * @id: ipc id to look for
572 * Look for an id in the ipc ids idr and return associated ipc object.
574 * Call inside the RCU critical section.
575 * The ipc object is *not* locked on exit.
577 struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id)
579 struct kern_ipc_perm *out;
580 int lid = ipcid_to_idx(id);
582 if (unlikely(!ids->tables_initialized))
583 return ERR_PTR(-EINVAL);
585 out = idr_find(&ids->ipcs_idr, lid);
586 if (!out)
587 return ERR_PTR(-EINVAL);
589 return out;
593 * ipc_lock - lock an ipc structure without rwsem held
594 * @ids: ipc identifier set
595 * @id: ipc id to look for
597 * Look for an id in the ipc ids idr and lock the associated ipc object.
599 * The ipc object is locked on successful exit.
601 struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
603 struct kern_ipc_perm *out;
605 rcu_read_lock();
606 out = ipc_obtain_object_idr(ids, id);
607 if (IS_ERR(out))
608 goto err;
610 spin_lock(&out->lock);
613 * ipc_rmid() may have already freed the ID while ipc_lock()
614 * was spinning: here verify that the structure is still valid.
615 * Upon races with RMID, return -EIDRM, thus indicating that
616 * the ID points to a removed identifier.
618 if (ipc_valid_object(out))
619 return out;
621 spin_unlock(&out->lock);
622 out = ERR_PTR(-EIDRM);
623 err:
624 rcu_read_unlock();
625 return out;
629 * ipc_obtain_object_check
630 * @ids: ipc identifier set
631 * @id: ipc id to look for
633 * Similar to ipc_obtain_object_idr() but also checks
634 * the ipc object reference counter.
636 * Call inside the RCU critical section.
637 * The ipc object is *not* locked on exit.
639 struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id)
641 struct kern_ipc_perm *out = ipc_obtain_object_idr(ids, id);
643 if (IS_ERR(out))
644 goto out;
646 if (ipc_checkid(out, id))
647 return ERR_PTR(-EINVAL);
648 out:
649 return out;
653 * ipcget - Common sys_*get() code
654 * @ns: namespace
655 * @ids: ipc identifier set
656 * @ops: operations to be called on ipc object creation, permission checks
657 * and further checks
658 * @params: the parameters needed by the previous operations.
660 * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
662 int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
663 const struct ipc_ops *ops, struct ipc_params *params)
665 if (params->key == IPC_PRIVATE)
666 return ipcget_new(ns, ids, ops, params);
667 else
668 return ipcget_public(ns, ids, ops, params);
672 * ipc_update_perm - update the permissions of an ipc object
673 * @in: the permission given as input.
674 * @out: the permission of the ipc to set.
676 int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
678 kuid_t uid = make_kuid(current_user_ns(), in->uid);
679 kgid_t gid = make_kgid(current_user_ns(), in->gid);
680 if (!uid_valid(uid) || !gid_valid(gid))
681 return -EINVAL;
683 out->uid = uid;
684 out->gid = gid;
685 out->mode = (out->mode & ~S_IRWXUGO)
686 | (in->mode & S_IRWXUGO);
688 return 0;
692 * ipcctl_pre_down_nolock - retrieve an ipc and check permissions for some IPC_XXX cmd
693 * @ns: ipc namespace
694 * @ids: the table of ids where to look for the ipc
695 * @id: the id of the ipc to retrieve
696 * @cmd: the cmd to check
697 * @perm: the permission to set
698 * @extra_perm: one extra permission parameter used by msq
700 * This function does some common audit and permissions check for some IPC_XXX
701 * cmd and is called from semctl_down, shmctl_down and msgctl_down.
702 * It must be called without any lock held and:
704 * - retrieves the ipc with the given id in the given table.
705 * - performs some audit and permission check, depending on the given cmd
706 * - returns a pointer to the ipc object or otherwise, the corresponding
707 * error.
709 * Call holding the both the rwsem and the rcu read lock.
711 struct kern_ipc_perm *ipcctl_pre_down_nolock(struct ipc_namespace *ns,
712 struct ipc_ids *ids, int id, int cmd,
713 struct ipc64_perm *perm, int extra_perm)
715 kuid_t euid;
716 int err = -EPERM;
717 struct kern_ipc_perm *ipcp;
719 ipcp = ipc_obtain_object_check(ids, id);
720 if (IS_ERR(ipcp)) {
721 err = PTR_ERR(ipcp);
722 goto err;
725 audit_ipc_obj(ipcp);
726 if (cmd == IPC_SET)
727 audit_ipc_set_perm(extra_perm, perm->uid,
728 perm->gid, perm->mode);
730 euid = current_euid();
731 if (uid_eq(euid, ipcp->cuid) || uid_eq(euid, ipcp->uid) ||
732 ns_capable(ns->user_ns, CAP_SYS_ADMIN))
733 return ipcp; /* successful lookup */
734 err:
735 return ERR_PTR(err);
738 #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
742 * ipc_parse_version - ipc call version
743 * @cmd: pointer to command
745 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
746 * The @cmd value is turned from an encoding command and version into
747 * just the command code.
749 int ipc_parse_version(int *cmd)
751 if (*cmd & IPC_64) {
752 *cmd ^= IPC_64;
753 return IPC_64;
754 } else {
755 return IPC_OLD;
759 #endif /* CONFIG_ARCH_WANT_IPC_PARSE_VERSION */
761 #ifdef CONFIG_PROC_FS
762 struct ipc_proc_iter {
763 struct ipc_namespace *ns;
764 struct ipc_proc_iface *iface;
768 * This routine locks the ipc structure found at least at position pos.
770 static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
771 loff_t *new_pos)
773 struct kern_ipc_perm *ipc;
774 int total, id;
776 total = 0;
777 for (id = 0; id < pos && total < ids->in_use; id++) {
778 ipc = idr_find(&ids->ipcs_idr, id);
779 if (ipc != NULL)
780 total++;
783 if (total >= ids->in_use)
784 return NULL;
786 for (; pos < IPCMNI; pos++) {
787 ipc = idr_find(&ids->ipcs_idr, pos);
788 if (ipc != NULL) {
789 *new_pos = pos + 1;
790 rcu_read_lock();
791 ipc_lock_object(ipc);
792 return ipc;
796 /* Out of range - return NULL to terminate iteration */
797 return NULL;
800 static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
802 struct ipc_proc_iter *iter = s->private;
803 struct ipc_proc_iface *iface = iter->iface;
804 struct kern_ipc_perm *ipc = it;
806 /* If we had an ipc id locked before, unlock it */
807 if (ipc && ipc != SEQ_START_TOKEN)
808 ipc_unlock(ipc);
810 return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
814 * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
815 * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
817 static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
819 struct ipc_proc_iter *iter = s->private;
820 struct ipc_proc_iface *iface = iter->iface;
821 struct ipc_ids *ids;
823 ids = &iter->ns->ids[iface->ids];
826 * Take the lock - this will be released by the corresponding
827 * call to stop().
829 down_read(&ids->rwsem);
831 /* pos < 0 is invalid */
832 if (*pos < 0)
833 return NULL;
835 /* pos == 0 means header */
836 if (*pos == 0)
837 return SEQ_START_TOKEN;
839 /* Find the (pos-1)th ipc */
840 return sysvipc_find_ipc(ids, *pos - 1, pos);
843 static void sysvipc_proc_stop(struct seq_file *s, void *it)
845 struct kern_ipc_perm *ipc = it;
846 struct ipc_proc_iter *iter = s->private;
847 struct ipc_proc_iface *iface = iter->iface;
848 struct ipc_ids *ids;
850 /* If we had a locked structure, release it */
851 if (ipc && ipc != SEQ_START_TOKEN)
852 ipc_unlock(ipc);
854 ids = &iter->ns->ids[iface->ids];
855 /* Release the lock we took in start() */
856 up_read(&ids->rwsem);
859 static int sysvipc_proc_show(struct seq_file *s, void *it)
861 struct ipc_proc_iter *iter = s->private;
862 struct ipc_proc_iface *iface = iter->iface;
864 if (it == SEQ_START_TOKEN) {
865 seq_puts(s, iface->header);
866 return 0;
869 return iface->show(s, it);
872 static const struct seq_operations sysvipc_proc_seqops = {
873 .start = sysvipc_proc_start,
874 .stop = sysvipc_proc_stop,
875 .next = sysvipc_proc_next,
876 .show = sysvipc_proc_show,
879 static int sysvipc_proc_open(struct inode *inode, struct file *file)
881 struct ipc_proc_iter *iter;
883 iter = __seq_open_private(file, &sysvipc_proc_seqops, sizeof(*iter));
884 if (!iter)
885 return -ENOMEM;
887 iter->iface = PDE_DATA(inode);
888 iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
890 return 0;
893 static int sysvipc_proc_release(struct inode *inode, struct file *file)
895 struct seq_file *seq = file->private_data;
896 struct ipc_proc_iter *iter = seq->private;
897 put_ipc_ns(iter->ns);
898 return seq_release_private(inode, file);
901 static const struct file_operations sysvipc_proc_fops = {
902 .open = sysvipc_proc_open,
903 .read = seq_read,
904 .llseek = seq_lseek,
905 .release = sysvipc_proc_release,
907 #endif /* CONFIG_PROC_FS */