util.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * linux/ipc/util.h
  3. * Copyright (C) 1999 Christoph Rohland
  4. *
  5. * ipc helper functions (c) 1999 Manfred Spraul <manfred@colorfullife.com>
  6. * namespaces support. 2006 OpenVZ, SWsoft Inc.
  7. * Pavel Emelianov <xemul@openvz.org>
  8. */
  9. #ifndef _IPC_UTIL_H
  10. #define _IPC_UTIL_H
  11. #include <linux/idr.h>
  12. #include <linux/err.h>
  13. #define USHRT_MAX 0xffff
  14. #define SEQ_MULTIPLIER (IPCMNI)
  15. void sem_init (void);
  16. void msg_init (void);
  17. void shm_init (void);
  18. int sem_init_ns(struct ipc_namespace *ns);
  19. int msg_init_ns(struct ipc_namespace *ns);
  20. int shm_init_ns(struct ipc_namespace *ns);
  21. void sem_exit_ns(struct ipc_namespace *ns);
  22. void msg_exit_ns(struct ipc_namespace *ns);
  23. void shm_exit_ns(struct ipc_namespace *ns);
  24. struct ipc_ids {
  25. int in_use;
  26. unsigned short seq;
  27. unsigned short seq_max;
  28. struct rw_semaphore rw_mutex;
  29. struct idr ipcs_idr;
  30. };
  31. /*
  32. * Structure that holds the parameters needed by the ipc operations
  33. * (see after)
  34. */
  35. struct ipc_params {
  36. key_t key;
  37. int flg;
  38. union {
  39. size_t size; /* for shared memories */
  40. int nsems; /* for semaphores */
  41. } u; /* holds the getnew() specific param */
  42. };
  43. /*
  44. * Structure that holds some ipc operations. This structure is used to unify
  45. * the calls to sys_msgget(), sys_semget(), sys_shmget()
  46. * . routine to call to create a new ipc object. Can be one of newque,
  47. * newary, newseg
  48. * . routine to call to check permissions for a new ipc object.
  49. * Can be one of security_msg_associate, security_sem_associate,
  50. * security_shm_associate
  51. * . routine to call for an extra check if needed
  52. */
  53. struct ipc_ops {
  54. int (*getnew) (struct ipc_namespace *, struct ipc_params *);
  55. int (*associate) (struct kern_ipc_perm *, int);
  56. int (*more_checks) (struct kern_ipc_perm *, struct ipc_params *);
  57. };
  58. struct seq_file;
  59. void ipc_init_ids(struct ipc_ids *);
  60. #ifdef CONFIG_PROC_FS
  61. void __init ipc_init_proc_interface(const char *path, const char *header,
  62. int ids, int (*show)(struct seq_file *, void *));
  63. #else
  64. #define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
  65. #endif
  66. #define IPC_SEM_IDS 0
  67. #define IPC_MSG_IDS 1
  68. #define IPC_SHM_IDS 2
  69. #define ipcid_to_idx(id) ((id) % SEQ_MULTIPLIER)
  70. /* must be called with ids->rw_mutex acquired for writing */
  71. int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
  72. /* must be called with ids->rw_mutex acquired for reading */
  73. int ipc_get_maxid(struct ipc_ids *);
  74. /* must be called with both locks acquired. */
  75. void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
  76. /* must be called with ipcp locked */
  77. int ipcperms(struct kern_ipc_perm *ipcp, short flg);
  78. /* for rare, potentially huge allocations.
  79. * both function can sleep
  80. */
  81. void* ipc_alloc(int size);
  82. void ipc_free(void* ptr, int size);
  83. /*
  84. * For allocation that need to be freed by RCU.
  85. * Objects are reference counted, they start with reference count 1.
  86. * getref increases the refcount, the putref call that reduces the recount
  87. * to 0 schedules the rcu destruction. Caller must guarantee locking.
  88. */
  89. void* ipc_rcu_alloc(int size);
  90. void ipc_rcu_getref(void *ptr);
  91. void ipc_rcu_putref(void *ptr);
  92. /*
  93. * ipc_lock_down: called with rw_mutex held
  94. * ipc_lock: called without that lock held
  95. */
  96. struct kern_ipc_perm *ipc_lock_down(struct ipc_ids *, int);
  97. struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int);
  98. void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
  99. void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
  100. #if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__) || defined(__XTENSA__)
  101. /* On IA-64, we always use the "64-bit version" of the IPC structures. */
  102. # define ipc_parse_version(cmd) IPC_64
  103. #else
  104. int ipc_parse_version (int *cmd);
  105. #endif
  106. extern void free_msg(struct msg_msg *msg);
  107. extern struct msg_msg *load_msg(const void __user *src, int len);
  108. extern int store_msg(void __user *dest, struct msg_msg *msg, int len);
  109. extern int ipcget_new(struct ipc_namespace *, struct ipc_ids *,
  110. struct ipc_ops *, struct ipc_params *);
  111. extern int ipcget_public(struct ipc_namespace *, struct ipc_ids *,
  112. struct ipc_ops *, struct ipc_params *);
  113. static inline int ipc_buildid(struct ipc_ids *ids, int id, int seq)
  114. {
  115. return SEQ_MULTIPLIER * seq + id;
  116. }
  117. /*
  118. * Must be called with ipcp locked
  119. */
  120. static inline int ipc_checkid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp,
  121. int uid)
  122. {
  123. if (uid / SEQ_MULTIPLIER != ipcp->seq)
  124. return 1;
  125. return 0;
  126. }
  127. static inline void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
  128. {
  129. rcu_read_lock();
  130. spin_lock(&perm->lock);
  131. }
  132. static inline void ipc_unlock(struct kern_ipc_perm *perm)
  133. {
  134. spin_unlock(&perm->lock);
  135. rcu_read_unlock();
  136. }
  137. static inline struct kern_ipc_perm *ipc_lock_check_down(struct ipc_ids *ids,
  138. int id)
  139. {
  140. struct kern_ipc_perm *out;
  141. out = ipc_lock_down(ids, id);
  142. if (IS_ERR(out))
  143. return out;
  144. if (ipc_checkid(ids, out, id)) {
  145. ipc_unlock(out);
  146. return ERR_PTR(-EIDRM);
  147. }
  148. return out;
  149. }
  150. static inline struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids,
  151. int id)
  152. {
  153. struct kern_ipc_perm *out;
  154. out = ipc_lock(ids, id);
  155. if (IS_ERR(out))
  156. return out;
  157. if (ipc_checkid(ids, out, id)) {
  158. ipc_unlock(out);
  159. return ERR_PTR(-EIDRM);
  160. }
  161. return out;
  162. }
  163. /**
  164. * ipcget - Common sys_*get() code
  165. * @ns : namsepace
  166. * @ids : IPC identifier set
  167. * @ops : operations to be called on ipc object creation, permission checks
  168. * and further checks
  169. * @params : the parameters needed by the previous operations.
  170. *
  171. * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
  172. */
  173. static inline int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
  174. struct ipc_ops *ops, struct ipc_params *params)
  175. {
  176. if (params->key == IPC_PRIVATE)
  177. return ipcget_new(ns, ids, ops, params);
  178. else
  179. return ipcget_public(ns, ids, ops, params);
  180. }
  181. #endif