]>
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; | |
a9a5cd5d | 186 | memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size); |
1da177e4 LT |
187 | for(i=size;i<newsize;i++) { |
188 | new->p[i] = NULL; | |
189 | } | |
190 | old = ids->entries; | |
191 | ||
192 | /* | |
193 | * Use rcu_assign_pointer() to make sure the memcpyed contents | |
194 | * of the new array are visible before the new array becomes visible. | |
195 | */ | |
196 | rcu_assign_pointer(ids->entries, new); | |
197 | ||
198 | ipc_rcu_putref(old); | |
199 | return newsize; | |
200 | } | |
201 | ||
202 | /** | |
203 | * ipc_addid - add an IPC identifier | |
204 | * @ids: IPC identifier set | |
205 | * @new: new IPC permission set | |
206 | * @size: new size limit for the id array | |
207 | * | |
208 | * Add an entry 'new' to the IPC arrays. The permissions object is | |
209 | * initialised and the first free entry is set up and the id assigned | |
210 | * is returned. The list is returned in a locked state on success. | |
211 | * On failure the list is not locked and -1 is returned. | |
212 | * | |
5f921ae9 | 213 | * Called with ipc_ids.mutex held. |
1da177e4 LT |
214 | */ |
215 | ||
216 | int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size) | |
217 | { | |
218 | int id; | |
219 | ||
220 | size = grow_ary(ids,size); | |
221 | ||
222 | /* | |
223 | * rcu_dereference()() is not needed here since | |
5f921ae9 | 224 | * ipc_ids.mutex is held |
1da177e4 LT |
225 | */ |
226 | for (id = 0; id < size; id++) { | |
227 | if(ids->entries->p[id] == NULL) | |
228 | goto found; | |
229 | } | |
230 | return -1; | |
231 | found: | |
232 | ids->in_use++; | |
233 | if (id > ids->max_id) | |
234 | ids->max_id = id; | |
235 | ||
236 | new->cuid = new->uid = current->euid; | |
237 | new->gid = new->cgid = current->egid; | |
238 | ||
239 | new->seq = ids->seq++; | |
240 | if(ids->seq > ids->seq_max) | |
241 | ids->seq = 0; | |
242 | ||
243 | spin_lock_init(&new->lock); | |
244 | new->deleted = 0; | |
245 | rcu_read_lock(); | |
246 | spin_lock(&new->lock); | |
247 | ids->entries->p[id] = new; | |
248 | return id; | |
249 | } | |
250 | ||
251 | /** | |
252 | * ipc_rmid - remove an IPC identifier | |
253 | * @ids: identifier set | |
254 | * @id: Identifier to remove | |
255 | * | |
256 | * The identifier must be valid, and in use. The kernel will panic if | |
257 | * fed an invalid identifier. The entry is removed and internal | |
258 | * variables recomputed. The object associated with the identifier | |
259 | * is returned. | |
5f921ae9 | 260 | * ipc_ids.mutex and the spinlock for this ID is hold before this function |
1da177e4 LT |
261 | * is called, and remain locked on the exit. |
262 | */ | |
263 | ||
264 | struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id) | |
265 | { | |
266 | struct kern_ipc_perm* p; | |
267 | int lid = id % SEQ_MULTIPLIER; | |
9bc98fc6 | 268 | BUG_ON(lid >= ids->entries->size); |
1da177e4 LT |
269 | |
270 | /* | |
271 | * do not need a rcu_dereference()() here to force ordering | |
5f921ae9 | 272 | * on Alpha, since the ipc_ids.mutex is held. |
1da177e4 LT |
273 | */ |
274 | p = ids->entries->p[lid]; | |
275 | ids->entries->p[lid] = NULL; | |
9bc98fc6 | 276 | BUG_ON(p==NULL); |
1da177e4 LT |
277 | ids->in_use--; |
278 | ||
279 | if (lid == ids->max_id) { | |
280 | do { | |
281 | lid--; | |
282 | if(lid == -1) | |
283 | break; | |
284 | } while (ids->entries->p[lid] == NULL); | |
285 | ids->max_id = lid; | |
286 | } | |
287 | p->deleted = 1; | |
288 | return p; | |
289 | } | |
290 | ||
291 | /** | |
292 | * ipc_alloc - allocate ipc space | |
293 | * @size: size desired | |
294 | * | |
295 | * Allocate memory from the appropriate pools and return a pointer to it. | |
296 | * NULL is returned if the allocation fails | |
297 | */ | |
298 | ||
299 | void* ipc_alloc(int size) | |
300 | { | |
301 | void* out; | |
302 | if(size > PAGE_SIZE) | |
303 | out = vmalloc(size); | |
304 | else | |
305 | out = kmalloc(size, GFP_KERNEL); | |
306 | return out; | |
307 | } | |
308 | ||
309 | /** | |
310 | * ipc_free - free ipc space | |
311 | * @ptr: pointer returned by ipc_alloc | |
312 | * @size: size of block | |
313 | * | |
314 | * Free a block created with ipc_alloc. The caller must know the size | |
315 | * used in the allocation call. | |
316 | */ | |
317 | ||
318 | void ipc_free(void* ptr, int size) | |
319 | { | |
320 | if(size > PAGE_SIZE) | |
321 | vfree(ptr); | |
322 | else | |
323 | kfree(ptr); | |
324 | } | |
325 | ||
326 | /* | |
327 | * rcu allocations: | |
328 | * There are three headers that are prepended to the actual allocation: | |
329 | * - during use: ipc_rcu_hdr. | |
330 | * - during the rcu grace period: ipc_rcu_grace. | |
331 | * - [only if vmalloc]: ipc_rcu_sched. | |
332 | * Their lifetime doesn't overlap, thus the headers share the same memory. | |
333 | * Unlike a normal union, they are right-aligned, thus some container_of | |
334 | * forward/backward casting is necessary: | |
335 | */ | |
336 | struct ipc_rcu_hdr | |
337 | { | |
338 | int refcount; | |
339 | int is_vmalloc; | |
340 | void *data[0]; | |
341 | }; | |
342 | ||
343 | ||
344 | struct ipc_rcu_grace | |
345 | { | |
346 | struct rcu_head rcu; | |
347 | /* "void *" makes sure alignment of following data is sane. */ | |
348 | void *data[0]; | |
349 | }; | |
350 | ||
351 | struct ipc_rcu_sched | |
352 | { | |
353 | struct work_struct work; | |
354 | /* "void *" makes sure alignment of following data is sane. */ | |
355 | void *data[0]; | |
356 | }; | |
357 | ||
358 | #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \ | |
359 | sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr)) | |
360 | #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \ | |
361 | sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC) | |
362 | ||
363 | static inline int rcu_use_vmalloc(int size) | |
364 | { | |
365 | /* Too big for a single page? */ | |
366 | if (HDRLEN_KMALLOC + size > PAGE_SIZE) | |
367 | return 1; | |
368 | return 0; | |
369 | } | |
370 | ||
371 | /** | |
372 | * ipc_rcu_alloc - allocate ipc and rcu space | |
373 | * @size: size desired | |
374 | * | |
375 | * Allocate memory for the rcu header structure + the object. | |
376 | * Returns the pointer to the object. | |
377 | * NULL is returned if the allocation fails. | |
378 | */ | |
379 | ||
380 | void* ipc_rcu_alloc(int size) | |
381 | { | |
382 | void* out; | |
383 | /* | |
384 | * We prepend the allocation with the rcu struct, and | |
385 | * workqueue if necessary (for vmalloc). | |
386 | */ | |
387 | if (rcu_use_vmalloc(size)) { | |
388 | out = vmalloc(HDRLEN_VMALLOC + size); | |
389 | if (out) { | |
390 | out += HDRLEN_VMALLOC; | |
391 | container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1; | |
392 | container_of(out, struct ipc_rcu_hdr, data)->refcount = 1; | |
393 | } | |
394 | } else { | |
395 | out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL); | |
396 | if (out) { | |
397 | out += HDRLEN_KMALLOC; | |
398 | container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0; | |
399 | container_of(out, struct ipc_rcu_hdr, data)->refcount = 1; | |
400 | } | |
401 | } | |
402 | ||
403 | return out; | |
404 | } | |
405 | ||
406 | void ipc_rcu_getref(void *ptr) | |
407 | { | |
408 | container_of(ptr, struct ipc_rcu_hdr, data)->refcount++; | |
409 | } | |
410 | ||
411 | /** | |
1e5d5331 RD |
412 | * ipc_schedule_free - free ipc + rcu space |
413 | * @head: RCU callback structure for queued work | |
1da177e4 LT |
414 | * |
415 | * Since RCU callback function is called in bh, | |
416 | * we need to defer the vfree to schedule_work | |
417 | */ | |
418 | static void ipc_schedule_free(struct rcu_head *head) | |
419 | { | |
420 | struct ipc_rcu_grace *grace = | |
421 | container_of(head, struct ipc_rcu_grace, rcu); | |
422 | struct ipc_rcu_sched *sched = | |
423 | container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]); | |
424 | ||
425 | INIT_WORK(&sched->work, vfree, sched); | |
426 | schedule_work(&sched->work); | |
427 | } | |
428 | ||
429 | /** | |
1e5d5331 RD |
430 | * ipc_immediate_free - free ipc + rcu space |
431 | * @head: RCU callback structure that contains pointer to be freed | |
1da177e4 | 432 | * |
1e5d5331 | 433 | * Free from the RCU callback context |
1da177e4 LT |
434 | */ |
435 | static void ipc_immediate_free(struct rcu_head *head) | |
436 | { | |
437 | struct ipc_rcu_grace *free = | |
438 | container_of(head, struct ipc_rcu_grace, rcu); | |
439 | kfree(free); | |
440 | } | |
441 | ||
442 | void ipc_rcu_putref(void *ptr) | |
443 | { | |
444 | if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0) | |
445 | return; | |
446 | ||
447 | if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) { | |
448 | call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu, | |
449 | ipc_schedule_free); | |
450 | } else { | |
451 | call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu, | |
452 | ipc_immediate_free); | |
453 | } | |
454 | } | |
455 | ||
456 | /** | |
457 | * ipcperms - check IPC permissions | |
458 | * @ipcp: IPC permission set | |
459 | * @flag: desired permission set. | |
460 | * | |
461 | * Check user, group, other permissions for access | |
462 | * to ipc resources. return 0 if allowed | |
463 | */ | |
464 | ||
465 | int ipcperms (struct kern_ipc_perm *ipcp, short flag) | |
466 | { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */ | |
467 | int requested_mode, granted_mode; | |
468 | ||
469 | requested_mode = (flag >> 6) | (flag >> 3) | flag; | |
470 | granted_mode = ipcp->mode; | |
471 | if (current->euid == ipcp->cuid || current->euid == ipcp->uid) | |
472 | granted_mode >>= 6; | |
473 | else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid)) | |
474 | granted_mode >>= 3; | |
475 | /* is there some bit set in requested_mode but not in granted_mode? */ | |
476 | if ((requested_mode & ~granted_mode & 0007) && | |
477 | !capable(CAP_IPC_OWNER)) | |
478 | return -1; | |
479 | ||
480 | return security_ipc_permission(ipcp, flag); | |
481 | } | |
482 | ||
483 | /* | |
484 | * Functions to convert between the kern_ipc_perm structure and the | |
485 | * old/new ipc_perm structures | |
486 | */ | |
487 | ||
488 | /** | |
489 | * kernel_to_ipc64_perm - convert kernel ipc permissions to user | |
490 | * @in: kernel permissions | |
491 | * @out: new style IPC permissions | |
492 | * | |
493 | * Turn the kernel object 'in' into a set of permissions descriptions | |
494 | * for returning to userspace (out). | |
495 | */ | |
496 | ||
497 | ||
498 | void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out) | |
499 | { | |
500 | out->key = in->key; | |
501 | out->uid = in->uid; | |
502 | out->gid = in->gid; | |
503 | out->cuid = in->cuid; | |
504 | out->cgid = in->cgid; | |
505 | out->mode = in->mode; | |
506 | out->seq = in->seq; | |
507 | } | |
508 | ||
509 | /** | |
510 | * ipc64_perm_to_ipc_perm - convert old ipc permissions to new | |
511 | * @in: new style IPC permissions | |
512 | * @out: old style IPC permissions | |
513 | * | |
514 | * Turn the new style permissions object in into a compatibility | |
515 | * object and store it into the 'out' pointer. | |
516 | */ | |
517 | ||
518 | void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out) | |
519 | { | |
520 | out->key = in->key; | |
521 | SET_UID(out->uid, in->uid); | |
522 | SET_GID(out->gid, in->gid); | |
523 | SET_UID(out->cuid, in->cuid); | |
524 | SET_GID(out->cgid, in->cgid); | |
525 | out->mode = in->mode; | |
526 | out->seq = in->seq; | |
527 | } | |
528 | ||
529 | /* | |
530 | * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get() | |
5f921ae9 IM |
531 | * is called with shm_ids.mutex locked. Since grow_ary() is also called with |
532 | * shm_ids.mutex down(for Shared Memory), there is no need to add read | |
1da177e4 LT |
533 | * barriers here to gurantee the writes in grow_ary() are seen in order |
534 | * here (for Alpha). | |
535 | * | |
5f921ae9 IM |
536 | * However ipc_get() itself does not necessary require ipc_ids.mutex down. So |
537 | * if in the future ipc_get() is used by other places without ipc_ids.mutex | |
1da177e4 LT |
538 | * down, then ipc_get() needs read memery barriers as ipc_lock() does. |
539 | */ | |
540 | struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id) | |
541 | { | |
542 | struct kern_ipc_perm* out; | |
543 | int lid = id % SEQ_MULTIPLIER; | |
544 | if(lid >= ids->entries->size) | |
545 | return NULL; | |
546 | out = ids->entries->p[lid]; | |
547 | return out; | |
548 | } | |
549 | ||
550 | struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id) | |
551 | { | |
552 | struct kern_ipc_perm* out; | |
553 | int lid = id % SEQ_MULTIPLIER; | |
554 | struct ipc_id_ary* entries; | |
555 | ||
556 | rcu_read_lock(); | |
557 | entries = rcu_dereference(ids->entries); | |
558 | if(lid >= entries->size) { | |
559 | rcu_read_unlock(); | |
560 | return NULL; | |
561 | } | |
562 | out = entries->p[lid]; | |
563 | if(out == NULL) { | |
564 | rcu_read_unlock(); | |
565 | return NULL; | |
566 | } | |
567 | spin_lock(&out->lock); | |
568 | ||
569 | /* ipc_rmid() may have already freed the ID while ipc_lock | |
570 | * was spinning: here verify that the structure is still valid | |
571 | */ | |
572 | if (out->deleted) { | |
573 | spin_unlock(&out->lock); | |
574 | rcu_read_unlock(); | |
575 | return NULL; | |
576 | } | |
577 | return out; | |
578 | } | |
579 | ||
580 | void ipc_lock_by_ptr(struct kern_ipc_perm *perm) | |
581 | { | |
582 | rcu_read_lock(); | |
583 | spin_lock(&perm->lock); | |
584 | } | |
585 | ||
586 | void ipc_unlock(struct kern_ipc_perm* perm) | |
587 | { | |
588 | spin_unlock(&perm->lock); | |
589 | rcu_read_unlock(); | |
590 | } | |
591 | ||
592 | int ipc_buildid(struct ipc_ids* ids, int id, int seq) | |
593 | { | |
594 | return SEQ_MULTIPLIER*seq + id; | |
595 | } | |
596 | ||
597 | int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid) | |
598 | { | |
599 | if(uid/SEQ_MULTIPLIER != ipcp->seq) | |
600 | return 1; | |
601 | return 0; | |
602 | } | |
603 | ||
604 | #ifdef __ARCH_WANT_IPC_PARSE_VERSION | |
605 | ||
606 | ||
607 | /** | |
608 | * ipc_parse_version - IPC call version | |
609 | * @cmd: pointer to command | |
610 | * | |
611 | * Return IPC_64 for new style IPC and IPC_OLD for old style IPC. | |
612 | * The cmd value is turned from an encoding command and version into | |
613 | * just the command code. | |
614 | */ | |
615 | ||
616 | int ipc_parse_version (int *cmd) | |
617 | { | |
618 | if (*cmd & IPC_64) { | |
619 | *cmd ^= IPC_64; | |
620 | return IPC_64; | |
621 | } else { | |
622 | return IPC_OLD; | |
623 | } | |
624 | } | |
625 | ||
626 | #endif /* __ARCH_WANT_IPC_PARSE_VERSION */ | |
ae781774 MW |
627 | |
628 | #ifdef CONFIG_PROC_FS | |
629 | static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos) | |
630 | { | |
631 | struct ipc_proc_iface *iface = s->private; | |
632 | struct kern_ipc_perm *ipc = it; | |
633 | loff_t p; | |
634 | ||
635 | /* If we had an ipc id locked before, unlock it */ | |
636 | if (ipc && ipc != SEQ_START_TOKEN) | |
637 | ipc_unlock(ipc); | |
638 | ||
639 | /* | |
640 | * p = *pos - 1 (because id 0 starts at position 1) | |
641 | * + 1 (because we increment the position by one) | |
642 | */ | |
643 | for (p = *pos; p <= iface->ids->max_id; p++) { | |
644 | if ((ipc = ipc_lock(iface->ids, p)) != NULL) { | |
645 | *pos = p + 1; | |
646 | return ipc; | |
647 | } | |
648 | } | |
649 | ||
650 | /* Out of range - return NULL to terminate iteration */ | |
651 | return NULL; | |
652 | } | |
653 | ||
654 | /* | |
655 | * File positions: pos 0 -> header, pos n -> ipc id + 1. | |
656 | * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START. | |
657 | */ | |
658 | static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos) | |
659 | { | |
660 | struct ipc_proc_iface *iface = s->private; | |
661 | struct kern_ipc_perm *ipc; | |
662 | loff_t p; | |
663 | ||
664 | /* | |
665 | * Take the lock - this will be released by the corresponding | |
666 | * call to stop(). | |
667 | */ | |
5f921ae9 | 668 | mutex_lock(&iface->ids->mutex); |
ae781774 MW |
669 | |
670 | /* pos < 0 is invalid */ | |
671 | if (*pos < 0) | |
672 | return NULL; | |
673 | ||
674 | /* pos == 0 means header */ | |
675 | if (*pos == 0) | |
676 | return SEQ_START_TOKEN; | |
677 | ||
678 | /* Find the (pos-1)th ipc */ | |
679 | for (p = *pos - 1; p <= iface->ids->max_id; p++) { | |
680 | if ((ipc = ipc_lock(iface->ids, p)) != NULL) { | |
681 | *pos = p + 1; | |
682 | return ipc; | |
683 | } | |
684 | } | |
685 | return NULL; | |
686 | } | |
687 | ||
688 | static void sysvipc_proc_stop(struct seq_file *s, void *it) | |
689 | { | |
690 | struct kern_ipc_perm *ipc = it; | |
691 | struct ipc_proc_iface *iface = s->private; | |
692 | ||
693 | /* If we had a locked segment, release it */ | |
694 | if (ipc && ipc != SEQ_START_TOKEN) | |
695 | ipc_unlock(ipc); | |
696 | ||
697 | /* Release the lock we took in start() */ | |
5f921ae9 | 698 | mutex_unlock(&iface->ids->mutex); |
ae781774 MW |
699 | } |
700 | ||
701 | static int sysvipc_proc_show(struct seq_file *s, void *it) | |
702 | { | |
703 | struct ipc_proc_iface *iface = s->private; | |
704 | ||
705 | if (it == SEQ_START_TOKEN) | |
706 | return seq_puts(s, iface->header); | |
707 | ||
708 | return iface->show(s, it); | |
709 | } | |
710 | ||
711 | static struct seq_operations sysvipc_proc_seqops = { | |
712 | .start = sysvipc_proc_start, | |
713 | .stop = sysvipc_proc_stop, | |
714 | .next = sysvipc_proc_next, | |
715 | .show = sysvipc_proc_show, | |
716 | }; | |
717 | ||
718 | static int sysvipc_proc_open(struct inode *inode, struct file *file) { | |
719 | int ret; | |
720 | struct seq_file *seq; | |
721 | ||
722 | ret = seq_open(file, &sysvipc_proc_seqops); | |
723 | if (!ret) { | |
724 | seq = file->private_data; | |
725 | seq->private = PDE(inode)->data; | |
726 | } | |
727 | return ret; | |
728 | } | |
729 | ||
730 | static struct file_operations sysvipc_proc_fops = { | |
731 | .open = sysvipc_proc_open, | |
732 | .read = seq_read, | |
733 | .llseek = seq_lseek, | |
734 | .release = seq_release, | |
735 | }; | |
736 | #endif /* CONFIG_PROC_FS */ |