1 /* $NetBSD: freebsd_ipc.c,v 1.18 2026/09/20 13:45:28 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: freebsd_ipc.c,v 1.18 2026/09/20 13:45:28 riastradh Exp $"); 35 36 #if defined(_KERNEL_OPT) 37 #include "opt_sysv.h" 38 #endif 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/proc.h> 44 #include <sys/sem.h> 45 #include <sys/shm.h> 46 47 #include <sys/mount.h> 48 #include <sys/syscallargs.h> 49 50 #include <compat/sys/shm.h> 51 #include <compat/freebsd/freebsd_syscallargs.h> 52 53 #ifdef SYSVSEM 54 int 55 freebsd_sys_semsys(struct lwp *l, const struct freebsd_sys_semsys_args *uap, register_t *retval) 56 { 57 /* { 58 syscallarg(int) which; 59 syscallarg(int) a2; 60 syscallarg(int) a3; 61 syscallarg(int) a4; 62 syscallarg(int) a5; 63 } */ 64 struct compat_14_sys___semctl_args /* { 65 syscallarg(int) semid; 66 syscallarg(int) semnum; 67 syscallarg(int) cmd; 68 syscallarg(union __semun *) arg; 69 } */ __semctl_args; 70 struct sys_semget_args /* { 71 syscallarg(key_t) key; 72 syscallarg(int) nsems; 73 syscallarg(int) semflg; 74 } */ semget_args; 75 struct sys_semop_args /* { 76 syscallarg(int) semid; 77 syscallarg(struct sembuf *) sops; 78 syscallarg(u_int) nsops; 79 } */ semop_args; 80 struct sys_semconfig_args /* { 81 syscallarg(int) flag; 82 } */ semconfig_args; 83 84 switch (SCARG(uap, which)) { 85 case 0: /* __semctl() */ 86 memset(&__semctl_args, 0, sizeof(__semctl_args)); 87 SCARG(&__semctl_args, semid) = SCARG(uap, a2); 88 SCARG(&__semctl_args, semnum) = SCARG(uap, a3); 89 SCARG(&__semctl_args, cmd) = SCARG(uap, a4); 90 SCARG(&__semctl_args, arg) = (union __semun *)SCARG(uap, a5); 91 return (compat_14_sys___semctl(l, &__semctl_args, retval)); 92 93 case 1: /* semget() */ 94 memset(&semget_args, 0, sizeof(semget_args)); 95 SCARG(&semget_args, key) = SCARG(uap, a2); 96 SCARG(&semget_args, nsems) = SCARG(uap, a3); 97 SCARG(&semget_args, semflg) = SCARG(uap, a4); 98 return (sys_semget(l, &semget_args, retval)); 99 100 case 2: /* semop() */ 101 memset(&semop_args, 0, sizeof(semop_args)); 102 SCARG(&semop_args, semid) = SCARG(uap, a2); 103 SCARG(&semop_args, sops) = (struct sembuf *)SCARG(uap, a3); 104 SCARG(&semop_args, nsops) = SCARG(uap, a4); 105 return (sys_semop(l, &semop_args, retval)); 106 107 case 3: /* semconfig() */ 108 memset(&semconfig_args, 0, sizeof(semconfig_args)); 109 SCARG(&semconfig_args, flag) = SCARG(uap, a2); 110 return (sys_semconfig(l, &semconfig_args, retval)); 111 112 default: 113 return (EINVAL); 114 } 115 } 116 #endif 117 118 #ifdef SYSVSHM 119 int 120 freebsd_sys_shmsys(struct lwp *l, const struct freebsd_sys_shmsys_args *uap, register_t *retval) 121 { 122 /* { 123 syscallarg(int) which; 124 syscallarg(int) a2; 125 syscallarg(int) a3; 126 syscallarg(int) a4; 127 } */ 128 struct sys_shmat_args /* { 129 syscallarg(int) shmid; 130 syscallarg(void *) shmaddr; 131 syscallarg(int) shmflg; 132 } */ shmat_args; 133 struct compat_14_sys_shmctl_args /* { 134 syscallarg(int) shmid; 135 syscallarg(int) cmd; 136 syscallarg(struct shmid_ds14 *) buf; 137 } */ shmctl_args; 138 struct sys_shmdt_args /* { 139 syscallarg(void *) shmaddr; 140 } */ shmdt_args; 141 struct sys_shmget_args /* { 142 syscallarg(key_t) key; 143 syscallarg(int) size; 144 syscallarg(int) shmflg; 145 } */ shmget_args; 146 147 switch (SCARG(uap, which)) { 148 case 0: /* shmat() */ 149 memset(&shmat_args, 0, sizeof(shmat_args)); 150 SCARG(&shmat_args, shmid) = SCARG(uap, a2); 151 SCARG(&shmat_args, shmaddr) = (void *)SCARG(uap, a3); 152 SCARG(&shmat_args, shmflg) = SCARG(uap, a4); 153 return (sys_shmat(l, &shmat_args, retval)); 154 155 case 1: /* oshmctl() */ 156 /* XXX Need to translate shmid_ds format. */ 157 return (EINVAL); 158 159 case 2: /* shmdt() */ 160 memset(&shmdt_args, 0, sizeof(shmdt_args)); 161 SCARG(&shmdt_args, shmaddr) = (void *)SCARG(uap, a2); 162 return (sys_shmdt(l, &shmdt_args, retval)); 163 164 case 3: /* shmget() */ 165 memset(&shmget_args, 0, sizeof(shmget_args)); 166 SCARG(&shmget_args, key) = SCARG(uap, a2); 167 SCARG(&shmget_args, size) = SCARG(uap, a3); 168 SCARG(&shmget_args, shmflg) = SCARG(uap, a4); 169 return (sys_shmget(l, &shmget_args, retval)); 170 171 case 4: /* shmctl() */ 172 memset(&shmctl_args, 0, sizeof(shmctl_args)); 173 SCARG(&shmctl_args, shmid) = SCARG(uap, a2); 174 SCARG(&shmctl_args, cmd) = SCARG(uap, a3); 175 SCARG(&shmctl_args, buf) = (struct shmid_ds14 *)SCARG(uap, a4); 176 return (compat_14_sys_shmctl(l, &shmctl_args, retval)); 177 178 default: 179 return (EINVAL); 180 } 181 } 182 #endif 183 184 #ifdef SYSVMSG 185 int 186 freebsd_sys_msgsys(struct lwp *l, const struct freebsd_sys_msgsys_args *uap, register_t *retval) 187 { 188 /* { 189 syscallarg(int) which; 190 syscallarg(int) a2; 191 syscallarg(int) a3; 192 syscallarg(int) a4; 193 syscallarg(int) a5; 194 syscallarg(int) a6; 195 } */ 196 struct compat_14_sys_msgctl_args /* { 197 syscallarg(int) msqid; 198 syscallarg(int) cmd; 199 syscallarg(struct msqid_ds14 *) buf; 200 } */ msgctl_args; 201 struct sys_msgget_args /* { 202 syscallarg(key_t) key; 203 syscallarg(int) msgflg; 204 } */ msgget_args; 205 struct sys_msgsnd_args /* { 206 syscallarg(int) msqid; 207 syscallarg(void *) msgp; 208 syscallarg(size_t) msgsz; 209 syscallarg(int) msgflg; 210 } */ msgsnd_args; 211 struct sys_msgrcv_args /* { 212 syscallarg(int) msqid; 213 syscallarg(void *) msgp; 214 syscallarg(size_t) msgsz; 215 syscallarg(long) msgtyp; 216 syscallarg(int) msgflg; 217 } */ msgrcv_args; 218 219 switch (SCARG(uap, which)) { 220 case 0: /* msgctl()*/ 221 memset(&msgctl_args, 0, sizeof(msgctl_args)); 222 SCARG(&msgctl_args, msqid) = SCARG(uap, a2); 223 SCARG(&msgctl_args, cmd) = SCARG(uap, a3); 224 SCARG(&msgctl_args, buf) = 225 (struct msqid_ds14 *)SCARG(uap, a4); 226 return (compat_14_sys_msgctl(l, &msgctl_args, retval)); 227 228 case 1: /* msgget() */ 229 memset(&msgget_args, 0, sizeof(msgget_args)); 230 SCARG(&msgget_args, key) = SCARG(uap, a2); 231 SCARG(&msgget_args, msgflg) = SCARG(uap, a3); 232 return (sys_msgget(l, &msgget_args, retval)); 233 234 case 2: /* msgsnd() */ 235 memset(&msgsnd_args, 0, sizeof(msgsnd_args)); 236 SCARG(&msgsnd_args, msqid) = SCARG(uap, a2); 237 SCARG(&msgsnd_args, msgp) = (void *)SCARG(uap, a3); 238 SCARG(&msgsnd_args, msgsz) = SCARG(uap, a4); 239 SCARG(&msgsnd_args, msgflg) = SCARG(uap, a5); 240 return (sys_msgsnd(l, &msgsnd_args, retval)); 241 242 case 3: /* msgrcv() */ 243 memset(&msgrcv_args, 0, sizeof(msgrcv_args)); 244 SCARG(&msgrcv_args, msqid) = SCARG(uap, a2); 245 SCARG(&msgrcv_args, msgp) = (void *)SCARG(uap, a3); 246 SCARG(&msgrcv_args, msgsz) = SCARG(uap, a4); 247 SCARG(&msgrcv_args, msgtyp) = SCARG(uap, a5); 248 SCARG(&msgrcv_args, msgflg) = SCARG(uap, a6); 249 return (sys_msgrcv(l, &msgrcv_args, retval)); 250 251 default: 252 return (EINVAL); 253 } 254 } 255 #endif 256