1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
/* Marmot
* Copyright (C) 2003 James Willcox, Corey Bowers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "server_config.h"
#define _GNU_SOURCE
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <glib.h>
#include "gam_error.h"
#include "gam_poll_dnotify.h"
#include "gam_dnotify.h"
#include "gam_tree.h"
#include "gam_event.h"
#include "gam_server.h"
#include "gam_event.h"
#ifdef GAMIN_DEBUG_API
#include "gam_debugging.h"
#endif
/* just pulling a value out of nowhere here...may need tweaking */
#define MAX_QUEUE_SIZE 500
typedef struct {
char *path;
int fd;
int refcount;
int busy;
} DNotifyData;
static GHashTable *path_hash = NULL;
static GHashTable *fd_hash = NULL;
G_LOCK_DEFINE_STATIC(dnotify);
/* TODO: GQueue is not signal-safe, need to use something else */
static GQueue *changes = NULL;
static GIOChannel *pipe_read_ioc = NULL;
static GIOChannel *pipe_write_ioc = NULL;
static void
gam_dnotify_data_debug (gpointer key, gpointer value, gpointer user_data)
{
int deactivated;
DNotifyData *data = (DNotifyData *)value;
if (!data)
return;
deactivated = data->fd == -1 ? 1 : 0;
GAM_DEBUG(DEBUG_INFO, "dsub fd %d refs %d busy %d deactivated %d: %s\n", data->fd, data->refcount, data->busy, deactivated, data->path);
}
void
gam_dnotify_debug ()
{
if (path_hash == NULL)
return;
GAM_DEBUG(DEBUG_INFO, "Dumping dnotify subscriptions\n");
g_hash_table_foreach (path_hash, gam_dnotify_data_debug, NULL);
}
static DNotifyData *
gam_dnotify_data_new(const char *path, int fd)
{
DNotifyData *data;
data = g_new0(DNotifyData, 1);
data->path = g_strdup(path);
data->fd = fd;
data->busy = 0;
data->refcount = 1;
return data;
}
static void
gam_dnotify_data_free(DNotifyData * data)
{
g_free(data->path);
g_free(data);
}
static void
gam_dnotify_directory_handler_internal(const char *path, pollHandlerMode mode)
{
DNotifyData *data;
int fd;
switch (mode) {
case GAMIN_ACTIVATE:
GAM_DEBUG(DEBUG_INFO, "Adding %s to dnotify\n", path);
break;
case GAMIN_DEACTIVATE:
GAM_DEBUG(DEBUG_INFO, "Removing %s from dnotify\n", path);
break;
case GAMIN_FLOWCONTROLSTART:
GAM_DEBUG(DEBUG_INFO, "Start flow control for %s\n", path);
break;
case GAMIN_FLOWCONTROLSTOP:
GAM_DEBUG(DEBUG_INFO, "Stop flow control for %s\n", path);
break;
default:
gam_error(DEBUG_INFO, "Unknown DNotify operation %d for %s\n",
mode, path);
return;
}
G_LOCK(dnotify);
if (mode == GAMIN_ACTIVATE) {
if ((data = g_hash_table_lookup(path_hash, path)) != NULL) {
data->refcount++;
GAM_DEBUG(DEBUG_INFO, " found incremented refcount: %d\n",
data->refcount);
G_UNLOCK(dnotify);
#ifdef GAMIN_DEBUG_API
gam_debug_report(GAMDnotifyChange, path, data->refcount);
#endif
return;
}
fd = open(path, O_RDONLY);
if (fd < 0) {
G_UNLOCK(dnotify);
return;
}
data = gam_dnotify_data_new(path, fd);
g_hash_table_insert(fd_hash, GINT_TO_POINTER(data->fd), data);
g_hash_table_insert(path_hash, data->path, data);
fcntl(fd, F_SETSIG, SIGRTMIN);
fcntl(fd, F_NOTIFY,
DN_MODIFY | DN_CREATE | DN_DELETE | DN_RENAME | DN_ATTRIB |
DN_MULTISHOT);
GAM_DEBUG(DEBUG_INFO, "activated DNotify for %s\n", path);
#ifdef GAMIN_DEBUG_API
gam_debug_report(GAMDnotifyCreate, path, 0);
#endif
} else if (mode == GAMIN_DEACTIVATE) {
data = g_hash_table_lookup(path_hash, path);
if (!data) {
GAM_DEBUG(DEBUG_INFO, " not found !!!\n");
G_UNLOCK(dnotify);
return;
}
data->refcount--;
if (data->refcount == 0) {
close(data->fd);
GAM_DEBUG(DEBUG_INFO, "deactivated DNotify for %s\n",
data->path);
g_hash_table_remove(path_hash, data->path);
g_hash_table_remove(fd_hash, GINT_TO_POINTER(data->fd));
gam_dnotify_data_free(data);
#ifdef GAMIN_DEBUG_API
gam_debug_report(GAMDnotifyDelete, path, 0);
#endif
} else {
GAM_DEBUG(DEBUG_INFO, " found decremented refcount: %d\n",
data->refcount);
#ifdef GAMIN_DEBUG_API
gam_debug_report(GAMDnotifyChange, data->path, data->refcount);
#endif
}
} else if ((mode == GAMIN_FLOWCONTROLSTART) ||
(mode == GAMIN_FLOWCONTROLSTOP)) {
data = g_hash_table_lookup(path_hash, path);
if (!data) {
GAM_DEBUG(DEBUG_INFO, " not found !!!\n");
G_UNLOCK(dnotify);
return;
}
if (data != NULL) {
if (mode == GAMIN_FLOWCONTROLSTART) {
if (data->fd >= 0) {
close(data->fd);
g_hash_table_remove(fd_hash, GINT_TO_POINTER(data->fd));
data->fd = -1;
GAM_DEBUG(DEBUG_INFO, "deactivated DNotify for %s\n",
data->path);
#ifdef GAMIN_DEBUG_API
gam_debug_report(GAMDnotifyFlowOn, data->path, 0);
#endif
}
data->busy++;
} else {
if (data->busy > 0) {
data->busy--;
if (data->busy == 0) {
fd = open(data->path, O_RDONLY);
if (fd < 0) {
G_UNLOCK(dnotify);
GAM_DEBUG(DEBUG_INFO,
"Failed to reactivate DNotify for %s\n",
data->path);
return;
}
data->fd = fd;
g_hash_table_insert(fd_hash, GINT_TO_POINTER(data->fd),
data);
fcntl(fd, F_SETSIG, SIGRTMIN);
fcntl(fd, F_NOTIFY,
DN_MODIFY | DN_CREATE | DN_DELETE | DN_RENAME |
DN_ATTRIB | DN_MULTISHOT);
GAM_DEBUG(DEBUG_INFO, "Reactivated DNotify for %s\n",
data->path);
#ifdef GAMIN_DEBUG_API
gam_debug_report(GAMDnotifyFlowOff, path, 0);
#endif
}
}
}
}
} else {
GAM_DEBUG(DEBUG_INFO, "Unimplemented operation\n");
}
G_UNLOCK(dnotify);
}
static void
gam_dnotify_directory_handler(const char *path, pollHandlerMode mode)
{
GAM_DEBUG(DEBUG_INFO, "gam_dnotify_directory_handler %s : %d\n",
path, mode);
gam_dnotify_directory_handler_internal(path, mode);
}
static void
gam_dnotify_file_handler(const char *path, pollHandlerMode mode)
{
GAM_DEBUG(DEBUG_INFO, "gam_dnotify_file_handler %s : %d\n", path, mode);
if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
gam_dnotify_directory_handler_internal(path, mode);
} else {
GAM_DEBUG(DEBUG_INFO, " not a dir %s, failed !!!\n", path);
}
}
static void
dnotify_signal_handler(int sig, siginfo_t * si, void *sig_data)
{
if (changes->length > MAX_QUEUE_SIZE) {
GAM_DEBUG(DEBUG_INFO, "Queue Full\n");
return;
}
g_queue_push_head(changes, GINT_TO_POINTER(si->si_fd));
g_io_channel_write_chars(pipe_write_ioc, "bogus", 5, NULL, NULL);
g_io_channel_flush(pipe_write_ioc, NULL);
GAM_DEBUG(DEBUG_INFO, "signal handler done\n");
}
static void
overflow_signal_handler(int sig, siginfo_t * si, void *sig_data)
{
GAM_DEBUG(DEBUG_INFO, "**** signal queue overflow ***\n");
}
static gboolean
gam_dnotify_pipe_handler(gpointer user_data)
{
char buf[5000];
DNotifyData *data;
gpointer fd;
int i;
GAM_DEBUG(DEBUG_INFO, "gam_dnotify_pipe_handler()\n");
g_io_channel_read_chars(pipe_read_ioc, buf, sizeof(buf), NULL, NULL);
i = 0;
while ((fd = g_queue_pop_tail(changes)) != NULL) {
G_LOCK(dnotify);
data = g_hash_table_lookup(fd_hash, fd);
G_UNLOCK(dnotify);
if (data == NULL)
continue;
GAM_DEBUG(DEBUG_INFO, "handling signal\n");
gam_poll_generic_scan_directory(data->path);
i++;
}
GAM_DEBUG(DEBUG_INFO, "gam_dnotify_pipe_handler() done\n");
return TRUE;
}
/**
* @defgroup DNotify DNotify Backend
* @ingroup Backends
* @brief DNotify backend API
*
* Since version 2.4, Linux kernels have included the Linux Directory
* Notification system (dnotify). This backend uses dnotify to know when
* files are changed/created/deleted. Since dnotify doesn't tell us
* exactly what event happened to which file (just that some even happened
* in some directory), we still have to cache stat() information. For this,
* we can just use the code in the polling backend.
*
* @{
*/
/**
* Initializes the polling system. This must be called before
* any other functions in this module.
*
* @returns TRUE if initialization succeeded, FALSE otherwise
*/
gboolean
gam_dnotify_init(void)
{
struct sigaction act;
int fds[2];
GSource *source;
g_return_val_if_fail(gam_poll_dnotify_init (), FALSE);
if (pipe(fds) < 0) {
g_warning("Could not create pipe.\n");
return FALSE;
}
pipe_read_ioc = g_io_channel_unix_new(fds[0]);
pipe_write_ioc = g_io_channel_unix_new(fds[1]);
g_io_channel_set_flags(pipe_read_ioc, G_IO_FLAG_NONBLOCK, NULL);
g_io_channel_set_flags(pipe_write_ioc, G_IO_FLAG_NONBLOCK, NULL);
source = g_io_create_watch(pipe_read_ioc,
G_IO_IN | G_IO_HUP | G_IO_ERR);
g_source_set_callback(source, gam_dnotify_pipe_handler, NULL, NULL);
g_source_attach(source, NULL);
g_source_unref(source);
/* setup some signal stuff */
act.sa_sigaction = dnotify_signal_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
sigaction(SIGRTMIN, &act, NULL);
/* catch SIGIO as well (happens when the realtime queue fills up) */
act.sa_sigaction = overflow_signal_handler;
sigemptyset(&act.sa_mask);
sigaction(SIGIO, &act, NULL);
changes = g_queue_new();
path_hash = g_hash_table_new(g_str_hash, g_str_equal);
fd_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
GAM_DEBUG(DEBUG_INFO, "dnotify initialized\n");
gam_server_install_kernel_hooks (GAMIN_K_DNOTIFY,
gam_dnotify_add_subscription,
gam_dnotify_remove_subscription,
gam_dnotify_remove_all_for,
gam_dnotify_directory_handler,
gam_dnotify_file_handler);
return TRUE;
}
/**
* Adds a subscription to be monitored.
*
* @param sub a #GamSubscription to be polled
* @returns TRUE if adding the subscription succeeded, FALSE otherwise
*/
gboolean
gam_dnotify_add_subscription(GamSubscription * sub)
{
GAM_DEBUG(DEBUG_INFO, "dnotify: Adding subscription for %s\n", gam_subscription_get_path (sub));
if (!gam_poll_add_subscription(sub)) {
return FALSE;
}
return TRUE;
}
/**
* Removes a subscription which was being monitored.
*
* @param sub a #GamSubscription to remove
* @returns TRUE if removing the subscription succeeded, FALSE otherwise
*/
gboolean
gam_dnotify_remove_subscription(GamSubscription * sub)
{
GAM_DEBUG(DEBUG_INFO, "dnotify: Removing subscription for %s\n", gam_subscription_get_path (sub));
if (!gam_poll_remove_subscription(sub)) {
return FALSE;
}
return TRUE;
}
/**
* Stop monitoring all subscriptions for a given listener.
*
* @param listener a #GamListener
* @returns TRUE if removing the subscriptions succeeded, FALSE otherwise
*/
gboolean
gam_dnotify_remove_all_for(GamListener * listener)
{
GAM_DEBUG(DEBUG_INFO, "dnotify: Removing all subscriptions for %s\n", gam_listener_get_pidname (listener));
if (!gam_poll_remove_all_for(listener)) {
return FALSE;
}
return TRUE;
}
/** @} */
|