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 unsigned 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;
169 /* Set content of packet */
170 memcpy(packet->magic, FCE_PACKET_MAGIC, sizeof(packet->magic) );
171 packet->version = FCE_PACKET_VERSION;
172 packet->mode = event;
174 packet->event_id = event_id;
176 pathlen = strlen(path); /* exclude string terminator */
178 /* This should never happen, but before we bust this server, we send nonsense, fce listener has to cope */
179 if (pathlen >= MAXPATHLEN)
180 pathlen = MAXPATHLEN - 1;
182 packet->datalen = pathlen;
184 /* This is the payload len. Means: the packet has len bytes more until packet is finished */
185 data_len = FCE_PACKET_HEADER_SIZE + pathlen;
187 memcpy(packet->data, path, pathlen);
189 /* return the packet len */
194 * Handle Endianess and write into buffer w/o padding
196 static void pack_fce_packet(struct fce_packet *packet, unsigned char *buf, int maxlen)
198 unsigned char *p = buf;
200 memcpy(p, &packet->magic[0], sizeof(packet->magic));
201 p += sizeof(packet->magic);
203 *p = packet->version;
209 uint32_t *id = (uint32_t*)p;
210 *id = htonl(packet->event_id);
211 p += sizeof(packet->event_id);
213 uint16_t *l = ( uint16_t *)p;
214 *l = htons(packet->datalen);
215 p += sizeof(packet->datalen);
217 if (((p - buf) + packet->datalen) < maxlen) {
218 memcpy(p, &packet->data[0], packet->datalen);
223 * Send the fce information to all (connected) listeners
224 * We dont give return code because all errors are handled internally (I hope..)
226 static void send_fce_event(const char *path, int event)
228 static bool first_event = true;
230 struct fce_packet packet;
231 static uint32_t event_id = 0; /* the unique packet couter to detect packet/data loss. Going from 0xFFFFFFFF to 0x0 is a valid increment */
232 time_t now = time(NULL);
234 LOG(log_debug, logtype_fce, "send_fce_event: start");
237 if (first_event == true) {
240 /* Notify listeners the we start from the beginning */
241 send_fce_event( "", FCE_CONN_START );
244 /* build our data packet */
245 ssize_t data_len = build_fce_packet( &packet, path, event, ++event_id );
246 pack_fce_packet(&packet, iobuf, MAXIOBUF);
248 for (int i = 0; i < udp_sockets; i++)
251 struct udp_entry *udp_entry = udp_socket_list + i;
253 /* we had a problem earlier ? */
254 if (udp_entry->sock == -1)
256 /* We still have to wait ?*/
257 if (now < udp_entry->next_try_on_error)
261 udp_entry->sock = socket(udp_entry->addrinfo.ai_family,
262 udp_entry->addrinfo.ai_socktype,
263 udp_entry->addrinfo.ai_protocol);
265 if (udp_entry->sock == -1) {
266 /* failed again, so go to rest again */
267 LOG(log_error, logtype_fce, "Cannot recreate socket for fce UDP connection: errno %d", errno );
269 udp_entry->next_try_on_error = now + FCE_SOCKET_RETRY_DELAY_S;
273 udp_entry->next_try_on_error = 0;
275 /* Okay, we have a running socket again, send server that we had a problem on our side*/
276 data_len = build_fce_packet( &packet, "", FCE_CONN_BROKEN, 0 );
277 pack_fce_packet(&packet, iobuf, MAXIOBUF);
279 sendto(udp_entry->sock,
283 (struct sockaddr *)&udp_entry->sockaddr,
284 udp_entry->addrinfo.ai_addrlen);
286 /* Rebuild our original data packet */
287 data_len = build_fce_packet(&packet, path, event, event_id);
288 pack_fce_packet(&packet, iobuf, MAXIOBUF);
291 sent_data = sendto(udp_entry->sock,
295 (struct sockaddr *)&udp_entry->sockaddr,
296 udp_entry->addrinfo.ai_addrlen);
299 if (sent_data != data_len) {
300 /* Argh, socket broke, we close and retry later */
301 LOG(log_error, logtype_fce, "send_fce_event: error sending packet to %s:%s, transfered %d of %d: %s",
302 udp_entry->addr, udp_entry->port, sent_data, data_len, strerror(errno));
304 close( udp_entry->sock );
305 udp_entry->sock = -1;
306 udp_entry->next_try_on_error = now + FCE_SOCKET_RETRY_DELAY_S;
311 static int add_udp_socket(const char *target_ip, const char *target_port )
313 if (target_port == NULL)
314 target_port = FCE_DEFAULT_PORT_STRING;
316 if (udp_sockets >= FCE_MAX_UDP_SOCKS) {
317 LOG(log_error, logtype_fce, "Too many file change api UDP connections (max %d allowed)", FCE_MAX_UDP_SOCKS );
321 udp_socket_list[udp_sockets].addr = strdup(target_ip);
322 udp_socket_list[udp_sockets].port = strdup(target_port);
323 udp_socket_list[udp_sockets].sock = -1;
324 memset(&udp_socket_list[udp_sockets].addrinfo, 0, sizeof(struct addrinfo));
325 memset(&udp_socket_list[udp_sockets].sockaddr, 0, sizeof(struct sockaddr_storage));
326 udp_socket_list[udp_sockets].next_try_on_error = 0;
333 static void save_close_event(const char *path)
335 time_t now = time(NULL);
337 /* Check if it's a close for the same event as the last one */
338 if (last_close_event.time /* is there any saved event ? */
339 && (strcmp(path, last_close_event.path) != 0)) {
340 /* no, so send the saved event out now */
341 send_fce_event(last_close_event.path, FCE_FILE_MODIFY);
344 LOG(log_debug, logtype_fce, "save_close_event: %s", path);
346 last_close_event.time = now;
347 strncpy(last_close_event.path, path, MAXPATHLEN);
352 * Dispatcher for all incoming file change events
355 int fce_register(fce_ev_t event, const char *path, const char *oldpath, fce_obj_t type)
357 static bool first_event = true;
360 if (!(fce_ev_enabled & (1 << event)))
363 AFP_ASSERT(event >= FCE_FIRST_EVENT && event <= FCE_LAST_EVENT);
365 LOG(log_debug, logtype_fce, "register_fce(path: %s, type: %s, event: %s",
366 path , type == fce_dir ? "dir" : "file", fce_event_names[event]);
368 bname = basename_safe(path);
370 if (udp_sockets == 0)
371 /* No listeners configured */
377 /* do some initialization on the fly the first time */
379 fce_initialize_history();
383 /* handle files which should not cause events (.DS_Store atc. ) */
384 for (int i = 0; skip_files[i] != NULL; i++) {
385 if (strcmp(bname, skip_files[i]) == 0)
389 /* Can we ignore this event based on type or history? */
390 if (fce_handle_coalescation(event, path, type)) {
391 LOG(log_debug9, logtype_fce, "Coalesced fc event <%d> for <%s>", event, path);
396 case FCE_FILE_MODIFY:
397 save_close_event(path);
400 send_fce_event(path, event);
407 static void check_saved_close_events(int fmodwait)
409 time_t now = time(NULL);
411 /* check if configured holdclose time has passed */
412 if (last_close_event.time && ((last_close_event.time + fmodwait) < now)) {
413 LOG(log_debug, logtype_fce, "check_saved_close_events: sending event: %s", last_close_event.path);
414 /* yes, send event */
415 send_fce_event(&last_close_event.path[0], FCE_FILE_MODIFY);
416 last_close_event.path[0] = 0;
417 last_close_event.time = 0;
421 /******************** External calls start here **************************/
424 * API-Calls for file change api, called form outside (file.c directory.c ofork.c filedir.c)
426 void fce_pending_events(AFPObj *obj)
428 check_saved_close_events(obj->options.fce_fmodwait);
433 * Extern connect to afpd parameter, can be called multiple times for multiple listeners (up to MAX_UDP_SOCKS times)
436 int fce_add_udp_socket(const char *target)
438 const char *port = FCE_DEFAULT_PORT_STRING;
439 char target_ip[256] = {""};
441 strncpy(target_ip, target, sizeof(target_ip) -1);
443 char *port_delim = strchr( target_ip, ':' );
446 port = port_delim + 1;
448 return add_udp_socket(target_ip, port);
451 int fce_set_events(const char *events)
463 for (p = strtok(e, ", "); p; p = strtok(NULL, ", ")) {
464 if (strcmp(p, "fmod") == 0) {
465 fce_ev_enabled |= (1 << FCE_FILE_MODIFY);
466 } else if (strcmp(p, "fdel") == 0) {
467 fce_ev_enabled |= (1 << FCE_FILE_DELETE);
468 } else if (strcmp(p, "ddel") == 0) {
469 fce_ev_enabled |= (1 << FCE_DIR_DELETE);
470 } else if (strcmp(p, "fcre") == 0) {
471 fce_ev_enabled |= (1 << FCE_FILE_CREATE);
472 } else if (strcmp(p, "dcre") == 0) {
473 fce_ev_enabled |= (1 << FCE_DIR_CREATE);
485 void shortsleep( unsigned int us )
489 int main( int argc, char*argv[] )
493 char *port = FCE_DEFAULT_PORT_STRING;
494 char *host = "localhost";
495 int delay_between_events = 1000;
496 int event_code = FCE_FILE_MODIFY;
498 int duration_in_seconds = 0; // TILL ETERNITY
500 char *path = getcwd( pathbuff, sizeof(pathbuff) );
502 // FULLSPEED TEST IS "-s 1001" -> delay is 0 -> send packets without pause
504 while ((c = getopt(argc, argv, "d:e:h:p:P:s:")) != -1) {
507 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]);
511 duration_in_seconds = atoi(optarg);
514 event_code = atoi(optarg);
517 host = strdup(optarg);
520 port = strdup(optarg);
523 path = strdup(optarg);
526 delay_between_events = atoi(optarg);
531 sprintf(target, "%s:%s", host, port);
532 if (fce_add_udp_socket(target) != 0)
536 time_t start_time = time(NULL);
539 if (duration_in_seconds)
540 end_time = start_time + duration_in_seconds;
544 time_t now = time(NULL);
545 if (now > start_time)
548 fprintf( stdout, "%d events/s\n", ev_cnt );
551 if (end_time && now >= end_time)
554 fce_register(event_code, path, NULL, 0);
558 shortsleep( delay_between_events );