1 /*
2 * Copyright (C) 2018 Nicolas Bonnefon and other contributors
3 *
4 * This file is part of glogg.
5 *
6 * glogg is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * glogg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with glogg. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "kqueuewatchtowerdriver.h"
21
22 #include <sys/types.h>
23 #include <sys/event.h>
24 #include <sys/time.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27
28 #include "log.h"
29
30 #include "watchtowerlist.h"
31
KQueueWatchTowerDriver()32 KQueueWatchTowerDriver::KQueueWatchTowerDriver() : kqueue_fd_( kqueue() )
33 {
34 int pipefd[2];
35
36 pipe( pipefd );
37 fcntl( pipefd[0], F_SETFD, O_NONBLOCK );
38 fcntl( pipefd[1], F_SETFD, O_NONBLOCK );
39
40 breaking_pipe_read_fd_ = pipefd[0];
41 breaking_pipe_write_fd_ = pipefd[1];
42
43 struct kevent changelist;
44 const struct timespec no_timeout = { 0, 0 };
45
46 EV_SET( &changelist,
47 breaking_pipe_read_fd_, // ident
48 EVFILT_READ, // filter
49 EV_ADD, // flags
50 0, // fflags
51 NULL, // data
52 (void*) 0 // opaque data
53 );
54
55 // Add a watch for the breaking pipe
56 if ( kevent( kqueue_fd_, &changelist, 1, NULL, 0, &no_timeout ) != -1 )
57 LOG(logDEBUG) << "KQueueWatchTowerDriver::KQueueWatchTowerDriver breaking pipe monitored";
58 else
59 LOG(logERROR) << "KQueueWatchTowerDriver::KQueueWatchTowerDriver cannot add breaking pipe";
60 }
61
~KQueueWatchTowerDriver()62 KQueueWatchTowerDriver::~KQueueWatchTowerDriver()
63 {
64 close( breaking_pipe_read_fd_ );
65 close( breaking_pipe_write_fd_ );
66
67 close( kqueue_fd_ );
68 }
69
addFile(const std::string & file_name)70 KQueueWatchTowerDriver::FileId KQueueWatchTowerDriver::addFile(
71 const std::string& file_name )
72 {
73 struct kevent changelist;
74 const struct timespec no_timeout = { 0, 0 };
75 int fd = open( file_name.c_str(), O_EVTONLY );
76
77 if ( fd != -1 ) {
78 LOG(logDEBUG) << "KQueueWatchTowerDriver::addFile new fd " << fd << " for file " << file_name;
79 EV_SET( &changelist,
80 fd, // ident
81 EVFILT_VNODE, // filter
82 EV_ADD | EV_ENABLE | EV_CLEAR, // flags
83 NOTE_DELETE | NOTE_WRITE | NOTE_RENAME | NOTE_REVOKE, // fflags
84 NULL, // data
85 (void*) 0 // opaque data
86 );
87
88 // Add a watch for the inode
89 if ( kevent( kqueue_fd_, &changelist, 1, NULL, 0, &no_timeout ) != -1 )
90 LOG(logDEBUG) << "KQueueWatchTowerDriver::addFile new kevent added for fd " << fd;
91 else
92 LOG(logERROR) << "KQueueWatchTowerDriver::addFile cannot add file";
93 }
94 else {
95 LOG(logERROR) << "KQueueWatchTowerDriver::addFile cannot open " << file_name;
96 }
97
98 return { fd };
99 }
100
addSymlink(const std::string & file_name)101 KQueueWatchTowerDriver::SymlinkId KQueueWatchTowerDriver::addSymlink(
102 const std::string& file_name )
103 {
104 /*
105 int symlink_wd = inotify_add_watch( inotify_fd_, file_name.c_str(),
106 IN_DONT_FOLLOW | IN_DELETE_SELF | IN_MODIFY | IN_MOVE_SELF );
107 LOG(logDEBUG) << "INotifyWatchTower::addFile new inotify symlink_wd " << symlink_wd;
108 // (not sure a symlink can be modified but you never know)
109
110 return { symlink_wd };
111 */
112 return { 0 };
113 }
114
addDir(const std::string & file_name)115 KQueueWatchTowerDriver::DirId KQueueWatchTowerDriver::addDir(
116 const std::string& file_name )
117 {
118 /*
119 int dir_wd = inotify_add_watch( inotify_fd_, file_name.c_str(),
120 IN_CREATE | IN_MOVE | IN_ONLYDIR );
121 LOG(logDEBUG) << "INotifyWatchTower::addFile dir " << file_name
122 << " watched wd " << dir_wd;
123
124 return { dir_wd };
125 */
126 struct kevent changelist;
127 const struct timespec no_timeout = { 0, 0 };
128 int fd = open( file_name.c_str(), O_EVTONLY );
129
130 if ( fd != -1 ) {
131 LOG(logDEBUG) << "KQueueWatchTowerDriver::addDir new fd " << fd << " for dir " << file_name;
132 EV_SET( &changelist,
133 fd, // ident
134 EVFILT_VNODE, // filter
135 EV_ADD | EV_ENABLE | EV_CLEAR, // flags
136 NOTE_WRITE, // fflags
137 NULL, // data
138 (void*) 0 // opaque data
139 );
140
141 // Add a watch for the inode
142 if ( kevent( kqueue_fd_, &changelist, 1, NULL, 0, &no_timeout ) != -1 )
143 LOG(logDEBUG) << "KQueueWatchTowerDriver::addDir new kevent added for fd " << fd;
144 else
145 LOG(logERROR) << "KQueueWatchTowerDriver::addDir cannot add file";
146 }
147 else {
148 LOG(logERROR) << "KQueueWatchTowerDriver::addDir cannot open " << file_name;
149 }
150
151 return { fd };
152 }
153
removeFile(const KQueueWatchTowerDriver::FileId & file_id)154 void KQueueWatchTowerDriver::removeFile(
155 const KQueueWatchTowerDriver::FileId& file_id )
156 {
157 LOG(logDEBUG) << "KQueueWatchTowerDriver::removeFile removing fd " << file_id.fd_;
158 close( file_id.fd_ );
159 }
160
removeSymlink(const SymlinkId & symlink_id)161 void KQueueWatchTowerDriver::removeSymlink( const SymlinkId& symlink_id )
162 {
163 /*
164 if ( symlink_id.wd_ >= 0 )
165 inotify_rm_watch( inotify_fd_, symlink_id.wd_ );
166 */
167 }
168
removeDir(const DirId & dir_id)169 void KQueueWatchTowerDriver::removeDir( const DirId& dir_id )
170 {
171 /*
172 LOG(logDEBUG) << "INotifyWatchTower::removeDir removing inotify wd " << dir_id.wd_;
173
174 if ( dir_id.wd_ >= 0 )
175 inotify_rm_watch( inotify_fd_, dir_id.wd_ );
176 */
177 }
178
179 std::vector<KQueueWatchTowerDriver::KQueueObservedFile*>
waitAndProcessEvents(KQueueObservedFileList * list,std::unique_lock<std::mutex> * list_lock,std::vector<KQueueObservedFile * > * files_needing_readding,int timeout_ms)180 KQueueWatchTowerDriver::waitAndProcessEvents(
181 KQueueObservedFileList* list,
182 std::unique_lock<std::mutex>* list_lock,
183 std::vector<KQueueObservedFile*>* files_needing_readding,
184 int timeout_ms )
185 {
186 std::vector<KQueueObservedFile*> files_to_notify;
187 struct kevent event;
188 struct timespec timeout = { timeout_ms / 1000, (timeout_ms % 1000) * 1000000};
189
190 list_lock->unlock();
191 int event_count = kevent( kqueue_fd_, NULL, 0, &event, 1, timeout_ms ? &timeout : NULL );
192 list_lock->lock();
193
194 LOG(logDEBUG) << "KQueueWatchTowerDriver::waitAndProcessEvents: event_count " << event_count;
195
196 if ( event.flags == EV_ERROR )
197 {
198 LOG(logERROR) << "kqueue event error!";
199 }
200 else if ( event_count > 0 )
201 {
202 LOG(logDEBUG) << "kqueue event received: " << event.ident << " " <<
203 std::hex << event.filter << " " << event.flags << " " << event.fflags;
204
205 KQueueObservedFile* file = nullptr;
206
207 // Retrieve the file (if it was file)
208 file = list->searchByFileOrSymlinkWd( event.ident, event.ident );
209
210 if ( file )
211 {
212 LOG(logDEBUG) << "Adding file to notify list: " << std::hex << file;
213 files_to_notify.push_back( file );
214 }
215 else
216 {
217 /* We signal all watched files in this directory, yes it is wasteful,
218 * but kqueue doesn't give us enough information so we reach for
219 * the sledgehammer rather than trying to see what has changed in this dir!
220 */
221 LOG(logDEBUG) << "File not found, is it a directory?";
222 auto files_in_dir = list->searchByDirWd( event.ident );
223 LOG(logDEBUG) << files_in_dir.size() << " files in dir.";
224 files_to_notify.reserve( files_in_dir.size() );
225 files_to_notify.insert( files_to_notify.end(), files_in_dir.begin(), files_in_dir.end() );
226 }
227 }
228 else
229 {
230 LOG(logDEBUG) << "No kevent...";
231 }
232
233 return files_to_notify;
234 }
235
interruptWait()236 void KQueueWatchTowerDriver::interruptWait()
237 {
238 char byte = 'X';
239
240 (void) write( breaking_pipe_write_fd_, (void*) &byte, sizeof byte );
241 }
242