/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- dmtcp_event_hook
- process_fd_event
- close
- fclose
- closedir
- dup
- dup2
- dup3
1 /****************************************************************************
2 * Copyright (C) 2006-2013 by Jason Ansel, Kapil Arya, and Gene Cooperman *
3 * jansel@csail.mit.edu, kapil@ccs.neu.edu, gene@ccs.neu.edu *
4 * *
5 * This file is part of the dmtcp/src module of DMTCP (DMTCP:dmtcp/src). *
6 * *
7 * DMTCP:dmtcp/src is free software: you can redistribute it and/or *
8 * modify it under the terms of the GNU Lesser General Public License as *
9 * published by the Free Software Foundation, either version 3 of the *
10 * License, or (at your option) any later version. *
11 * *
12 * DMTCP:dmtcp/src is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with DMTCP:dmtcp/src. If not, see *
19 * <http://www.gnu.org/licenses/>. *
20 ****************************************************************************/
21
22 #include <unistd.h>
23 #include <sys/syscall.h>
24 #include <sys/errno.h>
25 #include <linux/version.h>
26 #include "dmtcp.h"
27 #include "jassert.h"
28 #include "ipc.h"
29
30 using namespace dmtcp;
31
32 void dmtcp_SSH_EventHook(DmtcpEvent_t event, DmtcpEventData_t *data);
33 void dmtcp_FileConnList_EventHook(DmtcpEvent_t event, DmtcpEventData_t *data);
34 void dmtcp_SocketConnList_EventHook(DmtcpEvent_t event, DmtcpEventData_t *data);
35 void dmtcp_EventConnList_EventHook(DmtcpEvent_t event, DmtcpEventData_t *data);
36
37 void dmtcp_FileConn_ProcessFdEvent(int event, int arg1, int arg2);
38 void dmtcp_SocketConn_ProcessFdEvent(int event, int arg1, int arg2);
39 void dmtcp_EventConn_ProcessFdEvent(int event, int arg1, int arg2);
40
41 extern "C"
42 void dmtcp_event_hook(DmtcpEvent_t event, DmtcpEventData_t *data)
43 {
44 dmtcp_SSH_EventHook(event, data);
45 dmtcp_FileConnList_EventHook(event, data);
46 dmtcp_SocketConnList_EventHook(event, data);
47 dmtcp_EventConnList_EventHook(event, data);
48
49 DMTCP_NEXT_EVENT_HOOK(event, data);
50 return;
51 }
52
53 extern "C" void process_fd_event(int event, int arg1, int arg2 = -1)
54 {
55 dmtcp_FileConn_ProcessFdEvent(event, arg1, arg2);
56 dmtcp_SocketConn_ProcessFdEvent(event, arg1, arg2);
57 dmtcp_EventConn_ProcessFdEvent(event, arg1, arg2);
58 }
59
60 extern "C" int close(int fd)
61 {
62 if (dmtcp_is_protected_fd(fd)) {
63 JTRACE("blocked attempt to close protected fd") (fd);
64 errno = EBADF;
65 return -1;
66 }
67
68 DMTCP_PLUGIN_DISABLE_CKPT();
69 int rv = _real_close(fd);
70 if (rv == 0 && dmtcp_is_running_state()) {
71 process_fd_event(SYS_close, fd);
72 }
73 DMTCP_PLUGIN_ENABLE_CKPT();
74 return rv;
75 }
76
77 extern "C" int fclose(FILE *fp)
78 {
79 int fd = fileno(fp);
80 if (dmtcp_is_protected_fd(fd)) {
81 JTRACE("blocked attempt to fclose protected fd") (fd);
82 errno = EBADF;
83 return -1;
84 }
85
86 DMTCP_PLUGIN_DISABLE_CKPT();
87 int rv = _real_fclose(fp);
88 if (rv == 0 && dmtcp_is_running_state()) {
89 process_fd_event(SYS_close, fd);
90 }
91 DMTCP_PLUGIN_ENABLE_CKPT();
92
93 return rv;
94 }
95
96 extern "C" int closedir(DIR *dir)
97 {
98 int fd = dirfd(dir);
99 if (dmtcp_is_protected_fd(fd)) {
100 JTRACE("blocked attempt to closedir protected fd") (fd);
101 errno = EBADF;
102 return -1;
103 }
104
105 DMTCP_PLUGIN_DISABLE_CKPT();
106 int rv = _real_closedir(dir);
107 if (rv == 0 && dmtcp_is_running_state()) {
108 process_fd_event(SYS_close, fd);
109 }
110 DMTCP_PLUGIN_ENABLE_CKPT();
111
112 return rv;
113 }
114
115 extern "C" int dup(int oldfd)
116 {
117 DMTCP_PLUGIN_DISABLE_CKPT();
118 int newfd = _real_dup(oldfd);
119 if (newfd != -1 && dmtcp_is_running_state()) {
120 process_fd_event(SYS_dup, oldfd, newfd);
121 }
122 DMTCP_PLUGIN_ENABLE_CKPT();
123 return newfd;
124 }
125
126 extern "C" int dup2(int oldfd, int newfd)
127 {
128 DMTCP_PLUGIN_DISABLE_CKPT();
129 int res = _real_dup2(oldfd, newfd);
130 if (res != -1 && newfd != oldfd && dmtcp_is_running_state()) {
131 process_fd_event(SYS_dup, oldfd, newfd);
132 }
133 DMTCP_PLUGIN_ENABLE_CKPT();
134 return newfd;
135 }
136
137 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)) && __GLIBC_PREREQ(2,9)
138 // dup3 appeared in Linux 2.6.27
139 extern "C" int dup3(int oldfd, int newfd, int flags)
140 {
141 DMTCP_PLUGIN_DISABLE_CKPT();
142 int res = _real_dup3(oldfd, newfd, flags);
143 if (res != -1 && newfd != oldfd && dmtcp_is_running_state()) {
144 process_fd_event(SYS_dup, oldfd, newfd);
145 }
146 DMTCP_PLUGIN_ENABLE_CKPT();
147 return newfd;
148 }
149 #endif
150