1 #ifndef GIT_SIMPLE_IPC_H
2 #define GIT_SIMPLE_IPC_H
5 * See Documentation/technical/api-simple-ipc.txt
8 #ifdef SUPPORTS_SIMPLE_IPC
12 * Simple IPC Client Side API.
15 enum ipc_active_state {
17 * The pipe/socket exists and the daemon is waiting for connections.
19 IPC_STATE__LISTENING = 0,
22 * The pipe/socket exists, but the daemon is not listening.
23 * Perhaps it is very busy.
24 * Perhaps the daemon died without deleting the path.
25 * Perhaps it is shutting down and draining existing clients.
26 * Perhaps it is dead, but other clients are lingering and
27 * still holding a reference to the pathname.
29 IPC_STATE__NOT_LISTENING,
32 * The requested pathname is bogus and no amount of retries
35 IPC_STATE__INVALID_PATH,
38 * The requested pathname is not found. This usually means
39 * that there is no daemon present.
41 IPC_STATE__PATH_NOT_FOUND,
43 IPC_STATE__OTHER_ERROR,
46 struct ipc_client_connect_options {
48 * Spin under timeout if the server is running but can't
49 * accept our connection yet. This should always be set
50 * unless you just want to poke the server and see if it
53 unsigned int wait_if_busy:1;
56 * Spin under timeout if the pipe/socket is not yet present
57 * on the file system. This is useful if we just started
58 * the service and need to wait for it to become ready.
60 unsigned int wait_if_not_found:1;
63 * Disallow chdir() when creating a Unix domain socket.
65 unsigned int uds_disallow_chdir:1;
68 #define IPC_CLIENT_CONNECT_OPTIONS_INIT { \
70 .wait_if_not_found = 0, \
71 .uds_disallow_chdir = 0, \
75 * Determine if a server is listening on this named pipe or socket using
76 * platform-specific logic. This might just probe the filesystem or it
77 * might make a trivial connection to the server using this pathname.
79 enum ipc_active_state ipc_get_active_state(const char *path);
81 struct ipc_client_connection {
86 * Try to connect to the daemon on the named pipe or socket.
88 * Returns IPC_STATE__LISTENING and a connection handle.
90 * Otherwise, returns info to help decide whether to retry or to
91 * spawn/respawn the server.
93 enum ipc_active_state ipc_client_try_connect(
95 const struct ipc_client_connect_options *options,
96 struct ipc_client_connection **p_connection);
98 void ipc_client_close_connection(struct ipc_client_connection *connection);
101 * Used by the client to synchronously send and receive a message with
102 * the server on the provided client connection.
104 * Returns 0 when successful.
106 * Calls error() and returns non-zero otherwise.
108 int ipc_client_send_command_to_connection(
109 struct ipc_client_connection *connection,
110 const char *message, size_t message_len,
111 struct strbuf *answer);
114 * Used by the client to synchronously connect and send and receive a
115 * message to the server listening at the given path.
117 * Returns 0 when successful.
119 * Calls error() and returns non-zero otherwise.
121 int ipc_client_send_command(const char *path,
122 const struct ipc_client_connect_options *options,
123 const char *message, size_t message_len,
124 struct strbuf *answer);
127 * Simple IPC Server Side API.
130 struct ipc_server_reply_data;
132 typedef int (ipc_server_reply_cb)(struct ipc_server_reply_data *,
133 const char *response,
134 size_t response_len);
137 * Prototype for an application-supplied callback to process incoming
138 * client IPC messages and compose a reply. The `application_cb` should
139 * use the provided `reply_cb` and `reply_data` to send an IPC response
140 * back to the client. The `reply_cb` callback can be called multiple
141 * times for chunking purposes. A reply message is optional and may be
142 * omitted if not necessary for the application.
144 * The return value from the application callback is ignored.
145 * The value `SIMPLE_IPC_QUIT` can be used to shutdown the server.
147 typedef int (ipc_server_application_cb)(void *application_data,
150 ipc_server_reply_cb *reply_cb,
151 struct ipc_server_reply_data *reply_data);
153 #define SIMPLE_IPC_QUIT -2
156 * Opaque instance data to represent an IPC server instance.
158 struct ipc_server_data;
161 * Control parameters for the IPC server instance.
162 * Use this to hide platform-specific settings.
164 struct ipc_server_opts
169 * Disallow chdir() when creating a Unix domain socket.
171 unsigned int uds_disallow_chdir:1;
175 * Start an IPC server instance in one or more background threads
176 * and return a handle to the pool.
178 * Returns 0 if the asynchronous server pool was started successfully.
180 * Returns -2 if we could not startup because another server is using
181 * the socket or named pipe.
183 * When a client IPC message is received, the `application_cb` will be
184 * called (possibly on a random thread) to handle the message and
185 * optionally compose a reply message.
187 int ipc_server_run_async(struct ipc_server_data **returned_server_data,
188 const char *path, const struct ipc_server_opts *opts,
189 ipc_server_application_cb *application_cb,
190 void *application_data);
193 * Gently signal the IPC server pool to shutdown. No new client
194 * connections will be accepted, but existing connections will be
195 * allowed to complete.
197 int ipc_server_stop_async(struct ipc_server_data *server_data);
200 * Block the calling thread until all threads in the IPC server pool
201 * have completed and been joined.
203 int ipc_server_await(struct ipc_server_data *server_data);
206 * Close and free all resource handles associated with the IPC server
209 void ipc_server_free(struct ipc_server_data *server_data);
212 * Run an IPC server instance and block the calling thread of the
213 * current process. It does not return until the IPC server has
214 * either shutdown or had an unrecoverable error.
216 * The IPC server handles incoming IPC messages from client processes
217 * and may use one or more background threads as necessary.
219 * Returns 0 after the server has completed successfully.
220 * Returns -1 if the server cannot be started.
221 * Returns -2 if we could not startup because another server is using
222 * the socket or named pipe.
224 * When a client IPC message is received, the `application_cb` will be
225 * called (possibly on a random thread) to handle the message and
226 * optionally compose a reply message.
228 * Note that `ipc_server_run()` is a synchronous wrapper around the
229 * above asynchronous routines. It effectively hides all of the
230 * server state and thread details from the caller and presents a
231 * simple synchronous interface.
233 int ipc_server_run(const char *path, const struct ipc_server_opts *opts,
234 ipc_server_application_cb *application_cb,
235 void *application_data);
237 #endif /* SUPPORTS_SIMPLE_IPC */
238 #endif /* GIT_SIMPLE_IPC_H */