blob: c9063267d4f8fc15a0be1dd00a3d83fb029c2333 [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
Mike Waychisonae781772005-09-06 15:17:09 -070038struct seq_file;
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070039
Nadia Derbey7ca7e562007-10-18 23:40:48 -070040void ipc_init_ids(struct ipc_ids *);
Mike Waychisonae781772005-09-06 15:17:09 -070041#ifdef CONFIG_PROC_FS
42void __init ipc_init_proc_interface(const char *path, const char *header,
Kirill Korotaev73ea4132006-10-02 02:18:20 -070043 int ids, int (*show)(struct seq_file *, void *));
Mike Waychisonae781772005-09-06 15:17:09 -070044#else
45#define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
46#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Kirill Korotaev73ea4132006-10-02 02:18:20 -070048#define IPC_SEM_IDS 0
49#define IPC_MSG_IDS 1
50#define IPC_SHM_IDS 2
51
Ingo Molnar5f921ae2006-03-26 01:37:17 -080052/* must be called with ids->mutex acquired.*/
Nadia Derbey7ca7e562007-10-18 23:40:48 -070053struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key);
54int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
55int ipc_get_maxid(struct ipc_ids *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/* must be called with both locks acquired. */
Nadia Derbey7ca7e562007-10-18 23:40:48 -070058void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60int ipcperms (struct kern_ipc_perm *ipcp, short flg);
61
62/* for rare, potentially huge allocations.
63 * both function can sleep
64 */
65void* ipc_alloc(int size);
66void ipc_free(void* ptr, int size);
67
68/*
69 * For allocation that need to be freed by RCU.
70 * Objects are reference counted, they start with reference count 1.
71 * getref increases the refcount, the putref call that reduces the recount
72 * to 0 schedules the rcu destruction. Caller must guarantee locking.
73 */
74void* ipc_rcu_alloc(int size);
75void ipc_rcu_getref(void *ptr);
76void ipc_rcu_putref(void *ptr);
77
78struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id);
79struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id);
80void ipc_lock_by_ptr(struct kern_ipc_perm *ipcp);
81void ipc_unlock(struct kern_ipc_perm* perm);
82int ipc_buildid(struct ipc_ids* ids, int id, int seq);
83int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid);
84
85void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
86void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
87
Chris Zankel813e6782005-07-12 13:58:25 -070088#if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__) || defined(__XTENSA__)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 /* On IA-64, we always use the "64-bit version" of the IPC structures. */
90# define ipc_parse_version(cmd) IPC_64
91#else
92int ipc_parse_version (int *cmd);
93#endif
94
95extern void free_msg(struct msg_msg *msg);
96extern struct msg_msg *load_msg(const void __user *src, int len);
97extern int store_msg(void __user *dest, struct msg_msg *msg, int len);
98
99#endif