blob: ca245fae2f98f2892cbd29427433804f6337f651 [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
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080023struct ipc_namespace;
24
Kirill Korotaev73ea4132006-10-02 02:18:20 -070025int sem_init_ns(struct ipc_namespace *ns);
26int msg_init_ns(struct ipc_namespace *ns);
27int shm_init_ns(struct ipc_namespace *ns);
28
29void sem_exit_ns(struct ipc_namespace *ns);
30void msg_exit_ns(struct ipc_namespace *ns);
31void shm_exit_ns(struct ipc_namespace *ns);
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033struct ipc_ids {
34 int in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 unsigned short seq;
36 unsigned short seq_max;
Nadia Derbey3e148c72007-10-18 23:40:54 -070037 struct rw_semaphore rw_mutex;
Nadia Derbey7ca7e562007-10-18 23:40:48 -070038 struct idr ipcs_idr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
Nadia Derbey7748dbf2007-10-18 23:40:49 -070041/*
42 * Structure that holds the parameters needed by the ipc operations
43 * (see after)
44 */
45struct 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 Derbeyf4566f02007-10-18 23:40:53 -070059 * . routine to call to check permissions for a new ipc object.
Nadia Derbey7748dbf2007-10-18 23:40:49 -070060 * 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 */
64struct ipc_ops {
65 int (*getnew) (struct ipc_namespace *, struct ipc_params *);
Nadia Derbey03f02c72007-10-18 23:40:51 -070066 int (*associate) (struct kern_ipc_perm *, int);
67 int (*more_checks) (struct kern_ipc_perm *, struct ipc_params *);
Nadia Derbey7748dbf2007-10-18 23:40:49 -070068};
69
Mike Waychisonae781772005-09-06 15:17:09 -070070struct seq_file;
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070071
Nadia Derbey7ca7e562007-10-18 23:40:48 -070072void ipc_init_ids(struct ipc_ids *);
Mike Waychisonae781772005-09-06 15:17:09 -070073#ifdef CONFIG_PROC_FS
74void __init ipc_init_proc_interface(const char *path, const char *header,
Kirill Korotaev73ea4132006-10-02 02:18:20 -070075 int ids, int (*show)(struct seq_file *, void *));
Mike Waychisonae781772005-09-06 15:17:09 -070076#else
77#define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
78#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Kirill Korotaev73ea4132006-10-02 02:18:20 -070080#define IPC_SEM_IDS 0
81#define IPC_MSG_IDS 1
82#define IPC_SHM_IDS 2
83
Nadia Derbeyce621f52007-10-18 23:40:52 -070084#define ipcid_to_idx(id) ((id) % SEQ_MULTIPLIER)
85
Nadia Derbey3e148c72007-10-18 23:40:54 -070086/* must be called with ids->rw_mutex acquired for writing */
Nadia Derbey7ca7e562007-10-18 23:40:48 -070087int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
Nadia Derbey3e148c72007-10-18 23:40:54 -070088
89/* must be called with ids->rw_mutex acquired for reading */
Nadia Derbey7ca7e562007-10-18 23:40:48 -070090int ipc_get_maxid(struct ipc_ids *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* must be called with both locks acquired. */
Nadia Derbey7ca7e562007-10-18 23:40:48 -070093void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Nadia Derbeyf4566f02007-10-18 23:40:53 -070095/* must be called with ipcp locked */
96int ipcperms(struct kern_ipc_perm *ipcp, short flg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/* for rare, potentially huge allocations.
99 * both function can sleep
100 */
101void* ipc_alloc(int size);
102void 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 */
110void* ipc_rcu_alloc(int size);
111void ipc_rcu_getref(void *ptr);
112void ipc_rcu_putref(void *ptr);
113
Nadia Derbey3e148c72007-10-18 23:40:54 -0700114/*
115 * ipc_lock_down: called with rw_mutex held
116 * ipc_lock: called without that lock held
117 */
118struct kern_ipc_perm *ipc_lock_down(struct ipc_ids *, int);
Nadia Derbey023a5352007-10-18 23:40:51 -0700119struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
122void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
123
Chris Zankel813e6782005-07-12 13:58:25 -0700124#if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__) || defined(__XTENSA__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 /* On IA-64, we always use the "64-bit version" of the IPC structures. */
126# define ipc_parse_version(cmd) IPC_64
127#else
128int ipc_parse_version (int *cmd);
129#endif
130
131extern void free_msg(struct msg_msg *msg);
132extern struct msg_msg *load_msg(const void __user *src, int len);
133extern int store_msg(void __user *dest, struct msg_msg *msg, int len);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700134
Nadia Derbey1b531f22007-10-18 23:40:55 -0700135static inline int ipc_buildid(int id, int seq)
Nadia Derbey28028312007-10-18 23:40:52 -0700136{
137 return SEQ_MULTIPLIER * seq + id;
138}
139
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700140/*
141 * Must be called with ipcp locked
142 */
Nadia Derbey1b531f22007-10-18 23:40:55 -0700143static inline int ipc_checkid(struct kern_ipc_perm *ipcp, int uid)
Nadia Derbey023a5352007-10-18 23:40:51 -0700144{
145 if (uid / SEQ_MULTIPLIER != ipcp->seq)
146 return 1;
147 return 0;
148}
149
150static inline void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
151{
152 rcu_read_lock();
153 spin_lock(&perm->lock);
154}
155
156static inline void ipc_unlock(struct kern_ipc_perm *perm)
157{
158 spin_unlock(&perm->lock);
159 rcu_read_unlock();
160}
161
Pavel Emelyanovb2d75cd2008-02-08 04:18:54 -0800162struct kern_ipc_perm *ipc_lock_check_down(struct ipc_ids *ids, int id);
163struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids, int id);
164int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
165 struct ipc_ops *ops, struct ipc_params *params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167#endif