blob: c8fd6b9d77b5e0bfddcae8b2bbf5f40d6ae4ea03 [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
13#define USHRT_MAX 0xffff
14#define SEQ_MULTIPLIER (IPCMNI)
15
16void sem_init (void);
17void msg_init (void);
18void shm_init (void);
19
Kirill Korotaev73ea4132006-10-02 02:18:20 -070020int sem_init_ns(struct ipc_namespace *ns);
21int msg_init_ns(struct ipc_namespace *ns);
22int shm_init_ns(struct ipc_namespace *ns);
23
24void sem_exit_ns(struct ipc_namespace *ns);
25void msg_exit_ns(struct ipc_namespace *ns);
26void shm_exit_ns(struct ipc_namespace *ns);
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028struct ipc_id_ary {
29 int size;
30 struct kern_ipc_perm *p[0];
31};
32
33struct ipc_ids {
34 int in_use;
35 int max_id;
36 unsigned short seq;
37 unsigned short seq_max;
Ingo Molnar5f921ae2006-03-26 01:37:17 -080038 struct mutex mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct ipc_id_ary nullentry;
40 struct ipc_id_ary* entries;
41};
42
Mike Waychisonae781772005-09-06 15:17:09 -070043struct seq_file;
Kirill Korotaev73ea4132006-10-02 02:18:20 -070044#ifdef CONFIG_IPC_NS
45#define __ipc_init
46#else
47#define __ipc_init __init
48#endif
49void __ipc_init ipc_init_ids(struct ipc_ids *ids, int size);
Mike Waychisonae781772005-09-06 15:17:09 -070050#ifdef CONFIG_PROC_FS
51void __init ipc_init_proc_interface(const char *path, const char *header,
Kirill Korotaev73ea4132006-10-02 02:18:20 -070052 int ids, int (*show)(struct seq_file *, void *));
Mike Waychisonae781772005-09-06 15:17:09 -070053#else
54#define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
55#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Kirill Korotaev73ea4132006-10-02 02:18:20 -070057#define IPC_SEM_IDS 0
58#define IPC_MSG_IDS 1
59#define IPC_SHM_IDS 2
60
Ingo Molnar5f921ae2006-03-26 01:37:17 -080061/* must be called with ids->mutex acquired.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070062int ipc_findkey(struct ipc_ids* ids, key_t key);
63int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size);
64
65/* must be called with both locks acquired. */
66struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id);
67
68int ipcperms (struct kern_ipc_perm *ipcp, short flg);
69
70/* for rare, potentially huge allocations.
71 * both function can sleep
72 */
73void* ipc_alloc(int size);
74void ipc_free(void* ptr, int size);
75
76/*
77 * For allocation that need to be freed by RCU.
78 * Objects are reference counted, they start with reference count 1.
79 * getref increases the refcount, the putref call that reduces the recount
80 * to 0 schedules the rcu destruction. Caller must guarantee locking.
81 */
82void* ipc_rcu_alloc(int size);
83void ipc_rcu_getref(void *ptr);
84void ipc_rcu_putref(void *ptr);
85
86struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id);
87struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id);
88void ipc_lock_by_ptr(struct kern_ipc_perm *ipcp);
89void ipc_unlock(struct kern_ipc_perm* perm);
90int ipc_buildid(struct ipc_ids* ids, int id, int seq);
91int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid);
92
93void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
94void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
95
Chris Zankel813e6782005-07-12 13:58:25 -070096#if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__) || defined(__XTENSA__)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 /* On IA-64, we always use the "64-bit version" of the IPC structures. */
98# define ipc_parse_version(cmd) IPC_64
99#else
100int ipc_parse_version (int *cmd);
101#endif
102
103extern void free_msg(struct msg_msg *msg);
104extern struct msg_msg *load_msg(const void __user *src, int len);
105extern int store_msg(void __user *dest, struct msg_msg *msg, int len);
106
107#endif