util.c 17 KB

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