xref: /illumos-gate/usr/src/uts/common/ktli/t_kspoll.c (revision 9b9d39d2a32ff806d2431dbcc50968ef1e6d46b2)
1 /*
2  * CDDL HEADER START
3  *
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
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 /*
41  * This function waits for timo clock ticks for something to arrive on
42  * the specified stream. If more than one client is hanging off of a
43  * single endpoint, and at least one has specified a non-zero timeout,
44  * then all will be woken.
45  *
46  * Returns:
47  * 	0 on success or positive error code. On
48  * 	success, "events" is set to
49  *	 0	on timeout or no events(timout = 0),
50  *	 1	if desired event has occurred
51  *
52  * Most of the code is from strwaitq().
53  */
54 
55 #include <sys/param.h>
56 #include <sys/types.h>
57 #include <sys/proc.h>
58 #include <sys/file.h>
59 #include <sys/user.h>
60 #include <sys/vnode.h>
61 #include <sys/errno.h>
62 #include <sys/stream.h>
63 #include <sys/ioctl.h>
64 #include <sys/stropts.h>
65 #include <sys/strsubr.h>
66 #include <sys/tihdr.h>
67 #include <sys/timod.h>
68 #include <sys/tiuser.h>
69 #include <sys/t_kuser.h>
70 #include <sys/signal.h>
71 #include <sys/siginfo.h>
72 #include <sys/time.h>
73 #include <sys/debug.h>
74 
75 /*
76  * Poll for an input event.
77  *
78  * timo is measured in ticks
79  */
80 int
81 t_kspoll(TIUSER *tiptr, int timo, int waitflg, int *events)
82 {
83 	file_t		*fp;
84 	vnode_t		*vp;
85 	klwp_t		*lwp = ttolwp(curthread);
86 	clock_t		timout;	/* milliseconds */
87 	int		error;
88 	u_char 		pri;
89 	int 		pflag;
90 	rval_t		rval;
91 
92 	fp = tiptr->fp;
93 	vp = fp->f_vnode;
94 
95 	if (events == NULL || ((waitflg & READWAIT) == 0))
96 		return (EINVAL);
97 
98 	/* Convert from ticks to milliseconds */
99 	if (timo < 0)
100 		timout = -1;
101 	else
102 		timout = TICK_TO_MSEC(timo);
103 
104 	/*
105 	 * Indicate that the lwp is not to be stopped while doing
106 	 * this network traffic.  This is to avoid deadlock while
107 	 * debugging a process via /proc.
108 	 */
109 	if (lwp != NULL)
110 		lwp->lwp_nostop++;
111 
112 	if (waitflg & NOINTR)
113 		pflag = MSG_ANY | MSG_HOLDSIG;
114 	else
115 		pflag = MSG_ANY;
116 	pri = 0;
117 	error = kstrgetmsg(vp, NULL, NULL, &pri, &pflag, timout, &rval);
118 
119 	if (lwp != NULL)
120 		lwp->lwp_nostop--;
121 
122 	/* Set the return *events. */
123 	if (error != 0) {
124 		if (error == ETIME) {
125 			*events = 0;
126 			error = 0;
127 		}
128 		return (error);
129 	}
130 	*events = 1;
131 	return (0);
132 }
133