util.h 4.5 KB

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