Home | History | Annotate | Line # | Download | only in common
      1 /*	$NetBSD: kern_ipc_10.c,v 1.27 2026/09/20 13:45:00 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Adam Glass and Charles M. Hannum.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     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. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Adam Glass and Charles M.
     17  *	Hannum.
     18  * 4. The names of the authors may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: kern_ipc_10.c,v 1.27 2026/09/20 13:45:00 riastradh Exp $");
     35 
     36 #ifdef _KERNEL_OPT
     37 #include "opt_compat_netbsd.h"
     38 #include "opt_sysv.h"
     39 #endif
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/proc.h>
     45 #include <sys/sem.h>
     46 #include <sys/shm.h>
     47 
     48 #include <sys/mount.h>
     49 #include <sys/syscallargs.h>
     50 
     51 #include <compat/common/compat_util.h>
     52 #include <compat/sys/shm.h>
     53 #include <compat/sys/sem.h>
     54 
     55 #if defined(SYSVSEM) && !defined(_LP64)
     56 int
     57 compat_10_sys_semsys(struct lwp *l, const struct compat_10_sys_semsys_args *uap, register_t *retval)
     58 {
     59 	/* {
     60 		syscallarg(int) which;
     61 		syscallarg(int) a2;
     62 		syscallarg(int) a3;
     63 		syscallarg(int) a4;
     64 		syscallarg(int) a5;
     65 	} */
     66 	struct sys_semget_args /* {
     67 		syscallarg(key_t) key;
     68 		syscallarg(int) nsems;
     69 		syscallarg(int) semflg;
     70 	} */ semget_args;
     71 	struct sys_semop_args /* {
     72 		syscallarg(int) semid;
     73 		syscallarg(struct sembuf *) sops;
     74 		syscallarg(u_int) nsops;
     75 	} */ semop_args;
     76 	struct sys_semconfig_args /* {
     77 		syscallarg(int) flag;
     78 	} */ semconfig_args;
     79 	struct semid_ds sembuf;
     80 	struct semid_ds14 osembuf;
     81 	void *pass_arg;
     82 	int a5 = SCARG(uap, a5);
     83 	int error;
     84 
     85 	switch (SCARG(uap, which)) {
     86 	case 0:						/* __semctl() */
     87 #define	semctl_semid	SCARG(uap, a2)
     88 #define	semctl_semnum	SCARG(uap, a3)
     89 #define	semctl_cmd	SCARG(uap, a4)
     90 #define	semctl_arg	((union __semun *)&a5)
     91 		pass_arg = get_semctl_arg(semctl_cmd, &sembuf, semctl_arg);
     92 		if (semctl_cmd == IPC_SET) {
     93 			error = copyin(semctl_arg->buf, &osembuf, sizeof osembuf);
     94 			if (error != 0)
     95 				return error;
     96 			__semid_ds14_to_native(&osembuf, &sembuf);
     97 		}
     98 		error = semctl1(l, semctl_semid, semctl_semnum, semctl_cmd,
     99 		    pass_arg, retval);
    100 		if (error == 0 && semctl_cmd == IPC_STAT) {
    101 			__native_to_semid_ds14(&sembuf, &osembuf);
    102 			error = copyout(&osembuf, semctl_arg->buf, sizeof(osembuf));
    103 		}
    104 		return error;
    105 #undef	semctl_semid
    106 #undef	semctl_semnum
    107 #undef	semctl_cmd
    108 #undef	semctl_arg
    109 
    110 	case 1:						/* semget() */
    111 		memset(&semget_args, 0, sizeof(semget_args));
    112 		SCARG(&semget_args, key) = SCARG(uap, a2);
    113 		SCARG(&semget_args, nsems) = SCARG(uap, a3);
    114 		SCARG(&semget_args, semflg) = SCARG(uap, a4);
    115 		return (sys_semget(l, &semget_args, retval));
    116 
    117 	case 2:						/* semop() */
    118 		memset(&semop_args, 0, sizeof(semop_args));
    119 		SCARG(&semop_args, semid) = SCARG(uap, a2);
    120 		SCARG(&semop_args, sops) =
    121 		    (struct sembuf *)(u_long)SCARG(uap, a3);
    122 		SCARG(&semop_args, nsops) = SCARG(uap, a4);
    123 		return (sys_semop(l, &semop_args, retval));
    124 
    125 	case 3:						/* semconfig() */
    126 		memset(&semconfig_args, 0, sizeof(semconfig_args));
    127 		SCARG(&semconfig_args, flag) = SCARG(uap, a2);
    128 		return (sys_semconfig(l, &semconfig_args, retval));
    129 
    130 	default:
    131 		return (EINVAL);
    132 	}
    133 }
    134 #endif
    135 
    136 #if defined(SYSVSHM) && !defined(_LP64)
    137 int
    138 compat_10_sys_shmsys(struct lwp *l, const struct compat_10_sys_shmsys_args *uap, register_t *retval)
    139 {
    140 	/* {
    141 		syscallarg(int) which;
    142 		syscallarg(int) a2;
    143 		syscallarg(int) a3;
    144 		syscallarg(int) a4;
    145 	} */
    146 	struct sys_shmat_args /* {
    147 		syscallarg(int) shmid;
    148 		syscallarg(void *) shmaddr;
    149 		syscallarg(int) shmflg;
    150 	} */ shmat_args;
    151 	struct compat_14_sys_shmctl_args /* {
    152 		syscallarg(int) shmid;
    153 		syscallarg(int) cmd;
    154 		syscallarg(struct shmid14_ds *) buf;
    155 	} */ shmctl_args;
    156 	struct sys_shmdt_args /* {
    157 		syscallarg(void *) shmaddr;
    158 	} */ shmdt_args;
    159 	struct sys_shmget_args /* {
    160 		syscallarg(key_t) key;
    161 		syscallarg(int) size;
    162 		syscallarg(int) shmflg;
    163 	} */ shmget_args;
    164 
    165 	switch (SCARG(uap, which)) {
    166 	case 0:						/* shmat() */
    167 		memset(&shmat_args, 0, sizeof(shmat_args));
    168 		SCARG(&shmat_args, shmid) = SCARG(uap, a2);
    169 		SCARG(&shmat_args, shmaddr) =
    170 		    (void *)(u_long)SCARG(uap, a3);
    171 		SCARG(&shmat_args, shmflg) = SCARG(uap, a4);
    172 		return (sys_shmat(l, &shmat_args, retval));
    173 
    174 	case 1:						/* shmctl() */
    175 		memset(&shmctl_args, 0, sizeof(shmctl_args));
    176 		SCARG(&shmctl_args, shmid) = SCARG(uap, a2);
    177 		SCARG(&shmctl_args, cmd) = SCARG(uap, a3);
    178 		SCARG(&shmctl_args, buf) =
    179 		    (struct shmid_ds14 *)(u_long)SCARG(uap, a4);
    180 		return (compat_14_sys_shmctl(l, &shmctl_args, retval));
    181 
    182 	case 2:						/* shmdt() */
    183 		memset(&shmdt_args, 0, sizeof(shmdt_args));
    184 		SCARG(&shmdt_args, shmaddr) =
    185 		    (void *)(u_long)SCARG(uap, a2);
    186 		return (sys_shmdt(l, &shmdt_args, retval));
    187 
    188 	case 3:						/* shmget() */
    189 		memset(&shmget_args, 0, sizeof(shmget_args));
    190 		SCARG(&shmget_args, key) = SCARG(uap, a2);
    191 		SCARG(&shmget_args, size) = SCARG(uap, a3);
    192 		SCARG(&shmget_args, shmflg) = SCARG(uap, a4);
    193 		return (sys_shmget(l, &shmget_args, retval));
    194 
    195 	default:
    196 		return (EINVAL);
    197 	}
    198 }
    199 #endif
    200 
    201 #if defined(SYSVMSG) && !defined(_LP64)
    202 int
    203 compat_10_sys_msgsys(struct lwp *l, const struct compat_10_sys_msgsys_args *uap, register_t *retval)
    204 {
    205 	/* {
    206 		syscallarg(int) which;
    207 		syscallarg(int) a2;
    208 		syscallarg(int) a3;
    209 		syscallarg(int) a4;
    210 		syscallarg(int) a5;
    211 		syscallarg(int) a6;
    212 	} */
    213 	struct compat_14_sys_msgctl_args /* {
    214 		syscallarg(int) msqid;
    215 		syscallarg(int) cmd;
    216 		syscallarg(struct msqid14_ds *) buf;
    217 	} */ msgctl_args;
    218 	struct sys_msgget_args /* {
    219 		syscallarg(key_t) key;
    220 		syscallarg(int) msgflg;
    221 	} */ msgget_args;
    222 	struct sys_msgsnd_args /* {
    223 		syscallarg(int) msqid;
    224 		syscallarg(void *) msgp;
    225 		syscallarg(size_t) msgsz;
    226 		syscallarg(int) msgflg;
    227 	} */ msgsnd_args;
    228 	struct sys_msgrcv_args /* {
    229 		syscallarg(int) msqid;
    230 		syscallarg(void *) msgp;
    231 		syscallarg(size_t) msgsz;
    232 		syscallarg(long) msgtyp;
    233 		syscallarg(int) msgflg;
    234 	} */ msgrcv_args;
    235 
    236 	switch (SCARG(uap, which)) {
    237 	case 0:					/* msgctl()*/
    238 		memset(&msgctl_args, 0, sizeof(msgctl_args));
    239 		SCARG(&msgctl_args, msqid) = SCARG(uap, a2);
    240 		SCARG(&msgctl_args, cmd) = SCARG(uap, a3);
    241 		SCARG(&msgctl_args, buf) =
    242 		    (struct msqid_ds14 *)(u_long)SCARG(uap, a4);
    243 		return (compat_14_sys_msgctl(l, &msgctl_args, retval));
    244 
    245 	case 1:					/* msgget() */
    246 		memset(&msgget_args, 0, sizeof(msgget_args));
    247 		SCARG(&msgget_args, key) = SCARG(uap, a2);
    248 		SCARG(&msgget_args, msgflg) = SCARG(uap, a3);
    249 		return (sys_msgget(l, &msgget_args, retval));
    250 
    251 	case 2:					/* msgsnd() */
    252 		memset(&msgsnd_args, 0, sizeof(msgsnd_args));
    253 		SCARG(&msgsnd_args, msqid) = SCARG(uap, a2);
    254 		SCARG(&msgsnd_args, msgp) =
    255 		    (void *)(u_long)SCARG(uap, a3);
    256 		SCARG(&msgsnd_args, msgsz) = SCARG(uap, a4);
    257 		SCARG(&msgsnd_args, msgflg) = SCARG(uap, a5);
    258 		return (sys_msgsnd(l, &msgsnd_args, retval));
    259 
    260 	case 3:					/* msgrcv() */
    261 		memset(&msgrcv_args, 0, sizeof(msgrcv_args));
    262 		SCARG(&msgrcv_args, msqid) = SCARG(uap, a2);
    263 		SCARG(&msgrcv_args, msgp) =
    264 		    (void *)(u_long)SCARG(uap, a3);
    265 		SCARG(&msgrcv_args, msgsz) = SCARG(uap, a4);
    266 		SCARG(&msgrcv_args, msgtyp) = SCARG(uap, a5);
    267 		SCARG(&msgrcv_args, msgflg) = SCARG(uap, a6);
    268 		return (sys_msgrcv(l, &msgrcv_args, retval));
    269 
    270 	default:
    271 		return (EINVAL);
    272 	}
    273 }
    274 #endif
    275