2 * Copyright (c) 2010 Mark Williams
3 * Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
5 * File change event API for netatalk
7 * for every detected filesystem change a UDP packet is sent to an arbitrary list
8 * of listeners. Each packet contains unix path of modified filesystem element,
9 * event reason, and a consecutive event id (32 bit). Technically we are UDP client and are sending
10 * out packets synchronuosly as they are created by the afp functions. This should not affect
11 * performance measurably. The only delaying calls occur during initialization, if we have to
12 * resolve non-IP hostnames to IP. All numeric data inside the packet is network byte order, so use
13 * ntohs / ntohl to resolve length and event id. Ideally a listener receives every packet with
14 * no gaps in event ids, starting with event id 1 and mode FCE_CONN_START followed by
15 * data events from id 2 up to 0xFFFFFFFF, followed by 0 to 0xFFFFFFFF and so on.
17 * A gap or not starting with 1 mode FCE_CONN_START or receiving mode FCE_CONN_BROKEN means that
18 * the listener has lost at least one filesystem event
20 * All Rights Reserved. See COPYRIGHT.
25 #endif /* HAVE_CONFIG_H */
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
40 #include <atalk/adouble.h>
41 #include <atalk/vfs.h>
42 #include <atalk/logger.h>
43 #include <atalk/afp.h>
44 #include <atalk/util.h>
45 #include <atalk/cnid.h>
46 #include <atalk/unix.h>
47 #include <atalk/fce_api.h>
48 #include <atalk/globals.h>
52 #include "directory.h"
56 // ONLY USED IN THIS FILE
57 #include "fce_api_internal.h"
59 extern int afprun_bg(int root, char *cmd);
61 /* We store our connection data here */
62 static struct udp_entry udp_socket_list[FCE_MAX_UDP_SOCKS];
63 static int udp_sockets = 0;
64 static bool udp_initialized = false;
65 static unsigned long fce_ev_enabled =
66 (1 << FCE_FILE_MODIFY) |
67 (1 << FCE_FILE_DELETE) |
68 (1 << FCE_DIR_DELETE) |
69 (1 << FCE_FILE_CREATE) |
70 (1 << FCE_DIR_CREATE) |
71 (1 << FCE_FILE_MOVE) |
76 static uint8_t fce_ev_info; /* flags of additional info to send in events */
79 static unsigned char iobuf[MAXIOBUF];
80 static const char **skip_files;
81 static struct fce_close_event last_close_event;
84 * This only initializes consecutive events beginning at 1, high
85 * numbered events must be initialized in the code
87 static char *fce_event_names[FCE_LAST_EVENT + 1] = {
102 * Initialize network structs for any listeners
103 * We dont give return code because all errors are handled internally (I hope..)
109 struct addrinfo hints, *servinfo, *p;
111 if (udp_initialized == true)
114 memset(&hints, 0, sizeof hints);
115 hints.ai_family = AF_UNSPEC;
116 hints.ai_socktype = SOCK_DGRAM;
118 for (int i = 0; i < udp_sockets; i++) {
119 struct udp_entry *udp_entry = udp_socket_list + i;
121 /* Close any pending sockets */
122 if (udp_entry->sock != -1)
123 close(udp_entry->sock);
125 if ((rv = getaddrinfo(udp_entry->addr, udp_entry->port, &hints, &servinfo)) != 0) {
126 LOG(log_error, logtype_fce, "fce_init_udp: getaddrinfo(%s:%s): %s",
127 udp_entry->addr, udp_entry->port, gai_strerror(rv));
131 /* loop through all the results and make a socket */
132 for (p = servinfo; p != NULL; p = p->ai_next) {
133 if ((udp_entry->sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
134 LOG(log_error, logtype_fce, "fce_init_udp: socket(%s:%s): %s",
135 udp_entry->addr, udp_entry->port, strerror(errno));
142 LOG(log_error, logtype_fce, "fce_init_udp: no socket for %s:%s",
143 udp_entry->addr, udp_entry->port);
145 udp_entry->addrinfo = *p;
146 memcpy(&udp_entry->addrinfo, p, sizeof(struct addrinfo));
147 memcpy(&udp_entry->sockaddr, p->ai_addr, sizeof(struct sockaddr_storage));
148 freeaddrinfo(servinfo);
151 udp_initialized = true;
156 if (udp_initialized == false )
159 for (int i = 0; i < udp_sockets; i++)
161 struct udp_entry *udp_entry = udp_socket_list + i;
163 /* Close any pending sockets */
164 if (udp_entry->sock != -1)
166 close( udp_entry->sock );
167 udp_entry->sock = -1;
170 udp_initialized = false;
174 * Construct a UDP packet for our listeners and return packet size
176 static ssize_t build_fce_packet(const AFPObj *obj,
191 uint8_t packet_info = fce_ev_info;
194 memcpy(p, FCE_PACKET_MAGIC, 8);
199 *p = FCE_PACKET_VERSION;
203 /* optional: options */
204 if (FCE_PACKET_VERSION > 1) {
206 packet_info |= FCE_EV_INFO_SRCPATH;
217 /* optional: padding */
218 if (FCE_PACKET_VERSION > 1) {
223 /* optional: reserved */
224 if (FCE_PACKET_VERSION > 1) {
230 uint32 = htonl(event_id);
231 memcpy(p, &uint32, sizeof(uint32));
233 datalen += sizeof(uint32);
236 if (packet_info & FCE_EV_INFO_PID) {
238 uint64 = hton64(uint64);
239 memcpy(p, &uint64, sizeof(uint64));
241 datalen += sizeof(uint64);
244 /* optional: username */
245 if (packet_info & FCE_EV_INFO_USER) {
246 uint16 = strlen(user);
247 uint16 = htons(uint16);
248 memcpy(p, &uint16, sizeof(uint16));
250 datalen += sizeof(uint16);
251 memcpy(p, user, strlen(user));
253 datalen += strlen(user);
257 if ((pathlen = strlen(path)) >= MAXPATHLEN)
258 pathlen = MAXPATHLEN - 1;
260 uint16 = htons(uint16);
261 memcpy(p, &uint16, sizeof(uint16));
263 datalen += sizeof(uint16);
264 memcpy(p, path, pathlen);
268 /* optional: source path */
269 if (packet_info & FCE_EV_INFO_SRCPATH) {
270 if ((pathlen = strlen(oldpath)) >= MAXPATHLEN)
271 pathlen = MAXPATHLEN - 1;
273 uint16 = htons(uint16);
274 memcpy(p, &uint16, sizeof(uint16));
276 datalen += sizeof(uint16);
277 memcpy(p, oldpath, pathlen);
282 /* return the packet len */
287 * Send the fce information to all (connected) listeners
288 * We dont give return code because all errors are handled internally (I hope..)
290 static void send_fce_event(const AFPObj *obj, int event, const char *path, const char *oldpath)
292 static bool first_event = true;
293 static uint32_t event_id = 0; /* the unique packet couter to detect packet/data loss. Going from 0xFFFFFFFF to 0x0 is a valid increment */
295 time_t now = time(NULL);
299 if (first_event == true) {
302 fce_event_names[FCE_CONN_START] = "FCE_CONN_START";
303 fce_event_names[FCE_CONN_BROKEN] = "FCE_CONN_BROKEN";
305 struct passwd *pwd = getpwuid(obj->uid);
306 user = strdup(pwd->pw_name);
308 switch (obj->fce_version) {
310 /* fce_ev_info unused */
313 fce_ev_info = FCE_EV_INFO_PID | FCE_EV_INFO_USER;
317 LOG(log_error, logtype_fce, "Unsupported FCE protocol version %d", obj->fce_version);
322 /* Notify listeners the we start from the beginning */
323 send_fce_event(obj, FCE_CONN_START, "", NULL);
327 if (obj->fce_notify_script) {
328 static bstring quote = NULL;
329 static bstring quoterep = NULL;
330 static bstring slash = NULL;
331 static bstring slashrep = NULL;
334 quote = bfromcstr("'");
335 quoterep = bfromcstr("'\\''");
336 slash = bfromcstr("\\");
337 slashrep = bfromcstr("\\\\");
340 bstring cmd = bformat("%s -v %d -e %s -i %" PRIu32 "",
341 obj->fce_notify_script,
343 fce_event_names[event],
347 bstring bpath = bfromcstr(path);
348 bfindreplace(bpath, slash, slashrep, 0);
349 bfindreplace(bpath, quote, quoterep, 0);
350 bformata(cmd, " -P '%s'", bdata(bpath));
353 if (fce_ev_info | FCE_EV_INFO_PID)
354 bformata(cmd, " -p %" PRIu64 "", (uint64_t)getpid());
355 if (fce_ev_info | FCE_EV_INFO_USER)
356 bformata(cmd, " -u %s", user);
358 bstring boldpath = bfromcstr(oldpath);
359 bfindreplace(boldpath, slash, slashrep, 0);
360 bfindreplace(boldpath, quote, quoterep, 0);
361 bformata(cmd, " -S '%s'", bdata(boldpath));
364 (void)afprun_bg(1, bdata(cmd));
368 for (int i = 0; i < udp_sockets; i++) {
370 struct udp_entry *udp_entry = udp_socket_list + i;
372 /* we had a problem earlier ? */
373 if (udp_entry->sock == -1) {
374 /* We still have to wait ?*/
375 if (now < udp_entry->next_try_on_error)
379 udp_entry->sock = socket(udp_entry->addrinfo.ai_family,
380 udp_entry->addrinfo.ai_socktype,
381 udp_entry->addrinfo.ai_protocol);
383 if (udp_entry->sock == -1) {
384 /* failed again, so go to rest again */
385 LOG(log_error, logtype_fce, "Cannot recreate socket for fce UDP connection: errno %d", errno );
387 udp_entry->next_try_on_error = now + FCE_SOCKET_RETRY_DELAY_S;
391 udp_entry->next_try_on_error = 0;
393 /* Okay, we have a running socket again, send server that we had a problem on our side*/
394 data_len = build_fce_packet(obj, iobuf, FCE_CONN_BROKEN, "", NULL, getpid(), user, 0);
396 sendto(udp_entry->sock,
400 (struct sockaddr *)&udp_entry->sockaddr,
401 udp_entry->addrinfo.ai_addrlen);
404 /* build our data packet */
405 data_len = build_fce_packet(obj, iobuf, event, path, oldpath, getpid(), user, event_id);
407 sent_data = sendto(udp_entry->sock,
411 (struct sockaddr *)&udp_entry->sockaddr,
412 udp_entry->addrinfo.ai_addrlen);
415 if (sent_data != data_len) {
416 /* Argh, socket broke, we close and retry later */
417 LOG(log_error, logtype_fce, "send_fce_event: error sending packet to %s:%s, transfered %d of %d: %s",
418 udp_entry->addr, udp_entry->port, sent_data, data_len, strerror(errno));
420 close( udp_entry->sock );
421 udp_entry->sock = -1;
422 udp_entry->next_try_on_error = now + FCE_SOCKET_RETRY_DELAY_S;
429 static int add_udp_socket(const char *target_ip, const char *target_port )
431 if (target_port == NULL)
432 target_port = FCE_DEFAULT_PORT_STRING;
434 if (udp_sockets >= FCE_MAX_UDP_SOCKS) {
435 LOG(log_error, logtype_fce, "Too many file change api UDP connections (max %d allowed)", FCE_MAX_UDP_SOCKS );
439 udp_socket_list[udp_sockets].addr = strdup(target_ip);
440 udp_socket_list[udp_sockets].port = strdup(target_port);
441 udp_socket_list[udp_sockets].sock = -1;
442 memset(&udp_socket_list[udp_sockets].addrinfo, 0, sizeof(struct addrinfo));
443 memset(&udp_socket_list[udp_sockets].sockaddr, 0, sizeof(struct sockaddr_storage));
444 udp_socket_list[udp_sockets].next_try_on_error = 0;
451 static void save_close_event(const AFPObj *obj, const char *path)
453 time_t now = time(NULL);
455 /* Check if it's a close for the same event as the last one */
456 if (last_close_event.time /* is there any saved event ? */
457 && (strcmp(path, last_close_event.path) != 0)) {
458 /* no, so send the saved event out now */
459 send_fce_event(obj, FCE_FILE_MODIFY,last_close_event.path, NULL);
462 LOG(log_debug, logtype_fce, "save_close_event: %s", path);
464 last_close_event.time = now;
465 strncpy(last_close_event.path, path, MAXPATHLEN);
468 static void fce_init_ign_names(const char *ignores)
471 char *names = strdup(ignores);
477 for (; names[i] && names[i] != '/'; i++)
484 skip_files = calloc(count + 1, sizeof(char *));
486 for (i = 0, p = strtok(names, "/"); p ; p = strtok(NULL, "/"))
487 skip_files[i++] = strdup(p);
494 * Dispatcher for all incoming file change events
497 int fce_register(const AFPObj *obj, fce_ev_t event, const char *path, const char *oldpath)
499 static bool first_event = true;
502 if (!(fce_ev_enabled & (1 << event)))
505 AFP_ASSERT(event >= FCE_FIRST_EVENT && event <= FCE_LAST_EVENT);
508 LOG(log_debug, logtype_fce, "register_fce(path: %s, event: %s)",
509 path, fce_event_names[event]);
511 bname = basename_safe(path);
513 if ((udp_sockets == 0) && (obj->fce_notify_script == NULL)) {
514 /* No listeners configured */
518 /* do some initialization on the fly the first time */
520 fce_initialize_history();
521 fce_init_ign_names(obj->fce_ign_names);
525 /* handle files which should not cause events (.DS_Store atc. ) */
526 for (int i = 0; skip_files[i] != NULL; i++) {
527 if (strcmp(bname, skip_files[i]) == 0)
531 /* Can we ignore this event based on type or history? */
532 if (fce_handle_coalescation(event, path)) {
533 LOG(log_debug9, logtype_fce, "Coalesced fc event <%d> for <%s>", event, path);
538 case FCE_FILE_MODIFY:
539 save_close_event(obj, path);
542 send_fce_event(obj, event, path, oldpath);
549 static void check_saved_close_events(const AFPObj *obj)
551 time_t now = time(NULL);
553 /* check if configured holdclose time has passed */
554 if (last_close_event.time && ((last_close_event.time + obj->options.fce_fmodwait) < now)) {
555 LOG(log_debug, logtype_fce, "check_saved_close_events: sending event: %s", last_close_event.path);
556 /* yes, send event */
557 send_fce_event(obj, FCE_FILE_MODIFY, &last_close_event.path[0], NULL);
558 last_close_event.path[0] = 0;
559 last_close_event.time = 0;
563 /******************** External calls start here **************************/
566 * API-Calls for file change api, called form outside (file.c directory.c ofork.c filedir.c)
568 void fce_pending_events(const AFPObj *obj)
572 check_saved_close_events(obj);
577 * Extern connect to afpd parameter, can be called multiple times for multiple listeners (up to MAX_UDP_SOCKS times)
580 int fce_add_udp_socket(const char *target)
582 const char *port = FCE_DEFAULT_PORT_STRING;
583 char target_ip[256] = {""};
585 strncpy(target_ip, target, sizeof(target_ip) -1);
587 char *port_delim = strchr( target_ip, ':' );
590 port = port_delim + 1;
592 return add_udp_socket(target_ip, port);
595 int fce_set_events(const char *events)
607 for (p = strtok(e, ", "); p; p = strtok(NULL, ", ")) {
608 if (strcmp(p, "fmod") == 0) {
609 fce_ev_enabled |= (1 << FCE_FILE_MODIFY);
610 } else if (strcmp(p, "fdel") == 0) {
611 fce_ev_enabled |= (1 << FCE_FILE_DELETE);
612 } else if (strcmp(p, "ddel") == 0) {
613 fce_ev_enabled |= (1 << FCE_DIR_DELETE);
614 } else if (strcmp(p, "fcre") == 0) {
615 fce_ev_enabled |= (1 << FCE_FILE_CREATE);
616 } else if (strcmp(p, "dcre") == 0) {
617 fce_ev_enabled |= (1 << FCE_DIR_CREATE);
618 } else if (strcmp(p, "fmov") == 0) {
619 fce_ev_enabled |= (1 << FCE_FILE_MOVE);
620 } else if (strcmp(p, "dmov") == 0) {
621 fce_ev_enabled |= (1 << FCE_DIR_MOVE);
622 } else if (strcmp(p, "login") == 0) {
623 fce_ev_enabled |= (1 << FCE_LOGIN);
624 } else if (strcmp(p, "logout") == 0) {
625 fce_ev_enabled |= (1 << FCE_LOGOUT);