util.h 4.7 KB

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