1 /*-
2 * Copyright (c) 1994-1995 Søren Schmidt
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: releng/8.3/sys/compat/linux/linux_ipc.c 222847 2011-06-08 03:55:58Z dchagin $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/syscallsubr.h>
35 #include <sys/sysproto.h>
36 #include <sys/proc.h>
37 #include <sys/limits.h>
38 #include <sys/msg.h>
39 #include <sys/sem.h>
40 #include <sys/shm.h>
41
42 #include "opt_compat.h"
43
44 #ifdef COMPAT_LINUX32
45 #include <machine/../linux32/linux.h>
46 #include <machine/../linux32/linux32_proto.h>
47 #include <machine/../linux32/linux32_ipc64.h>
48 #else
49 #include <machine/../linux/linux.h>
50 #include <machine/../linux/linux_proto.h>
51 #include <machine/../linux/linux_ipc64.h>
52 #endif
53 #include <compat/linux/linux_ipc.h>
54 #include <compat/linux/linux_util.h>
55
56 struct l_seminfo {
57 l_int semmap;
58 l_int semmni;
59 l_int semmns;
60 l_int semmnu;
61 l_int semmsl;
62 l_int semopm;
63 l_int semume;
64 l_int semusz;
65 l_int semvmx;
66 l_int semaem;
67 };
68
69 struct l_shminfo {
70 l_int shmmax;
71 l_int shmmin;
72 l_int shmmni;
73 l_int shmseg;
74 l_int shmall;
75 };
76
77 struct l_shm_info {
78 l_int used_ids;
79 l_ulong shm_tot; /* total allocated shm */
80 l_ulong shm_rss; /* total resident shm */
81 l_ulong shm_swp; /* total swapped shm */
82 l_ulong swap_attempts;
83 l_ulong swap_successes;
84 };
85
86 struct l_msginfo {
87 l_int msgpool;
88 l_int msgmap;
89 l_int msgmax;
90 l_int msgmnb;
91 l_int msgmni;
92 l_int msgssz;
93 l_int msgtql;
94 l_ushort msgseg;
95 };
96
97 static void
98 bsd_to_linux_shminfo( struct shminfo *bpp, struct l_shminfo *lpp)
99 {
100
101 lpp->shmmax = bpp->shmmax;
102 lpp->shmmin = bpp->shmmin;
103 lpp->shmmni = bpp->shmmni;
104 lpp->shmseg = bpp->shmseg;
105 lpp->shmall = bpp->shmall;
106 }
107
108 static void
109 bsd_to_linux_shm_info( struct shm_info *bpp, struct l_shm_info *lpp)
110 {
111
112 lpp->used_ids = bpp->used_ids ;
113 lpp->shm_tot = bpp->shm_tot ;
114 lpp->shm_rss = bpp->shm_rss ;
115 lpp->shm_swp = bpp->shm_swp ;
116 lpp->swap_attempts = bpp->swap_attempts ;
117 lpp->swap_successes = bpp->swap_successes ;
118 }
119
120 struct l_ipc_perm {
121 l_key_t key;
122 l_uid16_t uid;
123 l_gid16_t gid;
124 l_uid16_t cuid;
125 l_gid16_t cgid;
126 l_ushort mode;
127 l_ushort seq;
128 };
129
130 static void
131 linux_to_bsd_ipc_perm(struct l_ipc_perm *lpp, struct ipc_perm *bpp)
132 {
133
134 bpp->key = lpp->key;
135 bpp->uid = lpp->uid;
136 bpp->gid = lpp->gid;
137 bpp->cuid = lpp->cuid;
138 bpp->cgid = lpp->cgid;
139 bpp->mode = lpp->mode;
140 bpp->seq = lpp->seq;
141 }
142
143
144 static void
145 bsd_to_linux_ipc_perm(struct ipc_perm *bpp, struct l_ipc_perm *lpp)
146 {
147
148 lpp->key = bpp->key;
149 lpp->uid = bpp->uid;
150 lpp->gid = bpp->gid;
151 lpp->cuid = bpp->cuid;
152 lpp->cgid = bpp->cgid;
153 lpp->mode = bpp->mode;
154 lpp->seq = bpp->seq;
155 }
156
157 struct l_msqid_ds {
158 struct l_ipc_perm msg_perm;
159 l_uintptr_t msg_first; /* first message on queue,unused */
160 l_uintptr_t msg_last; /* last message in queue,unused */
161 l_time_t msg_stime; /* last msgsnd time */
162 l_time_t msg_rtime; /* last msgrcv time */
163 l_time_t msg_ctime; /* last change time */
164 l_ulong msg_lcbytes; /* Reuse junk fields for 32 bit */
165 l_ulong msg_lqbytes; /* ditto */
166 l_ushort msg_cbytes; /* current number of bytes on queue */
167 l_ushort msg_qnum; /* number of messages in queue */
168 l_ushort msg_qbytes; /* max number of bytes on queue */
169 l_pid_t msg_lspid; /* pid of last msgsnd */
170 l_pid_t msg_lrpid; /* last receive pid */
171 }
172 #if defined(__amd64__) && defined(COMPAT_LINUX32)
173 __packed
174 #endif
175 ;
176
177 struct l_semid_ds {
178 struct l_ipc_perm sem_perm;
179 l_time_t sem_otime;
180 l_time_t sem_ctime;
181 l_uintptr_t sem_base;
182 l_uintptr_t sem_pending;
183 l_uintptr_t sem_pending_last;
184 l_uintptr_t undo;
185 l_ushort sem_nsems;
186 }
187 #if defined(__amd64__) && defined(COMPAT_LINUX32)
188 __packed
189 #endif
190 ;
191
192 struct l_shmid_ds {
193 struct l_ipc_perm shm_perm;
194 l_int shm_segsz;
195 l_time_t shm_atime;
196 l_time_t shm_dtime;
197 l_time_t shm_ctime;
198 l_ushort shm_cpid;
199 l_ushort shm_lpid;
200 l_short shm_nattch;
201 l_ushort private1;
202 l_uintptr_t private2;
203 l_uintptr_t private3;
204 };
205
206 static void
207 linux_to_bsd_semid_ds(struct l_semid_ds *lsp, struct semid_ds *bsp)
208 {
209
210 linux_to_bsd_ipc_perm(&lsp->sem_perm, &bsp->sem_perm);
211 bsp->sem_otime = lsp->sem_otime;
212 bsp->sem_ctime = lsp->sem_ctime;
213 bsp->sem_nsems = lsp->sem_nsems;
214 bsp->sem_base = PTRIN(lsp->sem_base);
215 }
216
217 static void
218 bsd_to_linux_semid_ds(struct semid_ds *bsp, struct l_semid_ds *lsp)
219 {
220
221 bsd_to_linux_ipc_perm(&bsp->sem_perm, &lsp->sem_perm);
222 lsp->sem_otime = bsp->sem_otime;
223 lsp->sem_ctime = bsp->sem_ctime;
224 lsp->sem_nsems = bsp->sem_nsems;
225 lsp->sem_base = PTROUT(bsp->sem_base);
226 }
227
228 static void
229 linux_to_bsd_shmid_ds(struct l_shmid_ds *lsp, struct shmid_ds *bsp)
230 {
231
232 linux_to_bsd_ipc_perm(&lsp->shm_perm, &bsp->shm_perm);
233 bsp->shm_segsz = lsp->shm_segsz;
234 bsp->shm_lpid = lsp->shm_lpid;
235 bsp->shm_cpid = lsp->shm_cpid;
236 bsp->shm_nattch = lsp->shm_nattch;
237 bsp->shm_atime = lsp->shm_atime;
238 bsp->shm_dtime = lsp->shm_dtime;
239 bsp->shm_ctime = lsp->shm_ctime;
240 }
241
242 static void
243 bsd_to_linux_shmid_ds(struct shmid_ds *bsp, struct l_shmid_ds *lsp)
244 {
245
246 bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->shm_perm);
247 if (bsp->shm_segsz > INT_MAX)
248 lsp->shm_segsz = INT_MAX;
249 else
250 lsp->shm_segsz = bsp->shm_segsz;
251 lsp->shm_lpid = bsp->shm_lpid;
252 lsp->shm_cpid = bsp->shm_cpid;
253 if (bsp->shm_nattch > SHRT_MAX)
254 lsp->shm_nattch = SHRT_MAX;
255 else
256 lsp->shm_nattch = bsp->shm_nattch;
257 lsp->shm_atime = bsp->shm_atime;
258 lsp->shm_dtime = bsp->shm_dtime;
259 lsp->shm_ctime = bsp->shm_ctime;
260 lsp->private3 = 0;
261 }
262
263 static void
264 linux_to_bsd_msqid_ds(struct l_msqid_ds *lsp, struct msqid_ds *bsp)
265 {
266
267 linux_to_bsd_ipc_perm(&lsp->msg_perm, &bsp->msg_perm);
268 bsp->msg_cbytes = lsp->msg_cbytes;
269 bsp->msg_qnum = lsp->msg_qnum;
270 bsp->msg_qbytes = lsp->msg_qbytes;
271 bsp->msg_lspid = lsp->msg_lspid;
272 bsp->msg_lrpid = lsp->msg_lrpid;
273 bsp->msg_stime = lsp->msg_stime;
274 bsp->msg_rtime = lsp->msg_rtime;
275 bsp->msg_ctime = lsp->msg_ctime;
276 }
277
278 static void
279 bsd_to_linux_msqid_ds(struct msqid_ds *bsp, struct l_msqid_ds *lsp)
280 {
281
282 bsd_to_linux_ipc_perm(&bsp->msg_perm, &lsp->msg_perm);
283 lsp->msg_cbytes = bsp->msg_cbytes;
284 lsp->msg_qnum = bsp->msg_qnum;
285 lsp->msg_qbytes = bsp->msg_qbytes;
286 lsp->msg_lspid = bsp->msg_lspid;
287 lsp->msg_lrpid = bsp->msg_lrpid;
288 lsp->msg_stime = bsp->msg_stime;
289 lsp->msg_rtime = bsp->msg_rtime;
290 lsp->msg_ctime = bsp->msg_ctime;
291 }
292
293 static void
294 linux_ipc_perm_to_ipc64_perm(struct l_ipc_perm *in, struct l_ipc64_perm *out)
295 {
296
297 /* XXX: do we really need to do something here? */
298 out->key = in->key;
299 out->uid = in->uid;
300 out->gid = in->gid;
301 out->cuid = in->cuid;
302 out->cgid = in->cgid;
303 out->mode = in->mode;
304 out->seq = in->seq;
305 }
306
307 static int
308 linux_msqid_pullup(l_int ver, struct l_msqid_ds *linux_msqid, caddr_t uaddr)
309 {
310 struct l_msqid64_ds linux_msqid64;
311 int error;
312
313 if (ver == LINUX_IPC_64) {
314 error = copyin(uaddr, &linux_msqid64, sizeof(linux_msqid64));
315 if (error != 0)
316 return (error);
317
318 bzero(linux_msqid, sizeof(*linux_msqid));
319
320 linux_msqid->msg_perm.uid = linux_msqid64.msg_perm.uid;
321 linux_msqid->msg_perm.gid = linux_msqid64.msg_perm.gid;
322 linux_msqid->msg_perm.mode = linux_msqid64.msg_perm.mode;
323
324 if (linux_msqid64.msg_qbytes > USHRT_MAX)
325 linux_msqid->msg_lqbytes = linux_msqid64.msg_qbytes;
326 else
327 linux_msqid->msg_qbytes = linux_msqid64.msg_qbytes;
328 } else
329 error = copyin(uaddr, linux_msqid, sizeof(*linux_msqid));
330
331 return (error);
332 }
333
334 static int
335 linux_msqid_pushdown(l_int ver, struct l_msqid_ds *linux_msqid, caddr_t uaddr)
336 {
337 struct l_msqid64_ds linux_msqid64;
338
339 if (ver == LINUX_IPC_64) {
340 bzero(&linux_msqid64, sizeof(linux_msqid64));
341
342 linux_ipc_perm_to_ipc64_perm(&linux_msqid->msg_perm,
343 &linux_msqid64.msg_perm);
344
345 linux_msqid64.msg_stime = linux_msqid->msg_stime;
346 linux_msqid64.msg_rtime = linux_msqid->msg_rtime;
347 linux_msqid64.msg_ctime = linux_msqid->msg_ctime;
348
349 if (linux_msqid->msg_cbytes == 0)
350 linux_msqid64.msg_cbytes = linux_msqid->msg_lcbytes;
351 else
352 linux_msqid64.msg_cbytes = linux_msqid->msg_cbytes;
353
354 linux_msqid64.msg_qnum = linux_msqid->msg_qnum;
355
356 if (linux_msqid->msg_qbytes == 0)
357 linux_msqid64.msg_qbytes = linux_msqid->msg_lqbytes;
358 else
359 linux_msqid64.msg_qbytes = linux_msqid->msg_qbytes;
360
361 linux_msqid64.msg_lspid = linux_msqid->msg_lspid;
362 linux_msqid64.msg_lrpid = linux_msqid->msg_lrpid;
363
364 return (copyout(&linux_msqid64, uaddr, sizeof(linux_msqid64)));
365 } else
366 return (copyout(linux_msqid, uaddr, sizeof(*linux_msqid)));
367 }
368
369 static int
370 linux_semid_pullup(l_int ver, struct l_semid_ds *linux_semid, caddr_t uaddr)
371 {
372 struct l_semid64_ds linux_semid64;
373 int error;
374
375 if (ver == LINUX_IPC_64) {
376 error = copyin(uaddr, &linux_semid64, sizeof(linux_semid64));
377 if (error != 0)
378 return (error);
379
380 bzero(linux_semid, sizeof(*linux_semid));
381
382 linux_semid->sem_perm.uid = linux_semid64.sem_perm.uid;
383 linux_semid->sem_perm.gid = linux_semid64.sem_perm.gid;
384 linux_semid->sem_perm.mode = linux_semid64.sem_perm.mode;
385 } else
386 error = copyin(uaddr, linux_semid, sizeof(*linux_semid));
387
388 return (error);
389 }
390
391 static int
392 linux_semid_pushdown(l_int ver, struct l_semid_ds *linux_semid, caddr_t uaddr)
393 {
394 struct l_semid64_ds linux_semid64;
395
396 if (ver == LINUX_IPC_64) {
397 bzero(&linux_semid64, sizeof(linux_semid64));
398
399 linux_ipc_perm_to_ipc64_perm(&linux_semid->sem_perm,
400 &linux_semid64.sem_perm);
401
402 linux_semid64.sem_otime = linux_semid->sem_otime;
403 linux_semid64.sem_ctime = linux_semid->sem_ctime;
404 linux_semid64.sem_nsems = linux_semid->sem_nsems;
405
406 return (copyout(&linux_semid64, uaddr, sizeof(linux_semid64)));
407 } else
408 return (copyout(linux_semid, uaddr, sizeof(*linux_semid)));
409 }
410
411 static int
412 linux_shmid_pullup(l_int ver, struct l_shmid_ds *linux_shmid, caddr_t uaddr)
413 {
414 struct l_shmid64_ds linux_shmid64;
415 int error;
416
417 if (ver == LINUX_IPC_64) {
418 error = copyin(uaddr, &linux_shmid64, sizeof(linux_shmid64));
419 if (error != 0)
420 return (error);
421
422 bzero(linux_shmid, sizeof(*linux_shmid));
423
424 linux_shmid->shm_perm.uid = linux_shmid64.shm_perm.uid;
425 linux_shmid->shm_perm.gid = linux_shmid64.shm_perm.gid;
426 linux_shmid->shm_perm.mode = linux_shmid64.shm_perm.mode;
427 } else
428 error = copyin(uaddr, linux_shmid, sizeof(*linux_shmid));
429
430 return (error);
431 }
432
433 static int
434 linux_shmid_pushdown(l_int ver, struct l_shmid_ds *linux_shmid, caddr_t uaddr)
435 {
436 struct l_shmid64_ds linux_shmid64;
437
438 /*
439 * XXX: This is backwards and loses information in shm_nattch
440 * and shm_segsz. We should probably either expose the BSD
441 * shmid structure directly and convert it to either the
442 * non-64 or 64 variant directly or the code should always
443 * convert to the 64 variant and then truncate values into the
444 * non-64 variant if needed since the 64 variant has more
445 * precision.
446 */
447 if (ver == LINUX_IPC_64) {
448 bzero(&linux_shmid64, sizeof(linux_shmid64));
449
450 linux_ipc_perm_to_ipc64_perm(&linux_shmid->shm_perm,
451 &linux_shmid64.shm_perm);
452
453 linux_shmid64.shm_segsz = linux_shmid->shm_segsz;
454 linux_shmid64.shm_atime = linux_shmid->shm_atime;
455 linux_shmid64.shm_dtime = linux_shmid->shm_dtime;
456 linux_shmid64.shm_ctime = linux_shmid->shm_ctime;
457 linux_shmid64.shm_cpid = linux_shmid->shm_cpid;
458 linux_shmid64.shm_lpid = linux_shmid->shm_lpid;
459 linux_shmid64.shm_nattch = linux_shmid->shm_nattch;
460
461 return (copyout(&linux_shmid64, uaddr, sizeof(linux_shmid64)));
462 } else
463 return (copyout(linux_shmid, uaddr, sizeof(*linux_shmid)));
464 }
465
466 static int
467 linux_shminfo_pushdown(l_int ver, struct l_shminfo *linux_shminfo,
468 caddr_t uaddr)
469 {
470 struct l_shminfo64 linux_shminfo64;
471
472 if (ver == LINUX_IPC_64) {
473 bzero(&linux_shminfo64, sizeof(linux_shminfo64));
474
475 linux_shminfo64.shmmax = linux_shminfo->shmmax;
476 linux_shminfo64.shmmin = linux_shminfo->shmmin;
477 linux_shminfo64.shmmni = linux_shminfo->shmmni;
478 linux_shminfo64.shmseg = linux_shminfo->shmseg;
479 linux_shminfo64.shmall = linux_shminfo->shmall;
480
481 return (copyout(&linux_shminfo64, uaddr,
482 sizeof(linux_shminfo64)));
483 } else
484 return (copyout(linux_shminfo, uaddr, sizeof(*linux_shminfo)));
485 }
486
487 int
488 linux_semop(struct thread *td, struct linux_semop_args *args)
489 {
490 struct semop_args /* {
491 int semid;
492 struct sembuf *sops;
493 int nsops;
494 } */ bsd_args;
495
496 bsd_args.semid = args->semid;
497 bsd_args.sops = PTRIN(args->tsops);
498 bsd_args.nsops = args->nsops;
499 return (semop(td, &bsd_args));
500 }
501
502 int
503 linux_semget(struct thread *td, struct linux_semget_args *args)
504 {
505 struct semget_args /* {
506 key_t key;
507 int nsems;
508 int semflg;
509 } */ bsd_args;
510
511 if (args->nsems < 0)
512 return (EINVAL);
513 bsd_args.key = args->key;
514 bsd_args.nsems = args->nsems;
515 bsd_args.semflg = args->semflg;
516 return (semget(td, &bsd_args));
517 }
518
519 int
520 linux_semctl(struct thread *td, struct linux_semctl_args *args)
521 {
522 struct l_semid_ds linux_semid;
523 struct l_seminfo linux_seminfo;
524 struct semid_ds semid;
525 union semun semun;
526 register_t rval;
527 int cmd, error;
528
529 switch (args->cmd & ~LINUX_IPC_64) {
530 case LINUX_IPC_RMID:
531 cmd = IPC_RMID;
532 break;
533 case LINUX_GETNCNT:
534 cmd = GETNCNT;
535 break;
536 case LINUX_GETPID:
537 cmd = GETPID;
538 break;
539 case LINUX_GETVAL:
540 cmd = GETVAL;
541 break;
542 case LINUX_GETZCNT:
543 cmd = GETZCNT;
544 break;
545 case LINUX_SETVAL:
546 cmd = SETVAL;
547 semun.val = args->arg.val;
548 break;
549 case LINUX_IPC_SET:
550 cmd = IPC_SET;
551 error = linux_semid_pullup(args->cmd & LINUX_IPC_64,
552 &linux_semid, PTRIN(args->arg.buf));
553 if (error)
554 return (error);
555 linux_to_bsd_semid_ds(&linux_semid, &semid);
556 semun.buf = &semid;
557 return (kern_semctl(td, args->semid, args->semnum, cmd, &semun,
558 td->td_retval));
559 case LINUX_IPC_STAT:
560 case LINUX_SEM_STAT:
561 if ((args->cmd & ~LINUX_IPC_64) == LINUX_IPC_STAT)
562 cmd = IPC_STAT;
563 else
564 cmd = SEM_STAT;
565 semun.buf = &semid;
566 error = kern_semctl(td, args->semid, args->semnum, cmd, &semun,
567 &rval);
568 if (error)
569 return (error);
570 bsd_to_linux_semid_ds(&semid, &linux_semid);
571 error = linux_semid_pushdown(args->cmd & LINUX_IPC_64,
572 &linux_semid, PTRIN(args->arg.buf));
573 if (error == 0)
574 td->td_retval[0] = (cmd == SEM_STAT) ? rval : 0;
575 return (error);
576 case LINUX_IPC_INFO:
577 case LINUX_SEM_INFO:
578 bcopy(&seminfo, &linux_seminfo, sizeof(linux_seminfo) );
579 /* XXX BSD equivalent?
580 #define used_semids 10
581 #define used_sems 10
582 linux_seminfo.semusz = used_semids;
583 linux_seminfo.semaem = used_sems;
584 */
585 error = copyout(&linux_seminfo,
586 PTRIN(args->arg.buf), sizeof(linux_seminfo));
587 if (error)
588 return (error);
589 td->td_retval[0] = seminfo.semmni;
590 return (0); /* No need for __semctl call */
591 case LINUX_GETALL:
592 cmd = GETALL;
593 semun.val = args->arg.val;
594 break;
595 case LINUX_SETALL:
596 cmd = SETALL;
597 semun.val = args->arg.val;
598 break;
599 default:
600 linux_msg(td, "ipc type %d is not implemented",
601 args->cmd & ~LINUX_IPC_64);
602 return (EINVAL);
603 }
604 return (kern_semctl(td, args->semid, args->semnum, cmd, &semun,
605 td->td_retval));
606 }
607
608 int
609 linux_msgsnd(struct thread *td, struct linux_msgsnd_args *args)
610 {
611 const void *msgp;
612 long mtype;
613 l_long lmtype;
614 int error;
615
616 if ((l_long)args->msgsz < 0 || args->msgsz > (l_long)msginfo.msgmax)
617 return (EINVAL);
618 msgp = PTRIN(args->msgp);
619 if ((error = copyin(msgp, &lmtype, sizeof(lmtype))) != 0)
620 return (error);
621 mtype = (long)lmtype;
622 return (kern_msgsnd(td, args->msqid,
623 (const char *)msgp + sizeof(lmtype),
624 args->msgsz, args->msgflg, mtype));
625 }
626
627 int
628 linux_msgrcv(struct thread *td, struct linux_msgrcv_args *args)
629 {
630 void *msgp;
631 long mtype;
632 l_long lmtype;
633 int error;
634
635 if ((l_long)args->msgsz < 0 || args->msgsz > (l_long)msginfo.msgmax)
636 return (EINVAL);
637 msgp = PTRIN(args->msgp);
638 if ((error = kern_msgrcv(td, args->msqid,
639 (char *)msgp + sizeof(lmtype), args->msgsz,
640 args->msgtyp, args->msgflg, &mtype)) != 0)
641 return (error);
642 lmtype = (l_long)mtype;
643 return (copyout(&lmtype, msgp, sizeof(lmtype)));
644 }
645
646 int
647 linux_msgget(struct thread *td, struct linux_msgget_args *args)
648 {
649 struct msgget_args /* {
650 key_t key;
651 int msgflg;
652 } */ bsd_args;
653
654 bsd_args.key = args->key;
655 bsd_args.msgflg = args->msgflg;
656 return (msgget(td, &bsd_args));
657 }
658
659 int
660 linux_msgctl(struct thread *td, struct linux_msgctl_args *args)
661 {
662 int error, bsd_cmd;
663 struct l_msqid_ds linux_msqid;
664 struct msqid_ds bsd_msqid;
665
666 bsd_cmd = args->cmd & ~LINUX_IPC_64;
667 switch (bsd_cmd) {
668 case LINUX_IPC_INFO:
669 case LINUX_MSG_INFO: {
670 struct l_msginfo linux_msginfo;
671
672 /*
673 * XXX MSG_INFO uses the same data structure but returns different
674 * dynamic counters in msgpool, msgmap, and msgtql fields.
675 */
676 linux_msginfo.msgpool = (long)msginfo.msgmni *
677 (long)msginfo.msgmnb / 1024L; /* XXX MSG_INFO. */
678 linux_msginfo.msgmap = msginfo.msgmnb; /* XXX MSG_INFO. */
679 linux_msginfo.msgmax = msginfo.msgmax;
680 linux_msginfo.msgmnb = msginfo.msgmnb;
681 linux_msginfo.msgmni = msginfo.msgmni;
682 linux_msginfo.msgssz = msginfo.msgssz;
683 linux_msginfo.msgtql = msginfo.msgtql; /* XXX MSG_INFO. */
684 linux_msginfo.msgseg = msginfo.msgseg;
685 error = copyout(&linux_msginfo, PTRIN(args->buf),
686 sizeof(linux_msginfo));
687 if (error == 0)
688 td->td_retval[0] = msginfo.msgmni; /* XXX */
689
690 return (error);
691 }
692
693 /*
694 * TODO: implement this
695 * case LINUX_MSG_STAT:
696 */
697 case LINUX_IPC_STAT:
698 /* NOTHING */
699 break;
700
701 case LINUX_IPC_SET:
702 error = linux_msqid_pullup(args->cmd & LINUX_IPC_64,
703 &linux_msqid, PTRIN(args->buf));
704 if (error)
705 return (error);
706 linux_to_bsd_msqid_ds(&linux_msqid, &bsd_msqid);
707 break;
708
709 case LINUX_IPC_RMID:
710 /* NOTHING */
711 break;
712
713 default:
714 return (EINVAL);
715 break;
716 }
717
718 error = kern_msgctl(td, args->msqid, bsd_cmd, &bsd_msqid);
719 if (error != 0)
720 if (bsd_cmd != LINUX_IPC_RMID || error != EINVAL)
721 return (error);
722
723 if (bsd_cmd == LINUX_IPC_STAT) {
724 bsd_to_linux_msqid_ds(&bsd_msqid, &linux_msqid);
725 return (linux_msqid_pushdown(args->cmd & LINUX_IPC_64,
726 &linux_msqid, PTRIN(args->buf)));
727 }
728
729 return (0);
730 }
731
732 int
733 linux_shmat(struct thread *td, struct linux_shmat_args *args)
734 {
735 struct shmat_args /* {
736 int shmid;
737 void *shmaddr;
738 int shmflg;
739 } */ bsd_args;
740 int error;
741 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
742 l_uintptr_t addr;
743 #endif
744
745 bsd_args.shmid = args->shmid;
746 bsd_args.shmaddr = PTRIN(args->shmaddr);
747 bsd_args.shmflg = args->shmflg;
748 if ((error = shmat(td, &bsd_args)))
749 return (error);
750 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
751 addr = td->td_retval[0];
752 if ((error = copyout(&addr, PTRIN(args->raddr), sizeof(addr))))
753 return (error);
754 td->td_retval[0] = 0;
755 #endif
756 return (0);
757 }
758
759 int
760 linux_shmdt(struct thread *td, struct linux_shmdt_args *args)
761 {
762 struct shmdt_args /* {
763 void *shmaddr;
764 } */ bsd_args;
765
766 bsd_args.shmaddr = PTRIN(args->shmaddr);
767 return (shmdt(td, &bsd_args));
768 }
769
770 int
771 linux_shmget(struct thread *td, struct linux_shmget_args *args)
772 {
773 struct shmget_args /* {
774 key_t key;
775 int size;
776 int shmflg;
777 } */ bsd_args;
778
779 bsd_args.key = args->key;
780 bsd_args.size = args->size;
781 bsd_args.shmflg = args->shmflg;
782 return (shmget(td, &bsd_args));
783 }
784
785 int
786 linux_shmctl(struct thread *td, struct linux_shmctl_args *args)
787 {
788 struct l_shmid_ds linux_shmid;
789 struct l_shminfo linux_shminfo;
790 struct l_shm_info linux_shm_info;
791 struct shmid_ds bsd_shmid;
792 int error;
793
794 switch (args->cmd & ~LINUX_IPC_64) {
795
796 case LINUX_IPC_INFO: {
797 struct shminfo bsd_shminfo;
798
799 /* Perform shmctl wanting removed segments lookup */
800 error = kern_shmctl(td, args->shmid, IPC_INFO,
801 (void *)&bsd_shminfo, NULL);
802 if (error)
803 return (error);
804
805 bsd_to_linux_shminfo(&bsd_shminfo, &linux_shminfo);
806
807 return (linux_shminfo_pushdown(args->cmd & LINUX_IPC_64,
808 &linux_shminfo, PTRIN(args->buf)));
809 }
810
811 case LINUX_SHM_INFO: {
812 struct shm_info bsd_shm_info;
813
814 /* Perform shmctl wanting removed segments lookup */
815 error = kern_shmctl(td, args->shmid, SHM_INFO,
816 (void *)&bsd_shm_info, NULL);
817 if (error)
818 return (error);
819
820 bsd_to_linux_shm_info(&bsd_shm_info, &linux_shm_info);
821
822 return (copyout(&linux_shm_info, PTRIN(args->buf),
823 sizeof(struct l_shm_info)));
824 }
825
826 case LINUX_IPC_STAT:
827 /* Perform shmctl wanting removed segments lookup */
828 error = kern_shmctl(td, args->shmid, IPC_STAT,
829 (void *)&bsd_shmid, NULL);
830 if (error)
831 return (error);
832
833 bsd_to_linux_shmid_ds(&bsd_shmid, &linux_shmid);
834
835 return (linux_shmid_pushdown(args->cmd & LINUX_IPC_64,
836 &linux_shmid, PTRIN(args->buf)));
837
838 case LINUX_SHM_STAT:
839 /* Perform shmctl wanting removed segments lookup */
840 error = kern_shmctl(td, args->shmid, IPC_STAT,
841 (void *)&bsd_shmid, NULL);
842 if (error)
843 return (error);
844
845 bsd_to_linux_shmid_ds(&bsd_shmid, &linux_shmid);
846
847 return (linux_shmid_pushdown(args->cmd & LINUX_IPC_64,
848 &linux_shmid, PTRIN(args->buf)));
849
850 case LINUX_IPC_SET:
851 error = linux_shmid_pullup(args->cmd & LINUX_IPC_64,
852 &linux_shmid, PTRIN(args->buf));
853 if (error)
854 return (error);
855
856 linux_to_bsd_shmid_ds(&linux_shmid, &bsd_shmid);
857
858 /* Perform shmctl wanting removed segments lookup */
859 return (kern_shmctl(td, args->shmid, IPC_SET,
860 (void *)&bsd_shmid, NULL));
861
862 case LINUX_IPC_RMID: {
863 void *buf;
864
865 if (args->buf == 0)
866 buf = NULL;
867 else {
868 error = linux_shmid_pullup(args->cmd & LINUX_IPC_64,
869 &linux_shmid, PTRIN(args->buf));
870 if (error)
871 return (error);
872 linux_to_bsd_shmid_ds(&linux_shmid, &bsd_shmid);
873 buf = (void *)&bsd_shmid;
874 }
875 return (kern_shmctl(td, args->shmid, IPC_RMID, buf, NULL));
876 }
877
878 case LINUX_SHM_LOCK:
879 /* FALLTHROUGH */
880 case LINUX_SHM_UNLOCK:
881 /* FALLTHROUGH */
882 default:
883 linux_msg(td, "ipc type %d not implemented",
884 args->cmd & ~LINUX_IPC_64);
885 return (EINVAL);
886 }
887 }
888
889 MODULE_DEPEND(linux, sysvmsg, 1, 1, 1);
890 MODULE_DEPEND(linux, sysvsem, 1, 1, 1);
891 MODULE_DEPEND(linux, sysvshm, 1, 1, 1);
Cache object: c0ab09c4240a24bef4e350d2c36910a4
|