util.c 19 KB

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