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$");
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 static void
121 linux_to_bsd_ipc_perm(struct l_ipc_perm *lpp, struct ipc_perm *bpp)
122 {
123
124 bpp->key = lpp->key;
125 bpp->uid = lpp->uid;
126 bpp->gid = lpp->gid;
127 bpp->cuid = lpp->cuid;
128 bpp->cgid = lpp->cgid;
129 bpp->mode = lpp->mode;
130 bpp->seq = lpp->seq;
131 }
132
133
134 static void
135 bsd_to_linux_ipc_perm(struct ipc_perm *bpp, struct l_ipc_perm *lpp)
136 {
137
138 lpp->key = bpp->key;
139 lpp->uid = bpp->uid;
140 lpp->gid = bpp->gid;
141 lpp->cuid = bpp->cuid;
142 lpp->cgid = bpp->cgid;
143 lpp->mode = bpp->mode;
144 lpp->seq = bpp->seq;
145 }
146
147 struct l_msqid_ds {
148 struct l_ipc_perm msg_perm;
149 l_uintptr_t msg_first; /* first message on queue,unused */
150 l_uintptr_t msg_last; /* last message in queue,unused */
151 l_time_t msg_stime; /* last msgsnd time */
152 l_time_t msg_rtime; /* last msgrcv time */
153 l_time_t msg_ctime; /* last change time */
154 l_ulong msg_lcbytes; /* Reuse junk fields for 32 bit */
155 l_ulong msg_lqbytes; /* ditto */
156 l_ushort msg_cbytes; /* current number of bytes on queue */
157 l_ushort msg_qnum; /* number of messages in queue */
158 l_ushort msg_qbytes; /* max number of bytes on queue */
159 l_pid_t msg_lspid; /* pid of last msgsnd */
160 l_pid_t msg_lrpid; /* last receive pid */
161 }
162 #if defined(__amd64__) && defined(COMPAT_LINUX32)
163 __packed
164 #endif
165 ;
166
167 struct l_semid_ds {
168 struct l_ipc_perm sem_perm;
169 l_time_t sem_otime;
170 l_time_t sem_ctime;
171 l_uintptr_t sem_base;
172 l_uintptr_t sem_pending;
173 l_uintptr_t sem_pending_last;
174 l_uintptr_t undo;
175 l_ushort sem_nsems;
176 }
177 #if defined(__amd64__) && defined(COMPAT_LINUX32)
178 __packed
179 #endif
180 ;
181
182 struct l_shmid_ds {
183 struct l_ipc_perm shm_perm;
184 l_int shm_segsz;
185 l_time_t shm_atime;
186 l_time_t shm_dtime;
187 l_time_t shm_ctime;
188 l_ushort shm_cpid;
189 l_ushort shm_lpid;
190 l_short shm_nattch;
191 l_ushort private1;
192 l_uintptr_t private2;
193 l_uintptr_t private3;
194 };
195
196 static void
197 linux_to_bsd_semid_ds(struct l_semid_ds *lsp, struct semid_ds *bsp)
198 {
199
200 linux_to_bsd_ipc_perm(&lsp->sem_perm, &bsp->sem_perm);
201 bsp->sem_otime = lsp->sem_otime;
202 bsp->sem_ctime = lsp->sem_ctime;
203 bsp->sem_nsems = lsp->sem_nsems;
204 bsp->sem_base = PTRIN(lsp->sem_base);
205 }
206
207 static void
208 bsd_to_linux_semid_ds(struct semid_ds *bsp, struct l_semid_ds *lsp)
209 {
210
211 bsd_to_linux_ipc_perm(&bsp->sem_perm, &lsp->sem_perm);
212 lsp->sem_otime = bsp->sem_otime;
213 lsp->sem_ctime = bsp->sem_ctime;
214 lsp->sem_nsems = bsp->sem_nsems;
215 lsp->sem_base = PTROUT(bsp->sem_base);
216 }
217
218 static void
219 linux_to_bsd_shmid_ds(struct l_shmid_ds *lsp, struct shmid_ds *bsp)
220 {
221
222 linux_to_bsd_ipc_perm(&lsp->shm_perm, &bsp->shm_perm);
223 bsp->shm_segsz = lsp->shm_segsz;
224 bsp->shm_lpid = lsp->shm_lpid;
225 bsp->shm_cpid = lsp->shm_cpid;
226 bsp->shm_nattch = lsp->shm_nattch;
227 bsp->shm_atime = lsp->shm_atime;
228 bsp->shm_dtime = lsp->shm_dtime;
229 bsp->shm_ctime = lsp->shm_ctime;
230 }
231
232 static void
233 bsd_to_linux_shmid_ds(struct shmid_ds *bsp, struct l_shmid_ds *lsp)
234 {
235
236 bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->shm_perm);
237 if (bsp->shm_segsz > INT_MAX)
238 lsp->shm_segsz = INT_MAX;
239 else
240 lsp->shm_segsz = bsp->shm_segsz;
241 lsp->shm_lpid = bsp->shm_lpid;
242 lsp->shm_cpid = bsp->shm_cpid;
243 if (bsp->shm_nattch > SHRT_MAX)
244 lsp->shm_nattch = SHRT_MAX;
245 else
246 lsp->shm_nattch = bsp->shm_nattch;
247 lsp->shm_atime = bsp->shm_atime;
248 lsp->shm_dtime = bsp->shm_dtime;
249 lsp->shm_ctime = bsp->shm_ctime;
250 lsp->private3 = 0;
251 }
252
253 static void
254 linux_to_bsd_msqid_ds(struct l_msqid_ds *lsp, struct msqid_ds *bsp)
255 {
256
257 linux_to_bsd_ipc_perm(&lsp->msg_perm, &bsp->msg_perm);
258 bsp->msg_cbytes = lsp->msg_cbytes;
259 bsp->msg_qnum = lsp->msg_qnum;
260 bsp->msg_qbytes = lsp->msg_qbytes;
261 bsp->msg_lspid = lsp->msg_lspid;
262 bsp->msg_lrpid = lsp->msg_lrpid;
263 bsp->msg_stime = lsp->msg_stime;
264 bsp->msg_rtime = lsp->msg_rtime;
265 bsp->msg_ctime = lsp->msg_ctime;
266 }
267
268 static void
269 bsd_to_linux_msqid_ds(struct msqid_ds *bsp, struct l_msqid_ds *lsp)
270 {
271
272 bsd_to_linux_ipc_perm(&bsp->msg_perm, &lsp->msg_perm);
273 lsp->msg_cbytes = bsp->msg_cbytes;
274 lsp->msg_qnum = bsp->msg_qnum;
275 lsp->msg_qbytes = bsp->msg_qbytes;
276 lsp->msg_lspid = bsp->msg_lspid;
277 lsp->msg_lrpid = bsp->msg_lrpid;
278 lsp->msg_stime = bsp->msg_stime;
279 lsp->msg_rtime = bsp->msg_rtime;
280 lsp->msg_ctime = bsp->msg_ctime;
281 }
282
283 static void
284 linux_ipc_perm_to_ipc64_perm(struct l_ipc_perm *in, struct l_ipc64_perm *out)
285 {
286
287 /* XXX: do we really need to do something here? */
288 out->key = in->key;
289 out->uid = in->uid;
290 out->gid = in->gid;
291 out->cuid = in->cuid;
292 out->cgid = in->cgid;
293 out->mode = in->mode;
294 out->seq = in->seq;
295 }
296
297 static int
298 linux_msqid_pullup(l_int ver, struct l_msqid_ds *linux_msqid, caddr_t uaddr)
299 {
300 struct l_msqid64_ds linux_msqid64;
301 int error;
302
303 if (ver == LINUX_IPC_64) {
304 error = copyin(uaddr, &linux_msqid64, sizeof(linux_msqid64));
305 if (error != 0)
306 return (error);
307
308 bzero(linux_msqid, sizeof(*linux_msqid));
309
310 linux_msqid->msg_perm.uid = linux_msqid64.msg_perm.uid;
311 linux_msqid->msg_perm.gid = linux_msqid64.msg_perm.gid;
312 linux_msqid->msg_perm.mode = linux_msqid64.msg_perm.mode;
313
314 if (linux_msqid64.msg_qbytes > USHRT_MAX)
315 linux_msqid->msg_lqbytes = linux_msqid64.msg_qbytes;
316 else
317 linux_msqid->msg_qbytes = linux_msqid64.msg_qbytes;
318 } else
319 error = copyin(uaddr, linux_msqid, sizeof(*linux_msqid));
320
321 return (error);
322 }
323
324 static int
325 linux_msqid_pushdown(l_int ver, struct l_msqid_ds *linux_msqid, caddr_t uaddr)
326 {
327 struct l_msqid64_ds linux_msqid64;
328
329 if (ver == LINUX_IPC_64) {
330 bzero(&linux_msqid64, sizeof(linux_msqid64));
331
332 linux_ipc_perm_to_ipc64_perm(&linux_msqid->msg_perm,
333 &linux_msqid64.msg_perm);
334
335 linux_msqid64.msg_stime = linux_msqid->msg_stime;
336 linux_msqid64.msg_rtime = linux_msqid->msg_rtime;
337 linux_msqid64.msg_ctime = linux_msqid->msg_ctime;
338
339 if (linux_msqid->msg_cbytes == 0)
340 linux_msqid64.msg_cbytes = linux_msqid->msg_lcbytes;
341 else
342 linux_msqid64.msg_cbytes = linux_msqid->msg_cbytes;
343
344 linux_msqid64.msg_qnum = linux_msqid->msg_qnum;
345
346 if (linux_msqid->msg_qbytes == 0)
347 linux_msqid64.msg_qbytes = linux_msqid->msg_lqbytes;
348 else
349 linux_msqid64.msg_qbytes = linux_msqid->msg_qbytes;
350
351 linux_msqid64.msg_lspid = linux_msqid->msg_lspid;
352 linux_msqid64.msg_lrpid = linux_msqid->msg_lrpid;
353
354 return (copyout(&linux_msqid64, uaddr, sizeof(linux_msqid64)));
355 } else
356 return (copyout(linux_msqid, uaddr, sizeof(*linux_msqid)));
357 }
358
359 static int
360 linux_semid_pullup(l_int ver, struct l_semid_ds *linux_semid, caddr_t uaddr)
361 {
362 struct l_semid64_ds linux_semid64;
363 int error;
364
365 if (ver == LINUX_IPC_64) {
366 error = copyin(uaddr, &linux_semid64, sizeof(linux_semid64));
367 if (error != 0)
368 return (error);
369
370 bzero(linux_semid, sizeof(*linux_semid));
371
372 linux_semid->sem_perm.uid = linux_semid64.sem_perm.uid;
373 linux_semid->sem_perm.gid = linux_semid64.sem_perm.gid;
374 linux_semid->sem_perm.mode = linux_semid64.sem_perm.mode;
375 } else
376 error = copyin(uaddr, linux_semid, sizeof(*linux_semid));
377
378 return (error);
379 }
380
381 static int
382 linux_semid_pushdown(l_int ver, struct l_semid_ds *linux_semid, caddr_t uaddr)
383 {
384 struct l_semid64_ds linux_semid64;
385
386 if (ver == LINUX_IPC_64) {
387 bzero(&linux_semid64, sizeof(linux_semid64));
388
389 linux_ipc_perm_to_ipc64_perm(&linux_semid->sem_perm,
390 &linux_semid64.sem_perm);
391
392 linux_semid64.sem_otime = linux_semid->sem_otime;
393 linux_semid64.sem_ctime = linux_semid->sem_ctime;
394 linux_semid64.sem_nsems = linux_semid->sem_nsems;
395
396 return (copyout(&linux_semid64, uaddr, sizeof(linux_semid64)));
397 } else
398 return (copyout(linux_semid, uaddr, sizeof(*linux_semid)));
399 }
400
401 static int
402 linux_shmid_pullup(l_int ver, struct l_shmid_ds *linux_shmid, caddr_t uaddr)
403 {
404 struct l_shmid64_ds linux_shmid64;
405 int error;
406
407 if (ver == LINUX_IPC_64) {
408 error = copyin(uaddr, &linux_shmid64, sizeof(linux_shmid64));
409 if (error != 0)
410 return (error);
411
412 bzero(linux_shmid, sizeof(*linux_shmid));
413
414 linux_shmid->shm_perm.uid = linux_shmid64.shm_perm.uid;
415 linux_shmid->shm_perm.gid = linux_shmid64.shm_perm.gid;
416 linux_shmid->shm_perm.mode = linux_shmid64.shm_perm.mode;
417 } else
418 error = copyin(uaddr, linux_shmid, sizeof(*linux_shmid));
419
420 return (error);
421 }
422
423 static int
424 linux_shmid_pushdown(l_int ver, struct l_shmid_ds *linux_shmid, caddr_t uaddr)
425 {
426 struct l_shmid64_ds linux_shmid64;
427
428 /*
429 * XXX: This is backwards and loses information in shm_nattch
430 * and shm_segsz. We should probably either expose the BSD
431 * shmid structure directly and convert it to either the
432 * non-64 or 64 variant directly or the code should always
433 * convert to the 64 variant and then truncate values into the
434 * non-64 variant if needed since the 64 variant has more
435 * precision.
436 */
437 if (ver == LINUX_IPC_64) {
438 bzero(&linux_shmid64, sizeof(linux_shmid64));
439
440 linux_ipc_perm_to_ipc64_perm(&linux_shmid->shm_perm,
441 &linux_shmid64.shm_perm);
442
443 linux_shmid64.shm_segsz = linux_shmid->shm_segsz;
444 linux_shmid64.shm_atime = linux_shmid->shm_atime;
445 linux_shmid64.shm_dtime = linux_shmid->shm_dtime;
446 linux_shmid64.shm_ctime = linux_shmid->shm_ctime;
447 linux_shmid64.shm_cpid = linux_shmid->shm_cpid;
448 linux_shmid64.shm_lpid = linux_shmid->shm_lpid;
449 linux_shmid64.shm_nattch = linux_shmid->shm_nattch;
450
451 return (copyout(&linux_shmid64, uaddr, sizeof(linux_shmid64)));
452 } else
453 return (copyout(linux_shmid, uaddr, sizeof(*linux_shmid)));
454 }
455
456 static int
457 linux_shminfo_pushdown(l_int ver, struct l_shminfo *linux_shminfo,
458 caddr_t uaddr)
459 {
460 struct l_shminfo64 linux_shminfo64;
461
462 if (ver == LINUX_IPC_64) {
463 bzero(&linux_shminfo64, sizeof(linux_shminfo64));
464
465 linux_shminfo64.shmmax = linux_shminfo->shmmax;
466 linux_shminfo64.shmmin = linux_shminfo->shmmin;
467 linux_shminfo64.shmmni = linux_shminfo->shmmni;
468 linux_shminfo64.shmseg = linux_shminfo->shmseg;
469 linux_shminfo64.shmall = linux_shminfo->shmall;
470
471 return (copyout(&linux_shminfo64, uaddr,
472 sizeof(linux_shminfo64)));
473 } else
474 return (copyout(linux_shminfo, uaddr, sizeof(*linux_shminfo)));
475 }
476
477 int
478 linux_semop(struct thread *td, struct linux_semop_args *args)
479 {
480 struct semop_args /* {
481 int semid;
482 struct sembuf *sops;
483 int nsops;
484 } */ bsd_args;
485
486 bsd_args.semid = args->semid;
487 bsd_args.sops = PTRIN(args->tsops);
488 bsd_args.nsops = args->nsops;
489 return (sys_semop(td, &bsd_args));
490 }
491
492 int
493 linux_semget(struct thread *td, struct linux_semget_args *args)
494 {
495 struct semget_args /* {
496 key_t key;
497 int nsems;
498 int semflg;
499 } */ bsd_args;
500
501 if (args->nsems < 0)
502 return (EINVAL);
503 bsd_args.key = args->key;
504 bsd_args.nsems = args->nsems;
505 bsd_args.semflg = args->semflg;
506 return (sys_semget(td, &bsd_args));
507 }
508
509 int
510 linux_semctl(struct thread *td, struct linux_semctl_args *args)
511 {
512 struct l_semid_ds linux_semid;
513 struct l_seminfo linux_seminfo;
514 struct semid_ds semid;
515 union semun semun;
516 register_t rval;
517 int cmd, error;
518
519 memset(&linux_seminfo, 0, sizeof(linux_seminfo));
520 memset(&linux_semid, 0, sizeof(linux_semid));
521
522 switch (args->cmd & ~LINUX_IPC_64) {
523 case LINUX_IPC_RMID:
524 cmd = IPC_RMID;
525 break;
526 case LINUX_GETNCNT:
527 cmd = GETNCNT;
528 break;
529 case LINUX_GETPID:
530 cmd = GETPID;
531 break;
532 case LINUX_GETVAL:
533 cmd = GETVAL;
534 break;
535 case LINUX_GETZCNT:
536 cmd = GETZCNT;
537 break;
538 case LINUX_SETVAL:
539 cmd = SETVAL;
540 semun.val = args->arg.val;
541 break;
542 case LINUX_IPC_SET:
543 cmd = IPC_SET;
544 error = linux_semid_pullup(args->cmd & LINUX_IPC_64,
545 &linux_semid, PTRIN(args->arg.buf));
546 if (error)
547 return (error);
548 linux_to_bsd_semid_ds(&linux_semid, &semid);
549 semun.buf = &semid;
550 return (kern_semctl(td, args->semid, args->semnum, cmd, &semun,
551 td->td_retval));
552 case LINUX_IPC_STAT:
553 case LINUX_SEM_STAT:
554 if ((args->cmd & ~LINUX_IPC_64) == LINUX_IPC_STAT)
555 cmd = IPC_STAT;
556 else
557 cmd = SEM_STAT;
558 semun.buf = &semid;
559 error = kern_semctl(td, args->semid, args->semnum, cmd, &semun,
560 &rval);
561 if (error)
562 return (error);
563 bsd_to_linux_semid_ds(&semid, &linux_semid);
564 error = linux_semid_pushdown(args->cmd & LINUX_IPC_64,
565 &linux_semid, PTRIN(args->arg.buf));
566 if (error == 0)
567 td->td_retval[0] = (cmd == SEM_STAT) ? rval : 0;
568 return (error);
569 case LINUX_IPC_INFO:
570 case LINUX_SEM_INFO:
571 bcopy(&seminfo, &linux_seminfo.semmni, sizeof(linux_seminfo) -
572 sizeof(linux_seminfo.semmap) );
573 /*
574 * Linux does not use the semmap field but populates it with
575 * the defined value from SEMMAP, which really is redefined to
576 * SEMMNS, which they define as SEMMNI * SEMMSL. Try to
577 * simulate this returning our dynamic semmns value.
578 */
579 linux_seminfo.semmap = linux_seminfo.semmns;
580 /* XXX BSD equivalent?
581 #define used_semids 10
582 #define used_sems 10
583 linux_seminfo.semusz = used_semids;
584 linux_seminfo.semaem = used_sems;
585 */
586 error = copyout(&linux_seminfo,
587 PTRIN(args->arg.buf), sizeof(linux_seminfo));
588 if (error)
589 return (error);
590 td->td_retval[0] = seminfo.semmni;
591 return (0); /* No need for __semctl call */
592 case LINUX_GETALL:
593 cmd = GETALL;
594 semun.val = args->arg.val;
595 break;
596 case LINUX_SETALL:
597 cmd = SETALL;
598 semun.val = args->arg.val;
599 break;
600 default:
601 linux_msg(td, "ipc type %d is not implemented",
602 args->cmd & ~LINUX_IPC_64);
603 return (EINVAL);
604 }
605 return (kern_semctl(td, args->semid, args->semnum, cmd, &semun,
606 td->td_retval));
607 }
608
609 int
610 linux_msgsnd(struct thread *td, struct linux_msgsnd_args *args)
611 {
612 const void *msgp;
613 long mtype;
614 l_long lmtype;
615 int error;
616
617 if ((l_long)args->msgsz < 0 || args->msgsz > (l_long)msginfo.msgmax)
618 return (EINVAL);
619 msgp = PTRIN(args->msgp);
620 if ((error = copyin(msgp, &lmtype, sizeof(lmtype))) != 0)
621 return (error);
622 mtype = (long)lmtype;
623 return (kern_msgsnd(td, args->msqid,
624 (const char *)msgp + sizeof(lmtype),
625 args->msgsz, args->msgflg, mtype));
626 }
627
628 int
629 linux_msgrcv(struct thread *td, struct linux_msgrcv_args *args)
630 {
631 void *msgp;
632 long mtype;
633 l_long lmtype;
634 int error;
635
636 if ((l_long)args->msgsz < 0 || args->msgsz > (l_long)msginfo.msgmax)
637 return (EINVAL);
638 msgp = PTRIN(args->msgp);
639 if ((error = kern_msgrcv(td, args->msqid,
640 (char *)msgp + sizeof(lmtype), args->msgsz,
641 args->msgtyp, args->msgflg, &mtype)) != 0)
642 return (error);
643 lmtype = (l_long)mtype;
644 return (copyout(&lmtype, msgp, sizeof(lmtype)));
645 }
646
647 int
648 linux_msgget(struct thread *td, struct linux_msgget_args *args)
649 {
650 struct msgget_args /* {
651 key_t key;
652 int msgflg;
653 } */ bsd_args;
654
655 bsd_args.key = args->key;
656 bsd_args.msgflg = args->msgflg;
657 return (sys_msgget(td, &bsd_args));
658 }
659
660 int
661 linux_msgctl(struct thread *td, struct linux_msgctl_args *args)
662 {
663 int error, bsd_cmd;
664 struct l_msqid_ds linux_msqid;
665 struct msqid_ds bsd_msqid;
666
667 memset(&linux_msqid, 0, sizeof(linux_msqid));
668
669 bsd_cmd = args->cmd & ~LINUX_IPC_64;
670 switch (bsd_cmd) {
671 case LINUX_IPC_INFO:
672 case LINUX_MSG_INFO: {
673 struct l_msginfo linux_msginfo;
674
675 memset(&linux_msginfo, 0, sizeof(linux_msginfo));
676 /*
677 * XXX MSG_INFO uses the same data structure but returns different
678 * dynamic counters in msgpool, msgmap, and msgtql fields.
679 */
680 linux_msginfo.msgpool = (long)msginfo.msgmni *
681 (long)msginfo.msgmnb / 1024L; /* XXX MSG_INFO. */
682 linux_msginfo.msgmap = msginfo.msgmnb; /* XXX MSG_INFO. */
683 linux_msginfo.msgmax = msginfo.msgmax;
684 linux_msginfo.msgmnb = msginfo.msgmnb;
685 linux_msginfo.msgmni = msginfo.msgmni;
686 linux_msginfo.msgssz = msginfo.msgssz;
687 linux_msginfo.msgtql = msginfo.msgtql; /* XXX MSG_INFO. */
688 linux_msginfo.msgseg = msginfo.msgseg;
689 error = copyout(&linux_msginfo, PTRIN(args->buf),
690 sizeof(linux_msginfo));
691 if (error == 0)
692 td->td_retval[0] = msginfo.msgmni; /* XXX */
693
694 return (error);
695 }
696
697 /*
698 * TODO: implement this
699 * case LINUX_MSG_STAT:
700 */
701 case LINUX_IPC_STAT:
702 /* NOTHING */
703 break;
704
705 case LINUX_IPC_SET:
706 error = linux_msqid_pullup(args->cmd & LINUX_IPC_64,
707 &linux_msqid, PTRIN(args->buf));
708 if (error)
709 return (error);
710 linux_to_bsd_msqid_ds(&linux_msqid, &bsd_msqid);
711 break;
712
713 case LINUX_IPC_RMID:
714 /* NOTHING */
715 break;
716
717 default:
718 return (EINVAL);
719 break;
720 }
721
722 error = kern_msgctl(td, args->msqid, bsd_cmd, &bsd_msqid);
723 if (error != 0)
724 if (bsd_cmd != LINUX_IPC_RMID || error != EINVAL)
725 return (error);
726
727 if (bsd_cmd == LINUX_IPC_STAT) {
728 bsd_to_linux_msqid_ds(&bsd_msqid, &linux_msqid);
729 return (linux_msqid_pushdown(args->cmd & LINUX_IPC_64,
730 &linux_msqid, PTRIN(args->buf)));
731 }
732
733 return (0);
734 }
735
736 int
737 linux_shmat(struct thread *td, struct linux_shmat_args *args)
738 {
739 struct shmat_args /* {
740 int shmid;
741 void *shmaddr;
742 int shmflg;
743 } */ bsd_args;
744 int error;
745 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
746 l_uintptr_t addr;
747 #endif
748
749 bsd_args.shmid = args->shmid;
750 bsd_args.shmaddr = PTRIN(args->shmaddr);
751 bsd_args.shmflg = args->shmflg;
752 if ((error = sys_shmat(td, &bsd_args)))
753 return (error);
754 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
755 addr = td->td_retval[0];
756 if ((error = copyout(&addr, PTRIN(args->raddr), sizeof(addr))))
757 return (error);
758 td->td_retval[0] = 0;
759 #endif
760 return (0);
761 }
762
763 int
764 linux_shmdt(struct thread *td, struct linux_shmdt_args *args)
765 {
766 struct shmdt_args /* {
767 void *shmaddr;
768 } */ bsd_args;
769
770 bsd_args.shmaddr = PTRIN(args->shmaddr);
771 return (sys_shmdt(td, &bsd_args));
772 }
773
774 int
775 linux_shmget(struct thread *td, struct linux_shmget_args *args)
776 {
777 struct shmget_args /* {
778 key_t key;
779 int size;
780 int shmflg;
781 } */ bsd_args;
782
783 bsd_args.key = args->key;
784 bsd_args.size = args->size;
785 bsd_args.shmflg = args->shmflg;
786 return (sys_shmget(td, &bsd_args));
787 }
788
789 int
790 linux_shmctl(struct thread *td, struct linux_shmctl_args *args)
791 {
792 struct l_shmid_ds linux_shmid;
793 struct l_shminfo linux_shminfo;
794 struct l_shm_info linux_shm_info;
795 struct shmid_ds bsd_shmid;
796 int error;
797
798 memset(&linux_shm_info, 0, sizeof(linux_shm_info));
799 memset(&linux_shmid, 0, sizeof(linux_shmid));
800 memset(&linux_shminfo, 0, sizeof(linux_shminfo));
801
802 switch (args->cmd & ~LINUX_IPC_64) {
803
804 case LINUX_IPC_INFO: {
805 struct shminfo bsd_shminfo;
806
807 /* Perform shmctl wanting removed segments lookup */
808 error = kern_shmctl(td, args->shmid, IPC_INFO,
809 (void *)&bsd_shminfo, NULL);
810 if (error)
811 return (error);
812
813 bsd_to_linux_shminfo(&bsd_shminfo, &linux_shminfo);
814
815 return (linux_shminfo_pushdown(args->cmd & LINUX_IPC_64,
816 &linux_shminfo, PTRIN(args->buf)));
817 }
818
819 case LINUX_SHM_INFO: {
820 struct shm_info bsd_shm_info;
821
822 /* Perform shmctl wanting removed segments lookup */
823 error = kern_shmctl(td, args->shmid, SHM_INFO,
824 (void *)&bsd_shm_info, NULL);
825 if (error)
826 return (error);
827
828 bsd_to_linux_shm_info(&bsd_shm_info, &linux_shm_info);
829
830 return (copyout(&linux_shm_info, PTRIN(args->buf),
831 sizeof(struct l_shm_info)));
832 }
833
834 case LINUX_IPC_STAT:
835 /* Perform shmctl wanting removed segments lookup */
836 error = kern_shmctl(td, args->shmid, IPC_STAT,
837 (void *)&bsd_shmid, NULL);
838 if (error)
839 return (error);
840
841 bsd_to_linux_shmid_ds(&bsd_shmid, &linux_shmid);
842
843 return (linux_shmid_pushdown(args->cmd & LINUX_IPC_64,
844 &linux_shmid, PTRIN(args->buf)));
845
846 case LINUX_SHM_STAT:
847 /* Perform shmctl wanting removed segments lookup */
848 error = kern_shmctl(td, args->shmid, IPC_STAT,
849 (void *)&bsd_shmid, NULL);
850 if (error)
851 return (error);
852
853 bsd_to_linux_shmid_ds(&bsd_shmid, &linux_shmid);
854
855 return (linux_shmid_pushdown(args->cmd & LINUX_IPC_64,
856 &linux_shmid, PTRIN(args->buf)));
857
858 case LINUX_IPC_SET:
859 error = linux_shmid_pullup(args->cmd & LINUX_IPC_64,
860 &linux_shmid, PTRIN(args->buf));
861 if (error)
862 return (error);
863
864 linux_to_bsd_shmid_ds(&linux_shmid, &bsd_shmid);
865
866 /* Perform shmctl wanting removed segments lookup */
867 return (kern_shmctl(td, args->shmid, IPC_SET,
868 (void *)&bsd_shmid, NULL));
869
870 case LINUX_IPC_RMID: {
871 void *buf;
872
873 if (args->buf == 0)
874 buf = NULL;
875 else {
876 error = linux_shmid_pullup(args->cmd & LINUX_IPC_64,
877 &linux_shmid, PTRIN(args->buf));
878 if (error)
879 return (error);
880 linux_to_bsd_shmid_ds(&linux_shmid, &bsd_shmid);
881 buf = (void *)&bsd_shmid;
882 }
883 return (kern_shmctl(td, args->shmid, IPC_RMID, buf, NULL));
884 }
885
886 case LINUX_SHM_LOCK:
887 /* FALLTHROUGH */
888 case LINUX_SHM_UNLOCK:
889 /* FALLTHROUGH */
890 default:
891 linux_msg(td, "ipc type %d not implemented",
892 args->cmd & ~LINUX_IPC_64);
893 return (EINVAL);
894 }
895 }
896
897 MODULE_DEPEND(linux, sysvmsg, 1, 1, 1);
898 MODULE_DEPEND(linux, sysvsem, 1, 1, 1);
899 MODULE_DEPEND(linux, sysvshm, 1, 1, 1);
Cache object: 3975b51684bcc2eee5dfe77435b9e1b4
|