… | |
… | |
153 | |
153 | |
154 | #endif |
154 | #endif |
155 | |
155 | |
156 | #include <math.h> |
156 | #include <math.h> |
157 | #include <stdlib.h> |
157 | #include <stdlib.h> |
|
|
158 | #include <string.h> |
158 | #include <fcntl.h> |
159 | #include <fcntl.h> |
159 | #include <stddef.h> |
160 | #include <stddef.h> |
160 | |
161 | |
161 | #include <stdio.h> |
162 | #include <stdio.h> |
162 | |
163 | |
… | |
… | |
450 | */ |
451 | */ |
451 | #define TIME_EPSILON 0.0001220703125 /* 1/8192 */ |
452 | #define TIME_EPSILON 0.0001220703125 /* 1/8192 */ |
452 | |
453 | |
453 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
454 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
454 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
455 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
455 | /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */ |
|
|
456 | |
456 | |
457 | #if __GNUC__ >= 4 |
457 | #if __GNUC__ >= 4 |
458 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
458 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
459 | # define noinline __attribute__ ((noinline)) |
459 | # define noinline __attribute__ ((noinline)) |
460 | #else |
460 | #else |
… | |
… | |
1605 | #endif |
1605 | #endif |
1606 | #if EV_USE_INOTIFY |
1606 | #if EV_USE_INOTIFY |
1607 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1607 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1608 | #endif |
1608 | #endif |
1609 | #if EV_USE_SIGNALFD |
1609 | #if EV_USE_SIGNALFD |
1610 | sigfd = flags & EVFLAG_NOSIGFD ? -1 : -2; |
1610 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1611 | #endif |
1611 | #endif |
1612 | |
1612 | |
1613 | if (!(flags & 0x0000ffffU)) |
1613 | if (!(flags & 0x0000ffffU)) |
1614 | flags |= ev_recommended_backends (); |
1614 | flags |= ev_recommended_backends (); |
1615 | |
1615 | |
… | |
… | |
1659 | } |
1659 | } |
1660 | } |
1660 | } |
1661 | |
1661 | |
1662 | #if EV_USE_SIGNALFD |
1662 | #if EV_USE_SIGNALFD |
1663 | if (ev_is_active (&sigfd_w)) |
1663 | if (ev_is_active (&sigfd_w)) |
1664 | { |
|
|
1665 | /*ev_ref (EV_A);*/ |
|
|
1666 | /*ev_io_stop (EV_A_ &sigfd_w);*/ |
|
|
1667 | |
|
|
1668 | close (sigfd); |
1664 | close (sigfd); |
1669 | } |
|
|
1670 | #endif |
1665 | #endif |
1671 | |
1666 | |
1672 | #if EV_USE_INOTIFY |
1667 | #if EV_USE_INOTIFY |
1673 | if (fs_fd >= 0) |
1668 | if (fs_fd >= 0) |
1674 | close (fs_fd); |
1669 | close (fs_fd); |
… | |
… | |
2753 | # if EV_USE_SIGNALFD |
2748 | # if EV_USE_SIGNALFD |
2754 | if (sigfd < 0) /*TODO*/ |
2749 | if (sigfd < 0) /*TODO*/ |
2755 | # endif |
2750 | # endif |
2756 | { |
2751 | { |
2757 | # if _WIN32 |
2752 | # if _WIN32 |
|
|
2753 | evpipe_init (EV_A); |
|
|
2754 | |
2758 | signal (w->signum, ev_sighandler); |
2755 | signal (w->signum, ev_sighandler); |
2759 | # else |
2756 | # else |
2760 | struct sigaction sa; |
2757 | struct sigaction sa; |
2761 | |
2758 | |
2762 | evpipe_init (EV_A); |
2759 | evpipe_init (EV_A); |
… | |
… | |
2793 | signals [w->signum - 1].loop = 0; /* unattach from signal */ |
2790 | signals [w->signum - 1].loop = 0; /* unattach from signal */ |
2794 | #endif |
2791 | #endif |
2795 | #if EV_USE_SIGNALFD |
2792 | #if EV_USE_SIGNALFD |
2796 | if (sigfd >= 0) |
2793 | if (sigfd >= 0) |
2797 | { |
2794 | { |
2798 | sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D |
2795 | sigset_t ss; |
|
|
2796 | |
|
|
2797 | sigemptyset (&ss); |
|
|
2798 | sigaddset (&ss, w->signum); |
2799 | sigdelset (&sigfd_set, w->signum); |
2799 | sigdelset (&sigfd_set, w->signum); |
|
|
2800 | |
2800 | signalfd (sigfd, &sigfd_set, 0); |
2801 | signalfd (sigfd, &sigfd_set, 0); |
2801 | sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D |
2802 | sigprocmask (SIG_UNBLOCK, &ss, 0); |
2802 | /*TODO: maybe unblock signal? */ |
|
|
2803 | } |
2803 | } |
2804 | else |
2804 | else |
2805 | #endif |
2805 | #endif |
2806 | signal (w->signum, SIG_DFL); |
2806 | signal (w->signum, SIG_DFL); |
2807 | } |
2807 | } |
… | |
… | |
2860 | static void noinline |
2860 | static void noinline |
2861 | infy_add (EV_P_ ev_stat *w) |
2861 | infy_add (EV_P_ ev_stat *w) |
2862 | { |
2862 | { |
2863 | w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD); |
2863 | w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD); |
2864 | |
2864 | |
2865 | if (w->wd < 0) |
2865 | if (w->wd >= 0) |
|
|
2866 | { |
|
|
2867 | struct statfs sfs; |
|
|
2868 | |
|
|
2869 | /* now local changes will be tracked by inotify, but remote changes won't */ |
|
|
2870 | /* unless the filesystem is known to be local, we therefore still poll */ |
|
|
2871 | /* also do poll on <2.6.25, but with normal frequency */ |
|
|
2872 | |
|
|
2873 | if (!fs_2625) |
|
|
2874 | w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; |
|
|
2875 | else if (!statfs (w->path, &sfs) |
|
|
2876 | && (sfs.f_type == 0x1373 /* devfs */ |
|
|
2877 | || sfs.f_type == 0xEF53 /* ext2/3 */ |
|
|
2878 | || sfs.f_type == 0x3153464a /* jfs */ |
|
|
2879 | || sfs.f_type == 0x52654973 /* reiser3 */ |
|
|
2880 | || sfs.f_type == 0x01021994 /* tempfs */ |
|
|
2881 | || sfs.f_type == 0x58465342 /* xfs */)) |
|
|
2882 | w->timer.repeat = 0.; /* filesystem is local, kernel new enough */ |
|
|
2883 | else |
|
|
2884 | w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */ |
2866 | { |
2885 | } |
|
|
2886 | else |
|
|
2887 | { |
|
|
2888 | /* can't use inotify, continue to stat */ |
2867 | w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; |
2889 | w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; |
2868 | ev_timer_again (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ |
|
|
2869 | |
2890 | |
2870 | /* monitor some parent directory for speedup hints */ |
2891 | /* if path is not there, monitor some parent directory for speedup hints */ |
2871 | /* note that exceeding the hardcoded path limit is not a correctness issue, */ |
2892 | /* note that exceeding the hardcoded path limit is not a correctness issue, */ |
2872 | /* but an efficiency issue only */ |
2893 | /* but an efficiency issue only */ |
2873 | if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) |
2894 | if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) |
2874 | { |
2895 | { |
2875 | char path [4096]; |
2896 | char path [4096]; |
… | |
… | |
2891 | while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); |
2912 | while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); |
2892 | } |
2913 | } |
2893 | } |
2914 | } |
2894 | |
2915 | |
2895 | if (w->wd >= 0) |
2916 | if (w->wd >= 0) |
2896 | { |
|
|
2897 | struct statfs sfs; |
|
|
2898 | |
|
|
2899 | wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); |
2917 | wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); |
2900 | |
2918 | |
2901 | /* now local changes will be tracked by inotify, but remote changes won't */ |
2919 | /* now re-arm timer, if required */ |
2902 | /* unless the filesystem it known to be local, we therefore still poll */ |
2920 | if (ev_is_active (&w->timer)) ev_ref (EV_A); |
2903 | /* also do poll on <2.6.25, but with normal frequency */ |
|
|
2904 | |
|
|
2905 | if (fs_2625 && !statfs (w->path, &sfs)) |
|
|
2906 | if (sfs.f_type == 0x1373 /* devfs */ |
|
|
2907 | || sfs.f_type == 0xEF53 /* ext2/3 */ |
|
|
2908 | || sfs.f_type == 0x3153464a /* jfs */ |
|
|
2909 | || sfs.f_type == 0x52654973 /* reiser3 */ |
|
|
2910 | || sfs.f_type == 0x01021994 /* tempfs */ |
|
|
2911 | || sfs.f_type == 0x58465342 /* xfs */) |
|
|
2912 | return; |
|
|
2913 | |
|
|
2914 | w->timer.repeat = w->interval ? w->interval : fs_2625 ? NFS_STAT_INTERVAL : DEF_STAT_INTERVAL; |
|
|
2915 | ev_timer_again (EV_A_ &w->timer); |
2921 | ev_timer_again (EV_A_ &w->timer); |
2916 | } |
2922 | if (ev_is_active (&w->timer)) ev_unref (EV_A); |
2917 | } |
2923 | } |
2918 | |
2924 | |
2919 | static void noinline |
2925 | static void noinline |
2920 | infy_del (EV_P_ ev_stat *w) |
2926 | infy_del (EV_P_ ev_stat *w) |
2921 | { |
2927 | { |
… | |
… | |
2997 | return; |
3003 | return; |
2998 | |
3004 | |
2999 | fs_2625 = 1; |
3005 | fs_2625 = 1; |
3000 | } |
3006 | } |
3001 | |
3007 | |
|
|
3008 | inline_size int |
|
|
3009 | infy_newfd (void) |
|
|
3010 | { |
|
|
3011 | #if defined (IN_CLOEXEC) && defined (IN_NONBLOCK) |
|
|
3012 | int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); |
|
|
3013 | if (fd >= 0) |
|
|
3014 | return fd; |
|
|
3015 | #endif |
|
|
3016 | return inotify_init (); |
|
|
3017 | } |
|
|
3018 | |
3002 | inline_size void |
3019 | inline_size void |
3003 | infy_init (EV_P) |
3020 | infy_init (EV_P) |
3004 | { |
3021 | { |
3005 | if (fs_fd != -2) |
3022 | if (fs_fd != -2) |
3006 | return; |
3023 | return; |
3007 | |
3024 | |
3008 | fs_fd = -1; |
3025 | fs_fd = -1; |
3009 | |
3026 | |
3010 | check_2625 (EV_A); |
3027 | check_2625 (EV_A); |
3011 | |
3028 | |
3012 | fs_fd = inotify_init (); |
3029 | fs_fd = infy_newfd (); |
3013 | |
3030 | |
3014 | if (fs_fd >= 0) |
3031 | if (fs_fd >= 0) |
3015 | { |
3032 | { |
|
|
3033 | fd_intern (fs_fd); |
3016 | ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ); |
3034 | ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ); |
3017 | ev_set_priority (&fs_w, EV_MAXPRI); |
3035 | ev_set_priority (&fs_w, EV_MAXPRI); |
3018 | ev_io_start (EV_A_ &fs_w); |
3036 | ev_io_start (EV_A_ &fs_w); |
|
|
3037 | ev_unref (EV_A); |
3019 | } |
3038 | } |
3020 | } |
3039 | } |
3021 | |
3040 | |
3022 | inline_size void |
3041 | inline_size void |
3023 | infy_fork (EV_P) |
3042 | infy_fork (EV_P) |
… | |
… | |
3025 | int slot; |
3044 | int slot; |
3026 | |
3045 | |
3027 | if (fs_fd < 0) |
3046 | if (fs_fd < 0) |
3028 | return; |
3047 | return; |
3029 | |
3048 | |
|
|
3049 | ev_ref (EV_A); |
|
|
3050 | ev_io_stop (EV_A_ &fs_w); |
3030 | close (fs_fd); |
3051 | close (fs_fd); |
3031 | fs_fd = inotify_init (); |
3052 | fs_fd = infy_newfd (); |
|
|
3053 | |
|
|
3054 | if (fs_fd >= 0) |
|
|
3055 | { |
|
|
3056 | fd_intern (fs_fd); |
|
|
3057 | ev_io_set (&fs_w, fs_fd, EV_READ); |
|
|
3058 | ev_io_start (EV_A_ &fs_w); |
|
|
3059 | ev_unref (EV_A); |
|
|
3060 | } |
3032 | |
3061 | |
3033 | for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) |
3062 | for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) |
3034 | { |
3063 | { |
3035 | WL w_ = fs_hash [slot].head; |
3064 | WL w_ = fs_hash [slot].head; |
3036 | fs_hash [slot].head = 0; |
3065 | fs_hash [slot].head = 0; |
… | |
… | |
3043 | w->wd = -1; |
3072 | w->wd = -1; |
3044 | |
3073 | |
3045 | if (fs_fd >= 0) |
3074 | if (fs_fd >= 0) |
3046 | infy_add (EV_A_ w); /* re-add, no matter what */ |
3075 | infy_add (EV_A_ w); /* re-add, no matter what */ |
3047 | else |
3076 | else |
|
|
3077 | { |
|
|
3078 | w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; |
|
|
3079 | if (ev_is_active (&w->timer)) ev_ref (EV_A); |
3048 | ev_timer_again (EV_A_ &w->timer); |
3080 | ev_timer_again (EV_A_ &w->timer); |
|
|
3081 | if (ev_is_active (&w->timer)) ev_unref (EV_A); |
|
|
3082 | } |
3049 | } |
3083 | } |
3050 | } |
3084 | } |
3051 | } |
3085 | } |
3052 | |
3086 | |
3053 | #endif |
3087 | #endif |
… | |
… | |
3070 | static void noinline |
3104 | static void noinline |
3071 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
3105 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
3072 | { |
3106 | { |
3073 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
3107 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
3074 | |
3108 | |
3075 | /* we copy this here each the time so that */ |
3109 | ev_statdata prev = w->attr; |
3076 | /* prev has the old value when the callback gets invoked */ |
|
|
3077 | w->prev = w->attr; |
|
|
3078 | ev_stat_stat (EV_A_ w); |
3110 | ev_stat_stat (EV_A_ w); |
3079 | |
3111 | |
3080 | /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */ |
3112 | /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */ |
3081 | if ( |
3113 | if ( |
3082 | w->prev.st_dev != w->attr.st_dev |
3114 | prev.st_dev != w->attr.st_dev |
3083 | || w->prev.st_ino != w->attr.st_ino |
3115 | || prev.st_ino != w->attr.st_ino |
3084 | || w->prev.st_mode != w->attr.st_mode |
3116 | || prev.st_mode != w->attr.st_mode |
3085 | || w->prev.st_nlink != w->attr.st_nlink |
3117 | || prev.st_nlink != w->attr.st_nlink |
3086 | || w->prev.st_uid != w->attr.st_uid |
3118 | || prev.st_uid != w->attr.st_uid |
3087 | || w->prev.st_gid != w->attr.st_gid |
3119 | || prev.st_gid != w->attr.st_gid |
3088 | || w->prev.st_rdev != w->attr.st_rdev |
3120 | || prev.st_rdev != w->attr.st_rdev |
3089 | || w->prev.st_size != w->attr.st_size |
3121 | || prev.st_size != w->attr.st_size |
3090 | || w->prev.st_atime != w->attr.st_atime |
3122 | || prev.st_atime != w->attr.st_atime |
3091 | || w->prev.st_mtime != w->attr.st_mtime |
3123 | || prev.st_mtime != w->attr.st_mtime |
3092 | || w->prev.st_ctime != w->attr.st_ctime |
3124 | || prev.st_ctime != w->attr.st_ctime |
3093 | ) { |
3125 | ) { |
|
|
3126 | /* we only update w->prev on actual differences */ |
|
|
3127 | /* in case we test more often than invoke the callback, */ |
|
|
3128 | /* to ensure that prev is always different to attr */ |
|
|
3129 | w->prev = prev; |
|
|
3130 | |
3094 | #if EV_USE_INOTIFY |
3131 | #if EV_USE_INOTIFY |
3095 | if (fs_fd >= 0) |
3132 | if (fs_fd >= 0) |
3096 | { |
3133 | { |
3097 | infy_del (EV_A_ w); |
3134 | infy_del (EV_A_ w); |
3098 | infy_add (EV_A_ w); |
3135 | infy_add (EV_A_ w); |
… | |
… | |
3123 | |
3160 | |
3124 | if (fs_fd >= 0) |
3161 | if (fs_fd >= 0) |
3125 | infy_add (EV_A_ w); |
3162 | infy_add (EV_A_ w); |
3126 | else |
3163 | else |
3127 | #endif |
3164 | #endif |
|
|
3165 | { |
3128 | ev_timer_again (EV_A_ &w->timer); |
3166 | ev_timer_again (EV_A_ &w->timer); |
|
|
3167 | ev_unref (EV_A); |
|
|
3168 | } |
3129 | |
3169 | |
3130 | ev_start (EV_A_ (W)w, 1); |
3170 | ev_start (EV_A_ (W)w, 1); |
3131 | |
3171 | |
3132 | EV_FREQUENT_CHECK; |
3172 | EV_FREQUENT_CHECK; |
3133 | } |
3173 | } |
… | |
… | |
3142 | EV_FREQUENT_CHECK; |
3182 | EV_FREQUENT_CHECK; |
3143 | |
3183 | |
3144 | #if EV_USE_INOTIFY |
3184 | #if EV_USE_INOTIFY |
3145 | infy_del (EV_A_ w); |
3185 | infy_del (EV_A_ w); |
3146 | #endif |
3186 | #endif |
|
|
3187 | |
|
|
3188 | if (ev_is_active (&w->timer)) |
|
|
3189 | { |
|
|
3190 | ev_ref (EV_A); |
3147 | ev_timer_stop (EV_A_ &w->timer); |
3191 | ev_timer_stop (EV_A_ &w->timer); |
|
|
3192 | } |
3148 | |
3193 | |
3149 | ev_stop (EV_A_ (W)w); |
3194 | ev_stop (EV_A_ (W)w); |
3150 | |
3195 | |
3151 | EV_FREQUENT_CHECK; |
3196 | EV_FREQUENT_CHECK; |
3152 | } |
3197 | } |