1 /*
2  * linux/ipc/util.h
3  * Copyright (C) 1999 Christoph Rohland
4  *
5  * ipc helper functions (c) 1999 Manfred Spraul <manfreds@colorfullife.com>
6  */
7 
8 #define USHRT_MAX 0xffff
9 #define SEQ_MULTIPLIER	(IPCMNI)
10 
11 void sem_init (void);
12 void msg_init (void);
13 void shm_init (void);
14 
15 struct ipc_ids {
16 	int size;
17 	int in_use;
18 	int max_id;
19 	unsigned short seq;
20 	unsigned short seq_max;
21 	struct semaphore sem;
22 	spinlock_t ary;
23 	struct ipc_id* entries;
24 };
25 
26 struct ipc_id {
27 	struct kern_ipc_perm* p;
28 };
29 
30 
31 void __init ipc_init_ids(struct ipc_ids* ids, int size);
32 
33 /* must be called with ids->sem acquired.*/
34 int ipc_findkey(struct ipc_ids* ids, key_t key);
35 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size);
36 
37 /* must be called with both locks acquired. */
38 struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id);
39 
40 int ipcperms (struct kern_ipc_perm *ipcp, short flg);
41 
42 /* for rare, potentially huge allocations.
43  * both function can sleep
44  */
45 void* ipc_alloc(int size);
46 void ipc_free(void* ptr, int size);
47 
ipc_lockall(struct ipc_ids * ids)48 extern inline void ipc_lockall(struct ipc_ids* ids)
49 {
50 	spin_lock(&ids->ary);
51 }
52 
ipc_get(struct ipc_ids * ids,int id)53 extern inline struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
54 {
55 	struct kern_ipc_perm* out;
56 	int lid = id % SEQ_MULTIPLIER;
57 	if(lid >= ids->size)
58 		return NULL;
59 
60 	out = ids->entries[lid].p;
61 	return out;
62 }
63 
ipc_unlockall(struct ipc_ids * ids)64 extern inline void ipc_unlockall(struct ipc_ids* ids)
65 {
66 	spin_unlock(&ids->ary);
67 }
ipc_lock(struct ipc_ids * ids,int id)68 extern inline struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
69 {
70 	struct kern_ipc_perm* out;
71 	int lid = id % SEQ_MULTIPLIER;
72 	if(lid >= ids->size)
73 		return NULL;
74 
75 	spin_lock(&ids->ary);
76 	out = ids->entries[lid].p;
77 	if(out==NULL)
78 		spin_unlock(&ids->ary);
79 	return out;
80 }
81 
ipc_unlock(struct ipc_ids * ids,int id)82 extern inline void ipc_unlock(struct ipc_ids* ids, int id)
83 {
84 	spin_unlock(&ids->ary);
85 }
86 
ipc_buildid(struct ipc_ids * ids,int id,int seq)87 extern inline int ipc_buildid(struct ipc_ids* ids, int id, int seq)
88 {
89 	return SEQ_MULTIPLIER*seq + id;
90 }
91 
ipc_checkid(struct ipc_ids * ids,struct kern_ipc_perm * ipcp,int uid)92 extern inline int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
93 {
94 	if(uid/SEQ_MULTIPLIER != ipcp->seq)
95 		return 1;
96 	return 0;
97 }
98 
99 void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
100 void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
101 
102 #if defined(__ia64__) || defined(__hppa__)
103   /* On IA-64 and PA-RISC, we always use the "64-bit version" of the IPC structures.  */
104 # define ipc_parse_version(cmd)	IPC_64
105 #else
106 int ipc_parse_version (int *cmd);
107 #endif
108