Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple IPC Cleanups #893

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
3e4447f
pkt-line: eliminate the need for static buffer in packet_write_gently()
jeffhostetler Feb 13, 2021
f49e799
pkt-line: do not issue flush packets in write_packetized_*()
dscho Feb 13, 2021
51b4c93
pkt-line: (optionally) libify the packet readers
dscho Feb 13, 2021
8346bcf
pkt-line: add options argument to read_packetized_to_strbuf()
dscho Feb 13, 2021
92ac4a3
simple-ipc: design documentation for new IPC mechanism
jeffhostetler Feb 13, 2021
6b42359
simple-ipc: add win32 implementation
jeffhostetler Feb 13, 2021
b71d919
unix-socket: elimiate static unix_stream_socket() helper function
jeffhostetler Feb 13, 2021
5c53ef0
unix-socket: add backlog size option to unix_stream_listen()
jeffhostetler Feb 13, 2021
ca012e6
unix-socket: disallow chdir() when creating unix domain sockets
jeffhostetler Feb 13, 2021
9406d12
unix-socket: create `unix_stream_server__listen_with_lock()`
jeffhostetler Feb 13, 2021
efafaf0
simple-ipc: add Unix domain socket implementation
jeffhostetler Feb 13, 2021
edce16a
t0052: add simple-ipc tests and t/helper/test-simple-ipc tool
jeffhostetler Feb 17, 2021
d6ea768
pkt-line: remove buffer arg from write_packetized_from_fd_no_flush()
jeffhostetler Mar 2, 2021
6ef867b
unix-socket: simplify initialization of unix_stream_listen_opts
jeffhostetler Mar 2, 2021
4ae4429
unix-stream-server: create unix-stream-server.c
jeffhostetler Mar 2, 2021
7be4607
simple-ipc: move error handling up a level
jeffhostetler Mar 2, 2021
8910095
unix-stream-server: add st_dev and st_mode to socket stolen checks
jeffhostetler Mar 2, 2021
db0467a
test-simple-ipc: refactor command line option processing in helper
jeffhostetler Mar 3, 2021
1902c23
test-simple-ipc: add --token=<token> string option
jeffhostetler Mar 4, 2021
eadde22
simple-ipc: update design documentation with more details
jeffhostetler Mar 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
105 changes: 105 additions & 0 deletions Documentation/technical/api-simple-ipc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
Simple-IPC API
==============

The Simple-IPC API is a collection of `ipc_` prefixed library routines
and a basic communication protocol that allow an IPC-client process to
send an application-specific IPC-request message to an IPC-server
process and receive an application-specific IPC-response message.

Communication occurs over a named pipe on Windows and a Unix domain
socket on other platforms. IPC-clients and IPC-servers rendezvous at
a previously agreed-to application-specific pathname (which is outside
the scope of this design) that is local to the computer system.

The IPC-server routines within the server application process create a
thread pool to listen for connections and receive request messages
from multiple concurrent IPC-clients. When received, these messages
are dispatched up to the server application callbacks for handling.
IPC-server routines then incrementally relay responses back to the
IPC-client.

The IPC-client routines within a client application process connect
to the IPC-server and send a request message and wait for a response.
When received, the response is returned back the caller.

For example, the `fsmonitor--daemon` feature will be built as a server
application on top of the IPC-server library routines. It will have
threads watching for file system events and a thread pool waiting for
client connections. Clients, such as `git status` will request a list
of file system events since a point in time and the server will
respond with a list of changed files and directories. The formats of
the request and response are application-specific; the IPC-client and
IPC-server routines treat them as opaque byte streams.


Comparison with sub-process model
---------------------------------

The Simple-IPC mechanism differs from the existing `sub-process.c`
model (Documentation/technical/long-running-process-protocol.txt) and
used by applications like Git-LFS. In the LFS-style sub-process model
the helper is started by the foreground process, communication happens
via a pair of file descriptors bound to the stdin/stdout of the
sub-process, the sub-process only serves the current foreground
process, and the sub-process exits when the foreground process
terminates.

In the Simple-IPC model the server is a very long-running service. It
can service many clients at the same time and has a private socket or
named pipe connection to each active client. It might be started
(on-demand) by the current client process or it might have been
started by a previous client or by the OS at boot time. The server
process is not associated with a terminal and it persists after
clients terminate. Clients do not have access to the stdin/stdout of
the server process and therefore must communicate over sockets or
named pipes.


