3 #include "parse-options.h"
5 #include "fsmonitor-ipc.h"
6 #include "compat/fsmonitor/fsmonitor-fs-listen.h"
7 #include "fsmonitor--daemon.h"
8 #include "simple-ipc.h"
12 static const char * const builtin_fsmonitor__daemon_usage[] = {
13 N_("git fsmonitor--daemon start [<options>]"),
14 N_("git fsmonitor--daemon run [<options>]"),
15 N_("git fsmonitor--daemon stop"),
16 N_("git fsmonitor--daemon status"),
20 #ifdef HAVE_FSMONITOR_DAEMON_BACKEND
22 * Global state loaded from config.
24 #define FSMONITOR__IPC_THREADS "fsmonitor.ipcthreads"
25 static int fsmonitor__ipc_threads = 8;
27 #define FSMONITOR__START_TIMEOUT "fsmonitor.starttimeout"
28 static int fsmonitor__start_timeout_sec = 60;
30 static int fsmonitor_config(const char *var, const char *value, void *cb)
32 if (!strcmp(var, FSMONITOR__IPC_THREADS)) {
33 int i = git_config_int(var, value);
35 return error(_("value of '%s' out of range: %d"),
36 FSMONITOR__IPC_THREADS, i);
37 fsmonitor__ipc_threads = i;
41 if (!strcmp(var, FSMONITOR__START_TIMEOUT)) {
42 int i = git_config_int(var, value);
44 return error(_("value of '%s' out of range: %d"),
45 FSMONITOR__START_TIMEOUT, i);
46 fsmonitor__start_timeout_sec = i;
50 return git_default_config(var, value, cb);
56 * Send a "quit" command to the `git-fsmonitor--daemon` (if running)
57 * and wait for it to shutdown.
59 static int do_as_client__send_stop(void)
61 struct strbuf answer = STRBUF_INIT;
64 ret = fsmonitor_ipc__send_command("quit", &answer);
66 /* The quit command does not return any response data. */
67 strbuf_release(&answer);
72 trace2_region_enter("fsm_client", "polling-for-daemon-exit", NULL);
73 while (fsmonitor_ipc__get_state() == IPC_STATE__LISTENING)
75 trace2_region_leave("fsm_client", "polling-for-daemon-exit", NULL);
80 static int do_as_client__status(void)
82 enum ipc_active_state state = fsmonitor_ipc__get_state();
85 case IPC_STATE__LISTENING:
86 printf(_("The built-in file system monitor is active\n"));
90 printf(_("The built-in file system monitor is not active\n"));
95 enum fsmonitor_cookie_item_result {
96 FCIR_ERROR = -1, /* could not create cookie file ? */
102 struct fsmonitor_cookie_item {
103 struct hashmap_entry entry;
105 enum fsmonitor_cookie_item_result result;
108 static int cookies_cmp(const void *data, const struct hashmap_entry *he1,
109 const struct hashmap_entry *he2, const void *keydata)
111 const struct fsmonitor_cookie_item *a =
112 container_of(he1, const struct fsmonitor_cookie_item, entry);
113 const struct fsmonitor_cookie_item *b =
114 container_of(he2, const struct fsmonitor_cookie_item, entry);
116 return strcmp(a->name, keydata ? keydata : b->name);
119 static enum fsmonitor_cookie_item_result with_lock__wait_for_cookie(
120 struct fsmonitor_daemon_state *state)
122 /* assert current thread holding state->main_lock */
125 struct fsmonitor_cookie_item *cookie;
126 struct strbuf cookie_pathname = STRBUF_INIT;
127 struct strbuf cookie_filename = STRBUF_INIT;
128 enum fsmonitor_cookie_item_result result;
131 CALLOC_ARRAY(cookie, 1);
133 my_cookie_seq = state->cookie_seq++;
135 strbuf_addf(&cookie_filename, "%i-%i", getpid(), my_cookie_seq);
137 strbuf_addbuf(&cookie_pathname, &state->path_cookie_prefix);
138 strbuf_addbuf(&cookie_pathname, &cookie_filename);
140 cookie->name = strbuf_detach(&cookie_filename, NULL);
141 cookie->result = FCIR_INIT;
142 hashmap_entry_init(&cookie->entry, strhash(cookie->name));
144 hashmap_add(&state->cookies, &cookie->entry);
146 trace_printf_key(&trace_fsmonitor, "cookie-wait: '%s' '%s'",
147 cookie->name, cookie_pathname.buf);
150 * Create the cookie file on disk and then wait for a notification
151 * that the listener thread has seen it.
153 fd = open(cookie_pathname.buf, O_WRONLY | O_CREAT | O_EXCL, 0600);
156 unlink(cookie_pathname.buf);
159 * NEEDSWORK: This is an infinite wait (well, unless another
160 * thread sends us an abort). I'd like to change this to
161 * use `pthread_cond_timedwait()` and return an error/timeout
162 * and let the caller do the trivial response thing.
164 while (cookie->result == FCIR_INIT)
165 pthread_cond_wait(&state->cookies_cond,
168 error_errno(_("could not create fsmonitor cookie '%s'"),
171 cookie->result = FCIR_ERROR;
174 hashmap_remove(&state->cookies, &cookie->entry, NULL);
176 result = cookie->result;
178 free((char*)cookie->name);
180 strbuf_release(&cookie_pathname);
186 * Mark these cookies as _SEEN and wake up the corresponding client threads.
188 static void with_lock__mark_cookies_seen(struct fsmonitor_daemon_state *state,
189 const struct string_list *cookie_names)
191 /* assert current thread holding state->main_lock */
196 for (k = 0; k < cookie_names->nr; k++) {
197 struct fsmonitor_cookie_item key;
198 struct fsmonitor_cookie_item *cookie;
200 key.name = cookie_names->items[k].string;
201 hashmap_entry_init(&key.entry, strhash(key.name));
203 cookie = hashmap_get_entry(&state->cookies, &key, entry, NULL);
205 trace_printf_key(&trace_fsmonitor, "cookie-seen: '%s'",
207 cookie->result = FCIR_SEEN;
213 pthread_cond_broadcast(&state->cookies_cond);
217 * Set _ABORT on all pending cookies and wake up all client threads.
219 static void with_lock__abort_all_cookies(struct fsmonitor_daemon_state *state)
221 /* assert current thread holding state->main_lock */
223 struct hashmap_iter iter;
224 struct fsmonitor_cookie_item *cookie;
227 hashmap_for_each_entry(&state->cookies, &iter, cookie, entry) {
228 trace_printf_key(&trace_fsmonitor, "cookie-abort: '%s'",
230 cookie->result = FCIR_ABORT;
235 pthread_cond_broadcast(&state->cookies_cond);
239 * Requests to and from a FSMonitor Protocol V2 provider use an opaque
240 * "token" as a virtual timestamp. Clients can request a summary of all
241 * created/deleted/modified files relative to a token. In the response,
242 * clients receive a new token for the next (relative) request.
248 * The contents of the token are private and provider-specific.
250 * For the built-in fsmonitor--daemon, we define a token as follows:
252 * "builtin" ":" <token_id> ":" <sequence_nr>
254 * The "builtin" prefix is used as a namespace to avoid conflicts
255 * with other providers (such as Watchman).
257 * The <token_id> is an arbitrary OPAQUE string, such as a GUID,
258 * UUID, or {timestamp,pid}. It is used to group all filesystem
259 * events that happened while the daemon was monitoring (and in-sync
260 * with the filesystem).
262 * Unlike FSMonitor Protocol V1, it is not defined as a timestamp
263 * and does not define less-than/greater-than relationships.
264 * (There are too many race conditions to rely on file system
267 * The <sequence_nr> is a simple integer incremented whenever the
268 * daemon needs to make its state public. For example, if 1000 file
269 * system events come in, but no clients have requested the data,
270 * the daemon can continue to accumulate file changes in the same
271 * bin and does not need to advance the sequence number. However,
272 * as soon as a client does arrive, the daemon needs to start a new
273 * bin and increment the sequence number.
275 * The sequence number serves as the boundary between 2 sets
276 * of bins -- the older ones that the client has already seen
277 * and the newer ones that it hasn't.
279 * When a new <token_id> is created, the <sequence_nr> is reset to
286 * A new token_id is created:
288 * [1] each time the daemon is started.
290 * [2] any time that the daemon must re-sync with the filesystem
291 * (such as when the kernel drops or we miss events on a very
294 * [3] in response to a client "flush" command (for dropped event
297 * When a new token_id is created, the daemon is free to discard all
298 * cached filesystem events associated with any previous token_ids.
299 * Events associated with a non-current token_id will never be sent
300 * to a client. A token_id change implicitly means that the daemon
301 * has gap in its event history.
303 * Therefore, clients that present a token with a stale (non-current)
304 * token_id will always be given a trivial response.
306 struct fsmonitor_token_data {
307 struct strbuf token_id;
308 struct fsmonitor_batch *batch_head;
309 struct fsmonitor_batch *batch_tail;
310 uint64_t client_ref_count;
313 struct fsmonitor_batch {
314 struct fsmonitor_batch *next;
315 uint64_t batch_seq_nr;
316 const char **interned_paths;
321 static struct fsmonitor_token_data *fsmonitor_new_token_data(void)
323 static int test_env_value = -1;
324 static uint64_t flush_count = 0;
325 struct fsmonitor_token_data *token;
326 struct fsmonitor_batch *batch;
328 CALLOC_ARRAY(token, 1);
329 batch = fsmonitor_batch__new();
331 strbuf_init(&token->token_id, 0);
332 token->batch_head = batch;
333 token->batch_tail = batch;
334 token->client_ref_count = 0;
336 if (test_env_value < 0)
337 test_env_value = git_env_bool("GIT_TEST_FSMONITOR_TOKEN", 0);
339 if (!test_env_value) {
344 gettimeofday(&tv, NULL);
346 gmtime_r(&secs, &tm);
348 strbuf_addf(&token->token_id,
349 "%"PRIu64".%d.%4d%02d%02dT%02d%02d%02d.%06ldZ",
352 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
353 tm.tm_hour, tm.tm_min, tm.tm_sec,
356 strbuf_addf(&token->token_id, "test_%08x", test_env_value++);
360 * We created a new <token_id> and are starting a new series
361 * of tokens with a zero <seq_nr>.
363 * Since clients cannot guess our new (non test) <token_id>
364 * they will always receive a trivial response (because of the
365 * mismatch on the <token_id>). The trivial response will
366 * tell them our new <token_id> so that subsequent requests
367 * will be relative to our new series. (And when sending that
368 * response, we pin the current head of the batch list.)
370 * Even if the client correctly guesses the <token_id>, their
371 * request of "builtin:<token_id>:0" asks for all changes MORE
372 * RECENT than batch/bin 0.
374 * This implies that it is a waste to accumulate paths in the
375 * initial batch/bin (because they will never be transmitted).
377 * So the daemon could be running for days and watching the
378 * file system, but doesn't need to actually accumulate any
379 * paths UNTIL we need to set a reference point for a later
382 * However, it is very useful for testing to always have a
383 * reference point set. Pin batch 0 to force early file system
384 * events to accumulate.
387 batch->pinned_time = time(NULL);
392 struct fsmonitor_batch *fsmonitor_batch__new(void)
394 struct fsmonitor_batch *batch;
396 CALLOC_ARRAY(batch, 1);
401 struct fsmonitor_batch *fsmonitor_batch__pop(struct fsmonitor_batch *batch)
403 struct fsmonitor_batch *next;
411 * The actual strings within the array are interned, so we don't
414 free(batch->interned_paths);
419 void fsmonitor_batch__add_path(struct fsmonitor_batch *batch,
422 const char *interned_path = strintern(path);
424 trace_printf_key(&trace_fsmonitor, "event: %s", interned_path);
426 ALLOC_GROW(batch->interned_paths, batch->nr + 1, batch->alloc);
427 batch->interned_paths[batch->nr++] = interned_path;
430 static void fsmonitor_batch__combine(struct fsmonitor_batch *batch_dest,
431 const struct fsmonitor_batch *batch_src)
435 ALLOC_GROW(batch_dest->interned_paths,
436 batch_dest->nr + batch_src->nr + 1,
439 for (k = 0; k < batch_src->nr; k++)
440 batch_dest->interned_paths[batch_dest->nr++] =
441 batch_src->interned_paths[k];
445 * To keep the batch list from growing unbounded in response to filesystem
446 * activity, we try to truncate old batches from the end of the list as
447 * they become irrelevant.
449 * We assume that the .git/index will be updated with the most recent token
450 * any time the index is updated. And future commands will only ask for
451 * recent changes *since* that new token. So as tokens advance into the
452 * future, older batch items will never be requested/needed. So we can
453 * truncate them without loss of functionality.
455 * However, multiple commands may be talking to the daemon concurrently
456 * or perform a slow command, so a little "token skew" is possible.
457 * Therefore, we want this to be a little bit lazy and have a generous
460 * The current reader thread walked backwards in time from `token->batch_head`
461 * back to `batch_marker` somewhere in the middle of the batch list.
463 * Let's walk backwards in time from that marker an arbitrary delay
464 * and truncate the list there. Note that these timestamps are completely
465 * artificial (based on when we pinned the batch item) and not on any
466 * filesystem activity.
468 #define MY_TIME_DELAY_SECONDS (5 * 60) /* seconds */
470 static void with_lock__truncate_old_batches(
471 struct fsmonitor_daemon_state *state,
472 const struct fsmonitor_batch *batch_marker)
474 /* assert current thread holding state->main_lock */
476 const struct fsmonitor_batch *batch;
477 struct fsmonitor_batch *rest;
478 struct fsmonitor_batch *p;
483 trace_printf_key(&trace_fsmonitor, "Truncate: mark (%"PRIu64",%"PRIu64")",
484 batch_marker->batch_seq_nr,
485 (uint64_t)batch_marker->pinned_time);
487 for (batch = batch_marker; batch; batch = batch->next) {
490 if (!batch->pinned_time) /* an overflow batch */
493 t = batch->pinned_time + MY_TIME_DELAY_SECONDS;
494 if (t > batch_marker->pinned_time) /* too close to marker */
497 goto truncate_past_here;
503 state->current_token_data->batch_tail = (struct fsmonitor_batch *)batch;
505 rest = ((struct fsmonitor_batch *)batch)->next;
506 ((struct fsmonitor_batch *)batch)->next = NULL;
508 for (p = rest; p; p = fsmonitor_batch__pop(p)) {
509 trace_printf_key(&trace_fsmonitor,
510 "Truncate: kill (%"PRIu64",%"PRIu64")",
511 p->batch_seq_nr, (uint64_t)p->pinned_time);
515 static void fsmonitor_free_token_data(struct fsmonitor_token_data *token)
517 struct fsmonitor_batch *p;
522 assert(token->client_ref_count == 0);
524 strbuf_release(&token->token_id);
526 for (p = token->batch_head; p; p = fsmonitor_batch__pop(p))
533 * Flush all of our cached data about the filesystem. Call this if we
534 * lose sync with the filesystem and miss some notification events.
536 * [1] If we are missing events, then we no longer have a complete
537 * history of the directory (relative to our current start token).
538 * We should create a new token and start fresh (as if we just
541 * [2] Some of those lost events may have been for cookie files. We
542 * should assume the worst and abort them rather letting them starve.
544 * If there are no concurrent threads readering the current token data
545 * series, we can free it now. Otherwise, let the last reader free
548 * Either way, the old token data series is no longer associated with
551 static void with_lock__do_force_resync(struct fsmonitor_daemon_state *state)
553 /* assert current thread holding state->main_lock */
555 struct fsmonitor_token_data *free_me = NULL;
556 struct fsmonitor_token_data *new_one = NULL;
558 new_one = fsmonitor_new_token_data();
560 if (state->current_token_data->client_ref_count == 0)
561 free_me = state->current_token_data;
562 state->current_token_data = new_one;
564 fsmonitor_free_token_data(free_me);
566 with_lock__abort_all_cookies(state);
569 void fsmonitor_force_resync(struct fsmonitor_daemon_state *state)
571 pthread_mutex_lock(&state->main_lock);
572 with_lock__do_force_resync(state);
573 pthread_mutex_unlock(&state->main_lock);
577 * Format an opaque token string to send to the client.
579 static void with_lock__format_response_token(
580 struct strbuf *response_token,
581 const struct strbuf *response_token_id,
582 const struct fsmonitor_batch *batch)
584 /* assert current thread holding state->main_lock */
586 strbuf_reset(response_token);
587 strbuf_addf(response_token, "builtin:%s:%"PRIu64,
588 response_token_id->buf, batch->batch_seq_nr);
592 * Parse an opaque token from the client.
593 * Returns -1 on error.
595 static int fsmonitor_parse_client_token(const char *buf_token,
596 struct strbuf *requested_token_id,
602 strbuf_reset(requested_token_id);
605 if (!skip_prefix(buf_token, "builtin:", &p))
608 while (*p && *p != ':')
609 strbuf_addch(requested_token_id, *p++);
613 *seq_nr = (uint64_t)strtoumax(p, &p_end, 10);
620 KHASH_INIT(str, const char *, int, 0, kh_str_hash_func, kh_str_hash_equal);
622 static int do_handle_client(struct fsmonitor_daemon_state *state,
624 ipc_server_reply_cb *reply,
625 struct ipc_server_reply_data *reply_data)
627 struct fsmonitor_token_data *token_data = NULL;
628 struct strbuf response_token = STRBUF_INIT;
629 struct strbuf requested_token_id = STRBUF_INIT;
630 struct strbuf payload = STRBUF_INIT;
631 uint64_t requested_oldest_seq_nr = 0;
632 uint64_t total_response_len = 0;
634 const struct fsmonitor_batch *batch_head;
635 const struct fsmonitor_batch *batch;
636 intmax_t count = 0, duplicates = 0;
642 enum fsmonitor_cookie_item_result cookie_result;
645 * We expect `command` to be of the form:
647 * <command> := quit NUL
649 * | <V1-time-since-epoch-ns> NUL
650 * | <V2-opaque-fsmonitor-token> NUL
653 if (!strcmp(command, "quit")) {
655 * A client has requested over the socket/pipe that the
658 * Tell the IPC thread pool to shutdown (which completes
659 * the await in the main thread (which can stop the
660 * fsmonitor listener thread)).
662 * There is no reply to the client.
664 return SIMPLE_IPC_QUIT;
666 } else if (!strcmp(command, "flush")) {
668 * Flush all of our cached data and generate a new token
669 * just like if we lost sync with the filesystem.
671 * Then send a trivial response using the new token.
677 } else if (!skip_prefix(command, "builtin:", &p)) {
678 /* assume V1 timestamp or garbage */
682 strtoumax(command, &p_end, 10);
683 trace_printf_key(&trace_fsmonitor,
685 "fsmonitor: invalid command line '%s'" :
686 "fsmonitor: unsupported V1 protocol '%s'"),
692 /* We have "builtin:*" */
693 if (fsmonitor_parse_client_token(command, &requested_token_id,
694 &requested_oldest_seq_nr)) {
695 trace_printf_key(&trace_fsmonitor,
696 "fsmonitor: invalid V2 protocol token '%s'",
703 * We have a V2 valid token:
704 * "builtin:<token_id>:<seq_nr>"
710 pthread_mutex_lock(&state->main_lock);
712 if (!state->current_token_data)
713 BUG("fsmonitor state does not have a current token");
716 * Write a cookie file inside the directory being watched in
717 * an effort to flush out existing filesystem events that we
718 * actually care about. Suspend this client thread until we
719 * see the filesystem events for this cookie file.
721 * Creating the cookie lets us guarantee that our FS listener
722 * thread has drained the kernel queue and we are caught up
725 * If we cannot create the cookie (or otherwise guarantee that
726 * we are caught up), we send a trivial response. We have to
727 * assume that there might be some very, very recent activity
728 * on the FS still in flight.
731 cookie_result = with_lock__wait_for_cookie(state);
732 if (cookie_result != FCIR_SEEN) {
733 error(_("fsmonitor: cookie_result '%d' != SEEN"),
740 with_lock__do_force_resync(state);
743 * We mark the current head of the batch list as "pinned" so
744 * that the listener thread will treat this item as read-only
745 * (and prevent any more paths from being added to it) from
748 token_data = state->current_token_data;
749 batch_head = token_data->batch_head;
750 ((struct fsmonitor_batch *)batch_head)->pinned_time = time(NULL);
753 * FSMonitor Protocol V2 requires that we send a response header
754 * with a "new current token" and then all of the paths that changed
755 * since the "requested token". We send the seq_nr of the just-pinned
756 * head batch so that future requests from a client will be relative
759 with_lock__format_response_token(&response_token,
760 &token_data->token_id, batch_head);
762 reply(reply_data, response_token.buf, response_token.len + 1);
763 total_response_len += response_token.len + 1;
765 trace2_data_string("fsmonitor", the_repository, "response/token",
767 trace_printf_key(&trace_fsmonitor, "response token: %s",
771 if (strcmp(requested_token_id.buf, token_data->token_id.buf)) {
773 * The client last spoke to a different daemon
774 * instance -OR- the daemon had to resync with
775 * the filesystem (and lost events), so reject.
777 trace2_data_string("fsmonitor", the_repository,
778 "response/token", "different");
781 } else if (requested_oldest_seq_nr <
782 token_data->batch_tail->batch_seq_nr) {
784 * The client wants older events than we have for
785 * this token_id. This means that the end of our
786 * batch list was truncated and we cannot give the
787 * client a complete snapshot relative to their
790 trace_printf_key(&trace_fsmonitor,
791 "client requested truncated data");
797 pthread_mutex_unlock(&state->main_lock);
799 reply(reply_data, "/", 2);
801 trace2_data_intmax("fsmonitor", the_repository,
802 "response/trivial", 1);
804 strbuf_release(&response_token);
805 strbuf_release(&requested_token_id);
810 * We're going to hold onto a pointer to the current
811 * token-data while we walk the list of batches of files.
812 * During this time, we will NOT be under the lock.
813 * So we ref-count it.
815 * This allows the listener thread to continue prepending
816 * new batches of items to the token-data (which we'll ignore).
818 * AND it allows the listener thread to do a token-reset
819 * (and install a new `current_token_data`).
821 token_data->client_ref_count++;
823 pthread_mutex_unlock(&state->main_lock);
826 * The client request is relative to the token that they sent,
827 * so walk the batch list backwards from the current head back
828 * to the batch (sequence number) they named.
830 * We use khash to de-dup the list of pathnames.
832 * NEEDSWORK: each batch contains a list of interned strings,
833 * so we only need to do pointer comparisons here to build the
834 * hash table. Currently, we're still comparing the string
837 shown = kh_init_str();
838 for (batch = batch_head;
839 batch && batch->batch_seq_nr > requested_oldest_seq_nr;
840 batch = batch->next) {
843 for (k = 0; k < batch->nr; k++) {
844 const char *s = batch->interned_paths[k];
847 if (kh_get_str(shown, s) != kh_end(shown))
850 kh_put_str(shown, s, &hash_ret);
852 trace_printf_key(&trace_fsmonitor,
853 "send[%"PRIuMAX"]: %s",
856 /* Each path gets written with a trailing NUL */
857 s_len = strlen(s) + 1;
859 if (payload.len + s_len >=
860 LARGE_PACKET_DATA_MAX) {
861 reply(reply_data, payload.buf,
863 total_response_len += payload.len;
864 strbuf_reset(&payload);
867 strbuf_add(&payload, s, s_len);
874 reply(reply_data, payload.buf, payload.len);
875 total_response_len += payload.len;
878 kh_release_str(shown);
880 pthread_mutex_lock(&state->main_lock);
882 if (token_data->client_ref_count > 0)
883 token_data->client_ref_count--;
885 if (token_data->client_ref_count == 0) {
886 if (token_data != state->current_token_data) {
888 * The listener thread did a token-reset while we were
889 * walking the batch list. Therefore, this token is
890 * stale and can be discarded completely. If we are
891 * the last reader thread using this token, we own
894 fsmonitor_free_token_data(token_data);
897 * This batch is the first item in the list
898 * that is older than the requested sequence
899 * number and might be considered to be
900 * obsolete. See if we can truncate the list
901 * and save some memory.
903 with_lock__truncate_old_batches(state, batch);
907 pthread_mutex_unlock(&state->main_lock);
909 trace2_data_intmax("fsmonitor", the_repository, "response/length", total_response_len);
910 trace2_data_intmax("fsmonitor", the_repository, "response/count/files", count);
911 trace2_data_intmax("fsmonitor", the_repository, "response/count/duplicates", duplicates);
913 strbuf_release(&response_token);
914 strbuf_release(&requested_token_id);
915 strbuf_release(&payload);
920 static ipc_server_application_cb handle_client;
922 static int handle_client(void *data,
923 const char *command, size_t command_len,
924 ipc_server_reply_cb *reply,
925 struct ipc_server_reply_data *reply_data)
927 struct fsmonitor_daemon_state *state = data;
931 * The Simple IPC API now supports {char*, len} arguments, but
932 * FSMonitor always uses proper null-terminated strings, so
933 * we can ignore the command_len argument. (Trust, but verify.)
935 if (command_len != strlen(command))
936 BUG("FSMonitor assumes text messages");
938 trace_printf_key(&trace_fsmonitor, "requested token: %s", command);
940 trace2_region_enter("fsmonitor", "handle_client", the_repository);
941 trace2_data_string("fsmonitor", the_repository, "request", command);
943 result = do_handle_client(state, command, reply, reply_data);
945 trace2_region_leave("fsmonitor", "handle_client", the_repository);
950 #define FSMONITOR_DIR "fsmonitor--daemon"
951 #define FSMONITOR_COOKIE_DIR "cookies"
952 #define FSMONITOR_COOKIE_PREFIX (FSMONITOR_DIR "/" FSMONITOR_COOKIE_DIR "/")
954 enum fsmonitor_path_type fsmonitor_classify_path_workdir_relative(
957 if (fspathncmp(rel, ".git", 4))
958 return IS_WORKDIR_PATH;
964 return IS_WORKDIR_PATH; /* e.g. .gitignore */
967 if (!fspathncmp(rel, FSMONITOR_COOKIE_PREFIX,
968 strlen(FSMONITOR_COOKIE_PREFIX)))
969 return IS_INSIDE_DOT_GIT_WITH_COOKIE_PREFIX;
971 return IS_INSIDE_DOT_GIT;
974 enum fsmonitor_path_type fsmonitor_classify_path_gitdir_relative(
977 if (!fspathncmp(rel, FSMONITOR_COOKIE_PREFIX,
978 strlen(FSMONITOR_COOKIE_PREFIX)))
979 return IS_INSIDE_GITDIR_WITH_COOKIE_PREFIX;
981 return IS_INSIDE_GITDIR;
984 static enum fsmonitor_path_type try_classify_workdir_abs_path(
985 struct fsmonitor_daemon_state *state,
990 if (fspathncmp(path, state->path_worktree_watch.buf,
991 state->path_worktree_watch.len))
992 return IS_OUTSIDE_CONE;
994 rel = path + state->path_worktree_watch.len;
997 return IS_WORKDIR_PATH; /* it is the root dir exactly */
999 return IS_OUTSIDE_CONE;
1002 return fsmonitor_classify_path_workdir_relative(rel);
1005 enum fsmonitor_path_type fsmonitor_classify_path_absolute(
1006 struct fsmonitor_daemon_state *state,
1010 enum fsmonitor_path_type t;
1012 t = try_classify_workdir_abs_path(state, path);
1013 if (state->nr_paths_watching == 1)
1015 if (t != IS_OUTSIDE_CONE)
1018 if (fspathncmp(path, state->path_gitdir_watch.buf,
1019 state->path_gitdir_watch.len))
1020 return IS_OUTSIDE_CONE;
1022 rel = path + state->path_gitdir_watch.len;
1025 return IS_GITDIR; /* it is the <gitdir> exactly */
1027 return IS_OUTSIDE_CONE;
1030 return fsmonitor_classify_path_gitdir_relative(rel);
1034 * We try to combine small batches at the front of the batch-list to avoid
1035 * having a long list. This hopefully makes it a little easier when we want
1036 * to truncate and maintain the list. However, we don't want the paths array
1037 * to just keep growing and growing with realloc, so we insert an arbitrary
1040 #define MY_COMBINE_LIMIT (1024)
1042 void fsmonitor_publish(struct fsmonitor_daemon_state *state,
1043 struct fsmonitor_batch *batch,
1044 const struct string_list *cookie_names)
1046 if (!batch && !cookie_names->nr)
1049 pthread_mutex_lock(&state->main_lock);
1052 struct fsmonitor_batch *head;
1054 head = state->current_token_data->batch_head;
1056 BUG("token does not have batch");
1057 } else if (head->pinned_time) {
1059 * We cannot alter the current batch list
1062 * [a] it is being transmitted to at least one
1063 * client and the handle_client() thread has a
1064 * ref-count, but not a lock on the batch list
1065 * starting with this item.
1067 * [b] it has been transmitted in the past to
1068 * at least one client such that future
1069 * requests are relative to this head batch.
1071 * So, we can only prepend a new batch onto
1072 * the front of the list.
1074 batch->batch_seq_nr = head->batch_seq_nr + 1;
1076 state->current_token_data->batch_head = batch;
1077 } else if (!head->batch_seq_nr) {
1079 * Batch 0 is unpinned. See the note in
1080 * `fsmonitor_new_token_data()` about why we
1081 * don't need to accumulate these paths.
1083 fsmonitor_batch__pop(batch);
1084 } else if (head->nr + batch->nr > MY_COMBINE_LIMIT) {
1086 * The head batch in the list has never been
1087 * transmitted to a client, but folding the
1088 * contents of the new batch onto it would
1089 * exceed our arbitrary limit, so just prepend
1090 * the new batch onto the list.
1092 batch->batch_seq_nr = head->batch_seq_nr + 1;
1094 state->current_token_data->batch_head = batch;
1097 * We are free to append the paths in the given
1098 * batch onto the end of the current head batch.
1100 fsmonitor_batch__combine(head, batch);
1101 fsmonitor_batch__pop(batch);
1105 if (cookie_names->nr)
1106 with_lock__mark_cookies_seen(state, cookie_names);
1108 pthread_mutex_unlock(&state->main_lock);
1111 static void *fsmonitor_fs_listen__thread_proc(void *_state)
1113 struct fsmonitor_daemon_state *state = _state;
1115 trace2_thread_start("fsm-listen");
1117 trace_printf_key(&trace_fsmonitor, "Watching: worktree '%s'",
1118 state->path_worktree_watch.buf);
1119 if (state->nr_paths_watching > 1)
1120 trace_printf_key(&trace_fsmonitor, "Watching: gitdir '%s'",
1121 state->path_gitdir_watch.buf);
1123 fsmonitor_fs_listen__loop(state);
1125 pthread_mutex_lock(&state->main_lock);
1126 if (state->current_token_data &&
1127 state->current_token_data->client_ref_count == 0)
1128 fsmonitor_free_token_data(state->current_token_data);
1129 state->current_token_data = NULL;
1130 pthread_mutex_unlock(&state->main_lock);
1132 trace2_thread_exit();
1136 static int fsmonitor_run_daemon_1(struct fsmonitor_daemon_state *state)
1138 struct ipc_server_opts ipc_opts = {
1139 .nr_threads = fsmonitor__ipc_threads,
1142 * We know that there are no other active threads yet,
1143 * so we can let the IPC layer temporarily chdir() if
1144 * it needs to when creating the server side of the
1145 * Unix domain socket.
1147 .uds_disallow_chdir = 0
1151 * Start the IPC thread pool before the we've started the file
1152 * system event listener thread so that we have the IPC handle
1153 * before we need it.
1155 if (ipc_server_run_async(&state->ipc_server_data,
1156 fsmonitor_ipc__get_path(), &ipc_opts,
1157 handle_client, state))
1158 return error(_("could not start IPC thread pool"));
1161 * Start the fsmonitor listener thread to collect filesystem
1164 if (pthread_create(&state->listener_thread, NULL,
1165 fsmonitor_fs_listen__thread_proc, state) < 0) {
1166 ipc_server_stop_async(state->ipc_server_data);
1167 ipc_server_await(state->ipc_server_data);
1169 return error(_("could not start fsmonitor listener thread"));
1173 * The daemon is now fully functional in background threads.
1174 * Wait for the IPC thread pool to shutdown (whether by client
1175 * request or from filesystem activity).
1177 ipc_server_await(state->ipc_server_data);
1180 * The fsmonitor listener thread may have received a shutdown
1181 * event from the IPC thread pool, but it doesn't hurt to tell
1182 * it again. And wait for it to shutdown.
1184 fsmonitor_fs_listen__stop_async(state);
1185 pthread_join(state->listener_thread, NULL);
1187 return state->error_code;
1190 static int fsmonitor_run_daemon(void)
1192 struct fsmonitor_daemon_state state;
1195 memset(&state, 0, sizeof(state));
1197 hashmap_init(&state.cookies, cookies_cmp, NULL, 0);
1198 pthread_mutex_init(&state.main_lock, NULL);
1199 pthread_cond_init(&state.cookies_cond, NULL);
1200 state.error_code = 0;
1201 state.current_token_data = fsmonitor_new_token_data();
1203 /* Prepare to (recursively) watch the <worktree-root> directory. */
1204 strbuf_init(&state.path_worktree_watch, 0);
1205 strbuf_addstr(&state.path_worktree_watch, absolute_path(get_git_work_tree()));
1206 state.nr_paths_watching = 1;
1209 * We create/delete cookie files inside the .git directory to
1210 * help us keep sync with the file system. If ".git" is not a
1211 * directory, then <gitdir> is not inside the cone of
1212 * <worktree-root>, so set up a second watch for it.
1214 strbuf_init(&state.path_gitdir_watch, 0);
1215 strbuf_addbuf(&state.path_gitdir_watch, &state.path_worktree_watch);
1216 strbuf_addstr(&state.path_gitdir_watch, "/.git");
1217 if (!is_directory(state.path_gitdir_watch.buf)) {
1218 strbuf_reset(&state.path_gitdir_watch);
1219 strbuf_addstr(&state.path_gitdir_watch, absolute_path(get_git_dir()));
1220 state.nr_paths_watching = 2;
1224 * We will write filesystem syncing cookie files into
1225 * <gitdir>/<fsmonitor-dir>/<cookie-dir>/<pid>-<seq>.
1227 strbuf_init(&state.path_cookie_prefix, 0);
1228 strbuf_addbuf(&state.path_cookie_prefix, &state.path_gitdir_watch);
1230 strbuf_addch(&state.path_cookie_prefix, '/');
1231 strbuf_addstr(&state.path_cookie_prefix, FSMONITOR_DIR);
1232 mkdir(state.path_cookie_prefix.buf, 0777);
1234 strbuf_addch(&state.path_cookie_prefix, '/');
1235 strbuf_addstr(&state.path_cookie_prefix, FSMONITOR_COOKIE_DIR);
1236 mkdir(state.path_cookie_prefix.buf, 0777);
1238 strbuf_addch(&state.path_cookie_prefix, '/');
1241 * Confirm that we can create platform-specific resources for the
1242 * filesystem listener before we bother starting all the threads.
1244 if (fsmonitor_fs_listen__ctor(&state)) {
1245 err = error(_("could not initialize listener thread"));
1249 err = fsmonitor_run_daemon_1(&state);
1252 pthread_cond_destroy(&state.cookies_cond);
1253 pthread_mutex_destroy(&state.main_lock);
1254 fsmonitor_fs_listen__dtor(&state);
1256 ipc_server_free(state.ipc_server_data);
1258 strbuf_release(&state.path_worktree_watch);
1259 strbuf_release(&state.path_gitdir_watch);
1260 strbuf_release(&state.path_cookie_prefix);
1263 * NEEDSWORK: Consider "rm -rf <gitdir>/<fsmonitor-dir>"
1269 static int try_to_run_foreground_daemon(void)
1272 * Technically, we don't need to probe for an existing daemon
1273 * process, since we could just call `fsmonitor_run_daemon()`
1274 * and let it fail if the pipe/socket is busy.
1276 * However, this method gives us a nicer error message for a
1277 * common error case.
1279 if (fsmonitor_ipc__get_state() == IPC_STATE__LISTENING)
1280 die("fsmonitor--daemon is already running.");
1282 return !!fsmonitor_run_daemon();
1285 #ifndef GIT_WINDOWS_NATIVE
1287 * This is adapted from `daemonize()`. Use `fork()` to directly create
1288 * and run the daemon in a child process. The fork-parent returns the
1289 * child PID so that we can wait for the child to startup before exiting.
1291 static int spawn_background_fsmonitor_daemon(pid_t *pid)
1298 error_errno(_("setsid failed"));
1304 return !!fsmonitor_run_daemon();
1307 return error_errno(_("could not spawn fsmonitor--daemon in the background"));
1315 * Conceptually like `daemonize()` but different because Windows does not
1316 * have `fork(2)`. Spawn a normal Windows child process but without the
1317 * limitations of `start_command()` and `finish_command()`.
1319 static int spawn_background_fsmonitor_daemon(pid_t *pid)
1321 char git_exe[MAX_PATH];
1322 struct strvec args = STRVEC_INIT;
1325 GetModuleFileNameA(NULL, git_exe, MAX_PATH);
1327 in = open("/dev/null", O_RDONLY);
1328 out = open("/dev/null", O_WRONLY);
1330 strvec_push(&args, git_exe);
1331 strvec_push(&args, "fsmonitor--daemon");
1332 strvec_push(&args, "run");
1334 *pid = mingw_spawnvpe(args.v[0], args.v, NULL, NULL, in, out, out);
1338 strvec_clear(&args);
1341 return error(_("could not spawn fsmonitor--daemon in the background"));
1348 * This is adapted from `wait_or_whine()`. Watch the child process and
1349 * let it get started and begin listening for requests on the socket
1350 * before reporting our success.
1352 static int wait_for_background_startup(pid_t pid_child)
1356 enum ipc_active_state s;
1357 time_t time_limit, now;
1360 time_limit += fsmonitor__start_timeout_sec;
1363 pid_seen = waitpid(pid_child, &status, WNOHANG);
1366 return error_errno(_("waitpid failed"));
1367 else if (pid_seen == 0) {
1369 * The child is still running (this should be
1370 * the normal case). Try to connect to it on
1371 * the socket and see if it is ready for
1374 * If there is another daemon already running,
1375 * our child will fail to start (possibly
1376 * after a timeout on the lock), but we don't
1377 * care (who responds) if the socket is live.
1379 s = fsmonitor_ipc__get_state();
1380 if (s == IPC_STATE__LISTENING)
1384 if (now > time_limit)
1385 return error(_("fsmonitor--daemon not online yet"));
1386 } else if (pid_seen == pid_child) {
1388 * The new child daemon process shutdown while
1389 * it was starting up, so it is not listening
1392 * Try to ping the socket in the odd chance
1393 * that another daemon started (or was already
1394 * running) while our child was starting.
1396 * Again, we don't care who services the socket.
1398 s = fsmonitor_ipc__get_state();
1399 if (s == IPC_STATE__LISTENING)
1403 * We don't care about the WEXITSTATUS() nor
1404 * any of the WIF*(status) values because
1405 * `cmd_fsmonitor__daemon()` does the `!!result`
1406 * trick on all function return values.
1408 * So it is sufficient to just report the
1409 * early shutdown as an error.
1411 return error(_("fsmonitor--daemon failed to start"));
1413 return error(_("waitpid is confused"));
1417 static int try_to_start_background_daemon(void)
1423 * Before we try to create a background daemon process, see
1424 * if a daemon process is already listening. This makes it
1425 * easier for us to report an already-listening error to the
1426 * console, since our spawn/daemon can only report the success
1427 * of creating the background process (and not whether it
1428 * immediately exited).
1430 if (fsmonitor_ipc__get_state() == IPC_STATE__LISTENING)
1431 die("fsmonitor--daemon is already running.");
1434 * Run the actual daemon in a background process.
1436 ret = spawn_background_fsmonitor_daemon(&pid_child);
1441 * Wait (with timeout) for the background child process get
1442 * started and begin listening on the socket/pipe. This makes
1443 * the "start" command more synchronous and more reliable in
1446 ret = wait_for_background_startup(pid_child);
1451 int cmd_fsmonitor__daemon(int argc, const char **argv, const char *prefix)
1455 struct option options[] = {
1456 OPT_INTEGER(0, "ipc-threads",
1457 &fsmonitor__ipc_threads,
1458 N_("use <n> ipc worker threads")),
1459 OPT_INTEGER(0, "start-timeout",
1460 &fsmonitor__start_timeout_sec,
1461 N_("Max seconds to wait for background daemon startup")),
1467 usage_with_options(builtin_fsmonitor__daemon_usage, options);
1469 if (argc == 2 && !strcmp(argv[1], "-h"))
1470 usage_with_options(builtin_fsmonitor__daemon_usage, options);
1472 git_config(fsmonitor_config, NULL);
1478 argc = parse_options(argc, argv, prefix, options,
1479 builtin_fsmonitor__daemon_usage, 0);
1480 if (fsmonitor__ipc_threads < 1)
1481 die(_("invalid 'ipc-threads' value (%d)"),
1482 fsmonitor__ipc_threads);
1484 if (!strcmp(subcmd, "start"))
1485 return !!try_to_start_background_daemon();
1487 if (!strcmp(subcmd, "run"))
1488 return !!try_to_run_foreground_daemon();
1490 if (!strcmp(subcmd, "stop"))
1491 return !!do_as_client__send_stop();
1493 if (!strcmp(subcmd, "status"))
1494 return !!do_as_client__status();
1496 die(_("Unhandled subcommand '%s'"), subcmd);
1500 int cmd_fsmonitor__daemon(int argc, const char **argv, const char *prefix)
1502 struct option options[] = {
1506 if (argc == 2 && !strcmp(argv[1], "-h"))
1507 usage_with_options(builtin_fsmonitor__daemon_usage, options);
1509 die(_("fsmonitor--daemon not supported on this platform"));