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 | |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 23 | struct ipc_namespace; |
| 24 | |
Kirill Korotaev | 73ea413 | 2006-10-02 02:18:20 -0700 | [diff] [blame] | 25 | int sem_init_ns(struct ipc_namespace *ns); |
| 26 | int msg_init_ns(struct ipc_namespace *ns); |
| 27 | int shm_init_ns(struct ipc_namespace *ns); |
| 28 | |
| 29 | void sem_exit_ns(struct ipc_namespace *ns); |
| 30 | void msg_exit_ns(struct ipc_namespace *ns); |
| 31 | void shm_exit_ns(struct ipc_namespace *ns); |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | struct ipc_ids { |
| 34 | int in_use; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | unsigned short seq; |
| 36 | unsigned short seq_max; |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 37 | struct rw_semaphore rw_mutex; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 38 | struct idr ipcs_idr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 41 | /* |
| 42 | * Structure that holds the parameters needed by the ipc operations |
| 43 | * (see after) |
| 44 | */ |
| 45 | struct ipc_params { |
| 46 | key_t key; |
| 47 | int flg; |
| 48 | union { |
| 49 | size_t size; /* for shared memories */ |
| 50 | int nsems; /* for semaphores */ |
| 51 | } u; /* holds the getnew() specific param */ |
| 52 | }; |
| 53 | |
| 54 | /* |
| 55 | * Structure that holds some ipc operations. This structure is used to unify |
| 56 | * the calls to sys_msgget(), sys_semget(), sys_shmget() |
| 57 | * . routine to call to create a new ipc object. Can be one of newque, |
| 58 | * newary, newseg |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 59 | * . routine to call to check permissions for a new ipc object. |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 60 | * Can be one of security_msg_associate, security_sem_associate, |
| 61 | * security_shm_associate |
| 62 | * . routine to call for an extra check if needed |
| 63 | */ |
| 64 | struct ipc_ops { |
| 65 | int (*getnew) (struct ipc_namespace *, struct ipc_params *); |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 66 | int (*associate) (struct kern_ipc_perm *, int); |
| 67 | int (*more_checks) (struct kern_ipc_perm *, struct ipc_params *); |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 70 | struct seq_file; |
Cedric Le Goater | 7d69a1f | 2007-07-15 23:40:58 -0700 | [diff] [blame] | 71 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 72 | void ipc_init_ids(struct ipc_ids *); |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 73 | #ifdef CONFIG_PROC_FS |
| 74 | void __init ipc_init_proc_interface(const char *path, const char *header, |
Kirill Korotaev | 73ea413 | 2006-10-02 02:18:20 -0700 | [diff] [blame] | 75 | int ids, int (*show)(struct seq_file *, void *)); |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 76 | #else |
| 77 | #define ipc_init_proc_interface(path, header, ids, show) do {} while (0) |
| 78 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Kirill Korotaev | 73ea413 | 2006-10-02 02:18:20 -0700 | [diff] [blame] | 80 | #define IPC_SEM_IDS 0 |
| 81 | #define IPC_MSG_IDS 1 |
| 82 | #define IPC_SHM_IDS 2 |
| 83 | |
Nadia Derbey | ce621f5 | 2007-10-18 23:40:52 -0700 | [diff] [blame] | 84 | #define ipcid_to_idx(id) ((id) % SEQ_MULTIPLIER) |
| 85 | |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 86 | /* must be called with ids->rw_mutex acquired for writing */ |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 87 | int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int); |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 88 | |
| 89 | /* must be called with ids->rw_mutex acquired for reading */ |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 90 | int ipc_get_maxid(struct ipc_ids *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | /* must be called with both locks acquired. */ |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 93 | void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 95 | /* must be called with ipcp locked */ |
| 96 | int ipcperms(struct kern_ipc_perm *ipcp, short flg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | /* for rare, potentially huge allocations. |
| 99 | * both function can sleep |
| 100 | */ |
| 101 | void* ipc_alloc(int size); |
| 102 | void ipc_free(void* ptr, int size); |
| 103 | |
| 104 | /* |
| 105 | * For allocation that need to be freed by RCU. |
| 106 | * Objects are reference counted, they start with reference count 1. |
| 107 | * getref increases the refcount, the putref call that reduces the recount |
| 108 | * to 0 schedules the rcu destruction. Caller must guarantee locking. |
| 109 | */ |
| 110 | void* ipc_rcu_alloc(int size); |
| 111 | void ipc_rcu_getref(void *ptr); |
| 112 | void ipc_rcu_putref(void *ptr); |
| 113 | |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 114 | /* |
| 115 | * ipc_lock_down: called with rw_mutex held |
| 116 | * ipc_lock: called without that lock held |
| 117 | */ |
| 118 | struct kern_ipc_perm *ipc_lock_down(struct ipc_ids *, int); |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 119 | struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out); |
| 122 | void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out); |
| 123 | |
Chris Zankel | 813e678 | 2005-07-12 13:58:25 -0700 | [diff] [blame] | 124 | #if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__) || defined(__XTENSA__) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | /* On IA-64, we always use the "64-bit version" of the IPC structures. */ |
| 126 | # define ipc_parse_version(cmd) IPC_64 |
| 127 | #else |
| 128 | int ipc_parse_version (int *cmd); |
| 129 | #endif |
| 130 | |
| 131 | extern void free_msg(struct msg_msg *msg); |
| 132 | extern struct msg_msg *load_msg(const void __user *src, int len); |
| 133 | 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] | 134 | extern int ipcget_new(struct ipc_namespace *, struct ipc_ids *, |
| 135 | struct ipc_ops *, struct ipc_params *); |
| 136 | extern int ipcget_public(struct ipc_namespace *, struct ipc_ids *, |
| 137 | struct ipc_ops *, struct ipc_params *); |
| 138 | |
Nadia Derbey | 1b531f2 | 2007-10-18 23:40:55 -0700 | [diff] [blame] | 139 | static inline int ipc_buildid(int id, int seq) |
Nadia Derbey | 2802831 | 2007-10-18 23:40:52 -0700 | [diff] [blame] | 140 | { |
| 141 | return SEQ_MULTIPLIER * seq + id; |
| 142 | } |
| 143 | |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 144 | /* |
| 145 | * Must be called with ipcp locked |
| 146 | */ |
Nadia Derbey | 1b531f2 | 2007-10-18 23:40:55 -0700 | [diff] [blame] | 147 | static inline int ipc_checkid(struct kern_ipc_perm *ipcp, int uid) |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 148 | { |
| 149 | if (uid / SEQ_MULTIPLIER != ipcp->seq) |
| 150 | return 1; |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static inline void ipc_lock_by_ptr(struct kern_ipc_perm *perm) |
| 155 | { |
| 156 | rcu_read_lock(); |
| 157 | spin_lock(&perm->lock); |
| 158 | } |
| 159 | |
| 160 | static inline void ipc_unlock(struct kern_ipc_perm *perm) |
| 161 | { |
| 162 | spin_unlock(&perm->lock); |
| 163 | rcu_read_unlock(); |
| 164 | } |
| 165 | |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 166 | static inline struct kern_ipc_perm *ipc_lock_check_down(struct ipc_ids *ids, |
| 167 | int id) |
| 168 | { |
| 169 | struct kern_ipc_perm *out; |
| 170 | |
| 171 | out = ipc_lock_down(ids, id); |
| 172 | if (IS_ERR(out)) |
| 173 | return out; |
| 174 | |
Nadia Derbey | 1b531f2 | 2007-10-18 23:40:55 -0700 | [diff] [blame] | 175 | if (ipc_checkid(out, id)) { |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 176 | ipc_unlock(out); |
| 177 | return ERR_PTR(-EIDRM); |
| 178 | } |
| 179 | |
| 180 | return out; |
| 181 | } |
| 182 | |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 183 | static inline struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids, |
| 184 | int id) |
| 185 | { |
| 186 | struct kern_ipc_perm *out; |
| 187 | |
| 188 | out = ipc_lock(ids, id); |
| 189 | if (IS_ERR(out)) |
| 190 | return out; |
| 191 | |
Nadia Derbey | 1b531f2 | 2007-10-18 23:40:55 -0700 | [diff] [blame] | 192 | if (ipc_checkid(out, id)) { |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 193 | ipc_unlock(out); |
| 194 | return ERR_PTR(-EIDRM); |
| 195 | } |
| 196 | |
| 197 | return out; |
| 198 | } |
| 199 | |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 200 | /** |
| 201 | * ipcget - Common sys_*get() code |
| 202 | * @ns : namsepace |
| 203 | * @ids : IPC identifier set |
| 204 | * @ops : operations to be called on ipc object creation, permission checks |
| 205 | * and further checks |
| 206 | * @params : the parameters needed by the previous operations. |
| 207 | * |
| 208 | * Common routine called by sys_msgget(), sys_semget() and sys_shmget(). |
| 209 | */ |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 210 | static inline int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids, |
| 211 | struct ipc_ops *ops, struct ipc_params *params) |
| 212 | { |
| 213 | if (params->key == IPC_PRIVATE) |
| 214 | return ipcget_new(ns, ids, ops, params); |
| 215 | else |
| 216 | return ipcget_public(ns, ids, ops, params); |
| 217 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
| 219 | #endif |