Server startup and shutdown
---------------------------

How an application server based upon IPC-server is started is also
outside the scope of the Simple-IPC design and is a property of the
application using it. For example, the server might be started or
restarted during routine maintenance operations, or it might be
started as a system service during the system boot-up sequence, or it
might be started on-demand by a foreground Git command when needed.

Similarly, server shutdown is a property of the application using
the simple-ipc routines. For example, the server might decide to
shutdown when idle or only upon explicit request.


Simple-IPC protocol
-------------------

The Simple-IPC protocol consists of a single request message from the
client and an optional response message from the server. Both the
client and server messages are unlimited in length and are terminated
with a flush packet.

The pkt-line routines (Documentation/technical/protocol-common.txt)
are used to simplify buffer management during message generation,
transmission, and reception. A flush packet is used to mark the end
of the message. This allows the sender to incrementally generate and
transmit the message. It allows the receiver to incrementally receive
the message in chunks and to know when they have received the entire
message.

The actual byte format of the client request and server response
messages are application specific. The IPC layer transmits and
receives them as opaque byte buffers without any concern for the
content within. It is the job of the calling application layer to
understand the contents of the request and response messages.


Summary
-------

Conceptually, the Simple-IPC protocol is similar to an HTTP REST
request. Clients connect, make an application-specific and
stateless request, receive an application-specific
response, and disconnect. It is a one round trip facility for
querying the server. The Simple-IPC routines hide the socket,
named pipe, and thread pool details and allow the application
layer to focus on the application at hand.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ TEST_BUILTINS_OBJS += test-serve-v2.o
TEST_BUILTINS_OBJS += test-sha1.o
TEST_BUILTINS_OBJS += test-sha256.o
TEST_BUILTINS_OBJS += test-sigchain.o
TEST_BUILTINS_OBJS += test-simple-ipc.o
TEST_BUILTINS_OBJS += test-strcmp-offset.o
TEST_BUILTINS_OBJS += test-string-list.o
TEST_BUILTINS_OBJS += test-submodule-config.o
Expand Down Expand Up @@ -1677,6 +1678,14 @@ ifdef NO_UNIX_SOCKETS
BASIC_CFLAGS += -DNO_UNIX_SOCKETS
else
LIB_OBJS += unix-socket.o
LIB_OBJS += unix-stream-server.o
LIB_OBJS += compat/simple-ipc/ipc-shared.o
LIB_OBJS += compat/simple-ipc/ipc-unix-socket.o
endif

ifdef USE_WIN32_IPC
LIB_OBJS += compat/simple-ipc/ipc-shared.o
LIB_OBJS += compat/simple-ipc/ipc-win32.o
endif

ifdef NO_ICONV
Expand Down
3 changes: 2 additions & 1 deletion builtin/credential-cache--daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,10 @@ static int serve_cache_loop(int fd)

static void serve_cache(const char *socket_path, int debug)
{
struct unix_stream_listen_opts opts = UNIX_STREAM_LISTEN_OPTS_INIT;
int fd;

fd = unix_stream_listen(socket_path);
fd = unix_stream_listen(socket_path, &opts);
if (fd < 0)
die_errno("unable to bind to '%s'", socket_path);

Expand Down
2 changes: 1 addition & 1 deletion builtin/credential-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
static int send_request(const char *socket, const struct strbuf *out)
{
int got_data = 0;
int fd = unix_stream_connect(socket);
int fd = unix_stream_connect(socket, 0);

if (fd < 0)
return -1;
Expand Down
28 changes: 28 additions & 0 deletions compat/simple-ipc/ipc-shared.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "cache.h"
#include "simple-ipc.h"
#include "strbuf.h"
#include "pkt-line.h"
#include "thread-utils.h"

#ifdef SUPPORTS_SIMPLE_IPC

int ipc_server_run(const char *path, const struct ipc_server_opts *opts,
ipc_server_application_cb *application_cb,
void *application_data)
{
struct ipc_server_data *server_data = NULL;
int ret;

ret = ipc_server_run_async(&server_data, path, opts,
application_cb, application_data);
if (ret)
return ret;

ret = ipc_server_await(server_data);

ipc_server_free(server_data);

return ret;
}

#endif /* SUPPORTS_SIMPLE_IPC */