Ticket #95: avahi-daemon-inotify.diff

File avahi-daemon-inotify.diff, 9.5 KB (added by behanw, 5 years ago)

Add inotify support to avahi-daemon for the services directory

  • (a) /dev/null vs. (b) avahi-0.6.16/avahi-daemon/inotify/inotify-nosys.h

    a b  
     1/* 
     2 * This header is used if <sys/inotify.h> cannot be found. 
     3 * 
     4 * Inode based directory notification for Linux 
     5 * 
     6 * Copyright (C) 2005 John McCutchan 
     7 */ 
     8 
     9#ifndef _LINUX_INOTIFY_H 
     10#define _LINUX_INOTIFY_H 
     11 
     12#include <stdint.h> 
     13#include <sys/syscall.h> 
     14#include <unistd.h> 
     15#include <asm/unistd.h> 
     16 
     17/* 
     18 * struct inotify_event - structure read from the inotify device for each event 
     19 * 
     20 * When you are watching a directory, you will receive the filename for events 
     21 * such as IN_CREATE, IN_DELETE, IN_OPEN, IN_CLOSE, ..., relative to the wd. 
     22 */ 
     23struct inotify_event { 
     24    int     wd;     /* watch descriptor */ 
     25    uint32_t        mask;       /* watch mask */ 
     26    uint32_t        cookie;     /* cookie to synchronize two events */ 
     27    uint32_t        len;        /* length (including nulls) of name */ 
     28    char        name __flexarr; /* stub for possible name */ 
     29}; 
     30 
     31/* the following are legal, implemented events that user-space can watch for */ 
     32#define IN_ACCESS       0x00000001  /* File was accessed */ 
     33#define IN_MODIFY       0x00000002  /* File was modified */ 
     34#define IN_ATTRIB       0x00000004  /* Metadata changed */ 
     35#define IN_CLOSE_WRITE      0x00000008  /* Writtable file was closed */ 
     36#define IN_CLOSE_NOWRITE    0x00000010  /* Unwrittable file closed */ 
     37#define IN_OPEN         0x00000020  /* File was opened */ 
     38#define IN_MOVED_FROM       0x00000040  /* File was moved from X */ 
     39#define IN_MOVED_TO     0x00000080  /* File was moved to Y */ 
     40#define IN_CREATE       0x00000100  /* Subfile was created */ 
     41#define IN_DELETE       0x00000200  /* Subfile was deleted */ 
     42#define IN_DELETE_SELF      0x00000400  /* Self was deleted */ 
     43#define IN_MOVE_SELF        0x00000800  /* Self was moved */ 
     44 
     45/* the following are legal events.  they are sent as needed to any watch */ 
     46#define IN_UNMOUNT      0x00002000  /* Backing fs was unmounted */ 
     47#define IN_Q_OVERFLOW       0x00004000  /* Event queued overflowed */ 
     48#define IN_IGNORED      0x00008000  /* File was ignored */ 
     49 
     50/* helper events */ 
     51#define IN_CLOSE        (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) /* close */ 
     52#define IN_MOVE         (IN_MOVED_FROM | IN_MOVED_TO) /* moves */ 
     53 
     54/* special flags */ 
     55#define IN_ONLYDIR      0x01000000  /* only watch the path if it is a directory */ 
     56#define IN_DONT_FOLLOW      0x02000000  /* don't follow a sym link */ 
     57#define IN_MASK_ADD     0x20000000  /* add to the mask of an already existing watch */ 
     58#define IN_ISDIR        0x40000000  /* event occurred against dir */ 
     59#define IN_ONESHOT      0x80000000  /* only send event once */ 
     60 
     61/* 
     62 * All of the events - we build the list by hand so that we can add flags in 
     63 * the future and not break backward compatibility.  Apps will get only the 
     64 * events that they originally wanted.  Be sure to add new events here! 
     65 */ 
     66#define IN_ALL_EVENTS   (IN_ACCESS | IN_MODIFY | IN_ATTRIB | IN_CLOSE_WRITE | \ 
     67             IN_CLOSE_NOWRITE | IN_OPEN | IN_MOVED_FROM | \ 
     68             IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF | \ 
     69             IN_MOVE_SELF) 
     70 
     71#if 0 
     72#if defined (__alpha__) 
     73# define __NR_inotify_init 444 
     74# define __NR_inotify_add_watch 445 
     75# define __NR_inotify_rm_watch 446 
     76 
     77#elif defined (__arm__) 
     78# define __NR_inotify_init (__NR_SYSCALL_BASE+316) 
     79# define __NR_inotify_add_watch (__NR_SYSCALL_BASE+317) 
     80# define __NR_inotify_rm_watch (__NR_SYSCALL_BASE+318) 
     81 
     82#elif defined (__frv__) 
     83# define __NR_inotify_init 291 
     84# define __NR_inotify_add_watch 292 
     85# define __NR_inotify_rm_watch 293 
     86 
     87#elif defined(__i386__) 
     88# define __NR_inotify_init 291 
     89# define __NR_inotify_add_watch 292 
     90# define __NR_inotify_rm_watch 293 
     91 
     92#elif defined (__ia64__) 
     93# define __NR_inotify_init 1277 
     94# define __NR_inotify_add_watch 1278 
     95# define __NR_inotify_rm_watch 1279 
     96 
     97#elif defined (__mips__) 
     98# if _MIPS_SIM == _MIPS_SIM_ABI32 
     99#  define __NR_inotify_init (__NR_Linux + 284) 
     100#  define __NR_inotify_add_watch (__NR_Linux + 285) 
     101#  define __NR_inotify_rm_watch (__NR_Linux + 286) 
     102# endif 
     103# if _MIPS_SIM == _MIPS_SIM_ABI64 
     104#  define __NR_inotify_init (__NR_Linux + 243) 
     105#  define __NR_inotify_add_watch (__NR_Linux + 243) 
     106#  define __NR_inotify_rm_watch (__NR_Linux + 243) 
     107# endif 
     108# if _MIPS_SIM == _MIPS_SIM_NABI32 
     109#  define __NR_inotify_init (__NR_Linux + 247) 
     110#  define __NR_inotify_add_watch (__NR_Linux + 248) 
     111#  define __NR_inotify_rm_watch (__NR_Linux + 249) 
     112# endif 
     113 
     114#elif defined(__parisc__) 
     115# define __NR_inotify_init (__NR_Linux + 269) 
     116# define __NR_inotify_add_watch (__NR_Linux + 270) 
     117# define __NR_inotify_rm_watch (__NR_Linux + 271) 
     118 
     119#elif defined(__powerpc__) || defined(__powerpc64__) 
     120# define __NR_inotify_init 275 
     121# define __NR_inotify_add_watch 276 
     122# define __NR_inotify_rm_watch 277 
     123 
     124#elif defined (__s390__) 
     125# define __NR_inotify_init 284 
     126# define __NR_inotify_add_watch 285 
     127# define __NR_inotify_rm_watch 286 
     128 
     129#elif defined (__sh__) 
     130# define __NR_inotify_init 290 
     131# define __NR_inotify_add_watch 291 
     132# define __NR_inotify_rm_watch 292 
     133 
     134#elif defined (__sh64__) 
     135# define __NR_inotify_init 318 
     136# define __NR_inotify_add_watch 319 
     137# define __NR_inotify_rm_watch 320 
     138 
     139#elif defined (__sparc__) || defined (__sparc64__) 
     140# define __NR_inotify_init 151 
     141# define __NR_inotify_add_watch 152 
     142# define __NR_inotify_rm_watch 156 
     143 
     144#elif defined(__x86_64__) 
     145# define __NR_inotify_init 253 
     146# define __NR_inotify_add_watch 254 
     147# define __NR_inotify_rm_watch 255 
     148 
     149#else 
     150# error "Unsupported architecture!" 
     151#endif 
     152#endif 
     153 
     154#ifndef __NR_inotify_init 
     155# error "Unsupported architecture!" 
     156#endif 
     157#ifndef __NR_inotify_add_watch 
     158# error "Unsupported architecture!" 
     159#endif 
     160#ifndef __NR_inotify_rm_watch 
     161# error "Unsupported architecture!" 
     162#endif 
     163 
     164 
     165static inline int inotify_init (void) 
     166{ 
     167    return syscall (__NR_inotify_init); 
     168} 
     169 
     170static inline int inotify_add_watch (int fd, const char *name, uint32_t mask) 
     171{ 
     172    return syscall (__NR_inotify_add_watch, fd, name, mask); 
     173} 
     174 
     175static inline int inotify_rm_watch (int fd, uint32_t wd) 
     176{ 
     177    return syscall (__NR_inotify_rm_watch, fd, wd); 
     178} 
     179 
     180 
     181#endif  /* _LINUX_INOTIFY_H */ 
  • (a) /dev/null vs. (b) avahi-0.6.16/avahi-daemon/inotify/inotify.h

    a b  
     1#ifndef _INOTIFYTOOLS_INOTIFY_H 
     2#define _INOTIFYTOOLS_INOTIFY_H 1 
     3 
     4/* this is defined to 1 if <sys/inotify.h> works. */ 
     5#undef SYS_INOTIFY_H_EXISTS_AND_WORKS 
     6 
     7 
     8#ifdef SYS_INOTIFY_H_EXISTS_AND_WORKS 
     9#include <sys/inotify.h> 
     10#else 
     11#include "inotify-nosys.h" 
     12#endif // SYS_INOTIFY_H_EXISTS_AND_WORKS 
     13 
     14 
     15#endif 
     16 
  • avahi-daemon/main.c

    old new  
    4040#include <sys/time.h> 
    4141#include <sys/resource.h> 
    4242#include <sys/socket.h> 
     43#include <inotify/inotify.h> 
    4344 
    4445#include <libdaemon/dfork.h> 
    4546#include <libdaemon/dsignal.h> 
     
    713714    } 
    714715} 
    715716 
     717static void inotify_callback(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) { 
     718    char buffer[128]; 
     719    int inotify_fd = (int) userdata; 
     720 
     721    assert(watch); 
     722 
     723    //read(fd, &buffer, sizeof(buffer)) != sizeof(buffer); 
     724    if( read(inotify_fd, &buffer[0], sizeof(buffer)) < 0 ) { 
     725        avahi_log_error("Failed to read inotify event."); 
     726    return; 
     727    } 
     728 
     729    avahi_log_info("Files changed, reloading."); 
     730#ifdef ENABLE_CHROOT 
     731    static_service_load(config.use_chroot); 
     732    static_hosts_load(config.use_chroot); 
     733#else 
     734    static_service_load(0); 
     735    static_hosts_load(0); 
     736#endif 
     737    static_service_add_to_server(); 
     738    static_hosts_add_to_server(); 
     739 
     740    if (resolv_conf_entry_group) 
     741        avahi_s_entry_group_reset(resolv_conf_entry_group); 
     742 
     743    load_resolv_conf(); 
     744 
     745    update_wide_area_servers(); 
     746 
     747    if (config.publish_resolv_conf && resolv_conf && resolv_conf[0]) 
     748        resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf); 
     749} 
     750 
    716751/* Imported from ../avahi-client/nss-check.c */ 
    717752int avahi_nss_support(void); 
    718753 
     
    722757    const AvahiPoll *poll_api = NULL; 
    723758    AvahiWatch *sig_watch = NULL; 
    724759    int retval_is_sent = 0; 
     760    int inotify_fd = 0; 
     761    int inotify_wd = 0; 
     762    AvahiWatch *inotify_watch = NULL; 
    725763 
    726764    assert(c); 
    727765 
     
    745783        goto finish; 
    746784    } 
    747785 
     786    /* iNotify */ 
     787    if( (inotify_fd = inotify_init()) < 0 ) { 
     788        avahi_log_error( "Failed to initialize inotify"); 
     789        goto finish; 
     790    } 
     791    if( (inotify_wd = inotify_add_watch(inotify_fd, "/etc/avahi/services", 
     792            IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_MODIFY|IN_MOVED_FROM|IN_MOVED_TO)) < 0 ) { 
     793        avahi_log_error( "Failed to initialize add inotify watch"); 
     794        goto finish; 
     795    } 
     796    if (!(inotify_watch = poll_api->watch_new(poll_api, inotify_fd, AVAHI_WATCH_IN, inotify_callback, inotify_fd))) { 
     797        avahi_log_error( "Failed to create signal watcher"); 
     798        goto finish; 
     799    } 
     800 
    748801    if (simple_protocol_setup(poll_api) < 0) 
    749802        goto finish; 
    750803 
     
    847900    if (sig_watch) 
    848901        poll_api->watch_free(sig_watch); 
    849902 
     903    if (inotify_wd) 
     904        inotify_rm_watch(inotify_fd, inotify_wd); 
     905    if (inotify_watch) 
     906        poll_api->watch_free(inotify_watch); 
     907 
    850908    if (simple_poll_api) { 
    851909        avahi_simple_poll_free(simple_poll_api); 
    852910        simple_poll_api = NULL;