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 /* We store our connection data here */
60 static struct udp_entry udp_socket_list[FCE_MAX_UDP_SOCKS];
61 static int udp_sockets = 0;
62 static bool udp_initialized = false;
63 static unsigned long fce_ev_enabled =
64 (1 << FCE_FILE_MODIFY) |
65 (1 << FCE_FILE_DELETE) |
66 (1 << FCE_DIR_DELETE) |
67 (1 << FCE_FILE_CREATE) |
68 (1 << FCE_DIR_CREATE);
71 static char iobuf[MAXIOBUF];
72 static const char *skip_files[] =
77 static struct fce_close_event last_close_event;
79 static char *fce_event_names[] = {
90 * Initialize network structs for any listeners
91 * We dont give return code because all errors are handled internally (I hope..)
97 struct addrinfo hints, *servinfo, *p;
99 if (udp_initialized == true)
102 memset(&hints, 0, sizeof hints);
103 hints.ai_family = AF_UNSPEC;
104 hints.ai_socktype = SOCK_DGRAM;
106 for (int i = 0; i < udp_sockets; i++) {
107 struct udp_entry *udp_entry = udp_socket_list + i;
109 /* Close any pending sockets */
110 if (udp_entry->sock != -1)
111 close(udp_entry->sock);
113 if ((rv = getaddrinfo(udp_entry->addr, udp_entry->port, &hints, &servinfo)) != 0) {
114 LOG(log_error, logtype_fce, "fce_init_udp: getaddrinfo(%s:%s): %s",
115 udp_entry->addr, udp_entry->port, gai_strerror(rv));
119 /* loop through all the results and make a socket */
120 for (p = servinfo; p != NULL; p = p->ai_next) {
121 if ((udp_entry->sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
122 LOG(log_error, logtype_fce, "fce_init_udp: socket(%s:%s): %s",
123 udp_entry->addr, udp_entry->port, strerror(errno));
130 LOG(log_error, logtype_fce, "fce_init_udp: no socket for %s:%s",
131 udp_entry->addr, udp_entry->port);
133 udp_entry->addrinfo = *p;
134 memcpy(&udp_entry->addrinfo, p, sizeof(struct addrinfo));
135 memcpy(&udp_entry->sockaddr, p->ai_addr, sizeof(struct sockaddr_storage));
136 freeaddrinfo(servinfo);
139 udp_initialized = true;
144 if (udp_initialized == false )
147 for (int i = 0; i < udp_sockets; i++)
149 struct udp_entry *udp_entry = udp_socket_list + i;
151 /* Close any pending sockets */
152 if (udp_entry->sock != -1)
154 close( udp_entry->sock );
155 udp_entry->sock = -1;
158 udp_initialized = false;
162 * Construct a UDP packet for our listeners and return packet size
164 static ssize_t build_fce_packet( struct fce_packet *packet, const char *path, int event, uint32_t event_id )
167 ssize_t data_len = 0;
170 /* Set content of packet */
171 memcpy(packet->magic, FCE_PACKET_MAGIC, sizeof(packet->magic) );
172 packet->version = FCE_PACKET_VERSION;
173 packet->mode = event;
175 packet->event_id = event_id;
177 pathlen = strlen(path); /* exclude string terminator */
179 /* This should never happen, but before we bust this server, we send nonsense, fce listener has to cope */
180 if (pathlen >= MAXPATHLEN)
181 pathlen = MAXPATHLEN - 1;
183 packet->datalen = pathlen;
185 /* This is the payload len. Means: the packet has len bytes more until packet is finished */
186 data_len = FCE_PACKET_HEADER_SIZE + pathlen;
188 memcpy(packet->data, path, pathlen);
190 /* return the packet len */
195 * Handle Endianess and write into buffer w/o padding
197 static void pack_fce_packet(struct fce_packet *packet, unsigned char *buf, int maxlen)
199 unsigned char *p = buf;
201 memcpy(p, &packet->magic[0], sizeof(packet->magic));
202 p += sizeof(packet->magic);
204 *p = packet->version;
210 uint32_t *id = (uint32_t*)p;
211 *id = htonl(packet->event_id);
212 p += sizeof(packet->event_id);
214 uint16_t *l = ( uint16_t *)p;
215 *l = htons(packet->datalen);
216 p += sizeof(packet->datalen);
218 if (((p - buf) + packet->datalen) < maxlen) {
219 memcpy(p, &packet->data[0], packet->datalen);
224 * Send the fce information to all (connected) listeners
225 * We dont give return code because all errors are handled internally (I hope..)
227 static void send_fce_event(const char *path, int event)
229 static bool first_event = true;
231 struct fce_packet packet;
232 void *data = &packet;
233 static uint32_t event_id = 0; /* the unique packet couter to detect packet/data loss. Going from 0xFFFFFFFF to 0x0 is a valid increment */
234 time_t now = time(NULL);
236 LOG(log_debug, logtype_fce, "send_fce_event: start");
239 if (first_event == true) {
242 /* Notify listeners the we start from the beginning */
243 send_fce_event( "", FCE_CONN_START );
246 /* build our data packet */
247 ssize_t data_len = build_fce_packet( &packet, path, event, ++event_id );
248 pack_fce_packet(&packet, iobuf, MAXIOBUF);
250 for (int i = 0; i < udp_sockets; i++)
253 struct udp_entry *udp_entry = udp_socket_list + i;
255 /* we had a problem earlier ? */
256 if (udp_entry->sock == -1)
258 /* We still have to wait ?*/
259 if (now < udp_entry->next_try_on_error)
263 udp_entry->sock = socket(udp_entry->addrinfo.ai_family,
264 udp_entry->addrinfo.ai_socktype,
265 udp_entry->addrinfo.ai_protocol);
267 if (udp_entry->sock == -1) {
268 /* failed again, so go to rest again */
269 LOG(log_error, logtype_fce, "Cannot recreate socket for fce UDP connection: errno %d", errno );
271 udp_entry->next_try_on_error = now + FCE_SOCKET_RETRY_DELAY_S;
275 udp_entry->next_try_on_error = 0;
277 /* Okay, we have a running socket again, send server that we had a problem on our side*/
278 data_len = build_fce_packet( &packet, "", FCE_CONN_BROKEN, 0 );
279 pack_fce_packet(&packet, iobuf, MAXIOBUF);
281 sendto(udp_entry->sock,
285 (struct sockaddr *)&udp_entry->sockaddr,
286 udp_entry->addrinfo.ai_addrlen);
288 /* Rebuild our original data packet */
289 data_len = build_fce_packet(&packet, path, event, event_id);
290 pack_fce_packet(&packet, iobuf, MAXIOBUF);
293 sent_data = sendto(udp_entry->sock,
297 (struct sockaddr *)&udp_entry->sockaddr,
298 udp_entry->addrinfo.ai_addrlen);
301 if (sent_data != data_len) {
302 /* Argh, socket broke, we close and retry later */
303 LOG(log_error, logtype_fce, "send_fce_event: error sending packet to %s:%s, transfered %d of %d: %s",
304 udp_entry->addr, udp_entry->port, sent_data, data_len, strerror(errno));
306 close( udp_entry->sock );
307 udp_entry->sock = -1;
308 udp_entry->next_try_on_error = now + FCE_SOCKET_RETRY_DELAY_S;
313 static int add_udp_socket(const char *target_ip, const char *target_port )
315 if (target_port == NULL)
316 target_port = FCE_DEFAULT_PORT_STRING;
318 if (udp_sockets >= FCE_MAX_UDP_SOCKS) {
319 LOG(log_error, logtype_fce, "Too many file change api UDP connections (max %d allowed)", FCE_MAX_UDP_SOCKS );
323 udp_socket_list[udp_sockets].addr = strdup(target_ip);
324 udp_socket_list[udp_sockets].port = strdup(target_port);
325 udp_socket_list[udp_sockets].sock = -1;
326 memset(&udp_socket_list[udp_sockets].addrinfo, 0, sizeof(struct addrinfo));
327 memset(&udp_socket_list[udp_sockets].sockaddr, 0, sizeof(struct sockaddr_storage));
328 udp_socket_list[udp_sockets].next_try_on_error = 0;
335 static void save_close_event(const char *path)
337 time_t now = time(NULL);
339 /* Check if it's a close for the same event as the last one */
340 if (last_close_event.time /* is there any saved event ? */
341 && (strcmp(path, last_close_event.path) != 0)) {
342 /* no, so send the saved event out now */
343 send_fce_event(last_close_event.path, FCE_FILE_MODIFY);
346 LOG(log_debug, logtype_fce, "save_close_event: %s", path);
348 last_close_event.time = now;
349 strncpy(last_close_event.path, path, MAXPATHLEN);
354 * Dispatcher for all incoming file change events
357 int fce_register(fce_ev_t event, const char *path, const char *oldpath, fce_obj_t type)
359 static bool first_event = true;
362 if (!(fce_ev_enabled & (1 << event)))
365 AFP_ASSERT(event >= FCE_FIRST_EVENT && event <= FCE_LAST_EVENT);
367 LOG(log_debug, logtype_fce, "register_fce(path: %s, type: %s, event: %s",
368 path , type == fce_dir ? "dir" : "file", fce_event_names[event]);
370 bname = basename_safe(path);
372 if (udp_sockets == 0)
373 /* No listeners configured */
379 /* do some initialization on the fly the first time */
381 fce_initialize_history();
385 /* handle files which should not cause events (.DS_Store atc. ) */
386 for (int i = 0; skip_files[i] != NULL; i++) {
387 if (strcmp(bname, skip_files[i]) == 0)
391 /* Can we ignore this event based on type or history? */
392 if (fce_handle_coalescation(event, path, type)) {
393 LOG(log_debug9, logtype_fce, "Coalesced fc event <%d> for <%s>", event, path);
398 case FCE_FILE_MODIFY:
399 save_close_event(path);
402 send_fce_event(path, event);
409 static void check_saved_close_events(int fmodwait)
411 time_t now = time(NULL);
413 /* check if configured holdclose time has passed */
414 if (last_close_event.time && ((last_close_event.time + fmodwait) < now)) {
415 LOG(log_debug, logtype_fce, "check_saved_close_events: sending event: %s", last_close_event.path);
416 /* yes, send event */
417 send_fce_event(&last_close_event.path[0], FCE_FILE_MODIFY);
418 last_close_event.path[0] = 0;
419 last_close_event.time = 0;
423 /******************** External calls start here **************************/
426 * API-Calls for file change api, called form outside (file.c directory.c ofork.c filedir.c)
428 void fce_pending_events(AFPObj *obj)
430 check_saved_close_events(obj->options.fce_fmodwait);
435 * Extern connect to afpd parameter, can be called multiple times for multiple listeners (up to MAX_UDP_SOCKS times)
438 int fce_add_udp_socket(const char *target)
440 const char *port = FCE_DEFAULT_PORT_STRING;
441 char target_ip[256] = {""};
443 strncpy(target_ip, target, sizeof(target_ip) -1);
445 char *port_delim = strchr( target_ip, ':' );
448 port = port_delim + 1;
450 return add_udp_socket(target_ip, port);
453 int fce_set_events(const char *events)
465 for (p = strtok(e, ", "); p; p = strtok(NULL, ", ")) {
466 if (strcmp(p, "fmod") == 0) {
467 fce_ev_enabled |= (1 << FCE_FILE_MODIFY);
468 } else if (strcmp(p, "fdel") == 0) {
469 fce_ev_enabled |= (1 << FCE_FILE_DELETE);
470 } else if (strcmp(p, "ddel") == 0) {
471 fce_ev_enabled |= (1 << FCE_DIR_DELETE);
472 } else if (strcmp(p, "fcre") == 0) {
473 fce_ev_enabled |= (1 << FCE_FILE_CREATE);
474 } else if (strcmp(p, "dcre") == 0) {
475 fce_ev_enabled |= (1 << FCE_DIR_CREATE);
487 void shortsleep( unsigned int us )
491 int main( int argc, char*argv[] )
495 char *port = FCE_DEFAULT_PORT_STRING;
496 char *host = "localhost";
497 int delay_between_events = 1000;
498 int event_code = FCE_FILE_MODIFY;
500 int duration_in_seconds = 0; // TILL ETERNITY
502 char *path = getcwd( pathbuff, sizeof(pathbuff) );
504 // FULLSPEED TEST IS "-s 1001" -> delay is 0 -> send packets without pause
506 while ((c = getopt(argc, argv, "d:e:h:p:P:s:")) != -1) {
509 fprintf(stdout, "%s: [ -p Port -h Listener1 [ -h Listener2 ...] -P path -s Delay_between_events_in_us -e event_code -d Duration ]\n", argv[0]);
513 duration_in_seconds = atoi(optarg);
516 event_code = atoi(optarg);
519 host = strdup(optarg);
522 port = strdup(optarg);
525 path = strdup(optarg);
528 delay_between_events = atoi(optarg);
533 sprintf(target, "%s:%s", host, port);
534 if (fce_add_udp_socket(target) != 0)
538 time_t start_time = time(NULL);
541 if (duration_in_seconds)
542 end_time = start_time + duration_in_seconds;
546 time_t now = time(NULL);
547 if (now > start_time)
550 fprintf( stdout, "%d events/s\n", ev_cnt );
553 if (end_time && now >= end_time)
556 fce_register(event_code, path, NULL, 0);
560 shortsleep( delay_between_events );