/* [<][>][^][v][top][bottom][index][help] */
1 /****************************************************************************
2 * Copyright (C) 2006-2008 by Jason Ansel, Kapil Arya, Gene Cooperman, *
3 * and Rohan Garg *
4 * jansel@csail.mit.edu, kapil@ccs.neu.edu, gene@ccs.neu.edu, and *
5 * rohgarg@ccs.neu.edu *
6 * *
7 * This file is part of the dmtcp/src module of DMTCP (DMTCP:dmtcp/src). *
8 * *
9 * DMTCP:dmtcp/src is free software: you can redistribute it and/or *
10 * modify it under the terms of the GNU Lesser General Public License as *
11 * published by the Free Software Foundation, either version 3 of the *
12 * License, or (at your option) any later version. *
13 * *
14 * DMTCP:dmtcp/src is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU Lesser General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU Lesser General Public *
20 * License along with DMTCP:dmtcp/src. If not, see *
21 * <http://www.gnu.org/licenses/>. *
22 ****************************************************************************/
23
24 #pragma once
25 #ifndef EVENTCONNECTION_H
26 #define EVENTCONNECTION_H
27
28 // THESE INCLUDES ARE IN RANDOM ORDER. LET'S CLEAN IT UP AFTER RELEASE. - Gene
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <stdint.h>
34 #include <signal.h>
35 #include "connection.h"
36 #include "connectionlist.h"
37
38 #ifdef HAVE_SYS_INOTIFY_H
39 #include <sys/inotify.h>
40 #endif
41
42 #ifdef HAVE_SYS_EPOLL_H
43 # include <sys/epoll.h>
44 #else
45 /* KEEP THIS IN SYNC WITH syscallwrappers.h */
46 # ifndef _SYS_EPOLL_H
47 # define _SYS_EPOLL_H 1
48 struct epoll_event {int dummy;};
49 /* Valid opcodes("op" parameter) to issue to epoll_ctl(). */
50 # define EPOLL_CTL_ADD 1 /* Add a file decriptor to the interface. */
51 # define EPOLL_CTL_DEL 2 /* Remove a file decriptor from the interface. */
52 # define EPOLL_CTL_MOD 3 /* Change file decriptor epoll_event structure. */
53 # endif
54 #endif
55 #ifdef HAVE_SYS_EVENTFD_H
56 # include <sys/eventfd.h>
57 #else
58 enum { EFD_SEMAPHORE = 1 };
59 #endif
60 #ifdef HAVE_SYS_SIGNALFD_H
61 # include <sys/signalfd.h>
62 #else
63 # include <stdint.h>
64 struct signalfd_siginfo {uint32_t ssi_signo; int dummy;};
65 #endif
66
67 namespace dmtcp
68 {
69 #ifdef HAVE_SYS_EPOLL_H
70 class EpollConnection: public Connection
71 {
72 public:
73 enum EpollType
74 {
75 EPOLL_INVALID = Connection::EPOLL,
76 EPOLL_CREATE,
77 EPOLL_CTL,
78 EPOLL_WAIT
79 };
80
81 EpollConnection(int size, int type=EPOLL_CREATE)
82 :Connection(EPOLL),
83 _type(type),
84 _size(size)
85 {
86 JTRACE("new epoll connection created");
87 }
88
89 int epollType() const { return _type; }
90
91 virtual void drain();
92 virtual void refill(bool isRestart);
93 virtual void postRestart();
94 virtual void serializeSubClass(jalib::JBinarySerializer& o);
95
96 virtual string str() { return "EPOLL-FD: <Not-a-File>"; };
97
98 void onCTL(int op, int fd, struct epoll_event *event);
99
100 private:
101 EpollConnection& asEpoll();
102 int64_t _type; // current state of EPOLL
103 struct stat _stat; // not sure if stat makes sense in case of epfd
104 int64_t _size; // flags
105 map<int, struct epoll_event > _fdToEvent;
106 };
107 #endif
108
109 #ifdef HAVE_SYS_EVENTFD_H
110 class EventFdConnection: public Connection
111 {
112 public:
113 inline EventFdConnection(unsigned int initval, int flags)
114 :Connection(EVENTFD),
115 _initval(initval),
116 _flags(flags)
117 {
118 JTRACE("new eventfd connection created");
119 }
120
121 virtual void drain();
122 virtual void refill(bool isRestart);
123 virtual void postRestart();
124 virtual void serializeSubClass(jalib::JBinarySerializer& o);
125
126 virtual string str() { return "EVENT-FD: <Not-a-File>"; };
127
128 private:
129 uint64_t _initval; // initial counter value
130 int64_t _flags; // flags
131 int64_t evtfd;
132 };
133 #endif
134
135 #ifdef HAVE_SYS_SIGNALFD_H
136 class SignalFdConnection: public Connection
137 {
138 public:
139 inline SignalFdConnection(int signalfd, const sigset_t* mask, int flags)
140 :Connection(SIGNALFD),
141 signlfd(signalfd),
142 _flags(flags)
143 {
144 if (mask!=NULL)
145 _mask = *mask;
146 else
147 sigemptyset(&_mask);
148 memset(&_fdsi, 0, sizeof(_fdsi));
149 JTRACE("new signalfd connection created");
150 }
151
152 virtual void drain();
153 virtual void refill(bool isRestart);
154 virtual void postRestart();
155 virtual void serializeSubClass(jalib::JBinarySerializer& o);
156
157 virtual string str() { return "SIGNAL-FD: <Not-a-File>"; };
158
159 private:
160 int64_t signlfd;
161 int64_t _flags; // flags
162 sigset_t _mask; // mask for signals
163 struct signalfd_siginfo _fdsi;
164 };
165 #endif
166
167 #ifdef HAVE_SYS_INOTIFY_H
168 #ifdef DMTCP_USE_INOTIFY
169 class InotifyConnection: public Connection
170 {
171 public:
172 enum InotifyState {
173 INOTIFY_INVALID = INOTIFY,
174 INOTIFY_CREATE,
175 INOTIFY_ADD_WAIT
176 };
177
178 inline InotifyConnection (int flags)
179 :Connection(INOTIFY),
180 _flags (flags),
181 _state(INOTIFY_CREATE)
182 {
183 JTRACE ("new inotify connection created");
184 }
185
186 int inotifyState() const { return (int) _state; }
187 InotifyConnection& asInotify();
188
189 virtual void drain();
190 virtual void refill(bool isRestart);
191 virtual void postRestart();
192 virtual void serializeSubClass(jalib::JBinarySerializer& o);
193
194 virtual string str() { return "INOTIFY-FD: <Not-a-File>"; };
195
196 void map_inotify_fd_to_wd( int fd, int wd);
197 void add_watch_descriptors(int wd, int fd, const char *pathname,
198 uint32_t mask);
199 void remove_watch_descriptors(int wd);
200 private:
201 int64_t _flags; // flags
202 int64_t _state; // current state of INOTIFY
203 struct stat _stat; // not sure if stat makes sense in case of epfd
204 };
205 #endif
206 #endif
207 }
208
209 #endif