util.h 4.2 KB

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