]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/fce_api.c
Merge branch 'develop'
[netatalk.git] / etc / afpd / fce_api.c
1 /*
2  * Copyright (c) 2010 Mark Williams
3  * Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
4  *
5  * File change event API for netatalk
6  *
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.
16  *
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
19  * 
20  * All Rights Reserved.  See COPYRIGHT.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif /* HAVE_CONFIG_H */
26
27 #include <stdio.h>
28
29 #include <string.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <time.h>
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <netdb.h>
38 #include <stdbool.h>
39
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>
49
50 #include "fork.h"
51 #include "file.h"
52 #include "directory.h"
53 #include "desktop.h"
54 #include "volume.h"
55
56 // ONLY USED IN THIS FILE
57 #include "fce_api_internal.h"
58
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);
69
70 #define MAXIOBUF 1024
71 static char iobuf[MAXIOBUF];
72 static const char *skip_files[] = 
73 {
74         ".DS_Store",
75         NULL
76 };
77 static struct fce_close_event last_close_event;
78
79 static char *fce_event_names[] = {
80     "",
81     "FCE_FILE_MODIFY",
82     "FCE_FILE_DELETE",
83     "FCE_DIR_DELETE",
84     "FCE_FILE_CREATE",
85     "FCE_DIR_CREATE"
86 };
87
88 /*
89  *
90  * Initialize network structs for any listeners
91  * We dont give return code because all errors are handled internally (I hope..)
92  *
93  * */
94 void fce_init_udp()
95 {
96     int rv;
97     struct addrinfo hints, *servinfo, *p;
98
99     if (udp_initialized == true)
100         return;
101
102     memset(&hints, 0, sizeof hints);
103     hints.ai_family = AF_UNSPEC;
104     hints.ai_socktype = SOCK_DGRAM;
105
106     for (int i = 0; i < udp_sockets; i++) {
107         struct udp_entry *udp_entry = udp_socket_list + i;
108
109         /* Close any pending sockets */
110         if (udp_entry->sock != -1)
111             close(udp_entry->sock);
112
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));
116             continue;
117         }
118
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));
124                 continue;
125             }
126             break;
127         }
128
129         if (p == NULL) {
130             LOG(log_error, logtype_fce, "fce_init_udp: no socket for %s:%s",
131                 udp_entry->addr, udp_entry->port);
132         }
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);
137     }
138
139     udp_initialized = true;
140 }
141
142 void fce_cleanup()
143 {
144     if (udp_initialized == false )
145         return;
146
147     for (int i = 0; i < udp_sockets; i++)
148     {
149         struct udp_entry *udp_entry = udp_socket_list + i;
150
151         /* Close any pending sockets */
152         if (udp_entry->sock != -1)
153         {
154             close( udp_entry->sock );
155             udp_entry->sock = -1;
156         }
157     }
158     udp_initialized = false;
159 }
160
161 /*
162  * Construct a UDP packet for our listeners and return packet size
163  * */
164 static ssize_t build_fce_packet( struct fce_packet *packet, const char *path, int event, uint32_t event_id )
165 {
166     size_t pathlen = 0;
167     ssize_t data_len = 0;
168     uint64_t *t;
169
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;
174    
175     packet->event_id = event_id; 
176
177     pathlen = strlen(path); /* exclude string terminator */
178     
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;
182
183     packet->datalen = pathlen;
184
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;
187
188     memcpy(packet->data, path, pathlen);
189
190     /* return the packet len */
191     return data_len;
192 }
193
194 /*
195  * Handle Endianess and write into buffer w/o padding
196  **/ 
197 static void pack_fce_packet(struct fce_packet *packet, unsigned char *buf, int maxlen)
198 {
199     unsigned char *p = buf;
200
201     memcpy(p, &packet->magic[0], sizeof(packet->magic));
202     p += sizeof(packet->magic);
203
204     *p = packet->version;
205     p++;
206     
207     *p = packet->mode;
208     p++;
209     
210     uint32_t *id = (uint32_t*)p;
211     *id = htonl(packet->event_id);
212     p += sizeof(packet->event_id);
213
214     uint16_t *l = ( uint16_t *)p;
215     *l = htons(packet->datalen);
216     p += sizeof(packet->datalen);
217
218     if (((p - buf) +  packet->datalen) < maxlen) {
219         memcpy(p, &packet->data[0], packet->datalen);
220     }
221 }
222
223 /*
224  * Send the fce information to all (connected) listeners
225  * We dont give return code because all errors are handled internally (I hope..)
226  * */
227 static void send_fce_event(const char *path, int event)
228 {    
229     static bool first_event = true;
230
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);
235
236     LOG(log_debug, logtype_fce, "send_fce_event: start");
237
238     /* initialized ? */
239     if (first_event == true) {
240         first_event = false;
241         fce_init_udp();
242         /* Notify listeners the we start from the beginning */
243         send_fce_event( "", FCE_CONN_START );
244     }
245
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);
249
250     for (int i = 0; i < udp_sockets; i++)
251     {
252         int sent_data = 0;
253         struct udp_entry *udp_entry = udp_socket_list + i;
254
255         /* we had a problem earlier ? */
256         if (udp_entry->sock == -1)
257         {
258             /* We still have to wait ?*/
259             if (now < udp_entry->next_try_on_error)
260                 continue;
261
262             /* Reopen socket */
263             udp_entry->sock = socket(udp_entry->addrinfo.ai_family,
264                                      udp_entry->addrinfo.ai_socktype,
265                                      udp_entry->addrinfo.ai_protocol);
266             
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  );
270
271                 udp_entry->next_try_on_error = now + FCE_SOCKET_RETRY_DELAY_S;
272                 continue;
273             }
274
275             udp_entry->next_try_on_error = 0;
276
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);
280
281             sendto(udp_entry->sock,
282                    iobuf,
283                    data_len,
284                    0,
285                    (struct sockaddr *)&udp_entry->sockaddr,
286                    udp_entry->addrinfo.ai_addrlen);
287
288             /* Rebuild our original data packet */
289             data_len = build_fce_packet(&packet, path, event, event_id);
290             pack_fce_packet(&packet, iobuf, MAXIOBUF);
291         }
292
293         sent_data = sendto(udp_entry->sock,
294                            iobuf,
295                            data_len,
296                            0,
297                            (struct sockaddr *)&udp_entry->sockaddr,
298                            udp_entry->addrinfo.ai_addrlen);
299
300         /* Problems ? */
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));
305
306             close( udp_entry->sock );
307             udp_entry->sock = -1;
308             udp_entry->next_try_on_error = now + FCE_SOCKET_RETRY_DELAY_S;
309         }
310     }
311 }
312
313 static int add_udp_socket(const char *target_ip, const char *target_port )
314 {
315     if (target_port == NULL)
316         target_port = FCE_DEFAULT_PORT_STRING;
317
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 );
320         return AFPERR_PARAM;
321     }
322
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;
329
330     udp_sockets++;
331
332     return AFP_OK;
333 }
334
335 static void save_close_event(const char *path)
336 {
337     time_t now = time(NULL);
338
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);
344     }
345
346     LOG(log_debug, logtype_fce, "save_close_event: %s", path);
347
348     last_close_event.time = now;
349     strncpy(last_close_event.path, path, MAXPATHLEN);
350 }
351
352 /*
353  *
354  * Dispatcher for all incoming file change events
355  *
356  * */
357 int fce_register(fce_ev_t event, const char *path, const char *oldpath, fce_obj_t type)
358 {
359     static bool first_event = true;
360     const char *bname;
361
362     if (!(fce_ev_enabled & (1 << event)))
363         return AFP_OK;
364
365     AFP_ASSERT(event >= FCE_FIRST_EVENT && event <= FCE_LAST_EVENT);
366
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]);
369
370     bname = basename_safe(path);
371
372     if (udp_sockets == 0)
373         /* No listeners configured */
374         return AFP_OK;
375
376     if (path == NULL)
377         return AFPERR_PARAM;
378
379         /* do some initialization on the fly the first time */
380         if (first_event) {
381                 fce_initialize_history();
382         first_event = false;
383         }
384
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)
388                         return AFP_OK;
389         }
390
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);
394                 return AFP_OK;
395         }
396
397     switch (event) {
398     case FCE_FILE_MODIFY:
399         save_close_event(path);
400         break;
401     default:
402         send_fce_event(path, event);
403         break;
404     }
405
406     return AFP_OK;
407 }
408
409 static void check_saved_close_events(int fmodwait)
410 {
411     time_t now = time(NULL);
412
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;
420     }
421 }
422
423 /******************** External calls start here **************************/
424
425 /*
426  * API-Calls for file change api, called form outside (file.c directory.c ofork.c filedir.c)
427  * */
428 void fce_pending_events(AFPObj *obj)
429 {
430     check_saved_close_events(obj->options.fce_fmodwait);
431 }
432
433 /*
434  *
435  * Extern connect to afpd parameter, can be called multiple times for multiple listeners (up to MAX_UDP_SOCKS times)
436  *
437  * */
438 int fce_add_udp_socket(const char *target)
439 {
440         const char *port = FCE_DEFAULT_PORT_STRING;
441         char target_ip[256] = {""};
442
443         strncpy(target_ip, target, sizeof(target_ip) -1);
444
445         char *port_delim = strchr( target_ip, ':' );
446         if (port_delim) {
447                 *port_delim = 0;
448                 port = port_delim + 1;
449         }
450         return add_udp_socket(target_ip, port);
451 }
452
453 int fce_set_events(const char *events)
454 {
455     char *e;
456     char *p;
457     
458     if (events == NULL)
459         return AFPERR_PARAM;
460
461     e = strdup(events);
462
463     fce_ev_enabled = 0;
464
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);
476         }
477     }
478
479     free(e);
480
481     return AFP_OK;
482 }
483
484 #ifdef FCE_TEST_MAIN
485
486
487 void shortsleep( unsigned int us )
488 {    
489     usleep( us );
490 }
491 int main( int argc, char*argv[] )
492 {
493     int c,ret;
494
495     char *port = FCE_DEFAULT_PORT_STRING;
496     char *host = "localhost";
497     int delay_between_events = 1000;
498     int event_code = FCE_FILE_MODIFY;
499     char pathbuff[1024];
500     int duration_in_seconds = 0; // TILL ETERNITY
501     char target[256];
502     char *path = getcwd( pathbuff, sizeof(pathbuff) );
503
504     // FULLSPEED TEST IS "-s 1001" -> delay is 0 -> send packets without pause
505
506     while ((c = getopt(argc, argv, "d:e:h:p:P:s:")) != -1) {
507         switch(c) {
508         case '?':
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]);
510             exit(1);
511             break;
512         case 'd':
513             duration_in_seconds = atoi(optarg);
514             break;
515         case 'e':
516             event_code = atoi(optarg);
517             break;
518         case 'h':
519             host = strdup(optarg);
520             break;
521         case 'p':
522             port = strdup(optarg);
523             break;
524         case 'P':
525             path = strdup(optarg);
526             break;
527         case 's':
528             delay_between_events = atoi(optarg);
529             break;
530         }
531     }
532
533     sprintf(target, "%s:%s", host, port);
534     if (fce_add_udp_socket(target) != 0)
535         return 1;
536
537     int ev_cnt = 0;
538     time_t start_time = time(NULL);
539     time_t end_time = 0;
540
541     if (duration_in_seconds)
542         end_time = start_time + duration_in_seconds;
543
544     while (1)
545     {
546         time_t now = time(NULL);
547         if (now > start_time)
548         {
549             start_time = now;
550             fprintf( stdout, "%d events/s\n", ev_cnt );
551             ev_cnt = 0;
552         }
553         if (end_time && now >= end_time)
554             break;
555
556         fce_register(event_code, path, NULL, 0);
557         ev_cnt++;
558
559         
560         shortsleep( delay_between_events );
561     }
562 }
563 #endif /* TESTMAIN*/