util.c 19 KB

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