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