4 #include "simple-ipc.h"
5 #include "fsmonitor-ipc.h"
6 #include "repository.h"
7 #include "run-command.h"
11 #ifndef HAVE_FSMONITOR_DAEMON_BACKEND
14 * A trivial implementation of the fsmonitor_ipc__ API for unsupported
18 int fsmonitor_ipc__is_supported(void)
23 const char *fsmonitor_ipc__get_path(struct repository
*r
)
28 enum ipc_active_state
fsmonitor_ipc__get_state(void)
30 return IPC_STATE__OTHER_ERROR
;
33 int fsmonitor_ipc__send_query(const char *since_token
,
34 struct strbuf
*answer
)
39 int fsmonitor_ipc__send_command(const char *command
,
40 struct strbuf
*answer
)
47 int fsmonitor_ipc__is_supported(void)
52 enum ipc_active_state
fsmonitor_ipc__get_state(void)
54 return ipc_get_active_state(fsmonitor_ipc__get_path(the_repository
));
57 static int spawn_daemon(void)
59 struct child_process cmd
= CHILD_PROCESS_INIT
;
63 cmd
.trace2_child_class
= "fsmonitor";
64 strvec_pushl(&cmd
.args
, "fsmonitor--daemon", "start", NULL
);
66 return run_command(&cmd
);
69 int fsmonitor_ipc__send_query(const char *since_token
,
70 struct strbuf
*answer
)
73 int tried_to_spawn
= 0;
74 enum ipc_active_state state
= IPC_STATE__OTHER_ERROR
;
75 struct ipc_client_connection
*connection
= NULL
;
76 struct ipc_client_connect_options options
77 = IPC_CLIENT_CONNECT_OPTIONS_INIT
;
78 const char *tok
= since_token
? since_token
: "";
79 size_t tok_len
= since_token
? strlen(since_token
) : 0;
81 options
.wait_if_busy
= 1;
82 options
.wait_if_not_found
= 0;
84 trace2_region_enter("fsm_client", "query", NULL
);
85 trace2_data_string("fsm_client", NULL
, "query/command", tok
);
88 state
= ipc_client_try_connect(fsmonitor_ipc__get_path(the_repository
),
89 &options
, &connection
);
92 case IPC_STATE__LISTENING
:
93 ret
= ipc_client_send_command_to_connection(
94 connection
, tok
, tok_len
, answer
);
95 ipc_client_close_connection(connection
);
97 trace2_data_intmax("fsm_client", NULL
,
98 "query/response-length", answer
->len
);
101 case IPC_STATE__NOT_LISTENING
:
102 case IPC_STATE__PATH_NOT_FOUND
:
111 * Try again, but this time give the daemon a chance to
112 * actually create the pipe/socket.
114 * Granted, the daemon just started so it can't possibly have
115 * any FS cached yet, so we'll always get a trivial answer.
116 * BUT the answer should include a new token that can serve
117 * as the basis for subsequent requests.
119 options
.wait_if_not_found
= 1;
122 case IPC_STATE__INVALID_PATH
:
123 ret
= error(_("fsmonitor_ipc__send_query: invalid path '%s'"),
124 fsmonitor_ipc__get_path(the_repository
));
127 case IPC_STATE__OTHER_ERROR
:
129 ret
= error(_("fsmonitor_ipc__send_query: unspecified error on '%s'"),
130 fsmonitor_ipc__get_path(the_repository
));
135 trace2_region_leave("fsm_client", "query", NULL
);
140 int fsmonitor_ipc__send_command(const char *command
,
141 struct strbuf
*answer
)
143 struct ipc_client_connection
*connection
= NULL
;
144 struct ipc_client_connect_options options
145 = IPC_CLIENT_CONNECT_OPTIONS_INIT
;
147 enum ipc_active_state state
;
148 const char *c
= command
? command
: "";
149 size_t c_len
= command
? strlen(command
) : 0;
151 strbuf_reset(answer
);
153 options
.wait_if_busy
= 1;
154 options
.wait_if_not_found
= 0;
156 state
= ipc_client_try_connect(fsmonitor_ipc__get_path(the_repository
),
157 &options
, &connection
);
158 if (state
!= IPC_STATE__LISTENING
) {
159 die(_("fsmonitor--daemon is not running"));
163 ret
= ipc_client_send_command_to_connection(connection
, c
, c_len
,
165 ipc_client_close_connection(connection
);
168 die(_("could not send '%s' command to fsmonitor--daemon"), c
);