Blame view

ipc/util.h 7.04 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  /* SPDX-License-Identifier: GPL-2.0 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
  /*
   * linux/ipc/util.h
   * Copyright (C) 1999 Christoph Rohland
   *
624dffcbc   Christian Kujau   correct email add...
6
   * ipc helper functions (c) 1999 Manfred Spraul <manfred@colorfullife.com>
73ea41302   Kirill Korotaev   [PATCH] IPC names...
7
8
   * namespaces support.      2006 OpenVZ, SWsoft Inc.
   *                               Pavel Emelianov <xemul@openvz.org>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
10
11
12
   */
  
  #ifndef _IPC_UTIL_H
  #define _IPC_UTIL_H
232086b19   Johannes Weiner   ipc: unbreak 32-b...
13
  #include <linux/unistd.h>
023a53557   Nadia Derbey   ipc: integrate ip...
14
  #include <linux/err.h>
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
15

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
16
  #define SEQ_MULTIPLIER	(IPCMNI)
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
17
18
  int sem_init(void);
  int msg_init(void);
239521f31   Manfred Spraul   ipc: whitespace c...
19
  void shm_init(void);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20

ae5e1b22f   Pavel Emelyanov   namespaces: move ...
21
  struct ipc_namespace;
614b84cf4   Serge E. Hallyn   namespaces: mqueu...
22
  #ifdef CONFIG_POSIX_MQUEUE
7eafd7c74   Serge E. Hallyn   namespaces: ipc n...
23
24
  extern void mq_clear_sbinfo(struct ipc_namespace *ns);
  extern void mq_put_mnt(struct ipc_namespace *ns);
614b84cf4   Serge E. Hallyn   namespaces: mqueu...
25
  #else
7eafd7c74   Serge E. Hallyn   namespaces: ipc n...
26
27
  static inline void mq_clear_sbinfo(struct ipc_namespace *ns) { }
  static inline void mq_put_mnt(struct ipc_namespace *ns) { }
614b84cf4   Serge E. Hallyn   namespaces: mqueu...
28
29
30
  #endif
  
  #ifdef CONFIG_SYSVIPC
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
31
32
33
  int sem_init_ns(struct ipc_namespace *ns);
  int msg_init_ns(struct ipc_namespace *ns);
  int shm_init_ns(struct ipc_namespace *ns);
73ea41302   Kirill Korotaev   [PATCH] IPC names...
34
35
36
37
  
  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   Serge E. Hallyn   namespaces: mqueu...
38
  #else
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
39
40
41
  static inline int sem_init_ns(struct ipc_namespace *ns) { return 0; }
  static inline int msg_init_ns(struct ipc_namespace *ns) { return 0; }
  static inline int shm_init_ns(struct ipc_namespace *ns) { return 0; }
614b84cf4   Serge E. Hallyn   namespaces: mqueu...
42
43
44
45
46
  
  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   Kirill Korotaev   [PATCH] IPC names...
47

7748dbfaa   Nadia Derbey   ipc: unify the sy...
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  /*
   * 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   Nadia Derbey   ipc: fix wrong co...
66
   *      . routine to call to check permissions for a new ipc object.
7748dbfaa   Nadia Derbey   ipc: unify the sy...
67
68
69
70
71
   *        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   Paul McQuade   ipc, kernel: clea...
72
73
74
  	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   Nadia Derbey   ipc: unify the sy...
75
  };
ae7817745   Mike Waychison   [PATCH] ipc: add ...
76
  struct seq_file;
ed2ddbf88   Pierre Peiffer   IPC: make struct ...
77
  struct ipc_ids;
7d69a1f4a   Cedric Le Goater   remove CONFIG_UTS...
78

0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
79
  int ipc_init_ids(struct ipc_ids *);
ae7817745   Mike Waychison   [PATCH] ipc: add ...
80
81
  #ifdef CONFIG_PROC_FS
  void __init ipc_init_proc_interface(const char *path, const char *header,
73ea41302   Kirill Korotaev   [PATCH] IPC names...
82
  		int ids, int (*show)(struct seq_file *, void *));
ae7817745   Mike Waychison   [PATCH] ipc: add ...
83
84
85
  #else
  #define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86

73ea41302   Kirill Korotaev   [PATCH] IPC names...
87
88
89
  #define IPC_SEM_IDS	0
  #define IPC_MSG_IDS	1
  #define IPC_SHM_IDS	2
ce621f5ba   Nadia Derbey   ipc: introduce th...
90
  #define ipcid_to_idx(id) ((id) % SEQ_MULTIPLIER)
03f595668   Stanislav Kinsbursky   ipc: add sysctl t...
91
  #define ipcid_to_seqx(id) ((id) / SEQ_MULTIPLIER)
daf948c7d   Davidlohr Bueso   ipc: delete seq_m...
92
  #define IPCID_SEQ_MAX min_t(int, INT_MAX/SEQ_MULTIPLIER, USHRT_MAX)
ce621f5ba   Nadia Derbey   ipc: introduce th...
93

d9a605e40   Davidlohr Bueso   ipc: rename ids->...
94
  /* must be called with ids->rwsem acquired for writing */
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
95
  int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
3e148c799   Nadia Derbey   fix idr_find() lo...
96

d9a605e40   Davidlohr Bueso   ipc: rename ids->...
97
  /* must be called with ids->rwsem acquired for reading */
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
98
  int ipc_get_maxid(struct ipc_ids *);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
100
  
  /* must be called with both locks acquired. */
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
101
  void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102

0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
103
104
  /* must be called with both locks acquired. */
  void ipc_set_key_private(struct ipc_ids *, struct kern_ipc_perm *);
f4566f048   Nadia Derbey   ipc: fix wrong co...
105
  /* must be called with ipcp locked */
