util.c 19 KB

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