blob: 9ffea40457cedb17eca4cc9df13ed1b3a5f1a3f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/ipc/util.h
3 * Copyright (C) 1999 Christoph Rohland
4 *
Christian Kujau624dffc2006-01-15 02:43:54 +01005 * ipc helper functions (c) 1999 Manfred Spraul <manfred@colorfullife.com>
Kirill Korotaev73ea4132006-10-02 02:18:20 -07006 * namespaces support. 2006 OpenVZ, SWsoft Inc.
7 * Pavel Emelianov <xemul@openvz.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#ifndef _IPC_UTIL_H
11#define _IPC_UTIL_H
12
Nadia Derbey7ca7e562007-10-18 23:40:48 -070013#include <linux/idr.h>
Nadia Derbey023a5352007-10-18 23:40:51 -070014#include <linux/err.h>
Nadia Derbey7ca7e562007-10-18 23:40:48 -070015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#define USHRT_MAX 0xffff
17#define SEQ_MULTIPLIER (IPCMNI)
18
19void sem_init (void);
20void msg_init (void);
21void shm_init (void);
22
Kirill Korotaev73ea4132006-10-02 02:18:20 -070023int sem_init_ns(struct ipc_namespace *ns);
24int msg_init_ns(struct ipc_namespace *ns);
25int shm_init_ns(struct ipc_namespace *ns);
26
27void sem_exit_ns(struct ipc_namespace *ns);
28void msg_exit_ns(struct ipc_namespace *ns);
29void shm_exit_ns(struct ipc_namespace *ns);
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031struct ipc_ids {
32 int in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 unsigned short seq;
34 unsigned short seq_max;
Nadia Derbey3e148c72007-10-18 23:40:54 -070035 struct rw_semaphore rw_mutex;
Nadia Derbey7ca7e562007-10-18 23:40:48 -070036 struct idr ipcs_idr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
Nadia Derbey7748dbf2007-10-18 23:40:49 -070039/*
40 * Structure that holds the parameters needed by the ipc operations
41 * (see after)
42 */
43struct 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
Nadia Derbeyf4566f02007-10-18 23:40:53 -070057 * . routine to call to check permissions for a new ipc object.
Nadia Derbey7748dbf2007-10-18 23:40:49 -070058 * 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 */
62struct ipc_ops {
63 int (*getnew) (struct ipc_namespace *, struct ipc_params *);
Nadia Derbey03f02c72007-10-18 23:40:51 -070064 int (*associate) (struct kern_ipc_perm *, int);
65 int (*more_checks) (struct kern_ipc_perm *, struct ipc_params *);
Nadia Derbey7748dbf2007-10-18 23:40:49 -070066};
67
Mike Waychisonae781772005-09-06 15:17:09 -070068struct seq_file;
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070069
Nadia Derbey7ca7e562007-10-18 23:40:48 -070070void ipc_init_ids(struct ipc_ids *);
Mike Waychisonae781772005-09-06 15:17:09 -070071#ifdef CONFIG_PROC_FS
72void __init ipc_init_proc_interface(const char *path, const char *header,
Kirill Korotaev73ea4132006-10-02 02:18:20 -070073 int ids, int (*show)(struct seq_file *, void *));
Mike Waychisonae781772005-09-06 15:17:09 -070074#else
75#define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
76#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Kirill Korotaev73ea4132006-10-02 02:18:20 -070078#define IPC_SEM_IDS 0
79#define IPC_MSG_IDS 1
80#define IPC_SHM_IDS 2
81
Nadia Derbeyce621f52007-10-18 23:40:52 -070082#define ipcid_to_idx(id) ((id) % SEQ_MULTIPLIER)
83
Nadia Derbey3e148c72007-10-18 23:40:54 -070084/* must be called with ids->rw_mutex acquired for writing */
Nadia Derbey7ca7e562007-10-18 23:40:48 -070085int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
Nadia Derbey3e148c72007-10-18 23:40:54 -070086
87/* must be called with ids->rw_mutex acquired for reading */
Nadia Derbey7ca7e562007-10-18 23:40:48 -070088int ipc_get_maxid(struct ipc_ids *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/* must be called with both locks acquired. */
Nadia Derbey7ca7e562007-10-18 23:40:48 -070091void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Nadia Derbeyf4566f02007-10-18 23:40:53 -070093/* must be called with ipcp locked */
94int ipcperms(struct kern_ipc_perm *ipcp, short flg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/* for rare, potentially huge allocations.
97 * both function can sleep
98 */
99void* ipc_alloc(int size);
100void ipc_free(void* ptr, int size);
101
102/*
103 * For allocation that need to be freed by RCU.
104 * Objects are reference counted, they start with reference count 1.
105 * getref increases the refcount, the putref call that reduces the recount
106 * to 0 schedules the rcu destruction. Caller must guarantee locking.
107 */
108void* ipc_rcu_alloc(int size);
109void ipc_rcu_getref(void *ptr);
110void ipc_rcu_putref(void *ptr);
111
Nadia Derbey3e148c72007-10-18 23:40:54 -0700112/*
113 * ipc_lock_down: called with rw_mutex held
114 * ipc_lock: called without that lock held
115 */
116struct kern_ipc_perm *ipc_lock_down(struct ipc_ids *, int);
Nadia Derbey023a5352007-10-18 23:40:51 -0700117struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
120void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
121
Chris Zankel813e6782005-07-12 13:58:25 -0700122#if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__) || defined(__XTENSA__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 /* On IA-64, we always use the "64-bit version" of the IPC structures. */
124# define ipc_parse_version(cmd) IPC_64
125#else
126int ipc_parse_version (int *cmd);
127#endif
128
129extern void free_msg(struct msg_msg *msg);
130extern struct msg_msg *load_msg(const void __user *src, int len);
131extern int store_msg(void __user *dest, struct msg_msg *msg, int len);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700132extern int ipcget_new(struct ipc_namespace *, struct ipc_ids *,
133 struct ipc_ops *, struct ipc_params *);
134extern int ipcget_public(struct ipc_namespace *, struct ipc_ids *,
135 struct ipc_ops *, struct ipc_params *);
136
Nadia Derbey1b531f22007-10-18 23:40:55 -0700137static inline int ipc_buildid(int id, int seq)
Nadia Derbey28028312007-10-18 23:40:52 -0700138{
139 return SEQ_MULTIPLIER * seq + id;
140}
141
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700142/*
143 * Must be called with ipcp locked
144 */
Nadia Derbey1b531f22007-10-18 23:40:55 -0700145static inline int ipc_checkid(struct kern_ipc_perm *ipcp, int uid)
Nadia Derbey023a5352007-10-18 23:40:51 -0700146{
147 if (uid / SEQ_MULTIPLIER != ipcp->seq)
148 return 1;
149 return 0;
150}
151
152static inline void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
153{
154 rcu_read_lock();
155 spin_lock(&perm->lock);
156}
157
158static inline void ipc_unlock(struct kern_ipc_perm *perm)
159{
160 spin_unlock(&perm->lock);
161 rcu_read_unlock();
162}
163
Nadia Derbey3e148c72007-10-18 23:40:54 -0700164static inline struct kern_ipc_perm *ipc_lock_check_down(struct ipc_ids *ids,
165 int id)
166{
167 struct kern_ipc_perm *out;
168
169 out = ipc_lock_down(ids, id);
170 if (IS_ERR(out))
171 return out;
172
Nadia Derbey1b531f22007-10-18 23:40:55 -0700173 if (ipc_checkid(out, id)) {
Nadia Derbey3e148c72007-10-18 23:40:54 -0700174 ipc_unlock(out);
175 return ERR_PTR(-EIDRM);
176 }
177
178 return out;
179}
180
Nadia Derbey023a5352007-10-18 23:40:51 -0700181static inline struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids,
182 int id)
183{
184 struct kern_ipc_perm *out;
185
186 out = ipc_lock(ids, id);
187 if (IS_ERR(out))
188 return out;
189
Nadia Derbey1b531f22007-10-18 23:40:55 -0700190 if (ipc_checkid(out, id)) {
Nadia Derbey023a5352007-10-18 23:40:51 -0700191 ipc_unlock(out);
192 return ERR_PTR(-EIDRM);
193 }
194
195 return out;
196}
197
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700198/**
199 * ipcget - Common sys_*get() code
200 * @ns : namsepace
201 * @ids : IPC identifier set
202 * @ops : operations to be called on ipc object creation, permission checks
203 * and further checks
204 * @params : the parameters needed by the previous operations.
205 *
206 * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
207 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700208static inline int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
209 struct ipc_ops *ops, struct ipc_params *params)
210{
211 if (params->key == IPC_PRIVATE)
212 return ipcget_new(ns, ids, ops, params);
213 else
214 return ipcget_public(ns, ids, ops, params);
215}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217#endif