util.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #define USHRT_MAX 0xffff
  12. #define SEQ_MULTIPLIER (IPCMNI)
  13. void sem_init (void);
  14. void msg_init (void);
  15. void shm_init (void);
  16. int sem_init_ns(struct ipc_namespace *ns);
  17. int msg_init_ns(struct ipc_namespace *ns);
  18. int shm_init_ns(struct ipc_namespace *ns);
  19. void sem_exit_ns(struct ipc_namespace *ns);
  20. void msg_exit_ns(struct ipc_namespace *ns);
  21. void shm_exit_ns(struct ipc_namespace *ns);
  22. struct ipc_id_ary {
  23. int size;
  24. struct kern_ipc_perm *p[0];
  25. };
  26. struct ipc_ids {
  27. int in_use;
  28. int max_id;
  29. unsigned short seq;
  30. unsigned short seq_max;
  31. struct mutex mutex;
  32. struct ipc_id_ary nullentry;
  33. struct ipc_id_ary* entries;
  34. };
  35. struct seq_file;
  36. #ifdef CONFIG_IPC_NS
  37. #define __ipc_init
  38. #else
  39. #define __ipc_init __init
  40. #endif
  41. void __ipc_init ipc_init_ids(struct ipc_ids *ids, int size);
  42. #ifdef CONFIG_PROC_FS
  43. void __init ipc_init_proc_interface(const char *path, const char *header,
  44. int ids, int (*show)(struct seq_file *, void *));
  45. #else
  46. #define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
  47. #endif
  48. #define IPC_SEM_IDS 0
  49. #define IPC_MSG_IDS 1
  50. #define IPC_SHM_IDS 2
  51. /* must be called with ids->mutex acquired.*/
  52. int ipc_findkey(struct ipc_ids* ids, key_t key);
  53. int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size);
  54. /* must be called with both locks acquired. */
  55. struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id);
  56. int ipcperms (struct kern_ipc_perm *ipcp, short flg);
  57. /* for rare, potentially huge allocations.
  58. * both function can sleep
  59. */
  60. void* ipc_alloc(int size);
  61. void ipc_free(void* ptr, int size);
  62. /*
  63. * For allocation that need to be freed by RCU.
  64. * Objects are reference counted, they start with reference count 1.
  65. * getref increases the refcount, the putref call that reduces the recount
  66. * to 0 schedules the rcu destruction. Caller must guarantee locking.
  67. */
  68. void* ipc_rcu_alloc(int size);
  69. void ipc_rcu_getref(void *ptr);
  70. void ipc_rcu_putref(void *ptr);
  71. static inline void __ipc_fini_ids(struct ipc_ids *ids,
  72. struct ipc_id_ary *entries)
  73. {
  74. if (entries != &ids->nullentry)
  75. ipc_rcu_putref(entries);
  76. }
  77. static inline void ipc_fini_ids(struct ipc_ids *ids)
  78. {
  79. __ipc_fini_ids(ids, ids->entries);
  80. }
  81. struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id);
  82. struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id);
  83. void ipc_lock_by_ptr(struct kern_ipc_perm *ipcp);
  84. void ipc_unlock(struct kern_ipc_perm* perm);
  85. int ipc_buildid(struct ipc_ids* ids, int id, int seq);
  86. int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid);
  87. void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
  88. void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
  89. #if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__) || defined(__XTENSA__)
  90. /* On IA-64, we always use the "64-bit version" of the IPC structures. */
  91. # define ipc_parse_version(cmd) IPC_64
  92. #else
  93. int ipc_parse_version (int *cmd);
  94. #endif
  95. extern void free_msg(struct msg_msg *msg);
  96. extern struct msg_msg *load_msg(const void __user *src, int len);
  97. extern int store_msg(void __user *dest, struct msg_msg *msg, int len);
  98. #endif