util.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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. #include <linux/mm.h>
  20. #include <linux/shm.h>
  21. #include <linux/init.h>
  22. #include <linux/msg.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/slab.h>
  25. #include <linux/capability.h>
  26. #include <linux/highuid.h>
  27. #include <linux/security.h>
  28. #include <linux/rcupdate.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/audit.h>
  33. #include <linux/nsproxy.h>
  34. #include <asm/unistd.h>
  35. #include "util.h"
  36. struct ipc_proc_iface {
  37. const char *path;
  38. const char *header;
  39. int ids;
  40. int (*show)(struct seq_file *, void *);
  41. };
  42. struct ipc_namespace init_ipc_ns = {
  43. .kref = {
  44. .refcount = ATOMIC_INIT(2),
  45. },
  46. };
  47. static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)
  48. {
  49. int err;
  50. struct ipc_namespace *ns;
  51. err = -ENOMEM;
  52. ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
  53. if (ns == NULL)
  54. goto err_mem;
  55. err = sem_init_ns(ns);
  56. if (err)
  57. goto err_sem;
  58. err = msg_init_ns(ns);
  59. if (err)
  60. goto err_msg;
  61. err = shm_init_ns(ns);
  62. if (err)
  63. goto err_shm;
  64. kref_init(&ns->kref);
  65. return ns;
  66. err_shm:
  67. msg_exit_ns(ns);
  68. err_msg:
  69. sem_exit_ns(ns);
  70. err_sem:
  71. kfree(ns);
  72. err_mem:
  73. return ERR_PTR(err);
  74. }
  75. struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
  76. {
  77. struct ipc_namespace *new_ns;
  78. BUG_ON(!ns);
  79. get_ipc_ns(ns);
  80. if (!(flags & CLONE_NEWIPC))
  81. return ns;
  82. new_ns = clone_ipc_ns(ns);
  83. put_ipc_ns(ns);
  84. return new_ns;
  85. }
  86. void free_ipc_ns(struct kref *kref)
  87. {
  88. struct ipc_namespace *ns;
  89. ns = container_of(kref, struct ipc_namespace, kref);
  90. sem_exit_ns(ns);
  91. msg_exit_ns(ns);
  92. shm_exit_ns(ns);
  93. kfree(ns);
  94. }
  95. /**
  96. * ipc_init - initialise IPC subsystem
  97. *
  98. * The various system5 IPC resources (semaphores, messages and shared
  99. * memory) are initialised
  100. */
  101. static int __init ipc_init(void)
  102. {
  103. sem_init();
  104. msg_init();
  105. shm_init();
  106. return 0;
  107. }
  108. __initcall(ipc_init);
  109. /**
  110. * ipc_init_ids - initialise IPC identifiers
  111. * @ids: Identifier set
  112. *
  113. * Set up the sequence range to use for the ipc identifier range (limited
  114. * below IPCMNI) then initialise the ids idr.
  115. */
  116. void ipc_init_ids(struct ipc_ids *ids)
  117. {
  118. mutex_init(&ids->mutex);
  119. ids->in_use = 0;
  120. ids->seq = 0;
  121. {
  122. int seq_limit = INT_MAX/SEQ_MULTIPLIER;
  123. if(seq_limit > USHRT_MAX)
  124. ids->seq_max = USHRT_MAX;
  125. else
  126. ids->seq_max = seq_limit;
  127. }
  128. idr_init(&ids->ipcs_idr);
  129. }
  130. #ifdef CONFIG_PROC_FS
  131. static const struct file_operations sysvipc_proc_fops;
  132. /**
  133. * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
  134. * @path: Path in procfs
  135. * @header: Banner to be printed at the beginning of the file.
  136. * @ids: ipc id table to iterate.
  137. * @show: show routine.
  138. */
  139. void __init ipc_init_proc_interface(const char *path, const char *header,
  140. int ids, int (*show)(struct seq_file *, void *))
  141. {
  142. struct proc_dir_entry *pde;
  143. struct ipc_proc_iface *iface;
  144. iface = kmalloc(sizeof(*iface), GFP_KERNEL);
  145. if (!iface)
  146. return;
  147. iface->path = path;
  148. iface->header = header;
  149. iface->ids = ids;
  150. iface->show = show;
  151. pde = create_proc_entry(path,
  152. S_IRUGO, /* world readable */
  153. NULL /* parent dir */);
  154. if (pde) {
  155. pde->data = iface;
  156. pde->proc_fops = &sysvipc_proc_fops;
  157. } else {
  158. kfree(iface);
  159. }
  160. }
  161. #endif
  162. /**
  163. * ipc_findkey - find a key in an ipc identifier set
  164. * @ids: Identifier set
  165. * @key: The key to find
  166. *
  167. * Requires ipc_ids.mutex locked.
  168. * Returns the LOCKED pointer to the ipc structure if found or NULL
  169. * if not.
  170. * If key is found ipc contains its ipc structure
  171. */
  172. struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
  173. {
  174. struct kern_ipc_perm *ipc;
  175. int next_id;
  176. int total;
  177. for (total = 0, next_id = 0; total < ids->in_use; next_id++) {
  178. ipc = idr_find(&ids->ipcs_idr, next_id);
  179. if (ipc == NULL)
  180. continue;
  181. if (ipc->key != key) {
  182. total++;
  183. continue;
  184. }
  185. ipc_lock_by_ptr(ipc);
  186. return ipc;
  187. }
  188. return NULL;
  189. }
  190. /**
  191. * ipc_get_maxid - get the last assigned id
  192. * @ids: IPC identifier set
  193. *
  194. * Called with ipc_ids.mutex held.
  195. */
  196. int ipc_get_maxid(struct ipc_ids *ids)
  197. {
  198. struct kern_ipc_perm *ipc;
  199. int max_id = -1;
  200. int total, id;
  201. if (ids->in_use == 0)
  202. return -1;
  203. if (ids->in_use == IPCMNI)
  204. return IPCMNI - 1;
  205. /* Look for the last assigned id */
  206. total = 0;
  207. for (id = 0; id < IPCMNI && total < ids->in_use; id++) {
  208. ipc = idr_find(&ids->ipcs_idr, id);
  209. if (ipc != NULL) {
  210. max_id = id;
  211. total++;
  212. }
  213. }
  214. return max_id;
  215. }
  216. /**
  217. * ipc_addid - add an IPC identifier
  218. * @ids: IPC identifier set
  219. * @new: new IPC permission set
  220. * @size: limit for the number of used ids
  221. *
  222. * Add an entry 'new' to the IPC idr. The permissions object is
  223. * initialised and the first free entry is set up and the id assigned
  224. * is returned. The list is returned in a locked state on success.
  225. * On failure the list is not locked and -1 is returned.
  226. *
  227. * Called with ipc_ids.mutex held.
  228. */
  229. int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
  230. {
  231. int id, err;
  232. /*
  233. * rcu_dereference()() is not needed here since
  234. * ipc_ids.mutex is held
  235. */
  236. if (size > IPCMNI)
  237. size = IPCMNI;
  238. if (ids->in_use >= size)
  239. return -1;
  240. err = idr_get_new(&ids->ipcs_idr, new, &id);
  241. if (err)
  242. return -1;
  243. ids->in_use++;
  244. new->cuid = new->uid = current->euid;
  245. new->gid = new->cgid = current->egid;
  246. new->seq = ids->seq++;
  247. if(ids->seq > ids->seq_max)
  248. ids->seq = 0;
  249. spin_lock_init(&new->lock);
  250. new->deleted = 0;
  251. rcu_read_lock();
  252. spin_lock(&new->lock);
  253. return id;
  254. }
  255. /**
  256. * ipc_rmid - remove an IPC identifier
  257. * @ids: identifier set
  258. * @id: ipc perm structure containing the identifier to remove
  259. *
  260. * The identifier must be valid, and in use. The kernel will panic if
  261. * fed an invalid identifier. The entry is removed and internal
  262. * variables recomputed.
  263. * ipc_ids.mutex and the spinlock for this ID are held before this
  264. * function is called, and remain locked on the exit.
  265. */
  266. void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
  267. {
  268. int lid = ipcp->id % SEQ_MULTIPLIER;
  269. idr_remove(&ids->ipcs_idr, lid);
  270. ids->in_use--;
  271. ipcp->deleted = 1;
  272. return;
  273. }
  274. /**
  275. * ipc_alloc - allocate ipc space
  276. * @size: size desired
  277. *
  278. * Allocate memory from the appropriate pools and return a pointer to it.
  279. * NULL is returned if the allocation fails
  280. */
  281. void* ipc_alloc(int size)
  282. {
  283. void* out;
  284. if(size > PAGE_SIZE)
  285. out = vmalloc(size);
  286. else
  287. out = kmalloc(size, GFP_KERNEL);
  288. return out;
  289. }
  290. /**
  291. * ipc_free - free ipc space
  292. * @ptr: pointer returned by ipc_alloc
  293. * @size: size of block
  294. *
  295. * Free a block created with ipc_alloc(). The caller must know the size
  296. * used in the allocation call.
  297. */
  298. void ipc_free(void* ptr, int size)
  299. {
  300. if(size > PAGE_SIZE)
  301. vfree(ptr);
  302. else
  303. kfree(ptr);
  304. }
  305. /*
  306. * rcu allocations:
  307. * There are three headers that are prepended to the actual allocation:
  308. * - during use: ipc_rcu_hdr.
  309. * - during the rcu grace period: ipc_rcu_grace.
  310. * - [only if vmalloc]: ipc_rcu_sched.
  311. * Their lifetime doesn't overlap, thus the headers share the same memory.
  312. * Unlike a normal union, they are right-aligned, thus some container_of
  313. * forward/backward casting is necessary:
  314. */
  315. struct ipc_rcu_hdr
  316. {
  317. int refcount;
  318. int is_vmalloc;
  319. void *data[0];
  320. };
  321. struct ipc_rcu_grace
  322. {
  323. struct rcu_head rcu;
  324. /* "void *" makes sure alignment of following data is sane. */
  325. void *data[0];
  326. };
  327. struct ipc_rcu_sched
  328. {
  329. struct work_struct work;
  330. /* "void *" makes sure alignment of following data is sane. */
  331. void *data[0];
  332. };
  333. #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
  334. sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
  335. #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
  336. sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
  337. static inline int rcu_use_vmalloc(int size)
  338. {
  339. /* Too big for a single page? */
  340. if (HDRLEN_KMALLOC + size > PAGE_SIZE)
  341. return 1;
  342. return 0;
  343. }
  344. /**
  345. * ipc_rcu_alloc - allocate ipc and rcu space
  346. * @size: size desired
  347. *
  348. * Allocate memory for the rcu header structure + the object.
  349. * Returns the pointer to the object.
  350. * NULL is returned if the allocation fails.
  351. */
  352. void* ipc_rcu_alloc(int size)
  353. {
  354. void* out;
  355. /*
  356. * We prepend the allocation with the rcu struct, and
  357. * workqueue if necessary (for vmalloc).
  358. */
  359. if (rcu_use_vmalloc(size)) {
  360. out = vmalloc(HDRLEN_VMALLOC + size);
  361. if (out) {
  362. out += HDRLEN_VMALLOC;
  363. container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
  364. container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
  365. }
  366. } else {
  367. out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
  368. if (out) {
  369. out += HDRLEN_KMALLOC;
  370. container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
  371. container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
  372. }
  373. }
  374. return out;
  375. }
  376. void ipc_rcu_getref(void *ptr)
  377. {
  378. container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
  379. }
  380. static void ipc_do_vfree(struct work_struct *work)
  381. {
  382. vfree(container_of(work, struct ipc_rcu_sched, work));
  383. }
  384. /**
  385. * ipc_schedule_free - free ipc + rcu space
  386. * @head: RCU callback structure for queued work
  387. *
  388. * Since RCU callback function is called in bh,
  389. * we need to defer the vfree to schedule_work().
  390. */
  391. static void ipc_schedule_free(struct rcu_head *head)
  392. {
  393. struct ipc_rcu_grace *grace =
  394. container_of(head, struct ipc_rcu_grace, rcu);
  395. struct ipc_rcu_sched *sched =
  396. container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]);
  397. INIT_WORK(&sched->work, ipc_do_vfree);
  398. schedule_work(&sched->work);
  399. }
  400. /**
  401. * ipc_immediate_free - free ipc + rcu space
  402. * @head: RCU callback structure that contains pointer to be freed
  403. *
  404. * Free from the RCU callback context.
  405. */
  406. static void ipc_immediate_free(struct rcu_head *head)
  407. {
  408. struct ipc_rcu_grace *free =
  409. container_of(head, struct ipc_rcu_grace, rcu);
  410. kfree(free);
  411. }
  412. void ipc_rcu_putref(void *ptr)
  413. {
  414. if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
  415. return;
  416. if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
  417. call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
  418. ipc_schedule_free);
  419. } else {
  420. call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
  421. ipc_immediate_free);
  422. }
  423. }
  424. /**
  425. * ipcperms - check IPC permissions
  426. * @ipcp: IPC permission set
  427. * @flag: desired permission set.
  428. *
  429. * Check user, group, other permissions for access
  430. * to ipc resources. return 0 if allowed
  431. */
  432. int ipcperms (struct kern_ipc_perm *ipcp, short flag)
  433. { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
  434. int requested_mode, granted_mode, err;
  435. if (unlikely((err = audit_ipc_obj(ipcp))))
  436. return err;
  437. requested_mode = (flag >> 6) | (flag >> 3) | flag;
  438. granted_mode = ipcp->mode;
  439. if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
  440. granted_mode >>= 6;
  441. else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
  442. granted_mode >>= 3;
  443. /* is there some bit set in requested_mode but not in granted_mode? */
  444. if ((requested_mode & ~granted_mode & 0007) &&
  445. !capable(CAP_IPC_OWNER))
  446. return -1;
  447. return security_ipc_permission(ipcp, flag);
  448. }
  449. /*
  450. * Functions to convert between the kern_ipc_perm structure and the
  451. * old/new ipc_perm structures
  452. */
  453. /**
  454. * kernel_to_ipc64_perm - convert kernel ipc permissions to user
  455. * @in: kernel permissions
  456. * @out: new style IPC permissions
  457. *
  458. * Turn the kernel object @in into a set of permissions descriptions
  459. * for returning to userspace (@out).
  460. */
  461. void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
  462. {
  463. out->key = in->key;
  464. out->uid = in->uid;
  465. out->gid = in->gid;
  466. out->cuid = in->cuid;
  467. out->cgid = in->cgid;
  468. out->mode = in->mode;
  469. out->seq = in->seq;
  470. }
  471. /**
  472. * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
  473. * @in: new style IPC permissions
  474. * @out: old style IPC permissions
  475. *
  476. * Turn the new style permissions object @in into a compatibility
  477. * object and store it into the @out pointer.
  478. */
  479. void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
  480. {
  481. out->key = in->key;
  482. SET_UID(out->uid, in->uid);
  483. SET_GID(out->gid, in->gid);
  484. SET_UID(out->cuid, in->cuid);
  485. SET_GID(out->cgid, in->cgid);
  486. out->mode = in->mode;
  487. out->seq = in->seq;
  488. }
  489. /*
  490. * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
  491. * is called with shm_ids.mutex locked. Since grow_ary() is also called with
  492. * shm_ids.mutex down(for Shared Memory), there is no need to add read
  493. * barriers here to gurantee the writes in grow_ary() are seen in order
  494. * here (for Alpha).
  495. *
  496. * However ipc_get() itself does not necessary require ipc_ids.mutex down. So
  497. * if in the future ipc_get() is used by other places without ipc_ids.mutex
  498. * down, then ipc_get() needs read memery barriers as ipc_lock() does.
  499. */
  500. struct kern_ipc_perm *ipc_get(struct ipc_ids *ids, int id)
  501. {
  502. struct kern_ipc_perm *out;
  503. int lid = id % SEQ_MULTIPLIER;
  504. out = idr_find(&ids->ipcs_idr, lid);
  505. return out;
  506. }
  507. struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
  508. {
  509. struct kern_ipc_perm *out;
  510. int lid = id % SEQ_MULTIPLIER;
  511. rcu_read_lock();
  512. out = idr_find(&ids->ipcs_idr, lid);
  513. if (out == NULL) {
  514. rcu_read_unlock();
  515. return NULL;
  516. }
  517. spin_lock(&out->lock);
  518. /* ipc_rmid() may have already freed the ID while ipc_lock
  519. * was spinning: here verify that the structure is still valid
  520. */
  521. if (out->deleted) {
  522. spin_unlock(&out->lock);
  523. rcu_read_unlock();
  524. return NULL;
  525. }
  526. return out;
  527. }
  528. void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
  529. {
  530. rcu_read_lock();
  531. spin_lock(&perm->lock);
  532. }
  533. void ipc_unlock(struct kern_ipc_perm* perm)
  534. {
  535. spin_unlock(&perm->lock);
  536. rcu_read_unlock();
  537. }
  538. int ipc_buildid(struct ipc_ids* ids, int id, int seq)
  539. {
  540. return SEQ_MULTIPLIER*seq + id;
  541. }
  542. int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
  543. {
  544. if(uid/SEQ_MULTIPLIER != ipcp->seq)
  545. return 1;
  546. return 0;
  547. }
  548. #ifdef __ARCH_WANT_IPC_PARSE_VERSION
  549. /**
  550. * ipc_parse_version - IPC call version
  551. * @cmd: pointer to command
  552. *
  553. * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
  554. * The @cmd value is turned from an encoding command and version into
  555. * just the command code.
  556. */
  557. int ipc_parse_version (int *cmd)
  558. {
  559. if (*cmd & IPC_64) {
  560. *cmd ^= IPC_64;
  561. return IPC_64;
  562. } else {
  563. return IPC_OLD;
  564. }
  565. }
  566. #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
  567. #ifdef CONFIG_PROC_FS
  568. struct ipc_proc_iter {
  569. struct ipc_namespace *ns;
  570. struct ipc_proc_iface *iface;
  571. };
  572. /*
  573. * This routine locks the ipc structure found at least at position pos.
  574. */
  575. struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
  576. loff_t *new_pos)
  577. {
  578. struct kern_ipc_perm *ipc;
  579. int total, id;
  580. total = 0;
  581. for (id = 0; id < pos && total < ids->in_use; id++) {
  582. ipc = idr_find(&ids->ipcs_idr, id);
  583. if (ipc != NULL)
  584. total++;
  585. }
  586. if (total >= ids->in_use)
  587. return NULL;
  588. for ( ; pos < IPCMNI; pos++) {
  589. ipc = idr_find(&ids->ipcs_idr, pos);
  590. if (ipc != NULL) {
  591. *new_pos = pos + 1;
  592. ipc_lock_by_ptr(ipc);
  593. return ipc;
  594. }
  595. }
  596. /* Out of range - return NULL to terminate iteration */
  597. return NULL;
  598. }
  599. static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
  600. {
  601. struct ipc_proc_iter *iter = s->private;
  602. struct ipc_proc_iface *iface = iter->iface;
  603. struct kern_ipc_perm *ipc = it;
  604. /* If we had an ipc id locked before, unlock it */
  605. if (ipc && ipc != SEQ_START_TOKEN)
  606. ipc_unlock(ipc);
  607. return sysvipc_find_ipc(iter->ns->ids[iface->ids], *pos, pos);
  608. }
  609. /*
  610. * File positions: pos 0 -> header, pos n -> ipc id + 1.
  611. * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START.
  612. */
  613. static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
  614. {
  615. struct ipc_proc_iter *iter = s->private;
  616. struct ipc_proc_iface *iface = iter->iface;
  617. struct ipc_ids *ids;
  618. ids = iter->ns->ids[iface->ids];
  619. /*
  620. * Take the lock - this will be released by the corresponding
  621. * call to stop().
  622. */
  623. mutex_lock(&ids->mutex);
  624. /* pos < 0 is invalid */
  625. if (*pos < 0)
  626. return NULL;
  627. /* pos == 0 means header */
  628. if (*pos == 0)
  629. return SEQ_START_TOKEN;
  630. /* Find the (pos-1)th ipc */
  631. return sysvipc_find_ipc(ids, *pos - 1, pos);
  632. }
  633. static void sysvipc_proc_stop(struct seq_file *s, void *it)
  634. {
  635. struct kern_ipc_perm *ipc = it;
  636. struct ipc_proc_iter *iter = s->private;
  637. struct ipc_proc_iface *iface = iter->iface;
  638. struct ipc_ids *ids;
  639. /* If we had a locked segment, release it */
  640. if (ipc && ipc != SEQ_START_TOKEN)
  641. ipc_unlock(ipc);
  642. ids = iter->ns->ids[iface->ids];
  643. /* Release the lock we took in start() */
  644. mutex_unlock(&ids->mutex);
  645. }
  646. static int sysvipc_proc_show(struct seq_file *s, void *it)
  647. {
  648. struct ipc_proc_iter *iter = s->private;
  649. struct ipc_proc_iface *iface = iter->iface;
  650. if (it == SEQ_START_TOKEN)
  651. return seq_puts(s, iface->header);
  652. return iface->show(s, it);
  653. }
  654. static struct seq_operations sysvipc_proc_seqops = {
  655. .start = sysvipc_proc_start,
  656. .stop = sysvipc_proc_stop,
  657. .next = sysvipc_proc_next,
  658. .show = sysvipc_proc_show,
  659. };
  660. static int sysvipc_proc_open(struct inode *inode, struct file *file)
  661. {
  662. int ret;
  663. struct seq_file *seq;
  664. struct ipc_proc_iter *iter;
  665. ret = -ENOMEM;
  666. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  667. if (!iter)
  668. goto out;
  669. ret = seq_open(file, &sysvipc_proc_seqops);
  670. if (ret)
  671. goto out_kfree;
  672. seq = file->private_data;
  673. seq->private = iter;
  674. iter->iface = PDE(inode)->data;
  675. iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
  676. out:
  677. return ret;
  678. out_kfree:
  679. kfree(iter);
  680. goto out;
  681. }
  682. static int sysvipc_proc_release(struct inode *inode, struct file *file)
  683. {
  684. struct seq_file *seq = file->private_data;
  685. struct ipc_proc_iter *iter = seq->private;
  686. put_ipc_ns(iter->ns);
  687. return seq_release_private(inode, file);
  688. }
  689. static const struct file_operations sysvipc_proc_fops = {
  690. .open = sysvipc_proc_open,
  691. .read = seq_read,
  692. .llseek = seq_lseek,
  693. .release = sysvipc_proc_release,
  694. };
  695. #endif /* CONFIG_PROC_FS */