util.c 19 KB

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