Skip to content

Commit 2c5c4d3

Browse files
committed
Merge branch 'pt/fsmonitor-linux' into seen
The fsmonitor daemon has been implemented for Linux. * pt/fsmonitor-linux: fsmonitor: close inherited file descriptors and detach in daemon run-command: add close_fd_above_stderr option fsmonitor: add tests for Linux fsmonitor: implement filesystem change listener for Linux fsmonitor: deduplicate settings logic for Unix platforms fsmonitor: deduplicate IPC path logic for Unix platforms fsmonitor: use pthread_cond_timedwait for cookie wait compat/win32: add pthread_cond_timedwait fsmonitor: fix hashmap memory leak in fsmonitor_run_daemon fsmonitor: fix khash memory leak in do_handle_client
2 parents bea5b1d + d6426ff commit 2c5c4d3

18 files changed

Lines changed: 1278 additions & 62 deletions

Documentation/config/fsmonitor--daemon.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ fsmonitor.allowRemote::
44
behavior. Only respected when `core.fsmonitor` is set to `true`.
55

66
fsmonitor.socketDir::
7-
This Mac OS-specific option, if set, specifies the directory in
7+
This Mac OS and Linux-specific option, if set, specifies the directory in
88
which to create the Unix domain socket used for communication
99
between the fsmonitor daemon and various Git commands. The directory must
10-
reside on a native Mac OS filesystem. Only respected when `core.fsmonitor`
10+
reside on a native filesystem. Only respected when `core.fsmonitor`
1111
is set to `true`.

Documentation/git-fsmonitor--daemon.adoc

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ repositories; this may be overridden by setting `fsmonitor.allowRemote` to
7676
correctly with all network-mounted repositories, so such use is considered
7777
experimental.
7878

79-
On Mac OS, the inter-process communication (IPC) between various Git
79+
On Mac OS and Linux, the inter-process communication (IPC) between various Git
8080
commands and the fsmonitor daemon is done via a Unix domain socket (UDS) -- a
81-
special type of file -- which is supported by native Mac OS filesystems,
81+
special type of file -- which is supported by native Mac OS and Linux filesystems,
8282
but not on network-mounted filesystems, NTFS, or FAT32. Other filesystems
8383
may or may not have the needed support; the fsmonitor daemon is not guaranteed
8484
to work with these filesystems and such use is considered experimental.
@@ -87,13 +87,33 @@ By default, the socket is created in the `.git` directory. However, if the
8787
`.git` directory is on a network-mounted filesystem, it will instead be
8888
created at `$HOME/.git-fsmonitor-*` unless `$HOME` itself is on a
8989
network-mounted filesystem, in which case you must set the configuration
90-
variable `fsmonitor.socketDir` to the path of a directory on a Mac OS native
90+
variable `fsmonitor.socketDir` to the path of a directory on a native
9191
filesystem in which to create the socket file.
9292

9393
If none of the above directories (`.git`, `$HOME`, or `fsmonitor.socketDir`)
94-
is on a native Mac OS file filesystem the fsmonitor daemon will report an
94+
is on a native filesystem the fsmonitor daemon will report an
9595
error that will cause the daemon and the currently running command to exit.
9696

97+
LINUX CAVEATS
98+
~~~~~~~~~~~~~
99+
100+
On Linux, the fsmonitor daemon uses inotify to monitor filesystem events.
101+
The inotify system has per-user limits on the number of watches that can
102+
be created. The default limit is typically 8192 watches per user.
103+
104+
For large repositories with many directories, you may need to increase
105+
this limit. Check the current limit with:
106+
107+
cat /proc/sys/fs/inotify/max_user_watches
108+
109+
To temporarily increase the limit:
110+
111+
sudo sysctl fs.inotify.max_user_watches=65536
112+
113+
To make the change permanent, add to `/etc/sysctl.conf`:
114+
115+
fs.inotify.max_user_watches=65536
116+
97117
CONFIGURATION
98118
-------------
99119

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,13 +2380,13 @@ ifdef FSMONITOR_DAEMON_BACKEND
23802380
COMPAT_CFLAGS += -DHAVE_FSMONITOR_DAEMON_BACKEND
23812381
COMPAT_OBJS += compat/fsmonitor/fsm-listen-$(FSMONITOR_DAEMON_BACKEND).o
23822382
COMPAT_OBJS += compat/fsmonitor/fsm-health-$(FSMONITOR_DAEMON_BACKEND).o
2383-
COMPAT_OBJS += compat/fsmonitor/fsm-ipc-$(FSMONITOR_DAEMON_BACKEND).o
23842383
endif
23852384

