4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include <sys/types.h>
34 #include <sys/sysmacros.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/errno.h>
38 #include <sys/vnode.h>
41 #include <sys/stropts.h>
42 #include <sys/stream.h>
43 #include <sys/strsubr.h>
44 #include <sys/fs/fifonode.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/debug.h>
50 * STREAMS system calls.
53 int getmsg(int fdes
, struct strbuf
*ctl
, struct strbuf
*data
, int *flagsp
);
54 int putmsg(int fdes
, struct strbuf
*ctl
, struct strbuf
*data
, int flags
);
55 int getpmsg(int fdes
, struct strbuf
*ctl
, struct strbuf
*data
, int *prip
,
57 int putpmsg(int fdes
, struct strbuf
*ctl
, struct strbuf
*data
, int pri
,
60 static int msgio(int fdes
, struct strbuf
*ctl
, struct strbuf
*data
, int *rval
,
61 int mode
, unsigned char *prip
, int *flagsp
);
64 getmsg(int fdes
, struct strbuf
*ctl
, struct strbuf
*data
, int *flagsp
)
69 unsigned char pri
= 0;
73 * Convert between old flags (localflags) and new flags (realflags).
75 if (copyin(flagsp
, &localflags
, sizeof (*flagsp
)))
76 return (set_errno(EFAULT
));
83 realflags
= MSG_HIPRI
;
87 return (set_errno(EINVAL
));
90 if ((error
= msgio(fdes
, ctl
, data
, &rv
, FREAD
, &pri
,
93 * massage realflags based on localflags.
95 if (realflags
== MSG_HIPRI
)
96 localflags
= RS_HIPRI
;
99 if (copyout(&localflags
, flagsp
, sizeof (*flagsp
)))
103 return (set_errno(error
));
108 putmsg(int fdes
, struct strbuf
*ctl
, struct strbuf
*data
, int flags
)
110 unsigned char pri
= 0;
117 realflags
= MSG_HIPRI
;
119 case (RS_HIPRI
|MSG_XPG4
):
120 realflags
= MSG_HIPRI
|MSG_XPG4
;
123 realflags
= MSG_BAND
|MSG_XPG4
;
126 realflags
= MSG_BAND
;
130 return (set_errno(EINVAL
));
132 error
= msgio(fdes
, ctl
, data
, &rv
, FWRITE
, &pri
, &realflags
);
134 return (set_errno(error
));
140 getpmsg(int fdes
, struct strbuf
*ctl
, struct strbuf
*data
, int *prip
,
149 if (copyin(flagsp
, &flags
, sizeof (flags
)))
150 return (set_errno(EFAULT
));
151 if (copyin(prip
, &intpri
, sizeof (intpri
)))
152 return (set_errno(EFAULT
));
153 if ((intpri
> 255) || (intpri
< 0))
154 return (set_errno(EINVAL
));
155 pri
= (unsigned char)intpri
;
156 error
= msgio(fdes
, ctl
, data
, &rv
, FREAD
, &pri
, &flags
);
158 return (set_errno(error
));
159 if (copyout(&flags
, flagsp
, sizeof (flags
)))
160 return (set_errno(EFAULT
));
162 if (copyout(&intpri
, prip
, sizeof (intpri
)))
163 return (set_errno(EFAULT
));
168 putpmsg(int fdes
, struct strbuf
*ctl
, struct strbuf
*data
, int intpri
,
175 if ((intpri
> 255) || (intpri
< 0))
176 return (set_errno(EINVAL
));
177 pri
= (unsigned char)intpri
;
178 error
= msgio(fdes
, ctl
, data
, &rv
, FWRITE
, &pri
, &flags
);
180 return (set_errno(error
));
185 * Common code for getmsg and putmsg calls: check permissions,
186 * copy in args, do preliminary setup, and switch to
187 * appropriate stream routine.
190 msgio(int fdes
, struct strbuf
*ctl
, struct strbuf
*data
, int *rval
,
191 int mode
, unsigned char *prip
, int *flagsp
)
195 struct strbuf msgctl
, msgdata
;
198 klwp_t
*lwp
= ttolwp(curthread
);
201 if ((fp
= getf(fdes
)) == NULL
)
203 if ((fp
->f_flag
& mode
) == 0) {
208 if (vp
->v_type
== VFIFO
) {
211 * must use sd_vnode, could be named pipe
213 (void) fifo_vfastoff(vp
->v_stream
->sd_vnode
);
218 } else if ((vp
->v_type
!= VCHR
&& vp
->v_type
!= VSOCK
) ||
219 vp
->v_stream
== NULL
) {
224 copyin(ctl
, &msgctl
, sizeof (struct strbuf
))) {
228 if ((data
!= NULL
) &&
229 copyin(data
, &msgdata
, sizeof (struct strbuf
))) {
241 if (vp
->v_type
== VSOCK
) {
242 error
= sock_getmsg(vp
, &msgctl
, &msgdata
, prip
,
245 error
= strgetmsg(vp
, &msgctl
, &msgdata
, prip
,
254 lwp
->lwp_ru
.msgrcv
++;
255 if (((ctl
!= NULL
) &&
256 copyout(&msgctl
, ctl
, sizeof (struct strbuf
))) ||
258 copyout(&msgdata
, data
, sizeof (struct strbuf
)))) {
274 if (vp
->v_type
== VSOCK
) {
275 error
= sock_putmsg(vp
, &msgctl
, &msgdata
, *prip
, *flagsp
,
278 error
= strputmsg(vp
, &msgctl
, &msgdata
, *prip
, *flagsp
, flag
);
281 if (error
== 0 && lwp
!= NULL
)
282 lwp
->lwp_ru
.msgsnd
++;
287 #if defined(_LP64) && defined(_SYSCALL32)
289 static int msgio32(int fdes
, struct strbuf32
*ctl
, struct strbuf32
*data
,
290 int *rval
, int mode
, unsigned char *prip
, int *flagsp
);
293 getmsg32(int fdes
, struct strbuf32
*ctl
, struct strbuf32
*data
, int32_t *flagsp
)
298 unsigned char pri
= 0;
302 * Convert between old flags (localflags) and new flags (realflags).
304 if (copyin(flagsp
, &localflags
, sizeof (*flagsp
)))
305 return (set_errno(EFAULT
));
306 switch (localflags
) {
312 realflags
= MSG_HIPRI
;
316 return (set_errno(EINVAL
));
319 if ((error
= msgio32(fdes
, ctl
, data
, &rv
, FREAD
, &pri
,
322 * massage realflags based on localflags.
324 if (realflags
== MSG_HIPRI
)
325 localflags
= RS_HIPRI
;
328 if (copyout(&localflags
, flagsp
, sizeof (*flagsp
)))
332 return (set_errno(error
));
337 putmsg32(int fdes
, struct strbuf32
*ctl
, struct strbuf32
*data
, int32_t flags
)
339 unsigned char pri
= 0;
346 realflags
= MSG_HIPRI
;
348 case (RS_HIPRI
|MSG_XPG4
):
349 realflags
= MSG_HIPRI
|MSG_XPG4
;
352 realflags
= MSG_BAND
|MSG_XPG4
;
355 realflags
= MSG_BAND
;
359 return (set_errno(EINVAL
));
361 error
= msgio32(fdes
, ctl
, data
, &rv
, FWRITE
, &pri
, &realflags
);
363 return (set_errno(error
));
369 getpmsg32(int fdes
, struct strbuf32
*ctl
, struct strbuf32
*data
, int32_t *prip
,
378 if (copyin(flagsp
, &flags
, sizeof (*flagsp
)))
379 return (set_errno(EFAULT
));
380 if (copyin(prip
, &intpri
, sizeof (intpri
)))
381 return (set_errno(EFAULT
));
382 if ((intpri
> 255) || (intpri
< 0))
383 return (set_errno(EINVAL
));
384 pri
= (unsigned char)intpri
;
385 error
= msgio32(fdes
, ctl
, data
, &rv
, FREAD
, &pri
, &flags
);
387 return (set_errno(error
));
388 if (copyout(&flags
, flagsp
, sizeof (flags
)))
389 return (set_errno(EFAULT
));
391 if (copyout(&intpri
, prip
, sizeof (intpri
)))
392 return (set_errno(EFAULT
));
397 putpmsg32(int fdes
, struct strbuf32
*ctl
, struct strbuf32
*data
, int32_t intpri
,
404 if ((intpri
> 255) || (intpri
< 0))
405 return (set_errno(EINVAL
));
406 pri
= (unsigned char)intpri
;
407 error
= msgio32(fdes
, ctl
, data
, &rv
, FWRITE
, &pri
, &flags
);
409 return (set_errno(error
));
414 * Common code for getmsg and putmsg calls: check permissions,
415 * copy in args, do preliminary setup, and switch to
416 * appropriate stream routine.
419 msgio32(int fdes
, struct strbuf32
*ctl
, struct strbuf32
*data
, int *rval
,
420 int mode
, unsigned char *prip
, int *flagsp
)
424 struct strbuf32 msgctl32
, msgdata32
;
425 struct strbuf msgctl
, msgdata
;
428 klwp_t
*lwp
= ttolwp(curthread
);
431 if ((fp
= getf(fdes
)) == NULL
)
433 if ((fp
->f_flag
& mode
) == 0) {
438 if (vp
->v_type
== VFIFO
) {
441 * must use sd_vnode, could be named pipe
443 (void) fifo_vfastoff(vp
->v_stream
->sd_vnode
);
448 } else if ((vp
->v_type
!= VCHR
&& vp
->v_type
!= VSOCK
) ||
449 vp
->v_stream
== NULL
) {
454 if (copyin(ctl
, &msgctl32
, sizeof (msgctl32
))) {
458 msgctl
.len
= msgctl32
.len
;
459 msgctl
.maxlen
= msgctl32
.maxlen
;
460 msgctl
.buf
= (caddr_t
)(uintptr_t)msgctl32
.buf
;
463 if (copyin(data
, &msgdata32
, sizeof (msgdata32
))) {
467 msgdata
.len
= msgdata32
.len
;
468 msgdata
.maxlen
= msgdata32
.maxlen
;
469 msgdata
.buf
= (caddr_t
)(uintptr_t)msgdata32
.buf
;
479 if (vp
->v_type
== VSOCK
) {
480 error
= sock_getmsg(vp
, &msgctl
, &msgdata
, prip
,
483 error
= strgetmsg(vp
, &msgctl
, &msgdata
, prip
,
492 lwp
->lwp_ru
.msgrcv
++;
494 /* XX64 - range check */
495 msgctl32
.len
= msgctl
.len
;
496 msgctl32
.maxlen
= msgctl
.maxlen
;
497 msgctl32
.buf
= (caddr32_t
)(uintptr_t)msgctl
.buf
;
498 if (copyout(&msgctl32
, ctl
, sizeof (msgctl32
))) {
504 /* XX64 - range check */
505 msgdata32
.len
= msgdata
.len
;
506 msgdata32
.maxlen
= msgdata
.maxlen
;
507 msgdata32
.buf
= (caddr32_t
)(uintptr_t)msgdata
.buf
;
508 if (copyout(&msgdata32
, data
, sizeof (msgdata32
))) {
525 if (vp
->v_type
== VSOCK
) {
526 error
= sock_putmsg(vp
, &msgctl
, &msgdata
, *prip
, *flagsp
,
529 error
= strputmsg(vp
, &msgctl
, &msgdata
, *prip
, *flagsp
, flag
);
532 if (error
== 0 && lwp
!= NULL
)
533 lwp
->lwp_ru
.msgsnd
++;
537 #endif /* _LP64 && _SYSCALL32 */