util.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/unistd.h>
  12. #include <linux/err.h>
  13. #define SEQ_MULTIPLIER (IPCMNI)
  14. int sem_init(void);
  15. int msg_init(void);
  16. void shm_init(void);
  17. struct ipc_namespace;
  18. #ifdef CONFIG_POSIX_MQUEUE
  19. extern void mq_clear_sbinfo(struct ipc_namespace *ns);
  20. extern void mq_put_mnt(struct ipc_namespace *ns);
  21. #else
  22. static inline void mq_clear_sbinfo(struct ipc_namespace *ns) { }
  23. static inline void mq_put_mnt(struct ipc_namespace *ns) { }
  24. #endif
  25. #ifdef CONFIG_SYSVIPC
  26. int sem_init_ns(struct ipc_namespace *ns);
  27. int msg_init_ns(struct ipc_namespace *ns);
  28. int shm_init_ns(struct ipc_namespace *ns);
  29. void sem_exit_ns(struct ipc_namespace *ns);
  30. void msg_exit_ns(struct ipc_namespace *ns);
  31. void shm_exit_ns(struct ipc_namespace *ns);
  32. #else
  33. static inline int sem_init_ns(struct ipc_namespace *ns) { return 0; }
  34. static inline int msg_init_ns(struct ipc_namespace *ns) { return 0; }
  35. static inline int shm_init_ns(struct ipc_namespace *ns) { return 0; }
  36. static inline void sem_exit_ns(struct ipc_namespace *ns) { }
  37. static inline void msg_exit_ns(struct ipc_namespace *ns) { }
  38. static inline void shm_exit_ns(struct ipc_namespace *ns) { }
  39. #endif
  40. /*
  41. * Structure that holds the parameters needed by the ipc operations
  42. * (see after)
  43. */
  44. struct ipc_params {
  45. key_t key;
  46. int flg;
  47. union {
  48. size_t size; /* for shared memories */
  49. int nsems; /* for semaphores */
  50. } u; /* holds the getnew() specific param */
  51. };
  52. /*
  53. * Structure that holds some ipc operations. This structure is used to unify
  54. * the calls to sys_msgget(), sys_semget(), sys_shmget()
  55. * . routine to call to create a new ipc object. Can be one of newque,
  56. * newary, newseg
  57. * . routine to call to check permissions for a new ipc object.
  58. * Can be one of security_msg_associate, security_sem_associate,
  59. * security_shm_associate
  60. * . routine to call for an extra check if needed
  61. */
  62. struct ipc_ops {
  63. int (*getnew)(struct ipc_namespace *, struct ipc_params *);
  64. int (*associate)(struct kern_ipc_perm *, int);
  65. int (*more_checks)(struct kern_ipc_perm *, struct ipc_params *);
  66. };
  67. struct seq_file;
  68. struct ipc_ids;
  69. int ipc_init_ids(struct ipc_ids *);
  70. #ifdef CONFIG_PROC_FS
  71. void __init ipc_init_proc_interface(const char *path, const char *header,
  72. int ids, int (*show)(struct seq_file *, void *));
  73. #else
  74. #define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
  75. #endif
  76. #define IPC_SEM_IDS 0
  77. #define IPC_MSG_IDS 1
  78. #define IPC_SHM_IDS 2
  79. #define ipcid_to_idx(id) ((id) % SEQ_MULTIPLIER)
  80. #define ipcid_to_seqx(id) ((id) / SEQ_MULTIPLIER)
  81. #define IPCID_SEQ_MAX min_t(int, INT_MAX/SEQ_MULTIPLIER, USHRT_MAX)
  82. /* must be called with ids->rwsem acquired for writing */
  83. int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
  84. /* must be called with ids->rwsem acquired for reading */
  85. int ipc_get_maxid(struct ipc_ids *);
  86. /* must be called with both locks acquired. */
  87. void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
  88. /* must be called with both locks acquired. */
  89. void ipc_set_key_private(struct ipc_ids *, struct kern_ipc_perm *);
  90. /* must be called with ipcp locked */
  91. int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flg);
  92. /*
  93. * For allocation that need to be freed by RCU.
  94. * Objects are reference counted, they start with reference count 1.
  95. * getref increases the refcount, the putref call that reduces the recount
  96. * to 0 schedules the rcu destruction. Caller must guarantee locking.
  97. *
  98. * refcount is initialized by ipc_addid(), before that point call_rcu()
  99. * must be used.
  100. */
  101. int ipc_rcu_getref(struct kern_ipc_perm *ptr);
  102. void ipc_rcu_putref(struct kern_ipc_perm *ptr,
  103. void (*func)(struct rcu_head *head));
  104. struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int);
  105. struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id);
  106. void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
  107. void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
  108. int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out);
  109. struct kern_ipc_perm *ipcctl_pre_down_nolock(struct ipc_namespace *ns,
  110. struct ipc_ids *ids, int id, int cmd,
  111. struct ipc64_perm *perm, int extra_perm);
  112. #ifndef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
  113. /* On IA-64, we always use the "64-bit version" of the IPC structures. */
  114. # define ipc_parse_version(cmd) IPC_64
  115. #else
  116. int ipc_parse_version(int *cmd);
  117. #endif
  118. extern void free_msg(struct msg_msg *msg);
  119. extern struct msg_msg *load_msg(const void __user *src, size_t len);
  120. extern struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst);
  121. extern int store_msg(void __user *dest, struct msg_msg *msg, size_t len);
  122. static inline int ipc_buildid(int id, int seq)
  123. {
  124. return SEQ_MULTIPLIER * seq + id;
  125. }
  126. static inline int ipc_checkid(struct kern_ipc_perm *ipcp, int uid)
  127. {
  128. return uid / SEQ_MULTIPLIER != ipcp->seq;
  129. }
  130. static inline void ipc_lock_object(struct kern_ipc_perm *perm)
  131. {
  132. spin_lock(&perm->lock);
  133. }
  134. static inline void ipc_unlock_object(struct kern_ipc_perm *perm)
  135. {
  136. spin_unlock(&perm->lock);
  137. }
  138. static inline void ipc_assert_locked_object(struct kern_ipc_perm *perm)
  139. {
  140. assert_spin_locked(&perm->lock);
  141. }
  142. static inline void ipc_unlock(struct kern_ipc_perm *perm)
  143. {
  144. ipc_unlock_object(perm);
  145. rcu_read_unlock();
  146. }
  147. /*
  148. * ipc_valid_object() - helper to sort out IPC_RMID races for codepaths
  149. * where the respective ipc_ids.rwsem is not being held down.
  150. * Checks whether the ipc object is still around or if it's gone already, as
  151. * ipc_rmid() may have already freed the ID while the ipc lock was spinning.
  152. * Needs to be called with kern_ipc_perm.lock held -- exception made for one
  153. * checkpoint case at sys_semtimedop() as noted in code commentary.
  154. */
  155. static inline bool ipc_valid_object(struct kern_ipc_perm *perm)
  156. {
  157. return !perm->deleted;
  158. }
  159. struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id);
  160. int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
  161. const struct ipc_ops *ops, struct ipc_params *params);
  162. void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
  163. void (*free)(struct ipc_namespace *, struct kern_ipc_perm *));
  164. #ifdef CONFIG_COMPAT
  165. #include <linux/compat.h>
  166. struct compat_ipc_perm {
  167. key_t key;
  168. __compat_uid_t uid;
  169. __compat_gid_t gid;
  170. __compat_uid_t cuid;
  171. __compat_gid_t cgid;
  172. compat_mode_t mode;
  173. unsigned short seq;
  174. };
  175. void to_compat_ipc_perm(struct compat_ipc_perm *, struct ipc64_perm *);
  176. void to_compat_ipc64_perm(struct compat_ipc64_perm *, struct ipc64_perm *);
  177. int get_compat_ipc_perm(struct ipc64_perm *, struct compat_ipc_perm __user *);
  178. int get_compat_ipc64_perm(struct ipc64_perm *,
  179. struct compat_ipc64_perm __user *);
  180. static inline int compat_ipc_parse_version(int *cmd)
  181. {
  182. #ifdef CONFIG_ARCH_WANT_COMPAT_IPC_PARSE_VERSION
  183. int version = *cmd & IPC_64;
  184. *cmd &= ~IPC_64;
  185. return version;
  186. #else
  187. return IPC_64;
  188. #endif
  189. }
  190. #endif
  191. #endif