5 #include "environment.h"
7 #include "parse-options.h"
9 #include "fsmonitor-ipc.h"
10 #include "fsmonitor-path-utils.h"
11 #include "compat/fsmonitor/fsm-health.h"
12 #include "compat/fsmonitor/fsm-listen.h"
13 #include "fsmonitor--daemon.h"
14 #include "simple-ipc.h"
19 static const char * const builtin_fsmonitor__daemon_usage
[] = {
20 N_("git fsmonitor--daemon start [<options>]"),
21 N_("git fsmonitor--daemon run [<options>]"),
22 "git fsmonitor--daemon stop",
23 "git fsmonitor--daemon status",
27 #ifdef HAVE_FSMONITOR_DAEMON_BACKEND
29 * Global state loaded from config.
31 #define FSMONITOR__IPC_THREADS "fsmonitor.ipcthreads"
32 static int fsmonitor__ipc_threads
= 8;
34 #define FSMONITOR__START_TIMEOUT "fsmonitor.starttimeout"
35 static int fsmonitor__start_timeout_sec
= 60;
37 #define FSMONITOR__ANNOUNCE_STARTUP "fsmonitor.announcestartup"
38 static int fsmonitor__announce_startup
= 0;
40 static int fsmonitor_config(const char *var
, const char *value
, void *cb
)
42 if (!strcmp(var
, FSMONITOR__IPC_THREADS
)) {
43 int i
= git_config_int(var
, value
);
45 return error(_("value of '%s' out of range: %d"),
46 FSMONITOR__IPC_THREADS
, i
);
47 fsmonitor__ipc_threads
= i
;
51 if (!strcmp(var
, FSMONITOR__START_TIMEOUT
)) {
52 int i
= git_config_int(var
, value
);
54 return error(_("value of '%s' out of range: %d"),
55 FSMONITOR__START_TIMEOUT
, i
);
56 fsmonitor__start_timeout_sec
= i
;
60 if (!strcmp(var
, FSMONITOR__ANNOUNCE_STARTUP
)) {
62 int i
= git_config_bool_or_int(var
, value
, &is_bool
);
64 return error(_("value of '%s' not bool or int: %d"),
66 fsmonitor__announce_startup
= i
;
70 return git_default_config(var
, value
, cb
);
76 * Send a "quit" command to the `git-fsmonitor--daemon` (if running)
77 * and wait for it to shutdown.
79 static int do_as_client__send_stop(void)
81 struct strbuf answer
= STRBUF_INIT
;
84 ret
= fsmonitor_ipc__send_command("quit", &answer
);
86 /* The quit command does not return any response data. */
87 strbuf_release(&answer
);
92 trace2_region_enter("fsm_client", "polling-for-daemon-exit", NULL
);
93 while (fsmonitor_ipc__get_state() == IPC_STATE__LISTENING
)
95 trace2_region_leave("fsm_client", "polling-for-daemon-exit", NULL
);
100 static int do_as_client__status(void)
102 enum ipc_active_state state
= fsmonitor_ipc__get_state();
105 case IPC_STATE__LISTENING
:
106 printf(_("fsmonitor-daemon is watching '%s'\n"),
107 the_repository
->worktree
);
111 printf(_("fsmonitor-daemon is not watching '%s'\n"),
112 the_repository
->worktree
);
117 enum fsmonitor_cookie_item_result
{
118 FCIR_ERROR
= -1, /* could not create cookie file ? */
124 struct fsmonitor_cookie_item
{
125 struct hashmap_entry entry
;
127 enum fsmonitor_cookie_item_result result
;
130 static int cookies_cmp(const void *data
, const struct hashmap_entry
*he1
,
131 const struct hashmap_entry
*he2
, const void *keydata
)
133 const struct fsmonitor_cookie_item
*a
=
134 container_of(he1
, const struct fsmonitor_cookie_item
, entry
);
135 const struct fsmonitor_cookie_item
*b
=
136 container_of(he2
, const struct fsmonitor_cookie_item
, entry
);
138 return strcmp(a
->name
, keydata
? keydata
: b
->name
);
141 static enum fsmonitor_cookie_item_result
with_lock__wait_for_cookie(
142 struct fsmonitor_daemon_state
*state
)
144 /* assert current thread holding state->main_lock */
147 struct fsmonitor_cookie_item
*cookie
;
148 struct strbuf cookie_pathname
= STRBUF_INIT
;
149 struct strbuf cookie_filename
= STRBUF_INIT
;
150 enum fsmonitor_cookie_item_result result
;
153 CALLOC_ARRAY(cookie
, 1);
155 my_cookie_seq
= state
->cookie_seq
++;
157 strbuf_addf(&cookie_filename
, "%i-%i", getpid(), my_cookie_seq
);
159 strbuf_addbuf(&cookie_pathname
, &state
->path_cookie_prefix
);
160 strbuf_addbuf(&cookie_pathname
, &cookie_filename
);
162 cookie
->name
= strbuf_detach(&cookie_filename
, NULL
);
163 cookie
->result
= FCIR_INIT
;
164 hashmap_entry_init(&cookie
->entry
, strhash(cookie
->name
));
166 hashmap_add(&state
->cookies
, &cookie
->entry
);
168 trace_printf_key(&trace_fsmonitor
, "cookie-wait: '%s' '%s'",
169 cookie
->name
, cookie_pathname
.buf
);
172 * Create the cookie file on disk and then wait for a notification
173 * that the listener thread has seen it.
175 fd
= open(cookie_pathname
.buf
, O_WRONLY
| O_CREAT
| O_EXCL
, 0600);
177 error_errno(_("could not create fsmonitor cookie '%s'"),
180 cookie
->result
= FCIR_ERROR
;
185 * Technically, close() and unlink() can fail, but we don't
186 * care here. We only created the file to trigger a watch
187 * event from the FS to know that when we're up to date.
190 unlink(cookie_pathname
.buf
);
193 * Technically, this is an infinite wait (well, unless another
194 * thread sends us an abort). I'd like to change this to
195 * use `pthread_cond_timedwait()` and return an error/timeout
196 * and let the caller do the trivial response thing, but we
197 * don't have that routine in our thread-utils.
199 * After extensive beta testing I'm not really worried about
200 * this. Also note that the above open() and unlink() calls
201 * will cause at least two FS events on that path, so the odds
202 * of getting stuck are pretty slim.
204 while (cookie
->result
== FCIR_INIT
)
205 pthread_cond_wait(&state
->cookies_cond
,
209 hashmap_remove(&state
->cookies
, &cookie
->entry
, NULL
);
211 result
= cookie
->result
;
215 strbuf_release(&cookie_pathname
);
221 * Mark these cookies as _SEEN and wake up the corresponding client threads.
223 static void with_lock__mark_cookies_seen(struct fsmonitor_daemon_state
*state
,
224 const struct string_list
*cookie_names
)
226 /* assert current thread holding state->main_lock */
231 for (k
= 0; k
< cookie_names
->nr
; k
++) {
232 struct fsmonitor_cookie_item key
;
233 struct fsmonitor_cookie_item
*cookie
;
235 key
.name
= cookie_names
->items
[k
].string
;
236 hashmap_entry_init(&key
.entry
, strhash(key
.name
));
238 cookie
= hashmap_get_entry(&state
->cookies
, &key
, entry
, NULL
);
240 trace_printf_key(&trace_fsmonitor
, "cookie-seen: '%s'",
242 cookie
->result
= FCIR_SEEN
;
248 pthread_cond_broadcast(&state
->cookies_cond
);
252 * Set _ABORT on all pending cookies and wake up all client threads.
254 static void with_lock__abort_all_cookies(struct fsmonitor_daemon_state
*state
)
256 /* assert current thread holding state->main_lock */
258 struct hashmap_iter iter
;
259 struct fsmonitor_cookie_item
*cookie
;
262 hashmap_for_each_entry(&state
->cookies
, &iter
, cookie
, entry
) {
263 trace_printf_key(&trace_fsmonitor
, "cookie-abort: '%s'",
265 cookie
->result
= FCIR_ABORT
;
270 pthread_cond_broadcast(&state
->cookies_cond
);
274 * Requests to and from a FSMonitor Protocol V2 provider use an opaque
275 * "token" as a virtual timestamp. Clients can request a summary of all
276 * created/deleted/modified files relative to a token. In the response,
277 * clients receive a new token for the next (relative) request.
283 * The contents of the token are private and provider-specific.
285 * For the built-in fsmonitor--daemon, we define a token as follows:
287 * "builtin" ":" <token_id> ":" <sequence_nr>
289 * The "builtin" prefix is used as a namespace to avoid conflicts
290 * with other providers (such as Watchman).
292 * The <token_id> is an arbitrary OPAQUE string, such as a GUID,
293 * UUID, or {timestamp,pid}. It is used to group all filesystem
294 * events that happened while the daemon was monitoring (and in-sync
295 * with the filesystem).
297 * Unlike FSMonitor Protocol V1, it is not defined as a timestamp
298 * and does not define less-than/greater-than relationships.
299 * (There are too many race conditions to rely on file system
302 * The <sequence_nr> is a simple integer incremented whenever the
303 * daemon needs to make its state public. For example, if 1000 file
304 * system events come in, but no clients have requested the data,
305 * the daemon can continue to accumulate file changes in the same
306 * bin and does not need to advance the sequence number. However,
307 * as soon as a client does arrive, the daemon needs to start a new
308 * bin and increment the sequence number.
310 * The sequence number serves as the boundary between 2 sets
311 * of bins -- the older ones that the client has already seen
312 * and the newer ones that it hasn't.
314 * When a new <token_id> is created, the <sequence_nr> is reset to
321 * A new token_id is created:
323 * [1] each time the daemon is started.
325 * [2] any time that the daemon must re-sync with the filesystem
326 * (such as when the kernel drops or we miss events on a very
329 * [3] in response to a client "flush" command (for dropped event
332 * When a new token_id is created, the daemon is free to discard all
333 * cached filesystem events associated with any previous token_ids.
334 * Events associated with a non-current token_id will never be sent
335 * to a client. A token_id change implicitly means that the daemon
336 * has gap in its event history.
338 * Therefore, clients that present a token with a stale (non-current)
339 * token_id will always be given a trivial response.
341 struct fsmonitor_token_data
{
342 struct strbuf token_id
;
343 struct fsmonitor_batch
*batch_head
;
344 struct fsmonitor_batch
*batch_tail
;
345 uint64_t client_ref_count
;
348 struct fsmonitor_batch
{
349 struct fsmonitor_batch
*next
;
350 uint64_t batch_seq_nr
;
351 const char **interned_paths
;
356 static struct fsmonitor_token_data
*fsmonitor_new_token_data(void)
358 static int test_env_value
= -1;
359 static uint64_t flush_count
= 0;
360 struct fsmonitor_token_data
*token
;
361 struct fsmonitor_batch
*batch
;
363 CALLOC_ARRAY(token
, 1);
364 batch
= fsmonitor_batch__new();
366 strbuf_init(&token
->token_id
, 0);
367 token
->batch_head
= batch
;
368 token
->batch_tail
= batch
;
369 token
->client_ref_count
= 0;
371 if (test_env_value
< 0)
372 test_env_value
= git_env_bool("GIT_TEST_FSMONITOR_TOKEN", 0);
374 if (!test_env_value
) {
379 gettimeofday(&tv
, NULL
);
381 gmtime_r(&secs
, &tm
);
383 strbuf_addf(&token
->token_id
,
384 "%"PRIu64
".%d.%4d%02d%02dT%02d%02d%02d.%06ldZ",
387 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
,
388 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
,
391 strbuf_addf(&token
->token_id
, "test_%08x", test_env_value
++);
395 * We created a new <token_id> and are starting a new series
396 * of tokens with a zero <seq_nr>.
398 * Since clients cannot guess our new (non test) <token_id>
399 * they will always receive a trivial response (because of the
400 * mismatch on the <token_id>). The trivial response will
401 * tell them our new <token_id> so that subsequent requests
402 * will be relative to our new series. (And when sending that
403 * response, we pin the current head of the batch list.)
405 * Even if the client correctly guesses the <token_id>, their
406 * request of "builtin:<token_id>:0" asks for all changes MORE
407 * RECENT than batch/bin 0.
409 * This implies that it is a waste to accumulate paths in the
410 * initial batch/bin (because they will never be transmitted).
412 * So the daemon could be running for days and watching the
413 * file system, but doesn't need to actually accumulate any
414 * paths UNTIL we need to set a reference point for a later
417 * However, it is very useful for testing to always have a
418 * reference point set. Pin batch 0 to force early file system
419 * events to accumulate.
422 batch
->pinned_time
= time(NULL
);
427 struct fsmonitor_batch
*fsmonitor_batch__new(void)
429 struct fsmonitor_batch
*batch
;
431 CALLOC_ARRAY(batch
, 1);
436 void fsmonitor_batch__free_list(struct fsmonitor_batch
*batch
)
439 struct fsmonitor_batch
*next
= batch
->next
;
442 * The actual strings within the array of this batch
443 * are interned, so we don't own them. We only own
446 free(batch
->interned_paths
);
453 void fsmonitor_batch__add_path(struct fsmonitor_batch
*batch
,
456 const char *interned_path
= strintern(path
);
458 trace_printf_key(&trace_fsmonitor
, "event: %s", interned_path
);
460 ALLOC_GROW(batch
->interned_paths
, batch
->nr
+ 1, batch
->alloc
);
461 batch
->interned_paths
[batch
->nr
++] = interned_path
;
464 static void fsmonitor_batch__combine(struct fsmonitor_batch
*batch_dest
,
465 const struct fsmonitor_batch
*batch_src
)
469 ALLOC_GROW(batch_dest
->interned_paths
,
470 batch_dest
->nr
+ batch_src
->nr
+ 1,
473 for (k
= 0; k
< batch_src
->nr
; k
++)
474 batch_dest
->interned_paths
[batch_dest
->nr
++] =
475 batch_src
->interned_paths
[k
];
479 * To keep the batch list from growing unbounded in response to filesystem
480 * activity, we try to truncate old batches from the end of the list as
481 * they become irrelevant.
483 * We assume that the .git/index will be updated with the most recent token
484 * any time the index is updated. And future commands will only ask for
485 * recent changes *since* that new token. So as tokens advance into the
486 * future, older batch items will never be requested/needed. So we can
487 * truncate them without loss of functionality.
489 * However, multiple commands may be talking to the daemon concurrently
490 * or perform a slow command, so a little "token skew" is possible.
491 * Therefore, we want this to be a little bit lazy and have a generous
494 * The current reader thread walked backwards in time from `token->batch_head`
495 * back to `batch_marker` somewhere in the middle of the batch list.
497 * Let's walk backwards in time from that marker an arbitrary delay
498 * and truncate the list there. Note that these timestamps are completely
499 * artificial (based on when we pinned the batch item) and not on any
500 * filesystem activity.
502 * Return the obsolete portion of the list after we have removed it from
503 * the official list so that the caller can free it after leaving the lock.
505 #define MY_TIME_DELAY_SECONDS (5 * 60) /* seconds */
507 static struct fsmonitor_batch
*with_lock__truncate_old_batches(
508 struct fsmonitor_daemon_state
*state
,
509 const struct fsmonitor_batch
*batch_marker
)
511 /* assert current thread holding state->main_lock */
513 const struct fsmonitor_batch
*batch
;
514 struct fsmonitor_batch
*remainder
;
519 trace_printf_key(&trace_fsmonitor
, "Truncate: mark (%"PRIu64
",%"PRIu64
")",
520 batch_marker
->batch_seq_nr
,
521 (uint64_t)batch_marker
->pinned_time
);
523 for (batch
= batch_marker
; batch
; batch
= batch
->next
) {
526 if (!batch
->pinned_time
) /* an overflow batch */
529 t
= batch
->pinned_time
+ MY_TIME_DELAY_SECONDS
;
530 if (t
> batch_marker
->pinned_time
) /* too close to marker */
533 goto truncate_past_here
;
539 state
->current_token_data
->batch_tail
= (struct fsmonitor_batch
*)batch
;
541 remainder
= ((struct fsmonitor_batch
*)batch
)->next
;
542 ((struct fsmonitor_batch
*)batch
)->next
= NULL
;
547 static void fsmonitor_free_token_data(struct fsmonitor_token_data
*token
)
552 assert(token
->client_ref_count
== 0);
554 strbuf_release(&token
->token_id
);
556 fsmonitor_batch__free_list(token
->batch_head
);
562 * Flush all of our cached data about the filesystem. Call this if we
563 * lose sync with the filesystem and miss some notification events.
565 * [1] If we are missing events, then we no longer have a complete
566 * history of the directory (relative to our current start token).
567 * We should create a new token and start fresh (as if we just
570 * [2] Some of those lost events may have been for cookie files. We
571 * should assume the worst and abort them rather letting them starve.
573 * If there are no concurrent threads reading the current token data
574 * series, we can free it now. Otherwise, let the last reader free
577 * Either way, the old token data series is no longer associated with
580 static void with_lock__do_force_resync(struct fsmonitor_daemon_state
*state
)
582 /* assert current thread holding state->main_lock */
584 struct fsmonitor_token_data
*free_me
= NULL
;
585 struct fsmonitor_token_data
*new_one
= NULL
;
587 new_one
= fsmonitor_new_token_data();
589 if (state
->current_token_data
->client_ref_count
== 0)
590 free_me
= state
->current_token_data
;
591 state
->current_token_data
= new_one
;
593 fsmonitor_free_token_data(free_me
);
595 with_lock__abort_all_cookies(state
);
598 void fsmonitor_force_resync(struct fsmonitor_daemon_state
*state
)
600 pthread_mutex_lock(&state
->main_lock
);
601 with_lock__do_force_resync(state
);
602 pthread_mutex_unlock(&state
->main_lock
);
606 * Format an opaque token string to send to the client.
608 static void with_lock__format_response_token(
609 struct strbuf
*response_token
,
610 const struct strbuf
*response_token_id
,
611 const struct fsmonitor_batch
*batch
)
613 /* assert current thread holding state->main_lock */
615 strbuf_reset(response_token
);
616 strbuf_addf(response_token
, "builtin:%s:%"PRIu64
,
617 response_token_id
->buf
, batch
->batch_seq_nr
);
621 * Parse an opaque token from the client.
622 * Returns -1 on error.
624 static int fsmonitor_parse_client_token(const char *buf_token
,
625 struct strbuf
*requested_token_id
,
631 strbuf_reset(requested_token_id
);
634 if (!skip_prefix(buf_token
, "builtin:", &p
))
637 while (*p
&& *p
!= ':')
638 strbuf_addch(requested_token_id
, *p
++);
642 *seq_nr
= (uint64_t)strtoumax(p
, &p_end
, 10);
649 KHASH_INIT(str
, const char *, int, 0, kh_str_hash_func
, kh_str_hash_equal
)
651 static int do_handle_client(struct fsmonitor_daemon_state
*state
,
653 ipc_server_reply_cb
*reply
,
654 struct ipc_server_reply_data
*reply_data
)
656 struct fsmonitor_token_data
*token_data
= NULL
;
657 struct strbuf response_token
= STRBUF_INIT
;
658 struct strbuf requested_token_id
= STRBUF_INIT
;
659 struct strbuf payload
= STRBUF_INIT
;
660 uint64_t requested_oldest_seq_nr
= 0;
661 uint64_t total_response_len
= 0;
663 const struct fsmonitor_batch
*batch_head
;
664 const struct fsmonitor_batch
*batch
;
665 struct fsmonitor_batch
*remainder
= NULL
;
666 intmax_t count
= 0, duplicates
= 0;
672 enum fsmonitor_cookie_item_result cookie_result
;
675 * We expect `command` to be of the form:
677 * <command> := quit NUL
679 * | <V1-time-since-epoch-ns> NUL
680 * | <V2-opaque-fsmonitor-token> NUL
683 if (!strcmp(command
, "quit")) {
685 * A client has requested over the socket/pipe that the
688 * Tell the IPC thread pool to shutdown (which completes
689 * the await in the main thread (which can stop the
690 * fsmonitor listener thread)).
692 * There is no reply to the client.
694 return SIMPLE_IPC_QUIT
;
696 } else if (!strcmp(command
, "flush")) {
698 * Flush all of our cached data and generate a new token
699 * just like if we lost sync with the filesystem.
701 * Then send a trivial response using the new token.
706 } else if (!skip_prefix(command
, "builtin:", &p
)) {
707 /* assume V1 timestamp or garbage */
711 strtoumax(command
, &p_end
, 10);
712 trace_printf_key(&trace_fsmonitor
,
714 "fsmonitor: invalid command line '%s'" :
715 "fsmonitor: unsupported V1 protocol '%s'"),
721 /* We have "builtin:*" */
722 if (fsmonitor_parse_client_token(command
, &requested_token_id
,
723 &requested_oldest_seq_nr
)) {
724 trace_printf_key(&trace_fsmonitor
,
725 "fsmonitor: invalid V2 protocol token '%s'",
732 * We have a V2 valid token:
733 * "builtin:<token_id>:<seq_nr>"
739 pthread_mutex_lock(&state
->main_lock
);
741 if (!state
->current_token_data
)
742 BUG("fsmonitor state does not have a current token");
745 * Write a cookie file inside the directory being watched in
746 * an effort to flush out existing filesystem events that we
747 * actually care about. Suspend this client thread until we
748 * see the filesystem events for this cookie file.
750 * Creating the cookie lets us guarantee that our FS listener
751 * thread has drained the kernel queue and we are caught up
754 * If we cannot create the cookie (or otherwise guarantee that
755 * we are caught up), we send a trivial response. We have to
756 * assume that there might be some very, very recent activity
757 * on the FS still in flight.
760 cookie_result
= with_lock__wait_for_cookie(state
);
761 if (cookie_result
!= FCIR_SEEN
) {
762 error(_("fsmonitor: cookie_result '%d' != SEEN"),
769 with_lock__do_force_resync(state
);
772 * We mark the current head of the batch list as "pinned" so
773 * that the listener thread will treat this item as read-only
774 * (and prevent any more paths from being added to it) from
777 token_data
= state
->current_token_data
;
778 batch_head
= token_data
->batch_head
;
779 ((struct fsmonitor_batch
*)batch_head
)->pinned_time
= time(NULL
);
782 * FSMonitor Protocol V2 requires that we send a response header
783 * with a "new current token" and then all of the paths that changed
784 * since the "requested token". We send the seq_nr of the just-pinned
785 * head batch so that future requests from a client will be relative
788 with_lock__format_response_token(&response_token
,
789 &token_data
->token_id
, batch_head
);
791 reply(reply_data
, response_token
.buf
, response_token
.len
+ 1);
792 total_response_len
+= response_token
.len
+ 1;
794 trace2_data_string("fsmonitor", the_repository
, "response/token",
796 trace_printf_key(&trace_fsmonitor
, "response token: %s",
800 if (strcmp(requested_token_id
.buf
, token_data
->token_id
.buf
)) {
802 * The client last spoke to a different daemon
803 * instance -OR- the daemon had to resync with
804 * the filesystem (and lost events), so reject.
806 trace2_data_string("fsmonitor", the_repository
,
807 "response/token", "different");
810 } else if (requested_oldest_seq_nr
<
811 token_data
->batch_tail
->batch_seq_nr
) {
813 * The client wants older events than we have for
814 * this token_id. This means that the end of our
815 * batch list was truncated and we cannot give the
816 * client a complete snapshot relative to their
819 trace_printf_key(&trace_fsmonitor
,
820 "client requested truncated data");
826 pthread_mutex_unlock(&state
->main_lock
);
828 reply(reply_data
, "/", 2);
830 trace2_data_intmax("fsmonitor", the_repository
,
831 "response/trivial", 1);
837 * We're going to hold onto a pointer to the current
838 * token-data while we walk the list of batches of files.
839 * During this time, we will NOT be under the lock.
840 * So we ref-count it.
842 * This allows the listener thread to continue prepending
843 * new batches of items to the token-data (which we'll ignore).
845 * AND it allows the listener thread to do a token-reset
846 * (and install a new `current_token_data`).
848 token_data
->client_ref_count
++;
850 pthread_mutex_unlock(&state
->main_lock
);
853 * The client request is relative to the token that they sent,
854 * so walk the batch list backwards from the current head back
855 * to the batch (sequence number) they named.
857 * We use khash to de-dup the list of pathnames.
859 * NEEDSWORK: each batch contains a list of interned strings,
860 * so we only need to do pointer comparisons here to build the
861 * hash table. Currently, we're still comparing the string
864 shown
= kh_init_str();
865 for (batch
= batch_head
;
866 batch
&& batch
->batch_seq_nr
> requested_oldest_seq_nr
;
867 batch
= batch
->next
) {
870 for (k
= 0; k
< batch
->nr
; k
++) {
871 const char *s
= batch
->interned_paths
[k
];
874 if (kh_get_str(shown
, s
) != kh_end(shown
))
877 kh_put_str(shown
, s
, &hash_ret
);
879 trace_printf_key(&trace_fsmonitor
,
880 "send[%"PRIuMAX
"]: %s",
883 /* Each path gets written with a trailing NUL */
884 s_len
= strlen(s
) + 1;
886 if (payload
.len
+ s_len
>=
887 LARGE_PACKET_DATA_MAX
) {
888 reply(reply_data
, payload
.buf
,
890 total_response_len
+= payload
.len
;
891 strbuf_reset(&payload
);
894 strbuf_add(&payload
, s
, s_len
);
901 reply(reply_data
, payload
.buf
, payload
.len
);
902 total_response_len
+= payload
.len
;
905 kh_release_str(shown
);
907 pthread_mutex_lock(&state
->main_lock
);
909 if (token_data
->client_ref_count
> 0)
910 token_data
->client_ref_count
--;
912 if (token_data
->client_ref_count
== 0) {
913 if (token_data
!= state
->current_token_data
) {
915 * The listener thread did a token-reset while we were
916 * walking the batch list. Therefore, this token is
917 * stale and can be discarded completely. If we are
918 * the last reader thread using this token, we own
921 fsmonitor_free_token_data(token_data
);
924 * We are holding the lock and are the only
925 * reader of the ref-counted portion of the
926 * list, so we get the honor of seeing if the
927 * list can be truncated to save memory.
929 * The main loop did not walk to the end of the
930 * list, so this batch is the first item in the
931 * batch-list that is older than the requested
932 * end-point sequence number. See if the tail
933 * end of the list is obsolete.
935 remainder
= with_lock__truncate_old_batches(state
,
940 pthread_mutex_unlock(&state
->main_lock
);
943 fsmonitor_batch__free_list(remainder
);
945 trace2_data_intmax("fsmonitor", the_repository
, "response/length", total_response_len
);
946 trace2_data_intmax("fsmonitor", the_repository
, "response/count/files", count
);
947 trace2_data_intmax("fsmonitor", the_repository
, "response/count/duplicates", duplicates
);
950 strbuf_release(&response_token
);
951 strbuf_release(&requested_token_id
);
952 strbuf_release(&payload
);
957 static ipc_server_application_cb handle_client
;
959 static int handle_client(void *data
,
960 const char *command
, size_t command_len
,
961 ipc_server_reply_cb
*reply
,
962 struct ipc_server_reply_data
*reply_data
)
964 struct fsmonitor_daemon_state
*state
= data
;
968 * The Simple IPC API now supports {char*, len} arguments, but
969 * FSMonitor always uses proper null-terminated strings, so
970 * we can ignore the command_len argument. (Trust, but verify.)
972 if (command_len
!= strlen(command
))
973 BUG("FSMonitor assumes text messages");
975 trace_printf_key(&trace_fsmonitor
, "requested token: %s", command
);
977 trace2_region_enter("fsmonitor", "handle_client", the_repository
);
978 trace2_data_string("fsmonitor", the_repository
, "request", command
);
980 result
= do_handle_client(state
, command
, reply
, reply_data
);
982 trace2_region_leave("fsmonitor", "handle_client", the_repository
);
987 #define FSMONITOR_DIR "fsmonitor--daemon"
988 #define FSMONITOR_COOKIE_DIR "cookies"
989 #define FSMONITOR_COOKIE_PREFIX (FSMONITOR_DIR "/" FSMONITOR_COOKIE_DIR "/")
991 enum fsmonitor_path_type
fsmonitor_classify_path_workdir_relative(
994 if (fspathncmp(rel
, ".git", 4))
995 return IS_WORKDIR_PATH
;
1001 return IS_WORKDIR_PATH
; /* e.g. .gitignore */
1004 if (!fspathncmp(rel
, FSMONITOR_COOKIE_PREFIX
,
1005 strlen(FSMONITOR_COOKIE_PREFIX
)))
1006 return IS_INSIDE_DOT_GIT_WITH_COOKIE_PREFIX
;
1008 return IS_INSIDE_DOT_GIT
;
1011 enum fsmonitor_path_type
fsmonitor_classify_path_gitdir_relative(
1014 if (!fspathncmp(rel
, FSMONITOR_COOKIE_PREFIX
,
1015 strlen(FSMONITOR_COOKIE_PREFIX
)))
1016 return IS_INSIDE_GITDIR_WITH_COOKIE_PREFIX
;
1018 return IS_INSIDE_GITDIR
;
1021 static enum fsmonitor_path_type
try_classify_workdir_abs_path(
1022 struct fsmonitor_daemon_state
*state
,
1027 if (fspathncmp(path
, state
->path_worktree_watch
.buf
,
1028 state
->path_worktree_watch
.len
))
1029 return IS_OUTSIDE_CONE
;
1031 rel
= path
+ state
->path_worktree_watch
.len
;
1034 return IS_WORKDIR_PATH
; /* it is the root dir exactly */
1036 return IS_OUTSIDE_CONE
;
1039 return fsmonitor_classify_path_workdir_relative(rel
);
1042 enum fsmonitor_path_type
fsmonitor_classify_path_absolute(
1043 struct fsmonitor_daemon_state
*state
,
1047 enum fsmonitor_path_type t
;
1049 t
= try_classify_workdir_abs_path(state
, path
);
1050 if (state
->nr_paths_watching
== 1)
1052 if (t
!= IS_OUTSIDE_CONE
)
1055 if (fspathncmp(path
, state
->path_gitdir_watch
.buf
,
1056 state
->path_gitdir_watch
.len
))
1057 return IS_OUTSIDE_CONE
;
1059 rel
= path
+ state
->path_gitdir_watch
.len
;
1062 return IS_GITDIR
; /* it is the <gitdir> exactly */
1064 return IS_OUTSIDE_CONE
;
1067 return fsmonitor_classify_path_gitdir_relative(rel
);
1071 * We try to combine small batches at the front of the batch-list to avoid
1072 * having a long list. This hopefully makes it a little easier when we want
1073 * to truncate and maintain the list. However, we don't want the paths array
1074 * to just keep growing and growing with realloc, so we insert an arbitrary
1077 #define MY_COMBINE_LIMIT (1024)
1079 void fsmonitor_publish(struct fsmonitor_daemon_state
*state
,
1080 struct fsmonitor_batch
*batch
,
1081 const struct string_list
*cookie_names
)
1083 if (!batch
&& !cookie_names
->nr
)
1086 pthread_mutex_lock(&state
->main_lock
);
1089 struct fsmonitor_batch
*head
;
1091 head
= state
->current_token_data
->batch_head
;
1093 BUG("token does not have batch");
1094 } else if (head
->pinned_time
) {
1096 * We cannot alter the current batch list
1099 * [a] it is being transmitted to at least one
1100 * client and the handle_client() thread has a
1101 * ref-count, but not a lock on the batch list
1102 * starting with this item.
1104 * [b] it has been transmitted in the past to
1105 * at least one client such that future
1106 * requests are relative to this head batch.
1108 * So, we can only prepend a new batch onto
1109 * the front of the list.
1111 batch
->batch_seq_nr
= head
->batch_seq_nr
+ 1;
1113 state
->current_token_data
->batch_head
= batch
;
1114 } else if (!head
->batch_seq_nr
) {
1116 * Batch 0 is unpinned. See the note in
1117 * `fsmonitor_new_token_data()` about why we
1118 * don't need to accumulate these paths.
1120 fsmonitor_batch__free_list(batch
);
1121 } else if (head
->nr
+ batch
->nr
> MY_COMBINE_LIMIT
) {
1123 * The head batch in the list has never been
1124 * transmitted to a client, but folding the
1125 * contents of the new batch onto it would
1126 * exceed our arbitrary limit, so just prepend
1127 * the new batch onto the list.
1129 batch
->batch_seq_nr
= head
->batch_seq_nr
+ 1;
1131 state
->current_token_data
->batch_head
= batch
;
1134 * We are free to add the paths in the given
1135 * batch onto the end of the current head batch.
1137 fsmonitor_batch__combine(head
, batch
);
1138 fsmonitor_batch__free_list(batch
);
1142 if (cookie_names
->nr
)
1143 with_lock__mark_cookies_seen(state
, cookie_names
);
1145 pthread_mutex_unlock(&state
->main_lock
);
1148 static void *fsm_health__thread_proc(void *_state
)
1150 struct fsmonitor_daemon_state
*state
= _state
;
1152 trace2_thread_start("fsm-health");
1154 fsm_health__loop(state
);
1156 trace2_thread_exit();
1160 static void *fsm_listen__thread_proc(void *_state
)
1162 struct fsmonitor_daemon_state
*state
= _state
;
1164 trace2_thread_start("fsm-listen");
1166 trace_printf_key(&trace_fsmonitor
, "Watching: worktree '%s'",
1167 state
->path_worktree_watch
.buf
);
1168 if (state
->nr_paths_watching
> 1)
1169 trace_printf_key(&trace_fsmonitor
, "Watching: gitdir '%s'",
1170 state
->path_gitdir_watch
.buf
);
1172 fsm_listen__loop(state
);
1174 pthread_mutex_lock(&state
->main_lock
);
1175 if (state
->current_token_data
&&
1176 state
->current_token_data
->client_ref_count
== 0)
1177 fsmonitor_free_token_data(state
->current_token_data
);
1178 state
->current_token_data
= NULL
;
1179 pthread_mutex_unlock(&state
->main_lock
);
1181 trace2_thread_exit();
1185 static int fsmonitor_run_daemon_1(struct fsmonitor_daemon_state
*state
)
1187 struct ipc_server_opts ipc_opts
= {
1188 .nr_threads
= fsmonitor__ipc_threads
,
1191 * We know that there are no other active threads yet,
1192 * so we can let the IPC layer temporarily chdir() if
1193 * it needs to when creating the server side of the
1194 * Unix domain socket.
1196 .uds_disallow_chdir
= 0
1198 int health_started
= 0;
1199 int listener_started
= 0;
1203 * Start the IPC thread pool before the we've started the file
1204 * system event listener thread so that we have the IPC handle
1205 * before we need it.
1207 if (ipc_server_run_async(&state
->ipc_server_data
,
1208 state
->path_ipc
.buf
, &ipc_opts
,
1209 handle_client
, state
))
1211 _("could not start IPC thread pool on '%s'"),
1212 state
->path_ipc
.buf
);
1215 * Start the fsmonitor listener thread to collect filesystem
1218 if (pthread_create(&state
->listener_thread
, NULL
,
1219 fsm_listen__thread_proc
, state
)) {
1220 ipc_server_stop_async(state
->ipc_server_data
);
1221 err
= error(_("could not start fsmonitor listener thread"));
1224 listener_started
= 1;
1227 * Start the health thread to watch over our process.
1229 if (pthread_create(&state
->health_thread
, NULL
,
1230 fsm_health__thread_proc
, state
)) {
1231 ipc_server_stop_async(state
->ipc_server_data
);
1232 err
= error(_("could not start fsmonitor health thread"));
1238 * The daemon is now fully functional in background threads.
1239 * Our primary thread should now just wait while the threads
1244 * Wait for the IPC thread pool to shutdown (whether by client
1245 * request, from filesystem activity, or an error).
1247 ipc_server_await(state
->ipc_server_data
);
1250 * The fsmonitor listener thread may have received a shutdown
1251 * event from the IPC thread pool, but it doesn't hurt to tell
1252 * it again. And wait for it to shutdown.
1254 if (listener_started
) {
1255 fsm_listen__stop_async(state
);
1256 pthread_join(state
->listener_thread
, NULL
);
1259 if (health_started
) {
1260 fsm_health__stop_async(state
);
1261 pthread_join(state
->health_thread
, NULL
);
1266 if (state
->listen_error_code
)
1267 return state
->listen_error_code
;
1268 if (state
->health_error_code
)
1269 return state
->health_error_code
;
1273 static int fsmonitor_run_daemon(void)
1275 struct fsmonitor_daemon_state state
;
1279 memset(&state
, 0, sizeof(state
));
1281 hashmap_init(&state
.cookies
, cookies_cmp
, NULL
, 0);
1282 pthread_mutex_init(&state
.main_lock
, NULL
);
1283 pthread_cond_init(&state
.cookies_cond
, NULL
);
1284 state
.listen_error_code
= 0;
1285 state
.health_error_code
= 0;
1286 state
.current_token_data
= fsmonitor_new_token_data();
1288 /* Prepare to (recursively) watch the <worktree-root> directory. */
1289 strbuf_init(&state
.path_worktree_watch
, 0);
1290 strbuf_addstr(&state
.path_worktree_watch
, absolute_path(get_git_work_tree()));
1291 state
.nr_paths_watching
= 1;
1293 strbuf_init(&state
.alias
.alias
, 0);
1294 strbuf_init(&state
.alias
.points_to
, 0);
1295 if ((err
= fsmonitor__get_alias(state
.path_worktree_watch
.buf
, &state
.alias
)))
1299 * We create and delete cookie files somewhere inside the .git
1300 * directory to help us keep sync with the file system. If
1301 * ".git" is not a directory, then <gitdir> is not inside the
1302 * cone of <worktree-root>, so set up a second watch to watch
1303 * the <gitdir> so that we get events for the cookie files.
1305 strbuf_init(&state
.path_gitdir_watch
, 0);
1306 strbuf_addbuf(&state
.path_gitdir_watch
, &state
.path_worktree_watch
);
1307 strbuf_addstr(&state
.path_gitdir_watch
, "/.git");
1308 if (!is_directory(state
.path_gitdir_watch
.buf
)) {
1309 strbuf_reset(&state
.path_gitdir_watch
);
1310 strbuf_addstr(&state
.path_gitdir_watch
, absolute_path(get_git_dir()));
1311 state
.nr_paths_watching
= 2;
1315 * We will write filesystem syncing cookie files into
1316 * <gitdir>/<fsmonitor-dir>/<cookie-dir>/<pid>-<seq>.
1318 * The extra layers of subdirectories here keep us from
1319 * changing the mtime on ".git/" or ".git/foo/" when we create
1320 * or delete cookie files.
1322 * There have been problems with some IDEs that do a
1323 * non-recursive watch of the ".git/" directory and run a
1324 * series of commands any time something happens.
1326 * For example, if we place our cookie files directly in
1327 * ".git/" or ".git/foo/" then a `git status` (or similar
1328 * command) from the IDE will cause a cookie file to be
1329 * created in one of those dirs. This causes the mtime of
1330 * those dirs to change. This triggers the IDE's watch
1331 * notification. This triggers the IDE to run those commands
1332 * again. And the process repeats and the machine never goes
1335 * Adding the extra layers of subdirectories prevents the
1336 * mtime of ".git/" and ".git/foo" from changing when a
1337 * cookie file is created.
1339 strbuf_init(&state
.path_cookie_prefix
, 0);
1340 strbuf_addbuf(&state
.path_cookie_prefix
, &state
.path_gitdir_watch
);
1342 strbuf_addch(&state
.path_cookie_prefix
, '/');
1343 strbuf_addstr(&state
.path_cookie_prefix
, FSMONITOR_DIR
);
1344 mkdir(state
.path_cookie_prefix
.buf
, 0777);
1346 strbuf_addch(&state
.path_cookie_prefix
, '/');
1347 strbuf_addstr(&state
.path_cookie_prefix
, FSMONITOR_COOKIE_DIR
);
1348 mkdir(state
.path_cookie_prefix
.buf
, 0777);
1350 strbuf_addch(&state
.path_cookie_prefix
, '/');
1353 * We create a named-pipe or unix domain socket inside of the
1354 * ".git" directory. (Well, on Windows, we base our named
1355 * pipe in the NPFS on the absolute path of the git
1358 strbuf_init(&state
.path_ipc
, 0);
1359 strbuf_addstr(&state
.path_ipc
,
1360 absolute_path(fsmonitor_ipc__get_path(the_repository
)));
1363 * Confirm that we can create platform-specific resources for the
1364 * filesystem listener before we bother starting all the threads.
1366 if (fsm_listen__ctor(&state
)) {
1367 err
= error(_("could not initialize listener thread"));
1371 if (fsm_health__ctor(&state
)) {
1372 err
= error(_("could not initialize health thread"));
1377 * CD out of the worktree root directory.
1379 * The common Git startup mechanism causes our CWD to be the
1380 * root of the worktree. On Windows, this causes our process
1381 * to hold a locked handle on the CWD. This prevents the
1382 * worktree from being moved or deleted while the daemon is
1385 * We assume that our FS and IPC listener threads have either
1386 * opened all of the handles that they need or will do
1387 * everything using absolute paths.
1389 home
= getenv("HOME");
1390 if (home
&& *home
&& chdir(home
))
1391 die_errno(_("could not cd home '%s'"), home
);
1393 err
= fsmonitor_run_daemon_1(&state
);
1396 pthread_cond_destroy(&state
.cookies_cond
);
1397 pthread_mutex_destroy(&state
.main_lock
);
1398 fsm_listen__dtor(&state
);
1399 fsm_health__dtor(&state
);
1401 ipc_server_free(state
.ipc_server_data
);
1403 strbuf_release(&state
.path_worktree_watch
);
1404 strbuf_release(&state
.path_gitdir_watch
);
1405 strbuf_release(&state
.path_cookie_prefix
);
1406 strbuf_release(&state
.path_ipc
);
1407 strbuf_release(&state
.alias
.alias
);
1408 strbuf_release(&state
.alias
.points_to
);
1413 static int try_to_run_foreground_daemon(int detach_console
)
1416 * Technically, we don't need to probe for an existing daemon
1417 * process, since we could just call `fsmonitor_run_daemon()`
1418 * and let it fail if the pipe/socket is busy.
1420 * However, this method gives us a nicer error message for a
1421 * common error case.
1423 if (fsmonitor_ipc__get_state() == IPC_STATE__LISTENING
)
1424 die(_("fsmonitor--daemon is already running '%s'"),
1425 the_repository
->worktree
);
1427 if (fsmonitor__announce_startup
) {
1428 fprintf(stderr
, _("running fsmonitor-daemon in '%s'\n"),
1429 the_repository
->worktree
);
1433 #ifdef GIT_WINDOWS_NATIVE
1438 return !!fsmonitor_run_daemon();
1441 static start_bg_wait_cb bg_wait_cb
;
1443 static int bg_wait_cb(const struct child_process
*cp
, void *cb_data
)
1445 enum ipc_active_state s
= fsmonitor_ipc__get_state();
1448 case IPC_STATE__LISTENING
:
1449 /* child is "ready" */
1452 case IPC_STATE__NOT_LISTENING
:
1453 case IPC_STATE__PATH_NOT_FOUND
:
1454 /* give child more time */
1458 case IPC_STATE__INVALID_PATH
:
1459 case IPC_STATE__OTHER_ERROR
:
1460 /* all the time in world won't help */
1465 static int try_to_start_background_daemon(void)
1467 struct child_process cp
= CHILD_PROCESS_INIT
;
1468 enum start_bg_result sbgr
;
1471 * Before we try to create a background daemon process, see
1472 * if a daemon process is already listening. This makes it
1473 * easier for us to report an already-listening error to the
1474 * console, since our spawn/daemon can only report the success
1475 * of creating the background process (and not whether it
1476 * immediately exited).
1478 if (fsmonitor_ipc__get_state() == IPC_STATE__LISTENING
)
1479 die(_("fsmonitor--daemon is already running '%s'"),
1480 the_repository
->worktree
);
1482 if (fsmonitor__announce_startup
) {
1483 fprintf(stderr
, _("starting fsmonitor-daemon in '%s'\n"),
1484 the_repository
->worktree
);
1490 strvec_push(&cp
.args
, "fsmonitor--daemon");
1491 strvec_push(&cp
.args
, "run");
1492 strvec_push(&cp
.args
, "--detach");
1493 strvec_pushf(&cp
.args
, "--ipc-threads=%d", fsmonitor__ipc_threads
);
1499 sbgr
= start_bg_command(&cp
, bg_wait_cb
, NULL
,
1500 fsmonitor__start_timeout_sec
);
1509 return error(_("daemon failed to start"));
1512 return error(_("daemon not online yet"));
1515 return error(_("daemon terminated"));
1519 int cmd_fsmonitor__daemon(int argc
, const char **argv
, const char *prefix
)
1522 enum fsmonitor_reason reason
;
1523 int detach_console
= 0;
1525 struct option options
[] = {
1526 OPT_BOOL(0, "detach", &detach_console
, N_("detach from console")),
1527 OPT_INTEGER(0, "ipc-threads",
1528 &fsmonitor__ipc_threads
,
1529 N_("use <n> ipc worker threads")),
1530 OPT_INTEGER(0, "start-timeout",
1531 &fsmonitor__start_timeout_sec
,
1532 N_("max seconds to wait for background daemon startup")),
1537 git_config(fsmonitor_config
, NULL
);
1539 argc
= parse_options(argc
, argv
, prefix
, options
,
1540 builtin_fsmonitor__daemon_usage
, 0);
1542 usage_with_options(builtin_fsmonitor__daemon_usage
, options
);
1545 if (fsmonitor__ipc_threads
< 1)
1546 die(_("invalid 'ipc-threads' value (%d)"),
1547 fsmonitor__ipc_threads
);
1549 prepare_repo_settings(the_repository
);
1551 * If the repo is fsmonitor-compatible, explicitly set IPC-mode
1552 * (without bothering to load the `core.fsmonitor` config settings).
1554 * If the repo is not compatible, the repo-settings will be set to
1555 * incompatible rather than IPC, so we can use one of the __get
1556 * routines to detect the discrepancy.
1558 fsm_settings__set_ipc(the_repository
);
1560 reason
= fsm_settings__get_reason(the_repository
);
1561 if (reason
> FSMONITOR_REASON_OK
)
1563 fsm_settings__get_incompatible_msg(the_repository
,
1566 if (!strcmp(subcmd
, "start"))
1567 return !!try_to_start_background_daemon();
1569 if (!strcmp(subcmd
, "run"))
1570 return !!try_to_run_foreground_daemon(detach_console
);
1572 if (!strcmp(subcmd
, "stop"))
1573 return !!do_as_client__send_stop();
1575 if (!strcmp(subcmd
, "status"))
1576 return !!do_as_client__status();
1578 die(_("Unhandled subcommand '%s'"), subcmd
);
1582 int cmd_fsmonitor__daemon(int argc
, const char **argv
, const char *prefix UNUSED
)
1584 struct option options
[] = {
1588 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1589 usage_with_options(builtin_fsmonitor__daemon_usage
, options
);
1591 die(_("fsmonitor--daemon not supported on this platform"));