2 * Copyright (c) 2010 Mark Williams
4 * File change event API for netatalk
6 * for every detected filesystem change a UDP packet is sent to an arbitrary list
7 * of listeners. Each packet contains unix path of modified filesystem element,
8 * event reason, and a consecutive event id (32 bit). Technically we are UDP client and are sending
9 * out packets synchronuosly as they are created by the afp functions. This should not affect
10 * performance measurably. The only delaying calls occur during initialization, if we have to
11 * resolve non-IP hostnames to IP. All numeric data inside the packet is network byte order, so use
12 * ntohs / ntohl to resolve length and event id. Ideally a listener receives every packet with
13 * no gaps in event ids, starting with event id 1 and mode FCE_CONN_START followed by
14 * data events from id 2 up to 0xFFFFFFFF, followed by 0 to 0xFFFFFFFF and so on.
16 * A gap or not starting with 1 mode FCE_CONN_START or receiving mode FCE_CONN_BROKEN means that
17 * the listener has lost at least one filesystem event
19 * All Rights Reserved. See COPYRIGHT.
24 #endif /* HAVE_CONFIG_H */
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #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"
62 /* We store our connection data here */
63 static struct udp_entry udp_socket_list[FCE_MAX_UDP_SOCKS];
64 static int udp_sockets = 0;
65 static int udp_initialized = FCE_FALSE;
66 static unsigned long fce_ev_enabled =
67 (1 << FCE_FILE_MODIFY) |
68 (1 << FCE_FILE_DELETE) |
69 (1 << FCE_DIR_DELETE) |
70 (1 << FCE_FILE_CREATE) |
71 (1 << FCE_DIR_CREATE);
73 static uint64_t tm_used; /* used for passing to event handler */
75 static char iobuf[MAXIOBUF];
76 static const char *skip_files[] =
81 static struct fce_close_event last_close_event;
85 * Initialize network structs for any listeners
86 * We dont give return code because all errors are handled internally (I hope..)
92 struct addrinfo hints, *servinfo, *p;
94 if (udp_initialized == FCE_TRUE)
97 memset(&hints, 0, sizeof hints);
98 hints.ai_family = AF_UNSPEC;
99 hints.ai_socktype = SOCK_DGRAM;
101 for (int i = 0; i < udp_sockets; i++) {
102 struct udp_entry *udp_entry = udp_socket_list + i;
104 /* Close any pending sockets */
105 if (udp_entry->sock != -1)
106 close(udp_entry->sock);
108 if ((rv = getaddrinfo(udp_entry->addr, udp_entry->port, &hints, &servinfo)) != 0) {
109 LOG(log_error, logtype_afpd, "fce_init_udp: getaddrinfo(%s:%s): %s",
110 udp_entry->addr, udp_entry->port, gai_strerror(rv));
114 /* loop through all the results and make a socket */
115 for (p = servinfo; p != NULL; p = p->ai_next) {
116 if ((udp_entry->sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
117 LOG(log_error, logtype_afpd, "fce_init_udp: socket(%s:%s): %s",
118 udp_entry->addr, udp_entry->port, strerror(errno));
125 LOG(log_error, logtype_afpd, "fce_init_udp: no socket for %s:%s",
126 udp_entry->addr, udp_entry->port);
128 udp_entry->addrinfo = *p;
129 memcpy(&udp_entry->addrinfo, p, sizeof(struct addrinfo));
130 memcpy(&udp_entry->sockaddr, p->ai_addr, sizeof(struct sockaddr_storage));
131 freeaddrinfo(servinfo);
134 udp_initialized = FCE_TRUE;
139 if (udp_initialized == FCE_FALSE )
142 for (int i = 0; i < udp_sockets; i++)
144 struct udp_entry *udp_entry = udp_socket_list + i;
146 /* Close any pending sockets */
147 if (udp_entry->sock != -1)
149 close( udp_entry->sock );
150 udp_entry->sock = -1;
153 udp_initialized = FCE_FALSE;
157 * Construct a UDP packet for our listeners and return packet size
159 static ssize_t build_fce_packet( struct fce_packet *packet, char *path, int mode, uint32_t event_id )
162 ssize_t data_len = 0;
165 /* Set content of packet */
166 memcpy(packet->magic, FCE_PACKET_MAGIC, sizeof(packet->magic) );
167 packet->version = FCE_PACKET_VERSION;
170 packet->event_id = event_id;
172 pathlen = strlen(path); /* exclude string terminator */
174 /* This should never happen, but before we bust this server, we send nonsense, fce listener has to cope */
175 if (pathlen >= MAXPATHLEN)
176 pathlen = MAXPATHLEN - 1;
178 packet->datalen = pathlen;
180 /* This is the payload len. Means: the packet has len bytes more until packet is finished */
181 data_len = FCE_PACKET_HEADER_SIZE + pathlen;
185 t = (uint64_t *)packet->data;
186 *t = hton64(tm_used);
187 memcpy(packet->data + sizeof(tm_used), path, pathlen);
189 packet->datalen = pathlen + sizeof(tm_used);
190 data_len += sizeof(tm_used);
193 memcpy(packet->data, path, pathlen);
197 /* return the packet len */
202 * Handle Endianess and write into buffer w/o padding
204 static void pack_fce_packet(struct fce_packet *packet, unsigned char *buf, int maxlen)
206 unsigned char *p = buf;
208 memcpy(p, &packet->magic[0], sizeof(packet->magic));
209 p += sizeof(packet->magic);
211 *p = packet->version;
217 uint32_t *id = (uint32_t*)p;
218 *id = htonl(packet->event_id);
219 p += sizeof(packet->event_id);
221 uint16_t *l = ( uint16_t *)p;
222 *l = htons(packet->datalen);
223 p += sizeof(packet->datalen);
225 if (((p - buf) + packet->datalen) < maxlen) {
226 memcpy(p, &packet->data[0], packet->datalen);
231 * Send the fce information to all (connected) listeners
232 * We dont give return code because all errors are handled internally (I hope..)
234 static void send_fce_event( char *path, int mode )
236 static int first_event = FCE_TRUE;
238 struct fce_packet packet;
239 void *data = &packet;
240 static uint32_t event_id = 0; /* the unique packet couter to detect packet/data loss. Going from 0xFFFFFFFF to 0x0 is a valid increment */
241 time_t now = time(NULL);
243 LOG(log_debug, logtype_afpd, "send_fce_event: start");
246 if (first_event == FCE_TRUE) {
247 first_event = FCE_FALSE;
249 /* Notify listeners the we start from the beginning */
250 send_fce_event( "", FCE_CONN_START );
253 /* build our data packet */
254 ssize_t data_len = build_fce_packet( &packet, path, mode, ++event_id );
255 pack_fce_packet(&packet, iobuf, MAXIOBUF);
257 for (int i = 0; i < udp_sockets; i++)
260 struct udp_entry *udp_entry = udp_socket_list + i;
262 /* we had a problem earlier ? */
263 if (udp_entry->sock == -1)
265 /* We still have to wait ?*/
266 if (now < udp_entry->next_try_on_error)
270 udp_entry->sock = socket(udp_entry->addrinfo.ai_family,
271 udp_entry->addrinfo.ai_socktype,
272 udp_entry->addrinfo.ai_protocol);
274 if (udp_entry->sock == -1) {
275 /* failed again, so go to rest again */
276 LOG(log_error, logtype_afpd, "Cannot recreate socket for fce UDP connection: errno %d", errno );
278 udp_entry->next_try_on_error = now + FCE_SOCKET_RETRY_DELAY_S;
282 udp_entry->next_try_on_error = 0;
284 /* Okay, we have a running socket again, send server that we had a problem on our side*/
285 data_len = build_fce_packet( &packet, "", FCE_CONN_BROKEN, 0 );
286 pack_fce_packet(&packet, iobuf, MAXIOBUF);
288 sendto(udp_entry->sock,
292 (struct sockaddr *)&udp_entry->sockaddr,
293 udp_entry->addrinfo.ai_addrlen);
295 /* Rebuild our original data packet */
296 data_len = build_fce_packet( &packet, path, mode, event_id );
297 pack_fce_packet(&packet, iobuf, MAXIOBUF);
300 sent_data = sendto(udp_entry->sock,
304 (struct sockaddr *)&udp_entry->sockaddr,
305 udp_entry->addrinfo.ai_addrlen);
308 if (sent_data != data_len) {
309 /* Argh, socket broke, we close and retry later */
310 LOG(log_error, logtype_afpd, "send_fce_event: error sending packet to %s:%s, transfered %d of %d: %s",
311 udp_entry->addr, udp_entry->port, sent_data, data_len, strerror(errno));
313 close( udp_entry->sock );
314 udp_entry->sock = -1;
315 udp_entry->next_try_on_error = now + FCE_SOCKET_RETRY_DELAY_S;
320 static int add_udp_socket(const char *target_ip, const char *target_port )
322 if (target_port == NULL)
323 target_port = FCE_DEFAULT_PORT_STRING;
325 if (udp_sockets >= FCE_MAX_UDP_SOCKS) {
326 LOG(log_error, logtype_afpd, "Too many file change api UDP connections (max %d allowed)", FCE_MAX_UDP_SOCKS );
330 udp_socket_list[udp_sockets].addr = strdup(target_ip);
331 udp_socket_list[udp_sockets].port = strdup(target_port);
332 udp_socket_list[udp_sockets].sock = -1;
333 memset(&udp_socket_list[udp_sockets].addrinfo, 0, sizeof(struct addrinfo));
334 memset(&udp_socket_list[udp_sockets].sockaddr, 0, sizeof(struct sockaddr_storage));
335 udp_socket_list[udp_sockets].next_try_on_error = 0;
342 static void save_close_event(const char *path)
344 time_t now = time(NULL);
346 /* Check if it's a close for the same event as the last one */
347 if (last_close_event.time /* is there any saved event ? */
348 && (strcmp(path, last_close_event.path) != 0)) {
349 /* no, so send the saved event out now */
350 send_fce_event(last_close_event.path, FCE_FILE_MODIFY);
353 LOG(log_debug, logtype_afpd, "save_close_event: %s", path);
355 last_close_event.time = now;
356 strncpy(last_close_event.path, path, MAXPATHLEN);
361 * Dispatcher for all incoming file change events
364 static int register_fce(const char *u_name, int is_dir, int mode)
366 static int first_event = FCE_TRUE;
368 if (udp_sockets == 0)
369 /* No listeners configured */
375 /* do some initialization on the fly the first time */
377 fce_initialize_history();
378 first_event = FCE_FALSE;
381 /* handle files which should not cause events (.DS_Store atc. ) */
382 for (int i = 0; skip_files[i] != NULL; i++)
384 if (!strcmp( u_name, skip_files[i]))
389 char full_path_buffer[MAXPATHLEN + 1] = {""};
390 const char *cwd = getcwdpath();
392 if (mode == FCE_TM_SIZE) {
393 strlcpy(full_path_buffer, u_name, MAXPATHLEN);
394 } else if (!is_dir || mode == FCE_DIR_DELETE) {
395 if (strlen( cwd ) + strlen( u_name) + 1 >= MAXPATHLEN) {
396 LOG(log_error, logtype_afpd, "FCE file name too long: %s/%s", cwd, u_name );
399 sprintf( full_path_buffer, "%s/%s", cwd, u_name );
401 if (strlen( cwd ) >= MAXPATHLEN) {
402 LOG(log_error, logtype_afpd, "FCE directory name too long: %s", cwd);
405 strcpy( full_path_buffer, cwd);
408 /* Can we ignore this event based on type or history? */
409 if (!(mode & FCE_TM_SIZE) && fce_handle_coalescation( full_path_buffer, is_dir, mode ))
411 LOG(log_debug9, logtype_afpd, "Coalesced fc event <%d> for <%s>", mode, full_path_buffer );
415 LOG(log_debug9, logtype_afpd, "Detected fc event <%d> for <%s>", mode, full_path_buffer );
417 if (mode & FCE_FILE_MODIFY) {
418 save_close_event(full_path_buffer);
422 send_fce_event( full_path_buffer, mode );
427 static void check_saved_close_events(int fmodwait)
429 time_t now = time(NULL);
431 /* check if configured holdclose time has passed */
432 if (last_close_event.time && ((last_close_event.time + fmodwait) < now)) {
433 LOG(log_debug, logtype_afpd, "check_saved_close_events: sending event: %s", last_close_event.path);
434 /* yes, send event */
435 send_fce_event(&last_close_event.path[0], FCE_FILE_MODIFY);
436 last_close_event.path[0] = 0;
437 last_close_event.time = 0;
441 /******************** External calls start here **************************/
444 * API-Calls for file change api, called form outside (file.c directory.c ofork.c filedir.c)
446 #ifndef FCE_TEST_MAIN
448 void fce_pending_events(AFPObj *obj)
451 check_saved_close_events(obj->options.fce_fmodwait);
454 int fce_register_delete_file( struct path *path )
461 if (!(fce_ev_enabled & (1 << FCE_FILE_DELETE)))
464 ret = register_fce( path->u_name, false, FCE_FILE_DELETE );
468 int fce_register_delete_dir( char *name )
475 if (!(fce_ev_enabled & (1 << FCE_DIR_DELETE)))
478 ret = register_fce( name, true, FCE_DIR_DELETE);
483 int fce_register_new_dir( struct path *path )
490 if (!(fce_ev_enabled & (1 << FCE_DIR_CREATE)))
493 ret = register_fce( path->u_name, true, FCE_DIR_CREATE );
499 int fce_register_new_file( struct path *path )
506 if (!(fce_ev_enabled & (1 << FCE_FILE_CREATE)))
509 ret = register_fce( path->u_name, false, FCE_FILE_CREATE );
514 int fce_register_file_modification( struct ofork *ofork )
521 if (!(fce_ev_enabled & (1 << FCE_FILE_MODIFY)))
524 ret = register_fce(of_name(ofork), false, FCE_FILE_MODIFY );
529 int fce_register_tm_size(const char *vol, size_t used)
536 if (!(fce_ev_enabled & (1 << FCE_TM_SIZE)))
539 tm_used = used; /* oh what a hack */
540 ret = register_fce(vol, false, FCE_TM_SIZE);
548 * Extern connect to afpd parameter, can be called multiple times for multiple listeners (up to MAX_UDP_SOCKS times)
551 int fce_add_udp_socket(const char *target)
553 const char *port = FCE_DEFAULT_PORT_STRING;
554 char target_ip[256] = {""};
556 strncpy(target_ip, target, sizeof(target_ip) -1);
558 char *port_delim = strchr( target_ip, ':' );
561 port = port_delim + 1;
563 return add_udp_socket(target_ip, port);
566 int fce_set_events(const char *events)
578 for (p = strtok(e, ", "); p; p = strtok(NULL, ", ")) {
579 if (strcmp(p, "fmod") == 0) {
580 fce_ev_enabled |= (1 << FCE_FILE_MODIFY);
581 } else if (strcmp(p, "fdel") == 0) {
582 fce_ev_enabled |= (1 << FCE_FILE_DELETE);
583 } else if (strcmp(p, "ddel") == 0) {
584 fce_ev_enabled |= (1 << FCE_DIR_DELETE);
585 } else if (strcmp(p, "fcre") == 0) {
586 fce_ev_enabled |= (1 << FCE_FILE_CREATE);
587 } else if (strcmp(p, "dcre") == 0) {
588 fce_ev_enabled |= (1 << FCE_DIR_CREATE);
589 } else if (strcmp(p, "tmsz") == 0) {
590 fce_ev_enabled |= (1 << FCE_TM_SIZE);
602 void shortsleep( unsigned int us )
606 int main( int argc, char*argv[] )
610 char *port = FCE_DEFAULT_PORT_STRING;
611 char *host = "localhost";
612 int delay_between_events = 1000;
613 int event_code = FCE_FILE_MODIFY;
615 int duration_in_seconds = 0; // TILL ETERNITY
617 char *path = getcwd( pathbuff, sizeof(pathbuff) );
619 // FULLSPEED TEST IS "-s 1001" -> delay is 0 -> send packets without pause
621 while ((c = getopt(argc, argv, "d:e:h:p:P:s:")) != -1) {
624 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]);
628 duration_in_seconds = atoi(optarg);
631 event_code = atoi(optarg);
634 host = strdup(optarg);
637 port = strdup(optarg);
640 path = strdup(optarg);
643 delay_between_events = atoi(optarg);
648 sprintf(target, "%s:%s", host, port);
649 if (fce_add_udp_socket(target) != 0)
653 time_t start_time = time(NULL);
656 if (duration_in_seconds)
657 end_time = start_time + duration_in_seconds;
661 time_t now = time(NULL);
662 if (now > start_time)
665 fprintf( stdout, "%d events/s\n", ev_cnt );
668 if (end_time && now >= end_time)
671 register_fce( path, 0, event_code );
675 shortsleep( delay_between_events );