util.c 17 KB

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