debuggers.hg

view tools/vnet/vnetd/select.c @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children
line source
1 /*
2 * Copyright (C) 2003 - 2004 Mike Wray <mike.wray@hp.com>.
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2.1 of the
7 * License, or (at your option) any later version. This library is
8 * distributed in the hope that it will be useful, but WITHOUT ANY
9 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE.
11 * See the GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
18 #include <stdlib.h>
19 #include <errno.h>
20 #include <unistd.h>
22 #include "select.h"
24 #define MODULE_NAME "select"
25 #define DEBUG
26 #undef DEBUG
27 #include "debug.h"
29 /** Zero all the file descriptor sets.
30 *
31 * @param set select set
32 * @param fd file descriptor
33 * @return 0 on success, -1 otherwise
34 */
35 void SelectSet_zero(SelectSet *set){
36 set->n = 0;
37 FD_ZERO(&set->rd);
38 FD_ZERO(&set->wr);
39 FD_ZERO(&set->er);
40 }
42 /** Add a file descriptor to the set.
43 *
44 * @param set select set
45 * @param fd file descriptor
46 * @param mode mask of sets to add to
47 * @return 0 on success, -1 otherwise
48 */
49 void SelectSet_add(SelectSet *set, int fd, int mode){
50 if(fd < 0) return;
51 if(mode & SELECT_READ){
52 SelectSet_add_read(set, fd);
53 }
54 if(mode & SELECT_WRITE){
55 SelectSet_add_write(set, fd);
56 }
57 if(mode & SELECT_ERROR){
58 SelectSet_add_error(set, fd);
59 }
60 }
62 /** Add a file descriptor to the write set.
63 *
64 * @param set select set
65 * @param fd file descriptor
66 * @return 0 on success, -1 otherwise
67 */
68 void SelectSet_add_read(SelectSet *set, int fd){
69 dprintf("> fd=%d\n", fd);
70 if(fd < 0) return;
71 FD_SET(fd, &set->rd);
72 if(fd > set->n) set->n = fd;
73 }
75 /** Add a file descriptor to the write set.
76 *
77 * @param set select set
78 * @param fd file descriptor
79 * @return 0 on success, -1 otherwise
80 */
81 void SelectSet_add_write(SelectSet *set, int fd){
82 dprintf("> fd=%d\n", fd);
83 if(fd < 0) return;
84 FD_SET(fd, &set->wr);
85 if(fd > set->n) set->n = fd;
86 }
88 /** Add a file descriptor to the error set.
89 *
90 * @param set select set
91 * @param fd file descriptor
92 * @return 0 on success, -1 otherwise
93 */
94 void SelectSet_add_error(SelectSet *set, int fd){
95 dprintf("> fd=%d\n", fd);
96 if(fd < 0) return;
97 FD_SET(fd, &set->er);
98 if(fd > set->n) set->n = fd;
99 }
101 /** Select on file descriptors.
102 *
103 * @param set select set
104 * @param timeout timeout (may be NULL for no timeout)
105 * @return 0 on success, -1 otherwise
106 */
107 int SelectSet_select(SelectSet *set, struct timeval *timeout){
108 return select(set->n+1, &set->rd, &set->wr, &set->er, timeout);
109 }