Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/ipc/util.c | |
3 | * Copyright (C) 1992 Krishna Balasubramanian | |
4 | * | |
5 | * Sep 1997 - Call suser() last after "normal" permission checks so we | |
6 | * get BSD style process accounting right. | |
7 | * Occurs in several places in the IPC code. | |
8 | * Chris Evans, <chris@ferret.lmh.ox.ac.uk> | |
9 | * Nov 1999 - ipc helper functions, unified SMP locking | |
624dffcb | 10 | * Manfred Spraul <manfred@colorfullife.com> |
1da177e4 LT |
11 | * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary(). |
12 | * Mingming Cao <cmm@us.ibm.com> | |
13 | */ | |
14 | ||
15 | #include <linux/config.h> | |
16 | #include <linux/mm.h> | |
17 | #include <linux/shm.h> | |
18 | #include <linux/init.h> | |
19 | #include <linux/msg.h> | |
20 | #include <linux/smp_lock.h> | |
21 | #include <linux/vmalloc.h> | |
22 | #include <linux/slab.h> | |
c59ede7b | 23 | #include <linux/capability.h> |
1da177e4 LT |
24 | #include <linux/highuid.h> |
25 | #include <linux/security.h> | |
26 | #include <linux/rcupdate.h> | |
27 | #include <linux/workqueue.h> | |
ae781774 MW |
28 | #include <linux/seq_file.h> |
29 | #include <linux/proc_fs.h> | |
1da177e4 LT |
30 | |
31 | #include <asm/unistd.h> | |
32 | ||
33 | #include "util.h" | |
34 | ||
ae781774 MW |
35 | struct ipc_proc_iface { |
36 | const char *path; | |
37 | const char *header; | |
38 | struct ipc_ids *ids; | |
39 | int (*show)(struct seq_file *, void *); | |
40 | }; | |
41 | ||
1da177e4 LT |
42 | /** |
43 | * ipc_init - initialise IPC subsystem | |
44 | * | |
45 | * The various system5 IPC resources (semaphores, messages and shared | |
46 | * memory are initialised | |
47 | */ | |
48 | ||
49 | static int __init ipc_init(void) | |
50 | { | |
51 | sem_init(); | |
52 | msg_init(); | |
53 | shm_init(); | |
54 | return 0; | |
55 | } | |
56 | __initcall(ipc_init); | |
57 | ||
58 | /** | |
59 | * ipc_init_ids - initialise IPC identifiers | |
60 | * @ids: Identifier set | |
61 | * @size: Number of identifiers | |
62 | * | |
63 | * Given a size for the ipc identifier range (limited below IPCMNI) | |
64 | * set up the sequence range to use then allocate and initialise the | |
65 | * array itself. | |
66 | */ | |
67 | ||
68 | void __init ipc_init_ids(struct ipc_ids* ids, int size) | |
69 | { | |
70 | int i; | |
5f921ae9 IM |
71 | |
72 | mutex_init(&ids->mutex); | |
1da177e4 LT |
73 | |
74 | if(size > IPCMNI) | |
75 | size = IPCMNI; | |
76 | ids->in_use = 0; | |
77 | ids->max_id = -1; | |
78 | ids->seq = 0; | |
79 | { | |
80 | int seq_limit = INT_MAX/SEQ_MULTIPLIER; | |
81 | if(seq_limit > USHRT_MAX) | |
82 | ids->seq_max = USHRT_MAX; | |
83 | else | |
84 | ids->seq_max = seq_limit; | |
85 | } | |
86 | ||
87 | ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size + | |
88 | sizeof(struct ipc_id_ary)); | |
89 | ||
90 | if(ids->entries == NULL) { | |
91 | printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n"); | |
92 | size = 0; | |
93 | ids->entries = &ids->nullentry; | |
94 | } | |
95 | ids->entries->size = size; | |
96 | for(i=0;i<size;i++) | |
97 | ids->entries->p[i] = NULL; | |
98 | } | |
99 | ||
ae781774 MW |
100 | #ifdef CONFIG_PROC_FS |
101 | static struct file_operations sysvipc_proc_fops; | |
102 | /** | |
103 | * ipc_init_proc_interface - Create a proc interface for sysipc types | |
104 | * using a seq_file interface. | |
105 | * @path: Path in procfs | |
106 | * @header: Banner to be printed at the beginning of the file. | |
107 | * @ids: ipc id table to iterate. | |
108 | * @show: show routine. | |
109 | */ | |
110 | void __init ipc_init_proc_interface(const char *path, const char *header, | |
111 | struct ipc_ids *ids, | |
112 | int (*show)(struct seq_file *, void *)) | |
113 | { | |
114 | struct proc_dir_entry *pde; | |
115 | struct ipc_proc_iface *iface; | |
116 | ||
117 | iface = kmalloc(sizeof(*iface), GFP_KERNEL); | |
118 | if (!iface) | |
119 | return; | |
120 | iface->path = path; | |
121 | iface->header = header; | |
122 | iface->ids = ids; | |
123 | iface->show = show; | |
124 | ||
125 | pde = create_proc_entry(path, | |
126 | S_IRUGO, /* world readable */ | |
127 | NULL /* parent dir */); | |
128 | if (pde) { | |
129 | pde->data = iface; | |
130 | pde->proc_fops = &sysvipc_proc_fops; | |
131 | } else { | |
132 | kfree(iface); | |
133 | } | |
134 | } | |
135 | #endif | |
136 | ||
1da177e4 LT |
137 | /** |
138 | * ipc_findkey - find a key in an ipc identifier set | |
139 | * @ids: Identifier set | |
140 | * @key: The key to find | |
141 | * | |
5f921ae9 | 142 | * Requires ipc_ids.mutex locked. |
1da177e4 LT |
143 | * Returns the identifier if found or -1 if not. |
144 | */ | |
145 | ||
146 | int ipc_findkey(struct ipc_ids* ids, key_t key) | |
147 | { | |
148 | int id; | |
149 | struct kern_ipc_perm* p; | |
150 | int max_id = ids->max_id; | |
151 | ||
152 | /* | |
153 | * rcu_dereference() is not needed here | |
5f921ae9 | 154 | * since ipc_ids.mutex is held |
1da177e4 LT |
155 | */ |
156 | for (id = 0; id <= max_id; id++) { | |
157 | p = ids->entries->p[id]; | |
158 | if(p==NULL) | |
159 | continue; | |
160 | if (key == p->key) | |
161 | return id; | |
162 | } | |
163 | return -1; | |
164 | } | |
165 | ||
166 | /* | |
5f921ae9 | 167 | * Requires ipc_ids.mutex locked |
1da177e4 LT |
168 | */ |
169 | static int grow_ary(struct ipc_ids* ids, int newsize) | |
170 | { | |
171 | struct ipc_id_ary* new; | |
172 | struct ipc_id_ary* old; | |
173 | int i; | |
174 | int size = ids->entries->size; | |
175 | ||
176 | if(newsize > IPCMNI) | |
177 | newsize = IPCMNI; | |
178 | if(newsize <= size) | |
179 | return newsize; | |
180 | ||
181 | new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize + | |
182 | sizeof(struct ipc_id_ary)); | |
183 | if(new == NULL) | |
184 | return size; | |
185 | new->size = newsize; | |
186 | memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size + | |
187 | sizeof(struct ipc_id_ary)); | |
188 | for(i=size;i<newsize;i++) { | |
189 | new->p[i] = NULL; | |
190 | } | |
191 | old = ids->entries; | |
192 | ||
193 | /* | |
194 | * Use rcu_assign_pointer() to make sure the memcpyed contents | |
195 | * of the new array are visible before the new array becomes visible. | |
196 | */ | |
197 | rcu_assign_pointer(ids->entries, new); | |
198 | ||
199 | ipc_rcu_putref(old); | |
200 | return newsize; | |
201 | } | |
202 | ||
203 | /** | |
204 | * ipc_addid - add an IPC identifier | |
205 | * @ids: IPC identifier set | |
206 | * @new: new IPC permission set | |
207 | * @size: new size limit for the id array | |
208 | * | |
209 | * Add an entry 'new' to the IPC arrays. The permissions object is | |
210 | * initialised and the first free entry is set up and the id assigned | |
211 | * is returned. The list is returned in a locked state on success. | |
212 | * On failure the list is not locked and -1 is returned. | |
213 | * | |
5f921ae9 | 214 | * Called with ipc_ids.mutex held. |
1da177e4 LT |
215 | */ |
216 | ||
217 | int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size) | |
218 | { | |
219 | int id; | |
220 | ||
221 | size = grow_ary(ids,size); | |
222 | ||
223 | /* | |
224 | * rcu_dereference()() is not needed here since | |
5f921ae9 | 225 | * ipc_ids.mutex is held |
1da177e4 LT |
226 | */ |
227 | for (id = 0; id < size; id++) { | |
228 | if(ids->entries->p[id] == NULL) | |
229 | goto found; | |
230 | } | |
231 | return -1; | |
232 | found: | |
233 | ids->in_use++; | |
234 | if (id > ids->max_id) | |
235 | ids->max_id = id; | |
236 | ||
237 | new->cuid = new->uid = current->euid; | |
238 | new->gid = new->cgid = current->egid; | |
239 | ||
240 | new->seq = ids->seq++; | |
241 | if(ids->seq > ids->seq_max) | |
242 | ids->seq = 0; | |
243 | ||
244 | spin_lock_init(&new->lock); | |
245 | new->deleted = 0; | |
246 | rcu_read_lock(); | |
247 | spin_lock(&new->lock); | |
248 | ids->entries->p[id] = new; | |
249 | return id; | |
250 | } | |
251 | ||
252 | /** | |
253 | * ipc_rmid - remove an IPC identifier | |
254 | * @ids: identifier set | |
255 | * @id: Identifier to remove | |
256 | * | |
257 | * The identifier must be valid, and in use. The kernel will panic if | |
258 | * fed an invalid identifier. The entry is removed and internal | |
259 | * variables recomputed. The object associated with the identifier | |
260 | * is returned. | |
5f921ae9 | 261 | * ipc_ids.mutex and the spinlock for this ID is hold before this function |
1da177e4 LT |
262 | * is called, and remain locked on the exit. |
263 | */ | |
264 | ||
265 | struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id) | |
266 | { | |
267 | struct kern_ipc_perm* p; | |
268 | int lid = id % SEQ_MULTIPLIER; | |
269 | if(lid >= ids->entries->size) | |
270 | BUG(); | |
271 | ||
272 | /* | |
273 | * do not need a rcu_dereference()() here to force ordering | |
5f921ae9 | 274 | * on Alpha, since the ipc_ids.mutex is held. |
1da177e4 LT |
275 | */ |
276 | p = ids->entries->p[lid]; | |
277 | ids->entries->p[lid] = NULL; | |
278 | if(p==NULL) | |
279 | BUG(); | |
280 | ids->in_use--; | |
281 | ||
282 | if (lid == ids->max_id) { | |
283 | do { | |
284 | lid--; | |
285 | if(lid == -1) | |
286 | break; | |
287 | } while (ids->entries->p[lid] == NULL); | |
288 | ids->max_id = lid; | |
289 | } | |
290 | p->deleted = 1; | |
291 | return p; | |
292 | } | |
293 | ||
294 | /** | |
295 | * ipc_alloc - allocate ipc space | |
296 | * @size: size desired | |
297 | * | |
298 | * Allocate memory from the appropriate pools and return a pointer to it. | |
299 | * NULL is returned if the allocation fails | |
300 | */ | |
301 | ||
302 | void* ipc_alloc(int size) | |
303 | { | |
304 | void* out; | |
305 | if(size > PAGE_SIZE) | |
306 | out = vmalloc(size); | |
307 | else | |
308 | out = kmalloc(size, GFP_KERNEL); | |
309 | return out; | |
310 | } | |
311 | ||
312 | /** | |
313 | * ipc_free - free ipc space | |
314 | * @ptr: pointer returned by ipc_alloc | |
315 | * @size: size of block | |
316 | * | |
317 | * Free a block created with ipc_alloc. The caller must know the size | |
318 | * used in the allocation call. | |
319 | */ | |
320 | ||
321 | void ipc_free(void* ptr, int size) | |
322 | { | |
323 | if(size > PAGE_SIZE) | |
324 | vfree(ptr); | |
325 | else | |
326 | kfree(ptr); | |
327 | } | |
328 | ||
329 | /* | |
330 | * rcu allocations: | |
331 | * There are three headers that are prepended to the actual allocation: | |
332 | * - during use: ipc_rcu_hdr. | |
333 | * - during the rcu grace period: ipc_rcu_grace. | |
334 | * - [only if vmalloc]: ipc_rcu_sched. | |
335 | * Their lifetime doesn't overlap, thus the headers share the same memory. | |
336 | * Unlike a normal union, they are right-aligned, thus some container_of | |
337 | * forward/backward casting is necessary: | |
338 | */ | |
339 | struct ipc_rcu_hdr | |
340 | { | |
341 | int refcount; | |
342 | int is_vmalloc; | |
343 | void *data[0]; | |
344 | }; | |
345 | ||
346 | ||
347 | struct ipc_rcu_grace | |
348 | { | |
349 | struct rcu_head rcu; | |
350 | /* "void *" makes sure alignment of following data is sane. */ | |
351 | void *data[0]; | |
352 | }; | |
353 | ||
354 | struct ipc_rcu_sched | |
355 | { | |
356 | struct work_struct work; | |
357 | /* "void *" makes sure alignment of following data is sane. */ | |
358 | void *data[0]; | |
359 | }; | |
360 | ||
361 | #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \ | |
362 | sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr)) | |
363 | #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \ | |
364 | sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC) | |
365 | ||
366 | static inline int rcu_use_vmalloc(int size) | |
367 | { | |
368 | /* Too big for a single page? */ | |
369 | if (HDRLEN_KMALLOC + size > PAGE_SIZE) | |
370 | return 1; | |
371 | return 0; | |
372 | } | |
373 | ||
374 | /** | |
375 | * ipc_rcu_alloc - allocate ipc and rcu space | |
376 | * @size: size desired | |
377 | * | |
378 | * Allocate memory for the rcu header structure + the object. | |
379 | * Returns the pointer to the object. | |
380 | * NULL is returned if the allocation fails. | |
381 | */ | |
382 | ||
383 | void* ipc_rcu_alloc(int size) | |
384 | { | |
385 | void* out; | |
386 | /* | |
387 | * We prepend the allocation with the rcu struct, and | |
388 | * workqueue if necessary (for vmalloc). | |
389 | */ | |
390 | if (rcu_use_vmalloc(size)) { | |
391 | out = vmalloc(HDRLEN_VMALLOC + size); | |
392 | if (out) { | |
393 | out += HDRLEN_VMALLOC; | |
394 | container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1; | |
395 | container_of(out, struct ipc_rcu_hdr, data)->refcount = 1; | |
396 | } | |
397 | } else { | |
398 | out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL); | |
399 | if (out) { | |
400 | out += HDRLEN_KMALLOC; | |
401 | container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0; | |
402 | container_of(out, struct ipc_rcu_hdr, data)->refcount = 1; | |
403 | } | |
404 | } | |
405 | ||
406 | return out; | |
407 | } | |
408 | ||
409 | void ipc_rcu_getref(void *ptr) | |
410 | { | |
411 | container_of(ptr, struct ipc_rcu_hdr, data)->refcount++; | |
412 | } | |
413 | ||
414 | /** | |
1e5d5331 RD |
415 | * ipc_schedule_free - free ipc + rcu space |
416 | * @head: RCU callback structure for queued work | |
1da177e4 LT |
417 | * |
418 | * Since RCU callback function is called in bh, | |
419 | * we need to defer the vfree to schedule_work | |
420 | */ | |
421 | static void ipc_schedule_free(struct rcu_head *head) | |
422 | { | |
423 | struct ipc_rcu_grace *grace = | |
424 | container_of(head, struct ipc_rcu_grace, rcu); | |
425 | struct ipc_rcu_sched *sched = | |
426 | container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]); | |
427 | ||
428 | INIT_WORK(&sched->work, vfree, sched); | |
429 | schedule_work(&sched->work); | |
430 | } | |
431 | ||
432 | /** | |
1e5d5331 RD |
433 | * ipc_immediate_free - free ipc + rcu space |
434 | * @head: RCU callback structure that contains pointer to be freed | |
1da177e4 | 435 | * |
1e5d5331 | 436 | * Free from the RCU callback context |
1da177e4 LT |
437 | */ |
438 | static void ipc_immediate_free(struct rcu_head *head) | |
439 | { | |
440 | struct ipc_rcu_grace *free = | |
441 | container_of(head, struct ipc_rcu_grace, rcu); | |
442 | kfree(free); | |
443 | } | |
444 | ||
445 | void ipc_rcu_putref(void *ptr) | |
446 | { | |
447 | if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0) | |
448 | return; | |
449 | ||
450 | if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) { | |
451 | call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu, | |
452 | ipc_schedule_free); | |
453 | } else { | |
454 | call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu, | |
455 | ipc_immediate_free); | |
456 | } | |
457 | } | |
458 | ||
459 | /** | |
460 | * ipcperms - check IPC permissions | |
461 | * @ipcp: IPC permission set | |
462 | * @flag: desired permission set. | |
463 | * | |
464 | * Check user, group, other permissions for access | |
465 | * to ipc resources. return 0 if allowed | |
466 | */ | |
467 | ||
468 | int ipcperms (struct kern_ipc_perm *ipcp, short flag) | |
469 | { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */ | |
470 | int requested_mode, granted_mode; | |
471 | ||
472 | requested_mode = (flag >> 6) | (flag >> 3) | flag; | |
473 | granted_mode = ipcp->mode; | |
474 | if (current->euid == ipcp->cuid || current->euid == ipcp->uid) | |
475 | granted_mode >>= 6; | |
476 | else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid)) | |
477 | granted_mode >>= 3; | |
478 | /* is there some bit set in requested_mode but not in granted_mode? */ | |
479 | if ((requested_mode & ~granted_mode & 0007) && | |
480 | !capable(CAP_IPC_OWNER)) | |
481 | return -1; | |
482 | ||
483 | return security_ipc_permission(ipcp, flag); | |
484 | } | |
485 | ||
486 | /* | |
487 | * Functions to convert between the kern_ipc_perm structure and the | |
488 | * old/new ipc_perm structures | |
489 | */ | |
490 | ||
491 | /** | |
492 | * kernel_to_ipc64_perm - convert kernel ipc permissions to user | |
493 | * @in: kernel permissions | |
494 | * @out: new style IPC permissions | |
495 | * | |
496 | * Turn the kernel object 'in' into a set of permissions descriptions | |
497 | * for returning to userspace (out). | |
498 | */ | |
499 | ||
500 | ||
501 | void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out) | |
502 | { | |
503 | out->key = in->key; | |
504 | out->uid = in->uid; | |
505 | out->gid = in->gid; | |
506 | out->cuid = in->cuid; | |
507 | out->cgid = in->cgid; | |
508 | out->mode = in->mode; | |
509 | out->seq = in->seq; | |
510 | } | |
511 | ||
512 | /** | |
513 | * ipc64_perm_to_ipc_perm - convert old ipc permissions to new | |
514 | * @in: new style IPC permissions | |
515 | * @out: old style IPC permissions | |
516 | * | |
517 | * Turn the new style permissions object in into a compatibility | |
518 | * object and store it into the 'out' pointer. | |
519 | */ | |
520 | ||
521 | void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out) | |
522 | { | |
523 | out->key = in->key; | |
524 | SET_UID(out->uid, in->uid); | |
525 | SET_GID(out->gid, in->gid); | |
526 | SET_UID(out->cuid, in->cuid); | |
527 | SET_GID(out->cgid, in->cgid); | |
528 | out->mode = in->mode; | |
529 | out->seq = in->seq; | |
530 | } | |
531 | ||
532 | /* | |
533 | * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get() | |
5f921ae9 IM |
534 | * is called with shm_ids.mutex locked. Since grow_ary() is also called with |
535 | * shm_ids.mutex down(for Shared Memory), there is no need to add read | |
1da177e4 LT |
536 | * barriers here to gurantee the writes in grow_ary() are seen in order |
537 | * here (for Alpha). | |
538 | * | |
5f921ae9 IM |
539 | * However ipc_get() itself does not necessary require ipc_ids.mutex down. So |
540 | * if in the future ipc_get() is used by other places without ipc_ids.mutex | |
1da177e4 LT |
541 | * down, then ipc_get() needs read memery barriers as ipc_lock() does. |
542 | */ | |
543 | struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id) | |
544 | { | |
545 | struct kern_ipc_perm* out; | |
546 | int lid = id % SEQ_MULTIPLIER; | |
547 | if(lid >= ids->entries->size) | |
548 | return NULL; | |
549 | out = ids->entries->p[lid]; | |
550 | return out; | |
551 | } | |
552 | ||
553 | struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id) | |
554 | { | |
555 | struct kern_ipc_perm* out; | |
556 | int lid = id % SEQ_MULTIPLIER; | |
557 | struct ipc_id_ary* entries; | |
558 | ||
559 | rcu_read_lock(); | |
560 | entries = rcu_dereference(ids->entries); | |
561 | if(lid >= entries->size) { | |
562 | rcu_read_unlock(); | |
563 | return NULL; | |
564 | } | |
565 | out = entries->p[lid]; | |
566 | if(out == NULL) { | |
567 | rcu_read_unlock(); | |
568 | return NULL; | |
569 | } | |
570 | spin_lock(&out->lock); | |
571 | ||
572 | /* ipc_rmid() may have already freed the ID while ipc_lock | |
573 | * was spinning: here verify that the structure is still valid | |
574 | */ | |
575 | if (out->deleted) { | |
576 | spin_unlock(&out->lock); | |
577 | rcu_read_unlock(); | |
578 | return NULL; | |
579 | } | |
580 | return out; | |
581 | } | |
582 | ||
583 | void ipc_lock_by_ptr(struct kern_ipc_perm *perm) | |
584 | { | |
585 | rcu_read_lock(); | |
586 | spin_lock(&perm->lock); | |
587 | } | |
588 | ||
589 | void ipc_unlock(struct kern_ipc_perm* perm) | |
590 | { | |
591 | spin_unlock(&perm->lock); | |
592 | rcu_read_unlock(); | |
593 | } | |
594 | ||
595 | int ipc_buildid(struct ipc_ids* ids, int id, int seq) | |
596 | { | |
597 | return SEQ_MULTIPLIER*seq + id; | |
598 | } | |
599 | ||
600 | int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid) | |
601 | { | |
602 | if(uid/SEQ_MULTIPLIER != ipcp->seq) | |
603 | return 1; | |
604 | return 0; | |
605 | } | |
606 | ||
607 | #ifdef __ARCH_WANT_IPC_PARSE_VERSION | |
608 | ||
609 | ||
610 | /** | |
611 | * ipc_parse_version - IPC call version | |
612 | * @cmd: pointer to command | |
613 | * | |
614 | * Return IPC_64 for new style IPC and IPC_OLD for old style IPC. | |
615 | * The cmd value is turned from an encoding command and version into | |
616 | * just the command code. | |
617 | */ | |
618 | ||
619 | int ipc_parse_version (int *cmd) | |
620 | { | |
621 | if (*cmd & IPC_64) { | |
622 | *cmd ^= IPC_64; | |
623 | return IPC_64; | |
624 | } else { | |
625 | return IPC_OLD; | |
626 | } | |
627 | } | |
628 | ||
629 | #endif /* __ARCH_WANT_IPC_PARSE_VERSION */ | |
ae781774 MW |
630 | |
631 | #ifdef CONFIG_PROC_FS | |
632 | static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos) | |
633 | { | |
634 | struct ipc_proc_iface *iface = s->private; | |
635 | struct kern_ipc_perm *ipc = it; | |
636 | loff_t p; | |
637 | ||
638 | /* If we had an ipc id locked before, unlock it */ | |
639 | if (ipc && ipc != SEQ_START_TOKEN) | |
640 | ipc_unlock(ipc); | |
641 | ||
642 | /* | |
643 | * p = *pos - 1 (because id 0 starts at position 1) | |
644 | * + 1 (because we increment the position by one) | |
645 | */ | |
646 | for (p = *pos; p <= iface->ids->max_id; p++) { | |
647 | if ((ipc = ipc_lock(iface->ids, p)) != NULL) { | |
648 | *pos = p + 1; | |
649 | return ipc; | |
650 | } | |
651 | } | |
652 | ||
653 | /* Out of range - return NULL to terminate iteration */ | |
654 | return NULL; | |
655 | } | |
656 | ||
657 | /* | |
658 | * File positions: pos 0 -> header, pos n -> ipc id + 1. | |
659 | * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START. | |
660 | */ | |
661 | static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos) | |
662 | { | |
663 | struct ipc_proc_iface *iface = s->private; | |
664 | struct kern_ipc_perm *ipc; | |
665 | loff_t p; | |
666 | ||
667 | /* | |
668 | * Take the lock - this will be released by the corresponding | |
669 | * call to stop(). | |
670 | */ | |
5f921ae9 | 671 | mutex_lock(&iface->ids->mutex); |
ae781774 MW |
672 | |
673 | /* pos < 0 is invalid */ | |
674 | if (*pos < 0) | |
675 | return NULL; | |
676 | ||
677 | /* pos == 0 means header */ | |
678 | if (*pos == 0) | |
679 | return SEQ_START_TOKEN; | |
680 | ||
681 | /* Find the (pos-1)th ipc */ | |
682 | for (p = *pos - 1; p <= iface->ids->max_id; p++) { | |
683 | if ((ipc = ipc_lock(iface->ids, p)) != NULL) { | |
684 | *pos = p + 1; | |
685 | return ipc; | |
686 | } | |
687 | } | |
688 | return NULL; | |
689 | } | |
690 | ||
691 | static void sysvipc_proc_stop(struct seq_file *s, void *it) | |
692 | { | |
693 | struct kern_ipc_perm *ipc = it; | |
694 | struct ipc_proc_iface *iface = s->private; | |
695 | ||
696 | /* If we had a locked segment, release it */ | |
697 | if (ipc && ipc != SEQ_START_TOKEN) | |
698 | ipc_unlock(ipc); | |
699 | ||
700 | /* Release the lock we took in start() */ | |
5f921ae9 | 701 | mutex_unlock(&iface->ids->mutex); |
ae781774 MW |
702 | } |
703 | ||
704 | static int sysvipc_proc_show(struct seq_file *s, void *it) | |
705 | { | |
706 | struct ipc_proc_iface *iface = s->private; | |
707 | ||
708 | if (it == SEQ_START_TOKEN) | |
709 | return seq_puts(s, iface->header); | |
710 | ||
711 | return iface->show(s, it); | |
712 | } | |
713 | ||
714 | static struct seq_operations sysvipc_proc_seqops = { | |
715 | .start = sysvipc_proc_start, | |
716 | .stop = sysvipc_proc_stop, | |
717 | .next = sysvipc_proc_next, | |
718 | .show = sysvipc_proc_show, | |
719 | }; | |
720 | ||
721 | static int sysvipc_proc_open(struct inode *inode, struct file *file) { | |
722 | int ret; | |
723 | struct seq_file *seq; | |
724 | ||
725 | ret = seq_open(file, &sysvipc_proc_seqops); | |
726 | if (!ret) { | |
727 | seq = file->private_data; | |
728 | seq->private = PDE(inode)->data; | |
729 | } | |
730 | return ret; | |
731 | } | |
732 | ||
733 | static struct file_operations sysvipc_proc_fops = { | |
734 | .open = sysvipc_proc_open, | |
735 | .read = seq_read, | |
736 | .llseek = seq_lseek, | |
737 | .release = seq_release, | |
738 | }; | |
739 | #endif /* CONFIG_PROC_FS */ |