b0e77598f   Serge E. Hallyn   userns: user name...
106
  int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
109
110
111
112
  /*
   * 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   Manfred Spraul   ipc/util.h: updat...
113
114
115
   *
   * refcount is initialized by ipc_addid(), before that point call_rcu()
   * must be used.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
   */
dba4cdd39   Manfred Spraul   ipc: merge ipc_rc...
117
118
119
  int ipc_rcu_getref(struct kern_ipc_perm *ptr);
  void ipc_rcu_putref(struct kern_ipc_perm *ptr,
  			void (*func)(struct rcu_head *head));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120

023a53557   Nadia Derbey   ipc: integrate ip...
121
  struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int);
55b7ae501   Davidlohr Bueso   ipc: rename ipc_o...
122
  struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
124
125
  
  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   Eric W. Biederman   userns: Convert i...
126
  int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out);
444d0f621   Davidlohr Bueso   ipc: introduce lo...
127
128
129
  struct kern_ipc_perm *ipcctl_pre_down_nolock(struct ipc_namespace *ns,
  					     struct ipc_ids *ids, int id, int cmd,
  					     struct ipc64_perm *perm, int extra_perm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
130

c1d7e01d7   Will Deacon   ipc: use Kconfig ...
131
  #ifndef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
46c0a8ca3   Paul McQuade   ipc, kernel: clea...
132
  /* On IA-64, we always use the "64-bit version" of the IPC structures.  */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
134
  # define ipc_parse_version(cmd)	IPC_64
  #else
239521f31   Manfred Spraul   ipc: whitespace c...
135
  int ipc_parse_version(int *cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
137
138
  #endif
  
  extern void free_msg(struct msg_msg *msg);
4e9b45a19   Mathias Krause   ipc, msg: fix mes...
139
  extern struct msg_msg *load_msg(const void __user *src, size_t len);
4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
140
  extern struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst);
4e9b45a19   Mathias Krause   ipc, msg: fix mes...
141
  extern int store_msg(void __user *dest, struct msg_msg *msg, size_t len);
7748dbfaa   Nadia Derbey   ipc: unify the sy...
142

1b531f213   Nadia Derbey   ipc: remove unnee...
143
  static inline int ipc_buildid(int id, int seq)
280283131   Nadia Derbey   ipc: inline ipc_b...
144
145
146
  {
  	return SEQ_MULTIPLIER * seq + id;
  }
1b531f213   Nadia Derbey   ipc: remove unnee...
147
  static inline int ipc_checkid(struct kern_ipc_perm *ipcp, int uid)
023a53557   Nadia Derbey   ipc: integrate ip...
148
  {
7bb4deff6   Davidlohr Bueso   ipc: remove bogus...
149
  	return uid / SEQ_MULTIPLIER != ipcp->seq;
023a53557   Nadia Derbey   ipc: integrate ip...
150
  }
1ca7003ab   Davidlohr Bueso   ipc: introduce ip...
151
  static inline void ipc_lock_object(struct kern_ipc_perm *perm)
023a53557   Nadia Derbey   ipc: integrate ip...
152
  {
023a53557   Nadia Derbey   ipc: integrate ip...
153
154
  	spin_lock(&perm->lock);
  }
1ca7003ab   Davidlohr Bueso   ipc: introduce ip...
155
  static inline void ipc_unlock_object(struct kern_ipc_perm *perm)
023a53557   Nadia Derbey   ipc: integrate ip...
156
157
  {
  	spin_unlock(&perm->lock);
023a53557   Nadia Derbey   ipc: integrate ip...
158
  }
1ca7003ab   Davidlohr Bueso   ipc: introduce ip...
159
  static inline void ipc_assert_locked_object(struct kern_ipc_perm *perm)
16df3674e   Davidlohr Bueso   ipc,sem: do not h...
160
  {
1ca7003ab   Davidlohr Bueso   ipc: introduce ip...
161
162
  	assert_spin_locked(&perm->lock);
  }
1ca7003ab   Davidlohr Bueso   ipc: introduce ip...
163
164
  static inline void ipc_unlock(struct kern_ipc_perm *perm)
  {
cf9d5d78d   Davidlohr Bueso   ipc: close open c...
165
  	ipc_unlock_object(perm);
1ca7003ab   Davidlohr Bueso   ipc: introduce ip...
166
167
  	rcu_read_unlock();
  }
0f3d2b013   Rafael Aquini   ipc: introduce ip...
168
169
170
171
172
173
174
175
176
177
  /*
   * 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   Rafael Aquini   ipc: change kern_...
178
  	return !perm->deleted;
0f3d2b013   Rafael Aquini   ipc: introduce ip...
179
  }
4d2bff5eb   Davidlohr Bueso   ipc: introduce ob...
180
  struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id);
b2d75cddc   Pavel Emelyanov   ipc: uninline som...
181
  int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
eb66ec44f   Mathias Krause   ipc: constify ipc...
182
  			const struct ipc_ops *ops, struct ipc_params *params);
665c7741f   Alexey Dobriyan   ipcns: move free_...
183
184
  void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
  		void (*free)(struct ipc_namespace *, struct kern_ipc_perm *));
553f770ef   Al Viro   ipc: move compat ...
185
186
187
188
189
190
191
192
193
194
195
196
  
  #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   Al Viro   semctl(): move co...
197
198
199
200
201
  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   Al Viro   ipc: move compat ...
202
203
204
205
206
207
208
209
210
211
212
  static inline int compat_ipc_parse_version(int *cmd)
  {
  #ifdef	CONFIG_ARCH_WANT_COMPAT_IPC_PARSE_VERSION
  	int version = *cmd & IPC_64;
  	*cmd &= ~IPC_64;
  	return version;
  #else
  	return IPC_64;
  #endif
  }
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
  #endif