Blame view
ipc/util.h
8.86 KB
b24413180
|
1 |
/* SPDX-License-Identifier: GPL-2.0 */ |
1da177e4c
|
2 3 4 5 |
/* * linux/ipc/util.h * Copyright (C) 1999 Christoph Rohland * |
624dffcbc
|
6 |
* ipc helper functions (c) 1999 Manfred Spraul <manfred@colorfullife.com> |
73ea41302
|
7 8 |
* namespaces support. 2006 OpenVZ, SWsoft Inc. * Pavel Emelianov <xemul@openvz.org> |
1da177e4c
|
9 10 11 12 |
*/ #ifndef _IPC_UTIL_H #define _IPC_UTIL_H |
232086b19
|
13 |
#include <linux/unistd.h> |
023a53557
|
14 |
#include <linux/err.h> |
15df03c87
|
15 |
#include <linux/ipc_namespace.h> |
7ca7e564e
|
16 |
|
5ac893b8c
|
17 18 19 20 21 22 23 24 25 26 27 28 |
/* * The IPC ID contains 2 separate numbers - index and sequence number. * By default, * bits 0-14: index (32k, 15 bits) * bits 15-30: sequence number (64k, 16 bits) * * When IPCMNI extension mode is turned on, the composition changes: * bits 0-23: index (16M, 24 bits) * bits 24-30: sequence number (128, 7 bits) */ #define IPCMNI_SHIFT 15 #define IPCMNI_EXTEND_SHIFT 24 |
99db46ea2
|
29 |
#define IPCMNI_EXTEND_MIN_CYCLE (RADIX_TREE_MAP_SIZE * RADIX_TREE_MAP_SIZE) |
5ac893b8c
|
30 31 32 33 34 35 |
#define IPCMNI (1 << IPCMNI_SHIFT) #define IPCMNI_EXTEND (1 << IPCMNI_EXTEND_SHIFT) #ifdef CONFIG_SYSVIPC_SYSCTL extern int ipc_mni; extern int ipc_mni_shift; |
99db46ea2
|
36 |
extern int ipc_min_cycle; |
5ac893b8c
|
37 |
|
3278a2c20
|
38 |
#define ipcmni_seq_shift() ipc_mni_shift |
5ac893b8c
|
39 40 41 42 43 |
#define IPCMNI_IDX_MASK ((1 << ipc_mni_shift) - 1) #else /* CONFIG_SYSVIPC_SYSCTL */ #define ipc_mni IPCMNI |
99db46ea2
|
44 |
#define ipc_min_cycle ((int)RADIX_TREE_MAP_SIZE) |
3278a2c20
|
45 |
#define ipcmni_seq_shift() IPCMNI_SHIFT |
5ac893b8c
|
46 47 |
#define IPCMNI_IDX_MASK ((1 << IPCMNI_SHIFT) - 1) #endif /* CONFIG_SYSVIPC_SYSCTL */ |
1da177e4c
|
48 |
|
eae04d25a
|
49 50 |
void sem_init(void); void msg_init(void); |
239521f31
|
51 |
void shm_init(void); |
1da177e4c
|
52 |
|
ae5e1b22f
|
53 |
struct ipc_namespace; |
03f1fc091
|
54 |
struct pid_namespace; |
ae5e1b22f
|
55 |
|
614b84cf4
|
56 |
#ifdef CONFIG_POSIX_MQUEUE |
7eafd7c74
|
57 58 |
extern void mq_clear_sbinfo(struct ipc_namespace *ns); extern void mq_put_mnt(struct ipc_namespace *ns); |
614b84cf4
|
59 |
#else |
7eafd7c74
|
60 61 |
static inline void mq_clear_sbinfo(struct ipc_namespace *ns) { } static inline void mq_put_mnt(struct ipc_namespace *ns) { } |
614b84cf4
|
62 63 64 |
#endif #ifdef CONFIG_SYSVIPC |
eae04d25a
|
65 66 67 |
void sem_init_ns(struct ipc_namespace *ns); void msg_init_ns(struct ipc_namespace *ns); void shm_init_ns(struct ipc_namespace *ns); |
73ea41302
|
68 69 70 71 |
void sem_exit_ns(struct ipc_namespace *ns); void msg_exit_ns(struct ipc_namespace *ns); void shm_exit_ns(struct ipc_namespace *ns); |
614b84cf4
|
72 |
#else |
eae04d25a
|
73 74 75 |
static inline void sem_init_ns(struct ipc_namespace *ns) { } static inline void msg_init_ns(struct ipc_namespace *ns) { } static inline void shm_init_ns(struct ipc_namespace *ns) { } |
614b84cf4
|
76 77 78 79 80 |
static inline void sem_exit_ns(struct ipc_namespace *ns) { } static inline void msg_exit_ns(struct ipc_namespace *ns) { } static inline void shm_exit_ns(struct ipc_namespace *ns) { } #endif |
73ea41302
|
81 |
|
7748dbfaa
|
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
/* * Structure that holds the parameters needed by the ipc operations * (see after) */ struct ipc_params { key_t key; int flg; union { size_t size; /* for shared memories */ int nsems; /* for semaphores */ } u; /* holds the getnew() specific param */ }; /* * Structure that holds some ipc operations. This structure is used to unify * the calls to sys_msgget(), sys_semget(), sys_shmget() * . routine to call to create a new ipc object. Can be one of newque, * newary, newseg |
f4566f048
|
100 |
* . routine to call to check permissions for a new ipc object. |
7748dbfaa
|
101 102 103 104 105 |
* Can be one of security_msg_associate, security_sem_associate, * security_shm_associate * . routine to call for an extra check if needed */ struct ipc_ops { |
46c0a8ca3
|
106 107 108 |
int (*getnew)(struct ipc_namespace *, struct ipc_params *); int (*associate)(struct kern_ipc_perm *, int); int (*more_checks)(struct kern_ipc_perm *, struct ipc_params *); |
7748dbfaa
|
109 |
}; |
ae7817745
|
110 |
struct seq_file; |
ed2ddbf88
|
111 |
struct ipc_ids; |
7d69a1f4a
|
112 |
|
eae04d25a
|
113 |
void ipc_init_ids(struct ipc_ids *ids); |
ae7817745
|
114 115 |
#ifdef CONFIG_PROC_FS void __init ipc_init_proc_interface(const char *path, const char *header, |
73ea41302
|
116 |
int ids, int (*show)(struct seq_file *, void *)); |
03f1fc091
|
117 |
struct pid_namespace *ipc_seq_pid_ns(struct seq_file *); |
ae7817745
|
118 119 120 |
#else #define ipc_init_proc_interface(path, header, ids, show) do {} while (0) #endif |
1da177e4c
|
121 |
|
73ea41302
|
122 123 124 |
#define IPC_SEM_IDS 0 #define IPC_MSG_IDS 1 #define IPC_SHM_IDS 2 |
5ac893b8c
|
125 |
#define ipcid_to_idx(id) ((id) & IPCMNI_IDX_MASK) |
3278a2c20
|
126 127 |
#define ipcid_to_seqx(id) ((id) >> ipcmni_seq_shift()) #define ipcid_seq_max() (INT_MAX >> ipcmni_seq_shift()) |
ce621f5ba
|
128 |
|
d9a605e40
|
129 |
/* must be called with ids->rwsem acquired for writing */ |
7ca7e564e
|
130 |
int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int); |
3e148c799
|
131 |
|
1da177e4c
|
132 |
/* must be called with both locks acquired. */ |
7ca7e564e
|
133 |
void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *); |
1da177e4c
|
134 |
|
0cfb6aee7
|
135 136 |
/* must be called with both locks acquired. */ void ipc_set_key_private(struct ipc_ids *, struct kern_ipc_perm *); |
f4566f048
|
137 |
/* must be called with ipcp locked */ |
b0e77598f
|
138 |
int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flg); |
1da177e4c
|
139 |
|
15df03c87
|
140 |
/** |
27c331a17
|
141 |
* ipc_get_maxidx - get the highest assigned index |
15df03c87
|
142 143 |
* @ids: ipc identifier set * |
b869d5be0
|
144 145 146 |
* The function returns the highest assigned index for @ids. The function * doesn't scan the idr tree, it uses a cached value. * |
15df03c87
|
147 148 |
* Called with ipc_ids.rwsem held for reading. */ |
27c331a17
|
149 |
static inline int ipc_get_maxidx(struct ipc_ids *ids) |
15df03c87
|
150 151 152 |
{ if (ids->in_use == 0) return -1; |
5ac893b8c
|
153 154 |
if (ids->in_use == ipc_mni) return ipc_mni - 1; |
15df03c87
|
155 |
|
27c331a17
|
156 |
return ids->max_idx; |
15df03c87
|
157 |
} |
1da177e4c
|
158 159 160 161 162 |
/* * For allocation that need to be freed by RCU. * Objects are reference counted, they start with reference count 1. * getref increases the refcount, the putref call that reduces the recount * to 0 schedules the rcu destruction. Caller must guarantee locking. |
62b49c990
|
163 164 165 |
* * refcount is initialized by ipc_addid(), before that point call_rcu() * must be used. |
1da177e4c
|
166 |
*/ |
2a9d64810
|
167 |
bool ipc_rcu_getref(struct kern_ipc_perm *ptr); |
dba4cdd39
|
168 169 |
void ipc_rcu_putref(struct kern_ipc_perm *ptr, void (*func)(struct rcu_head *head)); |
1da177e4c
|
170 |
|
55b7ae501
|
171 |
struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id); |
1da177e4c
|
172 173 174 |
void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out); void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out); |
1efdb69b0
|
175 |
int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out); |
4241c1a30
|
176 |
struct kern_ipc_perm *ipcctl_obtain_check(struct ipc_namespace *ns, |
444d0f621
|
177 178 |
struct ipc_ids *ids, int id, int cmd, struct ipc64_perm *perm, int extra_perm); |
1da177e4c
|
179 |
|
03f1fc091
|
180 181 182 183 184 185 186 187 |
static inline void ipc_update_pid(struct pid **pos, struct pid *pid) { struct pid *old = *pos; if (old != pid) { *pos = get_pid(pid); put_pid(old); } } |
275f22148
|
188 |
#ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION |
239521f31
|
189 |
int ipc_parse_version(int *cmd); |
1da177e4c
|
190 191 192 |
#endif extern void free_msg(struct msg_msg *msg); |
4e9b45a19
|
193 |
extern struct msg_msg *load_msg(const void __user *src, size_t len); |
4a674f34b
|
194 |
extern struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst); |
4e9b45a19
|
195 |
extern int store_msg(void __user *dest, struct msg_msg *msg, size_t len); |
7748dbfaa
|
196 |
|
27c331a17
|
197 |
static inline int ipc_checkid(struct kern_ipc_perm *ipcp, int id) |
023a53557
|
198 |
{ |
27c331a17
|
199 |
return ipcid_to_seqx(id) != ipcp->seq; |
023a53557
|
200 |
} |
1ca7003ab
|
201 |
static inline void ipc_lock_object(struct kern_ipc_perm *perm) |
023a53557
|
202 |
{ |
023a53557
|
203 204 |
spin_lock(&perm->lock); } |
1ca7003ab
|
205 |
static inline void ipc_unlock_object(struct kern_ipc_perm *perm) |
023a53557
|
206 207 |
{ spin_unlock(&perm->lock); |
023a53557
|
208 |
} |
1ca7003ab
|
209 |
static inline void ipc_assert_locked_object(struct kern_ipc_perm *perm) |
16df3674e
|
210 |
{ |
1ca7003ab
|
211 212 |
assert_spin_locked(&perm->lock); } |
1ca7003ab
|
213 214 |
static inline void ipc_unlock(struct kern_ipc_perm *perm) { |
cf9d5d78d
|
215 |
ipc_unlock_object(perm); |
1ca7003ab
|
216 217 |
rcu_read_unlock(); } |
0f3d2b013
|
218 219 220 221 222 223 224 225 226 227 |
/* * ipc_valid_object() - helper to sort out IPC_RMID races for codepaths * where the respective ipc_ids.rwsem is not being held down. * Checks whether the ipc object is still around or if it's gone already, as * ipc_rmid() may have already freed the ID while the ipc lock was spinning. * Needs to be called with kern_ipc_perm.lock held -- exception made for one * checkpoint case at sys_semtimedop() as noted in code commentary. */ static inline bool ipc_valid_object(struct kern_ipc_perm *perm) { |
72a8ff2f9
|
228 |
return !perm->deleted; |
0f3d2b013
|
229 |
} |
4d2bff5eb
|
230 |
struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id); |
b2d75cddc
|
231 |
int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids, |
eb66ec44f
|
232 |
const struct ipc_ops *ops, struct ipc_params *params); |
665c7741f
|
233 234 |
void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids, void (*free)(struct ipc_namespace *, struct kern_ipc_perm *)); |
553f770ef
|
235 |
|
8c81ddd2a
|
236 237 |
static inline int sem_check_semmni(struct ipc_namespace *ns) { /* |
5ac893b8c
|
238 |
* Check semmni range [0, ipc_mni] |
8c81ddd2a
|
239 240 |
* semmni is the last element of sem_ctls[4] array */ |
5ac893b8c
|
241 |
return ((ns->sem_ctls[3] < 0) || (ns->sem_ctls[3] > ipc_mni)) |
8c81ddd2a
|
242 243 |
? -ERANGE : 0; } |
553f770ef
|
244 245 246 247 248 249 250 251 252 253 254 |
#ifdef CONFIG_COMPAT #include <linux/compat.h> struct compat_ipc_perm { key_t key; __compat_uid_t uid; __compat_gid_t gid; __compat_uid_t cuid; __compat_gid_t cgid; compat_mode_t mode; unsigned short seq; }; |
c0ebccb6f
|
255 256 257 258 259 |
void to_compat_ipc_perm(struct compat_ipc_perm *, struct ipc64_perm *); void to_compat_ipc64_perm(struct compat_ipc64_perm *, struct ipc64_perm *); int get_compat_ipc_perm(struct ipc64_perm *, struct compat_ipc_perm __user *); int get_compat_ipc64_perm(struct ipc64_perm *, struct compat_ipc64_perm __user *); |
553f770ef
|
260 261 |
static inline int compat_ipc_parse_version(int *cmd) { |
553f770ef
|
262 263 264 |
int version = *cmd & IPC_64; *cmd &= ~IPC_64; return version; |
553f770ef
|
265 |
} |
41f4f0e2f
|
266 |
|
275f22148
|
267 268 |
long compat_ksys_old_semctl(int semid, int semnum, int cmd, int arg); long compat_ksys_old_msgctl(int msqid, int cmd, void __user *uptr); |
078faac9e
|
269 270 |
long compat_ksys_msgrcv(int msqid, compat_uptr_t msgp, compat_ssize_t msgsz, compat_long_t msgtyp, int msgflg); |
31c213f21
|
271 272 |
long compat_ksys_msgsnd(int msqid, compat_uptr_t msgp, compat_ssize_t msgsz, int msgflg); |
275f22148
|
273 |
long compat_ksys_old_shmctl(int shmid, int cmd, void __user *uptr); |
fb377eb80
|
274 275 |
#endif |
41f4f0e2f
|
276 |
|
1da177e4c
|
277 |
#endif |