23862385
ifdef FSMONITOR_OS_SETTINGS
23872386
COMPAT_CFLAGS += -DHAVE_FSMONITOR_OS_SETTINGS
2387+
COMPAT_OBJS += compat/fsmonitor/fsm-ipc-$(FSMONITOR_OS_SETTINGS).o
23882388
COMPAT_OBJS += compat/fsmonitor/fsm-settings-$(FSMONITOR_OS_SETTINGS).o
2389-
COMPAT_OBJS += compat/fsmonitor/fsm-path-utils-$(FSMONITOR_OS_SETTINGS).o
2389+
COMPAT_OBJS += compat/fsmonitor/fsm-path-utils-$(FSMONITOR_DAEMON_BACKEND).o
23902390
endif
23912391

23922392
ifdef WITH_BREAKING_CHANGES

builtin/fsmonitor--daemon.c

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ static int do_as_client__send_stop(void)
8686
{
8787
struct strbuf answer = STRBUF_INIT;
8888
int ret;
89+
int max_wait_ms = 30000;
90+
int elapsed_ms = 0;
8991

9092
ret = fsmonitor_ipc__send_command("quit", &answer);
9193

@@ -96,8 +98,16 @@ static int do_as_client__send_stop(void)
9698
return ret;
9799

98100
trace2_region_enter("fsm_client", "polling-for-daemon-exit", NULL);
99-
while (fsmonitor_ipc__get_state() == IPC_STATE__LISTENING)
101+
while (fsmonitor_ipc__get_state() == IPC_STATE__LISTENING) {
102+
if (elapsed_ms >= max_wait_ms) {
103+
trace2_region_leave("fsm_client",
104+
"polling-for-daemon-exit", NULL);
105+
return error(_("daemon did not stop within %d seconds"),
106+
max_wait_ms / 1000);
107+
}
100108
sleep_millisec(50);
109+
elapsed_ms += 50;
110+
}
101111
trace2_region_leave("fsm_client", "polling-for-daemon-exit", NULL);
102112

103113
return 0;
@@ -197,20 +207,31 @@ static enum fsmonitor_cookie_item_result with_lock__wait_for_cookie(
197207
unlink(cookie_pathname.buf);
198208

199209
/*
200-
* Technically, this is an infinite wait (well, unless another
201-
* thread sends us an abort). I'd like to change this to
202-
* use `pthread_cond_timedwait()` and return an error/timeout
203-
* and let the caller do the trivial response thing, but we
204-
* don't have that routine in our thread-utils.
205-
*
206-
* After extensive beta testing I'm not really worried about
207-
* this. Also note that the above open() and unlink() calls
208-
* will cause at least two FS events on that path, so the odds
209-
* of getting stuck are pretty slim.
210+
* Wait for the listener thread to observe the cookie file.
211+
* Time out after a short interval so that the client
212+
* does not hang forever if the filesystem does not deliver
213+
* events (e.g., on certain container/overlay filesystems
214+
* where inotify watches succeed but events never arrive).
210215
*/
211-
while (cookie->result == FCIR_INIT)
212-
pthread_cond_wait(&state->cookies_cond,
213-
&state->main_lock);
216+
{
217+
struct timeval now;
218+
struct timespec ts;
219+
int err = 0;
220+
221+
gettimeofday(&now, NULL);
222+
ts.tv_sec = now.tv_sec + 1;
223+
ts.tv_nsec = now.tv_usec * 1000;
224+
225+
while (cookie->result == FCIR_INIT && !err)
226+
err = pthread_cond_timedwait(&state->cookies_cond,
227+
&state->main_lock,
228+
&ts);
229+
if (err == ETIMEDOUT && cookie->result == FCIR_INIT) {
230+
trace_printf_key(&trace_fsmonitor,
231+
"cookie_wait timed out");
232+
cookie->result = FCIR_ERROR;
233+
}
234+
}
214235

215236
done:
216237
hashmap_remove(&state->cookies, &cookie->entry, NULL);
@@ -671,7 +692,7 @@ static int do_handle_client(struct fsmonitor_daemon_state *state,
671692
const struct fsmonitor_batch *batch;
672693
struct fsmonitor_batch *remainder = NULL;
673694
intmax_t count = 0, duplicates = 0;
674-
kh_str_t *shown;
695+
kh_str_t *shown = NULL;
675696
int hash_ret;
676697
int do_trivial = 0;
677698
int do_flush = 0;
@@ -909,8 +930,6 @@ static int do_handle_client(struct fsmonitor_daemon_state *state,
909930
total_response_len += payload.len;
910931
}
911932

912-
kh_release_str(shown);
913-
914933
pthread_mutex_lock(&state->main_lock);
915934

916935
if (token_data->client_ref_count > 0)
@@ -954,6 +973,7 @@ static int do_handle_client(struct fsmonitor_daemon_state *state,
954973
trace2_data_intmax("fsmonitor", the_repository, "response/count/duplicates", duplicates);
955974

956975
cleanup:
976+
kh_destroy_str(shown);
957977
strbuf_release(&response_token);
958978
strbuf_release(&requested_token_id);
959979
strbuf_release(&payload);
@@ -1405,6 +1425,7 @@ static int fsmonitor_run_daemon(void)
14051425
done:
14061426
pthread_cond_destroy(&state.cookies_cond);
14071427
pthread_mutex_destroy(&state.main_lock);
1428+
hashmap_clear(&state.cookies);
14081429
fsm_listen__dtor(&state);
14091430
fsm_health__dtor(&state);
14101431

@@ -1420,7 +1441,7 @@ static int fsmonitor_run_daemon(void)
14201441
return err;
14211442
}
14221443

1423-
static int try_to_run_foreground_daemon(int detach_console MAYBE_UNUSED)
1444+
static int try_to_run_foreground_daemon(int detach_console)
14241445
{
14251446
/*
14261447
* Technically, we don't need to probe for an existing daemon
@@ -1440,10 +1461,21 @@ static int try_to_run_foreground_daemon(int detach_console MAYBE_UNUSED)
14401461
fflush(stderr);
14411462
}
14421463

1464+
if (detach_console) {
14431465
#ifdef GIT_WINDOWS_NATIVE
1444-
if (detach_console)
14451466
FreeConsole();
1467+
#else
1468+
/*
1469+
* Create a new session so that the daemon is detached
1470+
* from the parent's process group. This prevents
1471+
* shells with job control (e.g. bash with "set -m")
1472+
* from waiting on the daemon when they wait for a
1473+
* foreground command that implicitly spawned it.
1474+
*/
1475+
if (setsid() == -1)
1476+
warning_errno(_("setsid failed"));
14461477
#endif
1478+
}
14471479

14481480
return !!fsmonitor_run_daemon();
14491481
}
@@ -1506,6 +1538,7 @@ static int try_to_start_background_daemon(void)
15061538
cp.no_stdin = 1;
15071539
cp.no_stdout = 1;
15081540
cp.no_stderr = 1;
1541+
cp.close_fd_above_stderr = 1;
15091542

15101543
sbgr = start_bg_command(&cp, bg_wait_cb, NULL,
15111544
fsmonitor__start_timeout_sec);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "git-compat-util.h"
2+
#include "config.h"
3+
#include "fsmonitor-ll.h"
4+
#include "fsm-health.h"
5+
#include "fsmonitor--daemon.h"
6+
7+
/*
8+
* The Linux fsmonitor implementation uses inotify which has its own
9+
* mechanisms for detecting filesystem unmount and other events that
10+
* would require the daemon to shutdown. Therefore, we don't need
11+
* a separate health thread like Windows does.
12+
*
13+
* These stub functions satisfy the interface requirements.
14+
*/
15+
16+
int fsm_health__ctor(struct fsmonitor_daemon_state *state UNUSED)
17+
{
18+
return 0;
19+
}
20+
21+
void fsm_health__dtor(struct fsmonitor_daemon_state *state UNUSED)
22+
{
23+
return;
24+
}
25+
26+
void fsm_health__loop(struct fsmonitor_daemon_state *state UNUSED)
27+
{
28+
return;
29+
}
30+
31+
void fsm_health__stop_async(struct fsmonitor_daemon_state *state UNUSED)
32+
{
33+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ const char *fsmonitor_ipc__get_path(struct repository *r)
2727
if (ipc_path)
2828
return ipc_path;
2929

30-
3130
/* By default the socket file is created in the .git directory */
3231
if (fsmonitor__is_fs_remote(r->gitdir) < 1) {
3332
ipc_path = fsmonitor_ipc__get_default_path();
3433
return ipc_path;
3534
}
3635

36+
if (!r->worktree)
37+
BUG("repository has no worktree");
38+
3739
git_SHA1_Init(&sha1ctx);
3840
git_SHA1_Update(&sha1ctx, r->worktree, strlen(r->worktree));
3941
git_SHA1_Final(hash, &sha1ctx);

0 commit comments

Comments
 (0)