xref: /illumos-gate/usr/src/uts/common/ktli/t_kspoll.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*
43  * This function waits for timo clock ticks for something to arrive on
44  * the specified stream. If more than one client is hanging off of a
45  * single endpoint, and at least one has specified a non-zero timeout,
46  * then all will be woken.
47  *
48  * Returns:
49  * 	0 on success or positive error code. On
50  * 	success, "events" is set to
51  *	 0	on timeout or no events(timout = 0),
52  *	 1	if desired event has occurred
53  *
54  * Most of the code is from strwaitq().
55  */
56 
57 #include <sys/param.h>
58 #include <sys/types.h>
59 #include <sys/proc.h>
60 #include <sys/file.h>
61 #include <sys/user.h>
62 #include <sys/vnode.h>
63 #include <sys/errno.h>
64 #include <sys/stream.h>
65 #include <sys/ioctl.h>
66 #include <sys/stropts.h>
67 #include <sys/strsubr.h>
68 #include <sys/tihdr.h>
69 #include <sys/timod.h>
70 #include <sys/tiuser.h>
71 #include <sys/t_kuser.h>
72 #include <sys/signal.h>
73 #include <sys/siginfo.h>
74 #include <sys/time.h>
75 #include <sys/debug.h>
76 
77 /*
78  * Poll for an input event.
79  *
80  * timo is measured in ticks
81  */
82 int
83 t_kspoll(TIUSER *tiptr, int timo, int waitflg, int *events)
84 {
85 	file_t		*fp;
86 	vnode_t		*vp;
87 	klwp_t		*lwp = ttolwp(curthread);
88 	clock_t		timout;	/* milliseconds */
89 	int		error;
90 	u_char 		pri;
91 	int 		pflag;
92 	rval_t		rval;
93 
94 	fp = tiptr->fp;
95 	vp = fp->f_vnode;
96 
97 	if (events == NULL || ((waitflg & READWAIT) == 0))
98 		return (EINVAL);
99 
100 	/* Convert from ticks to milliseconds */
101 	if (timo < 0)
102 		timout = -1;
103 	else
104 		timout = TICK_TO_MSEC(timo);
105 
106 	/*
107 	 * Indicate that the lwp is not to be stopped while doing
108 	 * this network traffic.  This is to avoid deadlock while
109 	 * debugging a process via /proc.
110 	 */
111 	if (lwp != NULL)
112 		lwp->lwp_nostop++;
113 
114 	if (waitflg & NOINTR)
115 		pflag = MSG_ANY | MSG_HOLDSIG;
116 	else
117 		pflag = MSG_ANY;
118 	pri = 0;
119 	error = kstrgetmsg(vp, NULL, NULL, &pri, &pflag, timout, &rval);
120 
121 	if (lwp != NULL)
122 		lwp->lwp_nostop--;
123 
124 	/* Set the return *events. */
125 	if (error != 0) {
126 		if (error == ETIME) {
127 			*events = 0;
128 			error = 0;
129 		}
130 		return (error);
131 	}
132 	*events = 1;
133 	return (0);
134 }
135