util.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. void ipc_init_ids(struct ipc_ids *ids, int size);
  37. #ifdef CONFIG_PROC_FS
  38. void __init ipc_init_proc_interface(const char *path, const char *header,
  39. int ids, int (*show)(struct seq_file *, void *));
  40. #else
  41. #define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
  42. #endif
  43. #define IPC_SEM_IDS 0
  44. #define IPC_MSG_IDS 1
  45. #define IPC_SHM_IDS 2
  46. /* must be called with ids->mutex acquired.*/
  47. int ipc_findkey(struct ipc_ids* ids, key_t key);
  48. int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size);
  49. /* must be called with both locks acquired. */
  50. struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id);
  51. int ipcperms (struct kern_ipc_perm *ipcp, short flg);
  52. /* for rare, potentially huge allocations.
  53. * both function can sleep
  54. */
  55. void* ipc_alloc(int size);
  56. void ipc_free(void* ptr, int size);
  57. /*
  58. * For allocation that need to be freed by RCU.
  59. * Objects are reference counted, they start with reference count 1.
  60. * getref increases the refcount, the putref call that reduces the recount
  61. * to 0 schedules the rcu destruction. Caller must guarantee locking.
  62. */
  63. void* ipc_rcu_alloc(int size);
  64. void ipc_rcu_getref(void *ptr);
  65. void ipc_rcu_putref(void *ptr);
  66. static inline void __ipc_fini_ids(struct ipc_ids *ids,
  67. struct ipc_id_ary *entries)
  68. {
  69. if (entries != &ids->nullentry)
  70. ipc_rcu_putref(entries);
  71. }
  72. static inline void ipc_fini_ids(struct ipc_ids *ids)
  73. {
  74. __ipc_fini_ids(ids, ids->entries);
  75. }
  76. struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id);
  77. struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id);
  78. void ipc_lock_by_ptr(struct kern_ipc_perm *ipcp);
  79. void ipc_unlock(struct kern_ipc_perm* perm);
  80. int ipc_buildid(struct ipc_ids* ids, int id, int seq);
  81. int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid);
  82. void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
  83. void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
  84. #if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__) || defined(__XTENSA__)
  85. /* On IA-64, we always use the "64-bit version" of the IPC structures. */
  86. # define ipc_parse_version(cmd) IPC_64
  87. #else
  88. int ipc_parse_version (int *cmd);
  89. #endif
  90. extern void free_msg(struct msg_msg *msg);
  91. extern struct msg_msg *load_msg(const void __user *src, int len);
  92. extern int store_msg(void __user *dest, struct msg_msg *msg, int len);
  93. #endif