blob: 30b2a6d7cbed9655cf059f688670ce93122e36df [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>
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#define USHRT_MAX 0xffff
16#define SEQ_MULTIPLIER (IPCMNI)
17
18void sem_init (void);
19void msg_init (void);
20void shm_init (void);
21
Kirill Korotaev73ea4132006-10-02 02:18:20 -070022int sem_init_ns(struct ipc_namespace *ns);
23int msg_init_ns(struct ipc_namespace *ns);
24int shm_init_ns(struct ipc_namespace *ns);
25
26void sem_exit_ns(struct ipc_namespace *ns);
27void msg_exit_ns(struct ipc_namespace *ns);
28void shm_exit_ns(struct ipc_namespace *ns);
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030struct ipc_ids {
31 int in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 unsigned short seq;
33 unsigned short seq_max;
Ingo Molnar5f921ae2006-03-26 01:37:17 -080034 struct mutex mutex;
Nadia Derbey7ca7e562007-10-18 23:40:48 -070035 struct idr ipcs_idr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036};
37
Nadia Derbey7748dbf2007-10-18 23:40:49 -070038/*
39 * Structure that holds the parameters needed by the ipc operations
40 * (see after)
41 */
42struct ipc_params {
43 key_t key;
44 int flg;
45 union {
46 size_t size; /* for shared memories */
47 int nsems; /* for semaphores */
48 } u; /* holds the getnew() specific param */
49};
50
51/*
52 * Structure that holds some ipc operations. This structure is used to unify
53 * the calls to sys_msgget(), sys_semget(), sys_shmget()
54 * . routine to call to create a new ipc object. Can be one of newque,
55 * newary, newseg
56 * . routine to call to call to check permissions for a new ipc object.
57 * Can be one of security_msg_associate, security_sem_associate,
58 * security_shm_associate
59 * . routine to call for an extra check if needed
60 */
61struct ipc_ops {
62 int (*getnew) (struct ipc_namespace *, struct ipc_params *);
63 int (*associate) (void *, int);
64 int (*more_checks) (void *, struct ipc_params *);
65};
66
Mike Waychisonae781772005-09-06 15:17:09 -070067struct seq_file;
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070068
Nadia Derbey7ca7e562007-10-18 23:40:48 -070069void ipc_init_ids(struct ipc_ids *);
Mike Waychisonae781772005-09-06 15:17:09 -070070#ifdef CONFIG_PROC_FS
71void __init ipc_init_proc_interface(const char *path, const char *header,
Kirill Korotaev73ea4132006-10-02 02:18:20 -070072 int ids, int (*show)(struct seq_file *, void *));
Mike Waychisonae781772005-09-06 15:17:09 -070073#else
74#define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
75#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Kirill Korotaev73ea4132006-10-02 02:18:20 -070077#define IPC_SEM_IDS 0
78#define IPC_MSG_IDS 1
79#define IPC_SHM_IDS 2
80
Ingo Molnar5f921ae2006-03-26 01:37:17 -080081/* must be called with ids->mutex acquired.*/
Nadia Derbey7ca7e562007-10-18 23:40:48 -070082int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
83int ipc_get_maxid(struct ipc_ids *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/* must be called with both locks acquired. */
Nadia Derbey7ca7e562007-10-18 23:40:48 -070086void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88int ipcperms (struct kern_ipc_perm *ipcp, short flg);
89
90/* for rare, potentially huge allocations.
91 * both function can sleep
92 */
93void* ipc_alloc(int size);
94void ipc_free(void* ptr, int size);
95
96/*
97 * For allocation that need to be freed by RCU.
98 * Objects are reference counted, they start with reference count 1.
99 * getref increases the refcount, the putref call that reduces the recount
100 * to 0 schedules the rcu destruction. Caller must guarantee locking.
101 */
102void* ipc_rcu_alloc(int size);
103void ipc_rcu_getref(void *ptr);
104void ipc_rcu_putref(void *ptr);
105
106struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id);
107struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id);
108void ipc_lock_by_ptr(struct kern_ipc_perm *ipcp);
109void ipc_unlock(struct kern_ipc_perm* perm);
110int ipc_buildid(struct ipc_ids* ids, int id, int seq);
111int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid);
112
113void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
114void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
115
Chris Zankel813e6782005-07-12 13:58:25 -0700116#if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__) || defined(__XTENSA__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /* On IA-64, we always use the "64-bit version" of the IPC structures. */
118# define ipc_parse_version(cmd) IPC_64
119#else
120int ipc_parse_version (int *cmd);
121#endif
122
123extern void free_msg(struct msg_msg *msg);
124extern struct msg_msg *load_msg(const void __user *src, int len);
125extern int store_msg(void __user *dest, struct msg_msg *msg, int len);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700126extern int ipcget_new(struct ipc_namespace *, struct ipc_ids *,
127 struct ipc_ops *, struct ipc_params *);
128extern int ipcget_public(struct ipc_namespace *, struct ipc_ids *,
129 struct ipc_ops *, struct ipc_params *);
130
131static inline int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
132 struct ipc_ops *ops, struct ipc_params *params)
133{
134 if (params->key == IPC_PRIVATE)
135 return ipcget_new(ns, ids, ops, params);
136 else
137 return ipcget_public(ns, ids, ops, params);
138}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140#endif