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