Stackdb
Stackdb is a stackable, multi-target and -level source debugger and memory forensics library.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
evloop.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, 2013, 2014 The University of Utah
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #ifndef __EVLOOP_H__
20 #define __EVLOOP_H__
21 
22 #include <sys/select.h>
23 #include <sys/time.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 
27 #include <glib.h>
28 
29 #define EVLOOP_FDTYPE_A 0
30 #define EVLOOP_FDTYPE_R 1
31 #define EVLOOP_FDTYPE_W 2
32 #define EVLOOP_FDTYPE_X 3
33 
34 #define EVLOOP_HRET_BADERROR -2
35 #define EVLOOP_HRET_ERROR -1
36 #define EVLOOP_HRET_SUCCESS 0
37 #define EVLOOP_HRET_REMOVETYPE 1
38 #define EVLOOP_HRET_REMOVEALLTYPES 2
39 #define EVLOOP_HRET_DONE_SUCCESS 3
40 #define EVLOOP_HRET_DONE_FAILURE 4
41 
42 struct evloop;
43 struct evloop_fdinfo;
44 
45 typedef enum {
46  EVLOOP_RETONINT = 1 << 0,
48 
49 /*
50  * Event loop handlers return 0 on success; 1 if they want the caller to
51  * remove the fd/fdtype tuple from the set; 2 if they want the caller to
52  * remove all fd types for this fd from the set (i.e.,
53  * read/write/exception, not just one type).
54  *
55  * They also can return -1 to indicate that evloop_run should quit looping
56  * -- this should only happen when the event simply cannot be handled
57  * and the only choice is to terminate the program/function calling
58  * evloop_run. Why? Because the caller of evloop_run is supposed to not be
59  * able to handle the semantics of the things it is running. Hmm, is
60  * this true?
61  */
62 typedef int (*evloop_handler_t)(int fd,int fdtype,void *state);
63 typedef int (*evloop_error_handler_t)(int errortype,int fd,int fdtype,
64  struct evloop_fdinfo *error_fdinfo);
65 
66 struct evloop {
67  int nfds;
68 
70 
71  fd_set rfds;
72  fd_set wfds;
73  fd_set xfds;
74 
75  fd_set rfds_master;
76  fd_set wfds_master;
77  fd_set xfds_master;
78 
79  GHashTable *tab;
80 };
81 
82 struct evloop_fdinfo {
83  int fd;
84 
86  void *rhstate;
88  void *whstate;
90  void *xhstate;
91 };
92 
93 /*
94  * Create an evloop. If you don't set @ehandler, if any of the per-fd
95  * handlers returns EVLOOP_HRET_ERROR or EVLOOP_HRET_BADERROR, that
96  * fd/fdtype pair will be removed from the evloop automatically.
97  */
99 
100 int evloop_maxsize(struct evloop *evloop);
101 
102 int evloop_set_fd(struct evloop *evloop,int fd,int fdtype,
103  evloop_handler_t handler,void *state);
104 int evloop_unset_fd(struct evloop *evloop,int fd,int fdtype);
105 
106 int evloop_run(struct evloop *evloop,evloop_flags_t flags,struct timeval *timeout,
107  struct evloop_fdinfo **error_fdinfo);
108 /*
109  * This is a glorified select. It returns 0 if there are no more FDs to
110  * handle (check via evloop_maxsize() < 0); returns 0 if the timeout is
111  * hit (check the @timeout struct); returns 0 if an FD was handled
112  * successfully (check @*handled_fdinfo and see if it was set); returns
113  * 0 with @hrc set to EVLOOP_HRET_DONE_FAILURE if it handled an FD
114  * successfully but the handler failed.
115  *
116  * Then, for error conditions: returns -1 with errno set if select
117  * failed; returns -1 on internal bug/user error (and sets errno EBADFD
118  * if select thought fd was in evloop set, but it was not set; or errno
119  * set to ENOENT if select claimed some fd was set but we couldn't find
120  * one); errno set to EBADSLT if the FD was set but there is no handler
121  * (this should only happen if user mucks with fdinfo data struct
122  * badly); ENOTSUP if the handler returns an unsupported error code.
123  *
124  * If flags & EVLOOP_RETONINT, if select() is interrupted, it returns -1
125  * and leaves errno set to EINTR. If !(flags & EVLOOP_RETONINT), the
126  * loop continues even if select() was interrupted. If you need to
127  * handle signals synchronously with respect to whatever you're looping
128  * over, setting flags | EVLOOP_RETONINT will help.
129  */
130 int evloop_handleone(struct evloop *evloop,evloop_flags_t flags,
131  struct timeval *timeout,
132  struct evloop_fdinfo **handled_fdinfo,
133  int *handled_fdtype,int *handled_hrc);
134 
135 void evloop_free(struct evloop *evloop);
136 
137 #endif /* __EVLOOP_H__ */
void * xhstate
Definition: evloop.h:90
evloop_handler_t xh
Definition: evloop.h:89
int evloop_unset_fd(struct evloop *evloop, int fd, int fdtype)
Definition: evloop.c:165
void evloop_free(struct evloop *evloop)
Definition: evloop.c:652
void * rhstate
Definition: evloop.h:86
evloop_error_handler_t eh
Definition: evloop.h:69
fd_set rfds
Definition: evloop.h:71
int evloop_handleone(struct evloop *evloop, evloop_flags_t flags, struct timeval *timeout, struct evloop_fdinfo **handled_fdinfo, int *handled_fdtype, int *handled_hrc)
Definition: evloop.c:434
fd_set wfds_master
Definition: evloop.h:76
evloop_flags_t
Definition: evloop.h:45
int evloop_run(struct evloop *evloop, evloop_flags_t flags, struct timeval *timeout, struct evloop_fdinfo **error_fdinfo)
Definition: evloop.c:195
Definition: evloop.h:66
evloop_handler_t wh
Definition: evloop.h:87
fd_set xfds
Definition: evloop.h:73
int nfds
Definition: evloop.h:67
fd_set wfds
Definition: evloop.h:72
evloop_handler_t rh
Definition: evloop.h:85
int evloop_set_fd(struct evloop *evloop, int fd, int fdtype, evloop_handler_t handler, void *state)
Definition: evloop.c:48
int evloop_maxsize(struct evloop *evloop)
Definition: evloop.c:191
void * whstate
Definition: evloop.h:88
struct evloop * evloop_create(evloop_error_handler_t ehandler)
Definition: evloop.c:33
fd_set xfds_master
Definition: evloop.h:77
fd_set rfds_master
Definition: evloop.h:75
GHashTable * tab
Definition: evloop.h:79
int(* evloop_error_handler_t)(int errortype, int fd, int fdtype, struct evloop_fdinfo *error_fdinfo)
Definition: evloop.h:63
int(* evloop_handler_t)(int fd, int fdtype, void *state)
Definition: evloop.h:62