1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/systm.h> 37*7c478bd9Sstevel@tonic-gate #include <sys/errno.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/file.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/proc.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h> 42*7c478bd9Sstevel@tonic-gate #include <sys/stream.h> 43*7c478bd9Sstevel@tonic-gate #include <sys/strsubr.h> 44*7c478bd9Sstevel@tonic-gate #include <sys/fs/fifonode.h> 45*7c478bd9Sstevel@tonic-gate #include <sys/socket.h> 46*7c478bd9Sstevel@tonic-gate #include <sys/socketvar.h> 47*7c478bd9Sstevel@tonic-gate #include <sys/debug.h> 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate /* 50*7c478bd9Sstevel@tonic-gate * STREAMS system calls. 51*7c478bd9Sstevel@tonic-gate */ 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate int getmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int *flagsp); 54*7c478bd9Sstevel@tonic-gate int putmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int flags); 55*7c478bd9Sstevel@tonic-gate int getpmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int *prip, 56*7c478bd9Sstevel@tonic-gate int *flagsp); 57*7c478bd9Sstevel@tonic-gate int putpmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int pri, 58*7c478bd9Sstevel@tonic-gate int flags); 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate static int msgio(int fdes, struct strbuf *ctl, struct strbuf *data, int *rval, 61*7c478bd9Sstevel@tonic-gate int mode, unsigned char *prip, int *flagsp); 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate int 64*7c478bd9Sstevel@tonic-gate getmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int *flagsp) 65*7c478bd9Sstevel@tonic-gate { 66*7c478bd9Sstevel@tonic-gate int error; 67*7c478bd9Sstevel@tonic-gate int localflags; 68*7c478bd9Sstevel@tonic-gate int realflags = 0; 69*7c478bd9Sstevel@tonic-gate unsigned char pri = 0; 70*7c478bd9Sstevel@tonic-gate int rv = 0; 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate /* 73*7c478bd9Sstevel@tonic-gate * Convert between old flags (localflags) and new flags (realflags). 74*7c478bd9Sstevel@tonic-gate */ 75*7c478bd9Sstevel@tonic-gate if (copyin(flagsp, &localflags, sizeof (*flagsp))) 76*7c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 77*7c478bd9Sstevel@tonic-gate switch (localflags) { 78*7c478bd9Sstevel@tonic-gate case 0: 79*7c478bd9Sstevel@tonic-gate realflags = MSG_ANY; 80*7c478bd9Sstevel@tonic-gate break; 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate case RS_HIPRI: 83*7c478bd9Sstevel@tonic-gate realflags = MSG_HIPRI; 84*7c478bd9Sstevel@tonic-gate break; 85*7c478bd9Sstevel@tonic-gate 86*7c478bd9Sstevel@tonic-gate default: 87*7c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 88*7c478bd9Sstevel@tonic-gate } 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate if ((error = msgio(fdes, ctl, data, &rv, FREAD, &pri, 91*7c478bd9Sstevel@tonic-gate &realflags)) == 0) { 92*7c478bd9Sstevel@tonic-gate /* 93*7c478bd9Sstevel@tonic-gate * massage realflags based on localflags. 94*7c478bd9Sstevel@tonic-gate */ 95*7c478bd9Sstevel@tonic-gate if (realflags == MSG_HIPRI) 96*7c478bd9Sstevel@tonic-gate localflags = RS_HIPRI; 97*7c478bd9Sstevel@tonic-gate else 98*7c478bd9Sstevel@tonic-gate localflags = 0; 99*7c478bd9Sstevel@tonic-gate if (copyout(&localflags, flagsp, sizeof (*flagsp))) 100*7c478bd9Sstevel@tonic-gate error = EFAULT; 101*7c478bd9Sstevel@tonic-gate } 102*7c478bd9Sstevel@tonic-gate if (error != 0) 103*7c478bd9Sstevel@tonic-gate return (set_errno(error)); 104*7c478bd9Sstevel@tonic-gate return (rv); 105*7c478bd9Sstevel@tonic-gate } 106*7c478bd9Sstevel@tonic-gate 107*7c478bd9Sstevel@tonic-gate int 108*7c478bd9Sstevel@tonic-gate putmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int flags) 109*7c478bd9Sstevel@tonic-gate { 110*7c478bd9Sstevel@tonic-gate unsigned char pri = 0; 111*7c478bd9Sstevel@tonic-gate int realflags; 112*7c478bd9Sstevel@tonic-gate int error; 113*7c478bd9Sstevel@tonic-gate int rv = 0; 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate switch (flags) { 116*7c478bd9Sstevel@tonic-gate case RS_HIPRI: 117*7c478bd9Sstevel@tonic-gate realflags = MSG_HIPRI; 118*7c478bd9Sstevel@tonic-gate break; 119*7c478bd9Sstevel@tonic-gate case (RS_HIPRI|MSG_XPG4): 120*7c478bd9Sstevel@tonic-gate realflags = MSG_HIPRI|MSG_XPG4; 121*7c478bd9Sstevel@tonic-gate break; 122*7c478bd9Sstevel@tonic-gate case MSG_XPG4: 123*7c478bd9Sstevel@tonic-gate realflags = MSG_BAND|MSG_XPG4; 124*7c478bd9Sstevel@tonic-gate break; 125*7c478bd9Sstevel@tonic-gate case 0: 126*7c478bd9Sstevel@tonic-gate realflags = MSG_BAND; 127*7c478bd9Sstevel@tonic-gate break; 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate default: 130*7c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 131*7c478bd9Sstevel@tonic-gate } 132*7c478bd9Sstevel@tonic-gate error = msgio(fdes, ctl, data, &rv, FWRITE, &pri, &realflags); 133*7c478bd9Sstevel@tonic-gate if (error != 0) 134*7c478bd9Sstevel@tonic-gate return (set_errno(error)); 135*7c478bd9Sstevel@tonic-gate return (rv); 136*7c478bd9Sstevel@tonic-gate } 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate 139*7c478bd9Sstevel@tonic-gate int 140*7c478bd9Sstevel@tonic-gate getpmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int *prip, 141*7c478bd9Sstevel@tonic-gate int *flagsp) 142*7c478bd9Sstevel@tonic-gate { 143*7c478bd9Sstevel@tonic-gate int error; 144*7c478bd9Sstevel@tonic-gate int flags; 145*7c478bd9Sstevel@tonic-gate int intpri; 146*7c478bd9Sstevel@tonic-gate unsigned char pri; 147*7c478bd9Sstevel@tonic-gate int rv = 0; 148*7c478bd9Sstevel@tonic-gate 149*7c478bd9Sstevel@tonic-gate if (copyin(flagsp, &flags, sizeof (flags))) 150*7c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 151*7c478bd9Sstevel@tonic-gate if (copyin(prip, &intpri, sizeof (intpri))) 152*7c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 153*7c478bd9Sstevel@tonic-gate if ((intpri > 255) || (intpri < 0)) 154*7c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 155*7c478bd9Sstevel@tonic-gate pri = (unsigned char)intpri; 156*7c478bd9Sstevel@tonic-gate error = msgio(fdes, ctl, data, &rv, FREAD, &pri, &flags); 157*7c478bd9Sstevel@tonic-gate if (error != 0) 158*7c478bd9Sstevel@tonic-gate return (set_errno(error)); 159*7c478bd9Sstevel@tonic-gate if (copyout(&flags, flagsp, sizeof (flags))) 160*7c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 161*7c478bd9Sstevel@tonic-gate intpri = (int)pri; 162*7c478bd9Sstevel@tonic-gate if (copyout(&intpri, prip, sizeof (intpri))) 163*7c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 164*7c478bd9Sstevel@tonic-gate return (rv); 165*7c478bd9Sstevel@tonic-gate } 166*7c478bd9Sstevel@tonic-gate 167*7c478bd9Sstevel@tonic-gate int 168*7c478bd9Sstevel@tonic-gate putpmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int intpri, 169*7c478bd9Sstevel@tonic-gate int flags) 170*7c478bd9Sstevel@tonic-gate { 171*7c478bd9Sstevel@tonic-gate unsigned char pri; 172*7c478bd9Sstevel@tonic-gate int rv = 0; 173*7c478bd9Sstevel@tonic-gate int error; 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate if ((intpri > 255) || (intpri < 0)) 176*7c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 177*7c478bd9Sstevel@tonic-gate pri = (unsigned char)intpri; 178*7c478bd9Sstevel@tonic-gate error = msgio(fdes, ctl, data, &rv, FWRITE, &pri, &flags); 179*7c478bd9Sstevel@tonic-gate if (error != 0) 180*7c478bd9Sstevel@tonic-gate return (set_errno(error)); 181*7c478bd9Sstevel@tonic-gate return (rv); 182*7c478bd9Sstevel@tonic-gate } 183*7c478bd9Sstevel@tonic-gate 184*7c478bd9Sstevel@tonic-gate /* 185*7c478bd9Sstevel@tonic-gate * Common code for getmsg and putmsg calls: check permissions, 186*7c478bd9Sstevel@tonic-gate * copy in args, do preliminary setup, and switch to 187*7c478bd9Sstevel@tonic-gate * appropriate stream routine. 188*7c478bd9Sstevel@tonic-gate */ 189*7c478bd9Sstevel@tonic-gate static int 190*7c478bd9Sstevel@tonic-gate msgio(int fdes, struct strbuf *ctl, struct strbuf *data, int *rval, 191*7c478bd9Sstevel@tonic-gate int mode, unsigned char *prip, int *flagsp) 192*7c478bd9Sstevel@tonic-gate { 193*7c478bd9Sstevel@tonic-gate file_t *fp; 194*7c478bd9Sstevel@tonic-gate vnode_t *vp; 195*7c478bd9Sstevel@tonic-gate struct strbuf msgctl, msgdata; 196*7c478bd9Sstevel@tonic-gate int error; 197*7c478bd9Sstevel@tonic-gate int flag; 198*7c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 199*7c478bd9Sstevel@tonic-gate rval_t rv; 200*7c478bd9Sstevel@tonic-gate 201*7c478bd9Sstevel@tonic-gate if ((fp = getf(fdes)) == NULL) 202*7c478bd9Sstevel@tonic-gate return (EBADF); 203*7c478bd9Sstevel@tonic-gate if ((fp->f_flag & mode) == 0) { 204*7c478bd9Sstevel@tonic-gate releasef(fdes); 205*7c478bd9Sstevel@tonic-gate return (EBADF); 206*7c478bd9Sstevel@tonic-gate } 207*7c478bd9Sstevel@tonic-gate vp = fp->f_vnode; 208*7c478bd9Sstevel@tonic-gate if (vp->v_type == VFIFO) { 209*7c478bd9Sstevel@tonic-gate if (vp->v_stream) { 210*7c478bd9Sstevel@tonic-gate /* 211*7c478bd9Sstevel@tonic-gate * must use sd_vnode, could be named pipe 212*7c478bd9Sstevel@tonic-gate */ 213*7c478bd9Sstevel@tonic-gate (void) fifo_vfastoff(vp->v_stream->sd_vnode); 214*7c478bd9Sstevel@tonic-gate } else { 215*7c478bd9Sstevel@tonic-gate releasef(fdes); 216*7c478bd9Sstevel@tonic-gate return (ENOSTR); 217*7c478bd9Sstevel@tonic-gate } 218*7c478bd9Sstevel@tonic-gate } else if ((vp->v_type != VCHR && vp->v_type != VSOCK) || 219*7c478bd9Sstevel@tonic-gate vp->v_stream == NULL) { 220*7c478bd9Sstevel@tonic-gate releasef(fdes); 221*7c478bd9Sstevel@tonic-gate return (ENOSTR); 222*7c478bd9Sstevel@tonic-gate } 223*7c478bd9Sstevel@tonic-gate if ((ctl != NULL) && 224*7c478bd9Sstevel@tonic-gate copyin(ctl, &msgctl, sizeof (struct strbuf))) { 225*7c478bd9Sstevel@tonic-gate releasef(fdes); 226*7c478bd9Sstevel@tonic-gate return (EFAULT); 227*7c478bd9Sstevel@tonic-gate } 228*7c478bd9Sstevel@tonic-gate if ((data != NULL) && 229*7c478bd9Sstevel@tonic-gate copyin(data, &msgdata, sizeof (struct strbuf))) { 230*7c478bd9Sstevel@tonic-gate releasef(fdes); 231*7c478bd9Sstevel@tonic-gate return (EFAULT); 232*7c478bd9Sstevel@tonic-gate } 233*7c478bd9Sstevel@tonic-gate 234*7c478bd9Sstevel@tonic-gate if (mode == FREAD) { 235*7c478bd9Sstevel@tonic-gate if (ctl == NULL) 236*7c478bd9Sstevel@tonic-gate msgctl.maxlen = -1; 237*7c478bd9Sstevel@tonic-gate if (data == NULL) 238*7c478bd9Sstevel@tonic-gate msgdata.maxlen = -1; 239*7c478bd9Sstevel@tonic-gate flag = fp->f_flag; 240*7c478bd9Sstevel@tonic-gate rv.r_val1 = 0; 241*7c478bd9Sstevel@tonic-gate if (vp->v_type == VSOCK) { 242*7c478bd9Sstevel@tonic-gate error = sock_getmsg(vp, &msgctl, &msgdata, prip, 243*7c478bd9Sstevel@tonic-gate flagsp, flag, &rv); 244*7c478bd9Sstevel@tonic-gate } else { 245*7c478bd9Sstevel@tonic-gate error = strgetmsg(vp, &msgctl, &msgdata, prip, 246*7c478bd9Sstevel@tonic-gate flagsp, flag, &rv); 247*7c478bd9Sstevel@tonic-gate } 248*7c478bd9Sstevel@tonic-gate *rval = rv.r_val1; 249*7c478bd9Sstevel@tonic-gate if (error != 0) { 250*7c478bd9Sstevel@tonic-gate releasef(fdes); 251*7c478bd9Sstevel@tonic-gate return (error); 252*7c478bd9Sstevel@tonic-gate } 253*7c478bd9Sstevel@tonic-gate if (lwp != NULL) 254*7c478bd9Sstevel@tonic-gate lwp->lwp_ru.msgrcv++; 255*7c478bd9Sstevel@tonic-gate if (((ctl != NULL) && 256*7c478bd9Sstevel@tonic-gate copyout(&msgctl, ctl, sizeof (struct strbuf))) || 257*7c478bd9Sstevel@tonic-gate ((data != NULL) && 258*7c478bd9Sstevel@tonic-gate copyout(&msgdata, data, sizeof (struct strbuf)))) { 259*7c478bd9Sstevel@tonic-gate releasef(fdes); 260*7c478bd9Sstevel@tonic-gate return (EFAULT); 261*7c478bd9Sstevel@tonic-gate } 262*7c478bd9Sstevel@tonic-gate releasef(fdes); 263*7c478bd9Sstevel@tonic-gate return (0); 264*7c478bd9Sstevel@tonic-gate } 265*7c478bd9Sstevel@tonic-gate 266*7c478bd9Sstevel@tonic-gate /* 267*7c478bd9Sstevel@tonic-gate * FWRITE case 268*7c478bd9Sstevel@tonic-gate */ 269*7c478bd9Sstevel@tonic-gate if (ctl == NULL) 270*7c478bd9Sstevel@tonic-gate msgctl.len = -1; 271*7c478bd9Sstevel@tonic-gate if (data == NULL) 272*7c478bd9Sstevel@tonic-gate msgdata.len = -1; 273*7c478bd9Sstevel@tonic-gate flag = fp->f_flag; 274*7c478bd9Sstevel@tonic-gate if (vp->v_type == VSOCK) { 275*7c478bd9Sstevel@tonic-gate error = sock_putmsg(vp, &msgctl, &msgdata, *prip, *flagsp, 276*7c478bd9Sstevel@tonic-gate flag); 277*7c478bd9Sstevel@tonic-gate } else { 278*7c478bd9Sstevel@tonic-gate error = strputmsg(vp, &msgctl, &msgdata, *prip, *flagsp, flag); 279*7c478bd9Sstevel@tonic-gate } 280*7c478bd9Sstevel@tonic-gate releasef(fdes); 281*7c478bd9Sstevel@tonic-gate if (error == 0 && lwp != NULL) 282*7c478bd9Sstevel@tonic-gate lwp->lwp_ru.msgsnd++; 283*7c478bd9Sstevel@tonic-gate return (error); 284*7c478bd9Sstevel@tonic-gate } 285*7c478bd9Sstevel@tonic-gate 286*7c478bd9Sstevel@tonic-gate 287*7c478bd9Sstevel@tonic-gate #if defined(_LP64) && defined(_SYSCALL32) 288*7c478bd9Sstevel@tonic-gate 289*7c478bd9Sstevel@tonic-gate static int msgio32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, 290*7c478bd9Sstevel@tonic-gate int *rval, int mode, unsigned char *prip, int *flagsp); 291*7c478bd9Sstevel@tonic-gate 292*7c478bd9Sstevel@tonic-gate int 293*7c478bd9Sstevel@tonic-gate getmsg32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int32_t *flagsp) 294*7c478bd9Sstevel@tonic-gate { 295*7c478bd9Sstevel@tonic-gate int error; 296*7c478bd9Sstevel@tonic-gate int32_t localflags; 297*7c478bd9Sstevel@tonic-gate int realflags = 0; 298*7c478bd9Sstevel@tonic-gate unsigned char pri = 0; 299*7c478bd9Sstevel@tonic-gate int rv = 0; 300*7c478bd9Sstevel@tonic-gate 301*7c478bd9Sstevel@tonic-gate /* 302*7c478bd9Sstevel@tonic-gate * Convert between old flags (localflags) and new flags (realflags). 303*7c478bd9Sstevel@tonic-gate */ 304*7c478bd9Sstevel@tonic-gate if (copyin(flagsp, &localflags, sizeof (*flagsp))) 305*7c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 306*7c478bd9Sstevel@tonic-gate switch (localflags) { 307*7c478bd9Sstevel@tonic-gate case 0: 308*7c478bd9Sstevel@tonic-gate realflags = MSG_ANY; 309*7c478bd9Sstevel@tonic-gate break; 310*7c478bd9Sstevel@tonic-gate 311*7c478bd9Sstevel@tonic-gate case RS_HIPRI: 312*7c478bd9Sstevel@tonic-gate realflags = MSG_HIPRI; 313*7c478bd9Sstevel@tonic-gate break; 314*7c478bd9Sstevel@tonic-gate 315*7c478bd9Sstevel@tonic-gate default: 316*7c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 317*7c478bd9Sstevel@tonic-gate } 318*7c478bd9Sstevel@tonic-gate 319*7c478bd9Sstevel@tonic-gate if ((error = msgio32(fdes, ctl, data, &rv, FREAD, &pri, 320*7c478bd9Sstevel@tonic-gate &realflags)) == 0) { 321*7c478bd9Sstevel@tonic-gate /* 322*7c478bd9Sstevel@tonic-gate * massage realflags based on localflags. 323*7c478bd9Sstevel@tonic-gate */ 324*7c478bd9Sstevel@tonic-gate if (realflags == MSG_HIPRI) 325*7c478bd9Sstevel@tonic-gate localflags = RS_HIPRI; 326*7c478bd9Sstevel@tonic-gate else 327*7c478bd9Sstevel@tonic-gate localflags = 0; 328*7c478bd9Sstevel@tonic-gate if (copyout(&localflags, flagsp, sizeof (*flagsp))) 329*7c478bd9Sstevel@tonic-gate error = EFAULT; 330*7c478bd9Sstevel@tonic-gate } 331*7c478bd9Sstevel@tonic-gate if (error != 0) 332*7c478bd9Sstevel@tonic-gate return (set_errno(error)); 333*7c478bd9Sstevel@tonic-gate return (rv); 334*7c478bd9Sstevel@tonic-gate } 335*7c478bd9Sstevel@tonic-gate 336*7c478bd9Sstevel@tonic-gate int 337*7c478bd9Sstevel@tonic-gate putmsg32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int32_t flags) 338*7c478bd9Sstevel@tonic-gate { 339*7c478bd9Sstevel@tonic-gate unsigned char pri = 0; 340*7c478bd9Sstevel@tonic-gate int realflags; 341*7c478bd9Sstevel@tonic-gate int error; 342*7c478bd9Sstevel@tonic-gate int rv = 0; 343*7c478bd9Sstevel@tonic-gate 344*7c478bd9Sstevel@tonic-gate switch (flags) { 345*7c478bd9Sstevel@tonic-gate case RS_HIPRI: 346*7c478bd9Sstevel@tonic-gate realflags = MSG_HIPRI; 347*7c478bd9Sstevel@tonic-gate break; 348*7c478bd9Sstevel@tonic-gate case (RS_HIPRI|MSG_XPG4): 349*7c478bd9Sstevel@tonic-gate realflags = MSG_HIPRI|MSG_XPG4; 350*7c478bd9Sstevel@tonic-gate break; 351*7c478bd9Sstevel@tonic-gate case MSG_XPG4: 352*7c478bd9Sstevel@tonic-gate realflags = MSG_BAND|MSG_XPG4; 353*7c478bd9Sstevel@tonic-gate break; 354*7c478bd9Sstevel@tonic-gate case 0: 355*7c478bd9Sstevel@tonic-gate realflags = MSG_BAND; 356*7c478bd9Sstevel@tonic-gate break; 357*7c478bd9Sstevel@tonic-gate 358*7c478bd9Sstevel@tonic-gate default: 359*7c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 360*7c478bd9Sstevel@tonic-gate } 361*7c478bd9Sstevel@tonic-gate error = msgio32(fdes, ctl, data, &rv, FWRITE, &pri, &realflags); 362*7c478bd9Sstevel@tonic-gate if (error != 0) 363*7c478bd9Sstevel@tonic-gate return (set_errno(error)); 364*7c478bd9Sstevel@tonic-gate return (rv); 365*7c478bd9Sstevel@tonic-gate } 366*7c478bd9Sstevel@tonic-gate 367*7c478bd9Sstevel@tonic-gate 368*7c478bd9Sstevel@tonic-gate int 369*7c478bd9Sstevel@tonic-gate getpmsg32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int32_t *prip, 370*7c478bd9Sstevel@tonic-gate int32_t *flagsp) 371*7c478bd9Sstevel@tonic-gate { 372*7c478bd9Sstevel@tonic-gate int error; 373*7c478bd9Sstevel@tonic-gate int32_t flags; 374*7c478bd9Sstevel@tonic-gate int32_t intpri; 375*7c478bd9Sstevel@tonic-gate unsigned char pri; 376*7c478bd9Sstevel@tonic-gate int rv = 0; 377*7c478bd9Sstevel@tonic-gate 378*7c478bd9Sstevel@tonic-gate if (copyin(flagsp, &flags, sizeof (*flagsp))) 379*7c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 380*7c478bd9Sstevel@tonic-gate if (copyin(prip, &intpri, sizeof (intpri))) 381*7c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 382*7c478bd9Sstevel@tonic-gate if ((intpri > 255) || (intpri < 0)) 383*7c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 384*7c478bd9Sstevel@tonic-gate pri = (unsigned char)intpri; 385*7c478bd9Sstevel@tonic-gate error = msgio32(fdes, ctl, data, &rv, FREAD, &pri, &flags); 386*7c478bd9Sstevel@tonic-gate if (error != 0) 387*7c478bd9Sstevel@tonic-gate return (set_errno(error)); 388*7c478bd9Sstevel@tonic-gate if (copyout(&flags, flagsp, sizeof (flags))) 389*7c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 390*7c478bd9Sstevel@tonic-gate intpri = (int)pri; 391*7c478bd9Sstevel@tonic-gate if (copyout(&intpri, prip, sizeof (intpri))) 392*7c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 393*7c478bd9Sstevel@tonic-gate return (rv); 394*7c478bd9Sstevel@tonic-gate } 395*7c478bd9Sstevel@tonic-gate 396*7c478bd9Sstevel@tonic-gate int 397*7c478bd9Sstevel@tonic-gate putpmsg32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int32_t intpri, 398*7c478bd9Sstevel@tonic-gate int32_t flags) 399*7c478bd9Sstevel@tonic-gate { 400*7c478bd9Sstevel@tonic-gate unsigned char pri; 401*7c478bd9Sstevel@tonic-gate int rv = 0; 402*7c478bd9Sstevel@tonic-gate int error; 403*7c478bd9Sstevel@tonic-gate 404*7c478bd9Sstevel@tonic-gate if ((intpri > 255) || (intpri < 0)) 405*7c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 406*7c478bd9Sstevel@tonic-gate pri = (unsigned char)intpri; 407*7c478bd9Sstevel@tonic-gate error = msgio32(fdes, ctl, data, &rv, FWRITE, &pri, &flags); 408*7c478bd9Sstevel@tonic-gate if (error != 0) 409*7c478bd9Sstevel@tonic-gate return (set_errno(error)); 410*7c478bd9Sstevel@tonic-gate return (rv); 411*7c478bd9Sstevel@tonic-gate } 412*7c478bd9Sstevel@tonic-gate 413*7c478bd9Sstevel@tonic-gate /* 414*7c478bd9Sstevel@tonic-gate * Common code for getmsg and putmsg calls: check permissions, 415*7c478bd9Sstevel@tonic-gate * copy in args, do preliminary setup, and switch to 416*7c478bd9Sstevel@tonic-gate * appropriate stream routine. 417*7c478bd9Sstevel@tonic-gate */ 418*7c478bd9Sstevel@tonic-gate static int 419*7c478bd9Sstevel@tonic-gate msgio32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int *rval, 420*7c478bd9Sstevel@tonic-gate int mode, unsigned char *prip, int *flagsp) 421*7c478bd9Sstevel@tonic-gate { 422*7c478bd9Sstevel@tonic-gate file_t *fp; 423*7c478bd9Sstevel@tonic-gate vnode_t *vp; 424*7c478bd9Sstevel@tonic-gate struct strbuf32 msgctl32, msgdata32; 425*7c478bd9Sstevel@tonic-gate struct strbuf msgctl, msgdata; 426*7c478bd9Sstevel@tonic-gate int error; 427*7c478bd9Sstevel@tonic-gate int flag; 428*7c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 429*7c478bd9Sstevel@tonic-gate rval_t rv; 430*7c478bd9Sstevel@tonic-gate 431*7c478bd9Sstevel@tonic-gate if ((fp = getf(fdes)) == NULL) 432*7c478bd9Sstevel@tonic-gate return (EBADF); 433*7c478bd9Sstevel@tonic-gate if ((fp->f_flag & mode) == 0) { 434*7c478bd9Sstevel@tonic-gate releasef(fdes); 435*7c478bd9Sstevel@tonic-gate return (EBADF); 436*7c478bd9Sstevel@tonic-gate } 437*7c478bd9Sstevel@tonic-gate vp = fp->f_vnode; 438*7c478bd9Sstevel@tonic-gate if (vp->v_type == VFIFO) { 439*7c478bd9Sstevel@tonic-gate if (vp->v_stream) { 440*7c478bd9Sstevel@tonic-gate /* 441*7c478bd9Sstevel@tonic-gate * must use sd_vnode, could be named pipe 442*7c478bd9Sstevel@tonic-gate */ 443*7c478bd9Sstevel@tonic-gate (void) fifo_vfastoff(vp->v_stream->sd_vnode); 444*7c478bd9Sstevel@tonic-gate } else { 445*7c478bd9Sstevel@tonic-gate releasef(fdes); 446*7c478bd9Sstevel@tonic-gate return (ENOSTR); 447*7c478bd9Sstevel@tonic-gate } 448*7c478bd9Sstevel@tonic-gate } else if ((vp->v_type != VCHR && vp->v_type != VSOCK) || 449*7c478bd9Sstevel@tonic-gate vp->v_stream == NULL) { 450*7c478bd9Sstevel@tonic-gate releasef(fdes); 451*7c478bd9Sstevel@tonic-gate return (ENOSTR); 452*7c478bd9Sstevel@tonic-gate } 453*7c478bd9Sstevel@tonic-gate if (ctl != NULL) { 454*7c478bd9Sstevel@tonic-gate if (copyin(ctl, &msgctl32, sizeof (msgctl32))) { 455*7c478bd9Sstevel@tonic-gate releasef(fdes); 456*7c478bd9Sstevel@tonic-gate return (EFAULT); 457*7c478bd9Sstevel@tonic-gate } 458*7c478bd9Sstevel@tonic-gate msgctl.len = msgctl32.len; 459*7c478bd9Sstevel@tonic-gate msgctl.maxlen = msgctl32.maxlen; 460*7c478bd9Sstevel@tonic-gate msgctl.buf = (caddr_t)(uintptr_t)msgctl32.buf; 461*7c478bd9Sstevel@tonic-gate } 462*7c478bd9Sstevel@tonic-gate if (data != NULL) { 463*7c478bd9Sstevel@tonic-gate if (copyin(data, &msgdata32, sizeof (msgdata32))) { 464*7c478bd9Sstevel@tonic-gate releasef(fdes); 465*7c478bd9Sstevel@tonic-gate return (EFAULT); 466*7c478bd9Sstevel@tonic-gate } 467*7c478bd9Sstevel@tonic-gate msgdata.len = msgdata32.len; 468*7c478bd9Sstevel@tonic-gate msgdata.maxlen = msgdata32.maxlen; 469*7c478bd9Sstevel@tonic-gate msgdata.buf = (caddr_t)(uintptr_t)msgdata32.buf; 470*7c478bd9Sstevel@tonic-gate } 471*7c478bd9Sstevel@tonic-gate 472*7c478bd9Sstevel@tonic-gate if (mode == FREAD) { 473*7c478bd9Sstevel@tonic-gate if (ctl == NULL) 474*7c478bd9Sstevel@tonic-gate msgctl.maxlen = -1; 475*7c478bd9Sstevel@tonic-gate if (data == NULL) 476*7c478bd9Sstevel@tonic-gate msgdata.maxlen = -1; 477*7c478bd9Sstevel@tonic-gate flag = fp->f_flag; 478*7c478bd9Sstevel@tonic-gate rv.r_val1 = 0; 479*7c478bd9Sstevel@tonic-gate if (vp->v_type == VSOCK) { 480*7c478bd9Sstevel@tonic-gate error = sock_getmsg(vp, &msgctl, &msgdata, prip, 481*7c478bd9Sstevel@tonic-gate flagsp, flag, &rv); 482*7c478bd9Sstevel@tonic-gate } else { 483*7c478bd9Sstevel@tonic-gate error = strgetmsg(vp, &msgctl, &msgdata, prip, 484*7c478bd9Sstevel@tonic-gate flagsp, flag, &rv); 485*7c478bd9Sstevel@tonic-gate } 486*7c478bd9Sstevel@tonic-gate *rval = rv.r_val1; 487*7c478bd9Sstevel@tonic-gate if (error != 0) { 488*7c478bd9Sstevel@tonic-gate releasef(fdes); 489*7c478bd9Sstevel@tonic-gate return (error); 490*7c478bd9Sstevel@tonic-gate } 491*7c478bd9Sstevel@tonic-gate if (lwp != NULL) 492*7c478bd9Sstevel@tonic-gate lwp->lwp_ru.msgrcv++; 493*7c478bd9Sstevel@tonic-gate if (ctl != NULL) { 494*7c478bd9Sstevel@tonic-gate /* XX64 - range check */ 495*7c478bd9Sstevel@tonic-gate msgctl32.len = msgctl.len; 496*7c478bd9Sstevel@tonic-gate msgctl32.maxlen = msgctl.maxlen; 497*7c478bd9Sstevel@tonic-gate msgctl32.buf = (caddr32_t)(uintptr_t)msgctl.buf; 498*7c478bd9Sstevel@tonic-gate if (copyout(&msgctl32, ctl, sizeof (msgctl32))) { 499*7c478bd9Sstevel@tonic-gate releasef(fdes); 500*7c478bd9Sstevel@tonic-gate return (EFAULT); 501*7c478bd9Sstevel@tonic-gate } 502*7c478bd9Sstevel@tonic-gate } 503*7c478bd9Sstevel@tonic-gate if (data != NULL) { 504*7c478bd9Sstevel@tonic-gate /* XX64 - range check */ 505*7c478bd9Sstevel@tonic-gate msgdata32.len = msgdata.len; 506*7c478bd9Sstevel@tonic-gate msgdata32.maxlen = msgdata.maxlen; 507*7c478bd9Sstevel@tonic-gate msgdata32.buf = (caddr32_t)(uintptr_t)msgdata.buf; 508*7c478bd9Sstevel@tonic-gate if (copyout(&msgdata32, data, sizeof (msgdata32))) { 509*7c478bd9Sstevel@tonic-gate releasef(fdes); 510*7c478bd9Sstevel@tonic-gate return (EFAULT); 511*7c478bd9Sstevel@tonic-gate } 512*7c478bd9Sstevel@tonic-gate } 513*7c478bd9Sstevel@tonic-gate releasef(fdes); 514*7c478bd9Sstevel@tonic-gate return (0); 515*7c478bd9Sstevel@tonic-gate } 516*7c478bd9Sstevel@tonic-gate 517*7c478bd9Sstevel@tonic-gate /* 518*7c478bd9Sstevel@tonic-gate * FWRITE case 519*7c478bd9Sstevel@tonic-gate */ 520*7c478bd9Sstevel@tonic-gate if (ctl == NULL) 521*7c478bd9Sstevel@tonic-gate msgctl.len = -1; 522*7c478bd9Sstevel@tonic-gate if (data == NULL) 523*7c478bd9Sstevel@tonic-gate msgdata.len = -1; 524*7c478bd9Sstevel@tonic-gate flag = fp->f_flag; 525*7c478bd9Sstevel@tonic-gate if (vp->v_type == VSOCK) { 526*7c478bd9Sstevel@tonic-gate error = sock_putmsg(vp, &msgctl, &msgdata, *prip, *flagsp, 527*7c478bd9Sstevel@tonic-gate flag); 528*7c478bd9Sstevel@tonic-gate } else { 529*7c478bd9Sstevel@tonic-gate error = strputmsg(vp, &msgctl, &msgdata, *prip, *flagsp, flag); 530*7c478bd9Sstevel@tonic-gate } 531*7c478bd9Sstevel@tonic-gate releasef(fdes); 532*7c478bd9Sstevel@tonic-gate if (error == 0 && lwp != NULL) 533*7c478bd9Sstevel@tonic-gate lwp->lwp_ru.msgsnd++; 534*7c478bd9Sstevel@tonic-gate return (error); 535*7c478bd9Sstevel@tonic-gate } 536*7c478bd9Sstevel@tonic-gate 537*7c478bd9Sstevel@tonic-gate #endif /* _LP64 && _SYSCALL32 */ 538