util.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/err.h>
  12. #define SEQ_MULTIPLIER (IPCMNI)
  13. void sem_init (void);
  14. void msg_init (void);
  15. void shm_init (void);
  16. struct ipc_namespace;
  17. #ifdef CONFIG_POSIX_MQUEUE
  18. extern void mq_clear_sbinfo(struct ipc_namespace *ns);
  19. extern void mq_put_mnt(struct ipc_namespace *ns);
  20. #else
  21. static inline void mq_clear_sbinfo(struct ipc_namespace *ns) { }
  22. static inline void mq_put_mnt(struct ipc_namespace *ns) { }
  23. #endif
  24. #ifdef CONFIG_SYSVIPC
  25. void sem_init_ns(struct ipc_namespace *ns);
  26. void msg_init_ns(struct ipc_namespace *ns);
  27. void shm_init_ns(struct ipc_namespace *ns);
  28. void sem_exit_ns(struct ipc_namespace *ns);
  29. void msg_exit_ns(struct ipc_namespace *ns);
  30. void shm_exit_ns(struct ipc_namespace *ns);
  31. #else
  32. static inline void sem_init_ns(struct ipc_namespace *ns) { }
  33. static inline void msg_init_ns(struct ipc_namespace *ns) { }
  34. static inline void shm_init_ns(struct ipc_namespace *ns) { }
  35. static inline void sem_exit_ns(struct ipc_namespace *ns) { }
  36. static inline void msg_exit_ns(struct ipc_namespace *ns) { }
  37. static inline void shm_exit_ns(struct ipc_namespace *ns) { }
  38. #endif
  39. /*
  40. * Structure that holds the parameters needed by the ipc operations
  41. * (see after)
  42. */
  43. struct ipc_params {
  44. key_t key;
  45. int flg;
  46. union {
  47. size_t size; /* for shared memories */
  48. int nsems; /* for semaphores */
  49. } u; /* holds the getnew() specific param */
  50. };
  51. /*
  52. * Structure that holds some ipc operations. This structure is used to unify
  53. * the calls to sys_msgget(), sys_semget(), sys_shmget()
  54. * . routine to call to create a new ipc object. Can be one of newque,
  55. * newary, newseg
  56. * . routine to call to check permissions for a new ipc object.
  57. * Can be one of security_msg_associate, security_sem_associate,
  58. * security_shm_associate
  59. * . routine to call for an extra check if needed
  60. */
  61. struct ipc_ops {
  62. int (*getnew) (struct ipc_namespace *, struct ipc_params *);
  63. int (*associate) (struct kern_ipc_perm *, int);
  64. int (*more_checks) (struct kern_ipc_perm *, struct ipc_params *);
  65. };
  66. struct seq_file;
  67. struct ipc_ids;
  68. void ipc_init_ids(struct ipc_ids *);
  69. #ifdef CONFIG_PROC_FS
  70. void __init ipc_init_proc_interface(const char *path, const char *header,
  71. int ids, int (*show)(struct seq_file *, void *));
  72. #else
  73. #define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
  74. #endif
  75. #define IPC_SEM_IDS 0
  76. #define IPC_MSG_IDS 1
  77. #define IPC_SHM_IDS 2
  78. #define ipcid_to_idx(id) ((id) % SEQ_MULTIPLIER)
  79. /* must be called with ids->rw_mutex acquired for writing */
  80. int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
  81. /* must be called with ids->rw_mutex acquired for reading */
  82. int ipc_get_maxid(struct ipc_ids *);
  83. /* must be called with both locks acquired. */
  84. void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
  85. /* must be called with ipcp locked */
  86. int ipcperms(struct kern_ipc_perm *ipcp, short flg);
  87. /* for rare, potentially huge allocations.
  88. * both function can sleep
  89. */
  90. void* ipc_alloc(int size);
  91. void ipc_free(void* ptr, int size);
  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. void* ipc_rcu_alloc(int size);
  99. void ipc_rcu_getref(void *ptr);
  100. void ipc_rcu_putref(void *ptr);
  101. struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int);
  102. void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
  103. void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
  104. void ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out);
  105. struct kern_ipc_perm *ipcctl_pre_down(struct ipc_ids *ids, int id, int cmd,
  106. struct ipc64_perm *perm, int extra_perm);
  107. #if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__) || defined(__XTENSA__)
  108. /* On IA-64, we always use the "64-bit version" of the IPC structures. */
  109. # define ipc_parse_version(cmd) IPC_64
  110. #else
  111. int ipc_parse_version (int *cmd);
  112. #endif
  113. extern void free_msg(struct msg_msg *msg);
  114. extern struct msg_msg *load_msg(const void __user *src, int len);
  115. extern int store_msg(void __user *dest, struct msg_msg *msg, int len);
  116. extern void recompute_msgmni(struct ipc_namespace *);
  117. static inline int ipc_buildid(int id, int seq)
  118. {
  119. return SEQ_MULTIPLIER * seq + id;
  120. }
  121. /*
  122. * Must be called with ipcp locked
  123. */
  124. static inline int ipc_checkid(struct kern_ipc_perm *ipcp, int uid)
  125. {
  126. if (uid / SEQ_MULTIPLIER != ipcp->seq)
  127. return 1;
  128. return 0;
  129. }
  130. static inline void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
  131. {
  132. rcu_read_lock();
  133. spin_lock(&perm->lock);
  134. }
  135. static inline void ipc_unlock(struct kern_ipc_perm *perm)
  136. {
  137. spin_unlock(&perm->lock);
  138. rcu_read_unlock();
  139. }
  140. struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids, int id);
  141. int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
  142. struct ipc_ops *ops, struct ipc_params *params);
  143. #endif