Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/ipc/util.h |
| 3 | * Copyright (C) 1999 Christoph Rohland |
| 4 | * |
Christian Kujau | 624dffc | 2006-01-15 02:43:54 +0100 | [diff] [blame] | 5 | * ipc helper functions (c) 1999 Manfred Spraul <manfred@colorfullife.com> |
Kirill Korotaev | 73ea413 | 2006-10-02 02:18:20 -0700 | [diff] [blame] | 6 | * namespaces support. 2006 OpenVZ, SWsoft Inc. |
| 7 | * Pavel Emelianov <xemul@openvz.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #ifndef _IPC_UTIL_H |
| 11 | #define _IPC_UTIL_H |
| 12 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 13 | #include <linux/idr.h> |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame^] | 14 | #include <linux/err.h> |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #define USHRT_MAX 0xffff |
| 17 | #define SEQ_MULTIPLIER (IPCMNI) |
| 18 | |
| 19 | void sem_init (void); |
| 20 | void msg_init (void); |
| 21 | void shm_init (void); |
| 22 | |
Kirill Korotaev | 73ea413 | 2006-10-02 02:18:20 -0700 | [diff] [blame] | 23 | int sem_init_ns(struct ipc_namespace *ns); |
| 24 | int msg_init_ns(struct ipc_namespace *ns); |
| 25 | int shm_init_ns(struct ipc_namespace *ns); |
| 26 | |
| 27 | void sem_exit_ns(struct ipc_namespace *ns); |
| 28 | void msg_exit_ns(struct ipc_namespace *ns); |
| 29 | void shm_exit_ns(struct ipc_namespace *ns); |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | struct ipc_ids { |
| 32 | int in_use; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | unsigned short seq; |
| 34 | unsigned short seq_max; |
Ingo Molnar | 5f921ae | 2006-03-26 01:37:17 -0800 | [diff] [blame] | 35 | struct mutex mutex; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 36 | struct idr ipcs_idr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 39 | /* |
| 40 | * Structure that holds the parameters needed by the ipc operations |
| 41 | * (see after) |
| 42 | */ |
| 43 | struct ipc_params { |
| 44 | key_t key; |
| 45 | int flg; |
| 46 | union { |
| 47 | size_t size; /* for shared memories */ |
| 48 | int nsems; /* for semaphores */ |
| 49 | } u; /* holds the getnew() specific param */ |
| 50 | }; |
| 51 | |
| 52 | /* |
| 53 | * Structure that holds some ipc operations. This structure is used to unify |
| 54 | * the calls to sys_msgget(), sys_semget(), sys_shmget() |
| 55 | * . routine to call to create a new ipc object. Can be one of newque, |
| 56 | * newary, newseg |
| 57 | * . routine to call to call to check permissions for a new ipc object. |
| 58 | * Can be one of security_msg_associate, security_sem_associate, |
| 59 | * security_shm_associate |
| 60 | * . routine to call for an extra check if needed |
| 61 | */ |
| 62 | struct ipc_ops { |
| 63 | int (*getnew) (struct ipc_namespace *, struct ipc_params *); |
| 64 | int (*associate) (void *, int); |
| 65 | int (*more_checks) (void *, struct ipc_params *); |
| 66 | }; |
| 67 | |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 68 | struct seq_file; |
Cedric Le Goater | 7d69a1f | 2007-07-15 23:40:58 -0700 | [diff] [blame] | 69 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 70 | void ipc_init_ids(struct ipc_ids *); |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 71 | #ifdef CONFIG_PROC_FS |
| 72 | void __init ipc_init_proc_interface(const char *path, const char *header, |
Kirill Korotaev | 73ea413 | 2006-10-02 02:18:20 -0700 | [diff] [blame] | 73 | int ids, int (*show)(struct seq_file *, void *)); |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 74 | #else |
| 75 | #define ipc_init_proc_interface(path, header, ids, show) do {} while (0) |
| 76 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Kirill Korotaev | 73ea413 | 2006-10-02 02:18:20 -0700 | [diff] [blame] | 78 | #define IPC_SEM_IDS 0 |
| 79 | #define IPC_MSG_IDS 1 |
| 80 | #define IPC_SHM_IDS 2 |
| 81 | |
Ingo Molnar | 5f921ae | 2006-03-26 01:37:17 -0800 | [diff] [blame] | 82 | /* must be called with ids->mutex acquired.*/ |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 83 | int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int); |
| 84 | int ipc_get_maxid(struct ipc_ids *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | /* must be called with both locks acquired. */ |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 87 | void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | int ipcperms (struct kern_ipc_perm *ipcp, short flg); |
| 90 | |
| 91 | /* for rare, potentially huge allocations. |
| 92 | * both function can sleep |
| 93 | */ |
| 94 | void* ipc_alloc(int size); |
| 95 | void ipc_free(void* ptr, int size); |
| 96 | |
| 97 | /* |
| 98 | * For allocation that need to be freed by RCU. |
| 99 | * Objects are reference counted, they start with reference count 1. |
| 100 | * getref increases the refcount, the putref call that reduces the recount |
| 101 | * to 0 schedules the rcu destruction. Caller must guarantee locking. |
| 102 | */ |
| 103 | void* ipc_rcu_alloc(int size); |
| 104 | void ipc_rcu_getref(void *ptr); |
| 105 | void ipc_rcu_putref(void *ptr); |
| 106 | |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame^] | 107 | struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | int ipc_buildid(struct ipc_ids* ids, int id, int seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out); |
| 111 | void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out); |
| 112 | |
Chris Zankel | 813e678 | 2005-07-12 13:58:25 -0700 | [diff] [blame] | 113 | #if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__) || defined(__XTENSA__) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | /* On IA-64, we always use the "64-bit version" of the IPC structures. */ |
| 115 | # define ipc_parse_version(cmd) IPC_64 |
| 116 | #else |
| 117 | int ipc_parse_version (int *cmd); |
| 118 | #endif |
| 119 | |
| 120 | extern void free_msg(struct msg_msg *msg); |
| 121 | extern struct msg_msg *load_msg(const void __user *src, int len); |
| 122 | extern int store_msg(void __user *dest, struct msg_msg *msg, int len); |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 123 | extern int ipcget_new(struct ipc_namespace *, struct ipc_ids *, |
| 124 | struct ipc_ops *, struct ipc_params *); |
| 125 | extern int ipcget_public(struct ipc_namespace *, struct ipc_ids *, |
| 126 | struct ipc_ops *, struct ipc_params *); |
| 127 | |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame^] | 128 | static inline int ipc_checkid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp, |
| 129 | int uid) |
| 130 | { |
| 131 | if (uid / SEQ_MULTIPLIER != ipcp->seq) |
| 132 | return 1; |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static inline void ipc_lock_by_ptr(struct kern_ipc_perm *perm) |
| 137 | { |
| 138 | rcu_read_lock(); |
| 139 | spin_lock(&perm->lock); |
| 140 | } |
| 141 | |
| 142 | static inline void ipc_unlock(struct kern_ipc_perm *perm) |
| 143 | { |
| 144 | spin_unlock(&perm->lock); |
| 145 | rcu_read_unlock(); |
| 146 | } |
| 147 | |
| 148 | static inline struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids, |
| 149 | int id) |
| 150 | { |
| 151 | struct kern_ipc_perm *out; |
| 152 | |
| 153 | out = ipc_lock(ids, id); |
| 154 | if (IS_ERR(out)) |
| 155 | return out; |
| 156 | |
| 157 | if (ipc_checkid(ids, out, id)) { |
| 158 | ipc_unlock(out); |
| 159 | return ERR_PTR(-EIDRM); |
| 160 | } |
| 161 | |
| 162 | return out; |
| 163 | } |
| 164 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 165 | static inline int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids, |
| 166 | struct ipc_ops *ops, struct ipc_params *params) |
| 167 | { |
| 168 | if (params->key == IPC_PRIVATE) |
| 169 | return ipcget_new(ns, ids, ops, params); |
| 170 | else |
| 171 | return ipcget_public(ns, ids, ops, params); |
| 172 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | #endif |