xref: /netbsd-src/usr.sbin/mopd/common/loop-linux2.c (revision 76c7fc5f6b13ed0b1508e6b313e88e59977ed78e)
1 /*	$NetBSD: loop-linux2.c,v 1.1 2016/06/08 01:11:49 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1993-95 Mats O Jansson.  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 Mats O Jansson.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "port.h"
33 #ifndef lint
34 __RCSID("$NetBSD: loop-linux2.c,v 1.1 2016/06/08 01:11:49 christos Exp $");
35 #endif
36 
37 #include <stdlib.h>
38 #include <strings.h>
39 #include <unistd.h>
40 #include <errno.h>
41 #if defined(__bsdi__) || defined(__FreeBSD__)
42 #include <sys/time.h>
43 #endif
44 #include <sys/ioctl.h>
45 
46 #include "os.h"
47 #include "common.h"
48 #include "mopdef.h"
49 
50 int
51 mopOpenRC(struct if_info *p, int trans)
52 {
53 #ifndef NORC
54 	return (*(p->iopen))(p->if_name,
55 			     O_RDWR,
56 			     MOP_K_PROTO_RC,
57 			     trans);
58 #else
59 	return -1;
60 #endif
61 }
62 
63 int
64 mopOpenDL(struct if_info *p, int trans)
65 {
66 #ifndef NODL
67 	return (*(p->iopen))(p->if_name,
68 			     O_RDWR,
69 			     MOP_K_PROTO_DL,
70 			     trans);
71 #else
72 	return -1;
73 #endif
74 }
75 
76 void
77 mopReadRC(void)
78 {
79 }
80 
81 void
82 mopReadDL(void)
83 {
84 }
85 
86 /*
87  * The list of all interfaces that are being listened to.  loop()
88  * "selects" on the descriptors in this list.
89  */
90 struct if_info *iflist;
91 
92 void   mopProcess(struct if_info *, u_char *);
93 
94 /*
95  * Loop indefinitely listening for MOP requests on the
96  * interfaces in 'iflist'.
97  */
98 void
99 Loop(void)
100 {
101 	u_char *buf, *bp, *ep;
102 	int     cc;
103 	fd_set  fds, listeners;
104 	int     bufsize = 1100, maxfd =0;
105 	struct if_info *ii;
106 
107 
108 	if (iflist == 0) {
109 		syslog(LOG_ERR, "no interfaces");
110 		exit(0);
111 	}
112 
113 	 buf = (u_char *) malloc((unsigned) bufsize);
114 
115 	if (buf == 0) {
116 		syslog(LOG_ERR, "malloc: %m");
117 		exit(0);
118 	}
119 	/*
120          * Find the highest numbered file descriptor for select().
121          * Initialize the set of descriptors to listen to.
122          */
123 	FD_ZERO(&fds);
124 	for (ii = iflist; ii; ii = ii->next) {
125 		if (ii->fd != -1) {
126 			FD_SET(ii->fd, &fds);
127 			if (ii->fd > maxfd)
128 				maxfd = ii->fd;
129 	        }
130 	}
131 	while (1) {
132 		listeners = fds;
133 		if (select(maxfd + 1, &listeners, (fd_set *) 0,
134 			(fd_set *) 0, (struct timeval *) 0) < 0) {
135 			syslog(LOG_ERR, "select: %m");
136 			exit(0);
137 		}
138 		for (ii = iflist; ii; ii = ii->next) {
139 			if (ii->fd != -1) {
140 				if (!FD_ISSET(ii->fd, &listeners))
141 					continue;
142 			}
143 	again:
144 			cc = read(ii->fd, (char *) buf, bufsize);
145 			/* Don't choke when we get ptraced */
146 			if (cc < 0 && errno == EINTR)
147 				goto again;
148 
149 			bp = buf;
150 			ep = bp + cc;
151 
152 			if(bp < ep) {
153 				mopProcess(ii,buf);
154 			}
155 
156 		}
157 	}
158 }
159