From patchwork Wed Sep 15 20:36:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Hostetler X-Patchwork-Id: 12497413 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8811EC433FE for ; Wed, 15 Sep 2021 20:36:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6FC4161131 for ; Wed, 15 Sep 2021 20:36:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231892AbhIOUhs (ORCPT ); Wed, 15 Sep 2021 16:37:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231689AbhIOUho (ORCPT ); Wed, 15 Sep 2021 16:37:44 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03204C061575 for ; Wed, 15 Sep 2021 13:36:21 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id i23so5876002wrb.2 for ; Wed, 15 Sep 2021 13:36:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=8h4thXFqHesJICrs185v4px0m1XRMJGNG6dP8TLer6A=; b=H+Bq7H2IRD4ZhLcC1a69GOZgkDzzFL9rnZbHfYAc8AFgO3vBOhiEhbL9eg9nOQX8dn /aLOzYoGW3x1B3TIq/bXtMTefsdFfUDlYJFPg66vLHABolJoP7rr/zXjtVLs5pFWDCbq WBf7CBFZAO7Vh98o6g+0cK9mPc2JuURU9gpZT+x+6RZ3VZ+ESjiad+8HlWeI2khd9DpE HC8+pZlMCPoT6uNz5WEoGfvQzx08gi17ys/JaBcAk4yaiHENU22RW9m5kRtWpcYLI97/ yPRxYiSAs+86B+CMvfZLfiIbHpRv2t3PQDcFkouYomMLv4Gh5iLDpJXrTnCxuv6RAe52 TGkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=8h4thXFqHesJICrs185v4px0m1XRMJGNG6dP8TLer6A=; b=wVclT4aYClVaocXzGIU/CWPSIPgJqNV8selBOhbqe7wueH3o9iP7dOFqioi7/quXJf B708VJSVFdMuXqXTvIwkJ9rKjKDYI6Cb/096lExsD70CkmvLAQIOgMHww2bbU9nZYXV7 6LQzk3oTfR4Jc7vRcYwh2so0EUK/TFylUE6jaY7ny7+VmtP6vwi+HN6l/MCL8s7qNGQe Z4A1wF4pEst7UW7UCfoZmdqLJ8QoyAneHTSqDpXsLVIwlnoclN4MMYMEACLoA9vpKLHY QH1cILw2KZqjfTb1KdzL6TEm6oAh/BkJFZywcttGHYUN/MOfr5I/JNpOZIqZeaTmwXZF ptBw== X-Gm-Message-State: AOAM533hD5QchssCWTa5ULRec9961stm1gxwz/yCMfDbjeE2d1vDviZt Ha9h6tRYSJmGWYZnluX/hIN060jIERU= X-Google-Smtp-Source: ABdhPJyb3qFdGWbObeCZcDlr/DgaaA97K5f0rEoNLEc3SqPXxO4XF0MugsGabT7/BgNhGjgor0MDFQ== X-Received: by 2002:adf:fd92:: with SMTP id d18mr2169418wrr.28.1631738179632; Wed, 15 Sep 2021 13:36:19 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id y197sm484309wmc.18.2021.09.15.13.36.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Sep 2021 13:36:19 -0700 (PDT) Message-Id: <5f557caee004f22cee33e8753063f0315459d7e1.1631738177.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 15 Sep 2021 20:36:11 +0000 Subject: [PATCH 1/7] trace2: fix memory leak of thread name Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff Hostetler , Jeff Hostetler Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Jeff Hostetler From: Jeff Hostetler Do not leak the thread name (contained within the thread context) when a thread terminates. Signed-off-by: Jeff Hostetler --- trace2/tr2_tls.c | 1 + 1 file changed, 1 insertion(+) diff --git a/trace2/tr2_tls.c b/trace2/tr2_tls.c index 067c23755fb..7da94aba522 100644 --- a/trace2/tr2_tls.c +++ b/trace2/tr2_tls.c @@ -95,6 +95,7 @@ void tr2tls_unset_self(void) pthread_setspecific(tr2tls_key, NULL); + strbuf_release(&ctx->thread_name); free(ctx->array_us_start); free(ctx); } From patchwork Wed Sep 15 20:36:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Hostetler X-Patchwork-Id: 12497417 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A3147C433EF for ; Wed, 15 Sep 2021 20:36:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 738D260240 for ; Wed, 15 Sep 2021 20:36:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232018AbhIOUhv (ORCPT ); Wed, 15 Sep 2021 16:37:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231703AbhIOUho (ORCPT ); Wed, 15 Sep 2021 16:37:44 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95AC3C061764 for ; Wed, 15 Sep 2021 13:36:21 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id q11so5825843wrr.9 for ; Wed, 15 Sep 2021 13:36:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=8Ey5pVclP/nMS6WKkIL4uf44AVx+0z97PoZxSVXx7Vk=; b=nKegNZaZZ1ho3lyqtxnwKhDqLwdPECHeAFkX63yAQznEVXgtTEh8pIaSk0FvpYl8G3 +LsuEjMge6/jn0LfhXZdl2Be8rTwpApIA8wlBLhd+7EDkclVmAkTtR2fd1aU4SvrMZH8 KHuLCcYgS3AcZ6iA2tsMAe/rXDCOjTz0k8LbpfX49HqWsKJshvgsUUJ3lxwfwW9BLHJd RmlRNi4NSqocqIVetjoMRnyEcQ93qAvD/GGnmTO6qIbEqjhlAPxY+92NIPszwo5v2pEb cSxZGmXpND5uCHkrmCdCHn0A31i2dMMifgtLD3jkaEXWoIFdgX2YNOqN43Ha/+elva4m QdGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=8Ey5pVclP/nMS6WKkIL4uf44AVx+0z97PoZxSVXx7Vk=; b=jeVB8VL905+iE3LXRVt3o3c2btEviQX4Jj3gFgp9LC0ZPTjQOcDkg8H31YHku8+qSO ceFPRhCIYaRMHj9qLVDh0xe+slocx7E4f3BXV0bNNQYtEq21A1vhiPAOGfiKQaxaeo5s rFeB4FTUtVyM681yJAYWPzVytS4HdYWN5NTVa0ANxXw99FFvsVaeLvCvqd/V9EOFP7Zd eUX1Xsb0TopgleRzH7B+o6UOSsT1rvtEbhbS6kOWIldewHLLnG9vJaauNMXpXJmBBTqD fHKqpoz4qME4fZYQZDF8qj3uwIYkxkOZ9EgaKcAtKfbQdKIHgYuEDPYBmmCIx7NIqoCh Q7Yw== X-Gm-Message-State: AOAM531MF095YskEPhw5hYwMPYUPr6Cxr/MO/KM+xaZIQ7NaBw3VIODL RK8dSsanuDBOpnHsar1405raS9aNXqE= X-Google-Smtp-Source: ABdhPJyKSPLssMNlLFSHe1sXThWWzoLv4trF19cao7sireSBLpdbJIvfIVnX9b8A8Vtx5yUndP+5KA== X-Received: by 2002:adf:f48b:: with SMTP id l11mr2113527wro.254.1631738180261; Wed, 15 Sep 2021 13:36:20 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id o17sm1028070wrj.96.2021.09.15.13.36.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Sep 2021 13:36:20 -0700 (PDT) Message-Id: <7182419f6dfa49d4d8415736943c72a575ab6753.1631738177.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 15 Sep 2021 20:36:12 +0000 Subject: [PATCH 2/7] simple-ipc: preparations for supporting binary messages. Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff Hostetler , Jeff Hostetler Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Jeff Hostetler From: Jeff Hostetler Add `command_len` argument to the Simple IPC API. In my original Simple IPC API, I assumed that the request would always be a null-terminated string of text characters. The command arg was just a `const char *`. I found a caller that would like to pass a binary command to the daemon, so I want to ammend the Simple IPC API to take `const char *command, size_t command_len` and pass that to the daemon. (Really, the first arg should just be a `void *` or `const unsigned byte *` to make that clearer.) Note, the response side has always been a `struct strbuf` which includes the buffer and length, so we already support returning a binary answer. (Yes, it feels a little weird returning a binary buffer in a `strbuf`, but it works.) Signed-off-by: Jeff Hostetler --- compat/simple-ipc/ipc-unix-socket.c | 14 +++++++----- compat/simple-ipc/ipc-win32.c | 14 +++++++----- simple-ipc.h | 7 ++++-- t/helper/test-simple-ipc.c | 34 +++++++++++++++++++---------- 4 files changed, 46 insertions(+), 23 deletions(-) diff --git a/compat/simple-ipc/ipc-unix-socket.c b/compat/simple-ipc/ipc-unix-socket.c index 1927e6ef4bc..4e28857a0a1 100644 --- a/compat/simple-ipc/ipc-unix-socket.c +++ b/compat/simple-ipc/ipc-unix-socket.c @@ -168,7 +168,8 @@ void ipc_client_close_connection(struct ipc_client_connection *connection) int ipc_client_send_command_to_connection( struct ipc_client_connection *connection, - const char *message, struct strbuf *answer) + const char *message, size_t message_len, + struct strbuf *answer) { int ret = 0; @@ -176,7 +177,7 @@ int ipc_client_send_command_to_connection( trace2_region_enter("ipc-client", "send-command", NULL); - if (write_packetized_from_buf_no_flush(message, strlen(message), + if (write_packetized_from_buf_no_flush(message, message_len, connection->fd) < 0 || packet_flush_gently(connection->fd) < 0) { ret = error(_("could not send IPC command")); @@ -197,7 +198,8 @@ done: int ipc_client_send_command(const char *path, const struct ipc_client_connect_options *options, - const char *message, struct strbuf *answer) + const char *message, size_t message_len, + struct strbuf *answer) { int ret = -1; enum ipc_active_state state; @@ -208,7 +210,9 @@ int ipc_client_send_command(const char *path, if (state != IPC_STATE__LISTENING) return ret; - ret = ipc_client_send_command_to_connection(connection, message, answer); + ret = ipc_client_send_command_to_connection(connection, + message, message_len, + answer); ipc_client_close_connection(connection); @@ -503,7 +507,7 @@ static int worker_thread__do_io( if (ret >= 0) { ret = worker_thread_data->server_data->application_cb( worker_thread_data->server_data->application_data, - buf.buf, do_io_reply_callback, &reply_data); + buf.buf, buf.len, do_io_reply_callback, &reply_data); packet_flush_gently(reply_data.fd); } diff --git a/compat/simple-ipc/ipc-win32.c b/compat/simple-ipc/ipc-win32.c index 8dc7bda087d..8e889d6a506 100644 --- a/compat/simple-ipc/ipc-win32.c +++ b/compat/simple-ipc/ipc-win32.c @@ -208,7 +208,8 @@ void ipc_client_close_connection(struct ipc_client_connection *connection) int ipc_client_send_command_to_connection( struct ipc_client_connection *connection, - const char *message, struct strbuf *answer) + const char *message, size_t message_len, + struct strbuf *answer) { int ret = 0; @@ -216,7 +217,7 @@ int ipc_client_send_command_to_connection( trace2_region_enter("ipc-client", "send-command", NULL); - if (write_packetized_from_buf_no_flush(message, strlen(message), + if (write_packetized_from_buf_no_flush(message, message_len, connection->fd) < 0 || packet_flush_gently(connection->fd) < 0) { ret = error(_("could not send IPC command")); @@ -239,7 +240,8 @@ done: int ipc_client_send_command(const char *path, const struct ipc_client_connect_options *options, - const char *message, struct strbuf *response) + const char *message, size_t message_len, + struct strbuf *response) { int ret = -1; enum ipc_active_state state; @@ -250,7 +252,9 @@ int ipc_client_send_command(const char *path, if (state != IPC_STATE__LISTENING) return ret; - ret = ipc_client_send_command_to_connection(connection, message, response); + ret = ipc_client_send_command_to_connection(connection, + message, message_len, + response); ipc_client_close_connection(connection); @@ -458,7 +462,7 @@ static int do_io(struct ipc_server_thread_data *server_thread_data) if (ret >= 0) { ret = server_thread_data->server_data->application_cb( server_thread_data->server_data->application_data, - buf.buf, do_io_reply_callback, &reply_data); + buf.buf, buf.len, do_io_reply_callback, &reply_data); packet_flush_gently(reply_data.fd); diff --git a/simple-ipc.h b/simple-ipc.h index 2c48a5ee004..9c7330fcda0 100644 --- a/simple-ipc.h +++ b/simple-ipc.h @@ -107,7 +107,8 @@ void ipc_client_close_connection(struct ipc_client_connection *connection); */ int ipc_client_send_command_to_connection( struct ipc_client_connection *connection, - const char *message, struct strbuf *answer); + const char *message, size_t message_len, + struct strbuf *answer); /* * Used by the client to synchronously connect and send and receive a @@ -119,7 +120,8 @@ int ipc_client_send_command_to_connection( */ int ipc_client_send_command(const char *path, const struct ipc_client_connect_options *options, - const char *message, struct strbuf *answer); + const char *message, size_t message_len, + struct strbuf *answer); /* * Simple IPC Server Side API. @@ -144,6 +146,7 @@ typedef int (ipc_server_reply_cb)(struct ipc_server_reply_data *, */ typedef int (ipc_server_application_cb)(void *application_data, const char *request, + size_t request_len, ipc_server_reply_cb *reply_cb, struct ipc_server_reply_data *reply_data); diff --git a/t/helper/test-simple-ipc.c b/t/helper/test-simple-ipc.c index 42040ef81b1..91345180750 100644 --- a/t/helper/test-simple-ipc.c +++ b/t/helper/test-simple-ipc.c @@ -112,7 +112,7 @@ static int app__slow_command(ipc_server_reply_cb *reply_cb, /* * The client sent a command followed by a (possibly very) large buffer. */ -static int app__sendbytes_command(const char *received, +static int app__sendbytes_command(const char *received, size_t received_len, ipc_server_reply_cb *reply_cb, struct ipc_server_reply_data *reply_data) { @@ -123,6 +123,13 @@ static int app__sendbytes_command(const char *received, int errs = 0; int ret; + /* + * The test is setup to send: + * "sendbytes" SP + */ + if (received_len < strlen("sendbytes ")) + BUG("received_len is short in app__sendbytes_command"); + if (skip_prefix(received, "sendbytes ", &p)) len_ballast = strlen(p); @@ -160,7 +167,7 @@ static ipc_server_application_cb test_app_cb; * by this application. */ static int test_app_cb(void *application_data, - const char *command, + const char *command, size_t command_len, ipc_server_reply_cb *reply_cb, struct ipc_server_reply_data *reply_data) { @@ -173,7 +180,7 @@ static int test_app_cb(void *application_data, if (application_data != (void*)&my_app_data) BUG("application_cb: application_data pointer wrong"); - if (!strcmp(command, "quit")) { + if (command_len == 4 && !strncmp(command, "quit", 4)) { /* * The client sent a "quit" command. This is an async * request for the server to shutdown. @@ -193,22 +200,23 @@ static int test_app_cb(void *application_data, return SIMPLE_IPC_QUIT; } - if (!strcmp(command, "ping")) { + if (command_len == 4 && !strncmp(command, "ping", 4)) { const char *answer = "pong"; return reply_cb(reply_data, answer, strlen(answer)); } - if (!strcmp(command, "big")) + if (command_len == 3 && !strncmp(command, "big", 3)) return app__big_command(reply_cb, reply_data); - if (!strcmp(command, "chunk")) + if (command_len == 5 && !strncmp(command, "chunk", 5)) return app__chunk_command(reply_cb, reply_data); - if (!strcmp(command, "slow")) + if (command_len == 4 && !strncmp(command, "slow", 4)) return app__slow_command(reply_cb, reply_data); - if (starts_with(command, "sendbytes ")) - return app__sendbytes_command(command, reply_cb, reply_data); + if (command_len >= 10 && starts_with(command, "sendbytes ")) + return app__sendbytes_command(command, command_len, + reply_cb, reply_data); return app__unhandled_command(command, reply_cb, reply_data); } @@ -488,7 +496,9 @@ static int client__send_ipc(void) options.wait_if_busy = 1; options.wait_if_not_found = 0; - if (!ipc_client_send_command(cl_args.path, &options, command, &buf)) { + if (!ipc_client_send_command(cl_args.path, &options, + command, strlen(command), + &buf)) { if (buf.len) { printf("%s\n", buf.buf); fflush(stdout); @@ -556,7 +566,9 @@ static int do_sendbytes(int bytecount, char byte, const char *path, strbuf_addstr(&buf_send, "sendbytes "); strbuf_addchars(&buf_send, byte, bytecount); - if (!ipc_client_send_command(path, options, buf_send.buf, &buf_resp)) { + if (!ipc_client_send_command(path, options, + buf_send.buf, buf_send.len, + &buf_resp)) { strbuf_rtrim(&buf_resp); printf("sent:%c%08d %s\n", byte, bytecount, buf_resp.buf); fflush(stdout); From patchwork Wed Sep 15 20:36:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jeff Hostetler X-Patchwork-Id: 12497415 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0135C433F5 for ; Wed, 15 Sep 2021 20:36:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D189061131 for ; Wed, 15 Sep 2021 20:36:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231986AbhIOUht (ORCPT ); Wed, 15 Sep 2021 16:37:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231766AbhIOUho (ORCPT ); Wed, 15 Sep 2021 16:37:44 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19722C061766 for ; Wed, 15 Sep 2021 13:36:22 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id t18so5925828wrb.0 for ; Wed, 15 Sep 2021 13:36:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:mime-version :content-transfer-encoding:fcc:to:cc; bh=WhOw3Obe0gOgQO+AfU1jSOzrMAyzSWPdmF+5plxu8Aw=; b=KxHrVsQVyeB95GG/Lx7VQURI6X7alBOnUTrA1q445BJqFtGiKWyaxpWOXlvz/mad1M 0x/fIopEyj0ngzHPQZgfGtwZwu/aSd2/e+WobQgMzVup1jnGK5uHWiiQcURnc/X+3qM1 CIqVBYNIvTfqAzciiDBVyFrs+t9zNkCEnyeK+sZe03JbQ8iugQie5DJf5k7nrCNleXWo HXgUr4NKxxUkNfx6BHHIztIIe1RwDB1sMANjBXb5PJNfSFGkwUTDKh+LUeyqtcbJgwyR d1pBYaQfyBaT/KazVmpmUn6FqNYNafLLdipZA99N7kR/79rgm6WzoIAaC9anBWt45jab iNcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:mime-version:content-transfer-encoding:fcc:to:cc; bh=WhOw3Obe0gOgQO+AfU1jSOzrMAyzSWPdmF+5plxu8Aw=; b=f6bqGxUrr/1/sVYQsX9Ycellovw1vRpC2uGXKsoSq1pVEVeKSiL5oPJAyj/frN65sV RRNb+5LS5Zq3G/NUrEluh7Nhjg8HxlFn9u7LorSnrLOTRcSDrV8NAAhpLpr4h6xSpdk7 N0exaY85Vg9/AOH+/loACnJ7bzl6+TKtV3DTD+mLq1OV5kWxu+0nNLANL1AeNMONQ90q qW53L9kABY6FjNDwub/D/84qyqtWkI4kvkcFlbHcW5JfV1qmKvNcQVaLJOmW0fmmUEkG JS9tPZ8s8uFT2QsysPC2Evu644U4USVAYetxsM7Gm9LLSf/V6UoqI3FmJDe/jUIFOwBS CTmw== X-Gm-Message-State: AOAM532T1O2oTBtcVsUOMj8GcWe96P82/7qopFwtPPwEglIzsMmNuwEG Lr4JGgoJaNPOa1AoHApVktVXoCa445g= X-Google-Smtp-Source: ABdhPJxfHiNaQ/z6xkbHYmxrlS56OCYaRtUTEisRJJkFrJloDqN2d/vlGCkz1DSjw66CJQgYzmSvuA== X-Received: by 2002:adf:f683:: with SMTP id v3mr2116048wrp.423.1631738180803; Wed, 15 Sep 2021 13:36:20 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id j2sm1017550wrq.35.2021.09.15.13.36.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Sep 2021 13:36:20 -0700 (PDT) Message-Id: <7de207828caa9e50906f39b955e39e9de2c1768f.1631738177.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 15 Sep 2021 20:36:13 +0000 Subject: [PATCH 3/7] simple-ipc: move definition of ipc_active_state outside of ifdef MIME-Version: 1.0 Fcc: Sent To: git@vger.kernel.org Cc: Jeff Hostetler , Jeff Hostetler Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Jeff Hostetler From: Jeff Hostetler From: Carlo Marcelo Arenas Belón Move the declartion of the `enum ipc_active_state` type outside of the SUPPORTS_SIMPLE_IPC ifdef. A later commit will introduce the `fsmonitor_ipc__*()` API and stub in a "mock" implementation that requires this enum in some function signatures. Signed-off-by: Carlo Marcelo Arenas Belón Signed-off-by: Jeff Hostetler --- simple-ipc.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/simple-ipc.h b/simple-ipc.h index 9c7330fcda0..b396293bdfc 100644 --- a/simple-ipc.h +++ b/simple-ipc.h @@ -5,13 +5,6 @@ * See Documentation/technical/api-simple-ipc.txt */ -#ifdef SUPPORTS_SIMPLE_IPC -#include "pkt-line.h" - -/* - * Simple IPC Client Side API. - */ - enum ipc_active_state { /* * The pipe/socket exists and the daemon is waiting for connections. @@ -43,6 +36,13 @@ enum ipc_active_state { IPC_STATE__OTHER_ERROR, }; +#ifdef SUPPORTS_SIMPLE_IPC +#include "pkt-line.h" + +/* + * Simple IPC Client Side API. + */ + struct ipc_client_connect_options { /* * Spin under timeout if the server is running but can't From patchwork Wed Sep 15 20:36:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Hostetler X-Patchwork-Id: 12497407 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EFED2C433EF for ; Wed, 15 Sep 2021 20:36:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C9DF661130 for ; Wed, 15 Sep 2021 20:36:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231860AbhIOUhq (ORCPT ); Wed, 15 Sep 2021 16:37:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60074 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231786AbhIOUho (ORCPT ); Wed, 15 Sep 2021 16:37:44 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B9C58C061767 for ; Wed, 15 Sep 2021 13:36:22 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id s24so3153430wmh.4 for ; Wed, 15 Sep 2021 13:36:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=I60jO8u2FyLK2dJK68MVRXusZqnqzffKspnxldXFzkM=; b=SKQ2qeCw4FeKjpOSUaJcEATo+tMMF+cE8f2xi9wY9fgylZRs1Y8X4YLlvIEM+leqRZ j1DPuGQcsvGG/LTgEL2/nicpaY0aNU5kQisti4P+3Pe5Nbl5ElZ/zcmUq1qwLlJLRHUj VtgDBy7kOmV2DtCPIjhoMkMCglv5e9mruFM3PhGaDVcRkHlsrrUioEPZ9D5Y8CZlX0wp rfoSq48UPV2LBUkSgnrx9adnqTXkL+MQ8OoGKZnURGsynPTH+D1wSLm39qtvZzK5jwoS +wiUAWkH2iYpJkhUYa1swAfYP9GdAWe7ad0m1NW9NdrcD7YatxWJV1tPYA3w1P1qsfQC K2tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=I60jO8u2FyLK2dJK68MVRXusZqnqzffKspnxldXFzkM=; b=Np/2IEee1kzL+qVdRwUjZU36XN/Nokj3ggmHGsazBpfpWtNbVUQwn2nzUuQxrmw1+u Anss9z1xTYx+AlNDAduxKQF0lDGkhXRNLfRM+/Lr9MYHUc0aMy4WiBfy1IUbPAkrRL0v ncCu4/yL3Qrhlv+eVEPafadb9czSEsRo7KXMepyvctWtGOOhqsiQf2gQ9fJeMpOVfvlX 6EuV0y1TJDoY42h6MWgozDiS2mWFcdAcebmkTBM+G/S1L02Hvr3OM+mydd3dc6fGw6aw oqY8EGfFl6PvL0B+ognHDbp7SLn1ponEXIhn+huTvkJSjrcPjup6daS279gCjf9s4a7C AIIQ== X-Gm-Message-State: AOAM530mHoU3byYnXXzECUoPyz58wXWL6CRvYZ4ExM7E59QI+9xh3v2k wEk7YhaFqscLV03yAHYCJh9NonCuN7Y= X-Google-Smtp-Source: ABdhPJy4lJ7XP9drButdO6LC5B6qVQkH1xmBa+GiymmNxgiMCZYDSk6lu0vJlvsuu9h6tNCggi36nA== X-Received: by 2002:a05:600c:40c4:: with SMTP id m4mr6575310wmh.64.1631738181357; Wed, 15 Sep 2021 13:36:21 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id a3sm987002wrt.28.2021.09.15.13.36.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Sep 2021 13:36:21 -0700 (PDT) Message-Id: <30b7bb247c3c8531119f99e293d84b1836297251.1631738177.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 15 Sep 2021 20:36:14 +0000 Subject: [PATCH 4/7] simple-ipc/ipc-win32: add trace2 debugging Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff Hostetler , Jeff Hostetler Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Jeff Hostetler From: Jeff Hostetler Create "ipc-debug" category events to log unexpected errors when creating Simple-IPC connections. Signed-off-by: Jeff Hostetler --- compat/simple-ipc/ipc-win32.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/compat/simple-ipc/ipc-win32.c b/compat/simple-ipc/ipc-win32.c index 8e889d6a506..6c8a308de13 100644 --- a/compat/simple-ipc/ipc-win32.c +++ b/compat/simple-ipc/ipc-win32.c @@ -49,6 +49,9 @@ static enum ipc_active_state get_active_state(wchar_t *pipe_path) if (GetLastError() == ERROR_FILE_NOT_FOUND) return IPC_STATE__PATH_NOT_FOUND; + trace2_data_intmax("ipc-debug", NULL, "getstate/waitpipe/gle", + (intmax_t)GetLastError()); + return IPC_STATE__OTHER_ERROR; } @@ -112,6 +115,11 @@ static enum ipc_active_state connect_to_server( if (GetLastError() == ERROR_SEM_TIMEOUT) return IPC_STATE__NOT_LISTENING; + gle = GetLastError(); + trace2_data_intmax("ipc-debug", NULL, + "connect/waitpipe/gle", + (intmax_t)gle); + return IPC_STATE__OTHER_ERROR; } @@ -133,17 +141,31 @@ static enum ipc_active_state connect_to_server( break; /* try again */ default: + trace2_data_intmax("ipc-debug", NULL, + "connect/createfile/gle", + (intmax_t)gle); + return IPC_STATE__OTHER_ERROR; } } if (!SetNamedPipeHandleState(hPipe, &mode, NULL, NULL)) { + gle = GetLastError(); + trace2_data_intmax("ipc-debug", NULL, + "connect/setpipestate/gle", + (intmax_t)gle); + CloseHandle(hPipe); return IPC_STATE__OTHER_ERROR; } *pfd = _open_osfhandle((intptr_t)hPipe, O_RDWR|O_BINARY); if (*pfd < 0) { + gle = GetLastError(); + trace2_data_intmax("ipc-debug", NULL, + "connect/openosfhandle/gle", + (intmax_t)gle); + CloseHandle(hPipe); return IPC_STATE__OTHER_ERROR; } From patchwork Wed Sep 15 20:36:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Hostetler X-Patchwork-Id: 12497411 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA497C433EF for ; Wed, 15 Sep 2021 20:36:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A05AE61101 for ; Wed, 15 Sep 2021 20:36:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231889AbhIOUhs (ORCPT ); Wed, 15 Sep 2021 16:37:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231792AbhIOUho (ORCPT ); Wed, 15 Sep 2021 16:37:44 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 41E92C061768 for ; Wed, 15 Sep 2021 13:36:23 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id e26so3181215wmk.2 for ; Wed, 15 Sep 2021 13:36:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=xH8KHPZqTUyNqvPLgX0jLXAWPh9eatde8fJJ9xbOwYY=; b=UNOuTP8j7FIF/xvbaKr3m6UH1HSqqVcRxOzBQjhI77gSHxFkicr4NvHjBYW+afhN68 UlKzAp8V+eWfqz1h8VXuUxqZOq/hBHoKhSfiiawz7nQQwRf5FxzgM+Ilf3st0aKTmfwz B6cu+SHpXOr5rOflMETy8LAbRBYLowo2Y67zmdLgLSSvL2VVTZY9Cp4EJSJL3wKsSsER c03V+9QT4ycZvFOl67ReMXrIQSCFtXoUFlPzUT8Ol+EYUSBiuWRRfAqrS1hjsyGo53Cn 9mSt6D2rv8FIMIfxVxU3tQBumJWP78gElKOOvPBxuFMu1fA9AlImmxW/t8VQF1YqW3qA nnpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=xH8KHPZqTUyNqvPLgX0jLXAWPh9eatde8fJJ9xbOwYY=; b=Yz8PM9gxon1/Pi/9W0J2U2UIpEa/9Ujr2Osgw/Fa6h6PhfxeCDO5ePTX0xDdLoJLzO Nz1uRMwW29JKWv0kD58L1hK8MczokbM2ajRh7hx6md8TeV72iUDglJAhBL1LDV/XEhDo 5ytMrdgo5SYppQtq/CGKJVxQRjmS3eIx9XxJbdQ6b9SBO1rUPdNmQ0euBsT4wx+CW31p LMq10kXSXYltwnKFgIAfJEw3PzFTcW2s0CFoAf3Y967iQbdmxRxJn79VVOYVjDvhstcI CS89zCNvPOr3/XmgCa7hTFsOefH+cUk7RBrng55OkFhaCclkfCRXija6uv8srS8Vgjco 25Pg== X-Gm-Message-State: AOAM532+wwVq1oBkvjXPZySrIyCYeRfGAhs0Vrs5XUcfPoMMoKzMQppS IKnpAw5z0L2jjVuz/YSgQmegbOs96NU= X-Google-Smtp-Source: ABdhPJxZlpwC21yNhjYzo0oix8EqYhFZM8GgIvdjFluX2E+YSp2Bnnlgbg4WbyCnjDtiSaL8AnmDsA== X-Received: by 2002:a1c:7e85:: with SMTP id z127mr1581341wmc.141.1631738181910; Wed, 15 Sep 2021 13:36:21 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id l19sm816256wrc.16.2021.09.15.13.36.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Sep 2021 13:36:21 -0700 (PDT) Message-Id: <5eadf71929559968cafa18d03c3a623b1adff926.1631738177.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 15 Sep 2021 20:36:15 +0000 Subject: [PATCH 5/7] simple-ipc/ipc-win32: add Windows ACL to named pipe Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff Hostetler , Jeff Hostetler Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Jeff Hostetler From: Jeff Hostetler Set an ACL on the named pipe to allow the well-known group EVERYONE to read and write to the IPC server's named pipe. In the event that the daemon was started with elevation, allow non-elevated clients to communicate with the daemon. Signed-off-by: Jeff Hostetler --- compat/simple-ipc/ipc-win32.c | 140 +++++++++++++++++++++++++++++++--- 1 file changed, 129 insertions(+), 11 deletions(-) diff --git a/compat/simple-ipc/ipc-win32.c b/compat/simple-ipc/ipc-win32.c index 6c8a308de13..374ae2f81c7 100644 --- a/compat/simple-ipc/ipc-win32.c +++ b/compat/simple-ipc/ipc-win32.c @@ -3,6 +3,8 @@ #include "strbuf.h" #include "pkt-line.h" #include "thread-utils.h" +#include "accctrl.h" +#include "aclapi.h" #ifndef SUPPORTS_SIMPLE_IPC /* @@ -591,11 +593,132 @@ finished: return NULL; } +/* + * We need to build a Windows "SECURITY_ATTRIBUTES" object and use it + * to apply an ACL when we create the initial instance of the Named + * Pipe. The construction is somewhat involved and consists of + * several sequential steps and intermediate objects. + * + * We use this structure to hold these intermediate pointers so that + * we can free them as a group. (It is unclear from the docs whether + * some of these intermediate pointers can be freed before we are + * finished using the "lpSA" member.) + */ +struct my_sa_data +{ + PSID pEveryoneSID; + PACL pACL; + PSECURITY_DESCRIPTOR pSD; + LPSECURITY_ATTRIBUTES lpSA; +}; + +static void init_sa(struct my_sa_data *d) +{ + memset(d, 0, sizeof(*d)); +} + +static void release_sa(struct my_sa_data *d) +{ + if (d->pEveryoneSID) + FreeSid(d->pEveryoneSID); + if (d->pACL) + LocalFree(d->pACL); + if (d->pSD) + LocalFree(d->pSD); + if (d->lpSA) + LocalFree(d->lpSA); + + memset(d, 0, sizeof(*d)); +} + +/* + * Create SECURITY_ATTRIBUTES to apply to the initial named pipe. The + * creator of the first server instance gets to set the ACLs on it. + * + * We allow the well-known group `EVERYONE` to have read+write access + * to the named pipe so that clients can send queries to the daemon + * and receive the response. + * + * Normally, this is not necessary since the daemon is usually + * automatically started by a foreground command like `git status`, + * but in those cases where an elevated Git command started the daemon + * (such that the daemon itself runs with elevation), we need to add + * the ACL so that non-elevated commands can write to it. + * + * The following document was helpful: + * https://docs.microsoft.com/en-us/windows/win32/secauthz/creating-a-security-descriptor-for-a-new-object-in-c-- + * + * Returns d->lpSA set to a SA or NULL. + */ +static LPSECURITY_ATTRIBUTES get_sa(struct my_sa_data *d) +{ + SID_IDENTIFIER_AUTHORITY sid_auth_world = SECURITY_WORLD_SID_AUTHORITY; +#define NR_EA (1) + EXPLICIT_ACCESS ea[NR_EA]; + DWORD dwResult; + + if (!AllocateAndInitializeSid(&sid_auth_world, 1, + SECURITY_WORLD_RID, 0,0,0,0,0,0,0, + &d->pEveryoneSID)) { + DWORD gle = GetLastError(); + trace2_data_intmax("ipc-debug", NULL, "alloc-world-sid/gle", + (intmax_t)gle); + goto fail; + } + + memset(ea, 0, NR_EA * sizeof(EXPLICIT_ACCESS)); + + ea[0].grfAccessPermissions = GENERIC_READ | GENERIC_WRITE; + ea[0].grfAccessMode = SET_ACCESS; + ea[0].grfInheritance = NO_INHERITANCE; + ea[0].Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE; + ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID; + ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; + ea[0].Trustee.ptstrName = (LPTSTR)d->pEveryoneSID; + + dwResult = SetEntriesInAcl(NR_EA, ea, NULL, &d->pACL); + if (dwResult != ERROR_SUCCESS) { + DWORD gle = GetLastError(); + trace2_data_intmax("ipc-debug", NULL, "set-acl-entry/gle", + (intmax_t)gle); + trace2_data_intmax("ipc-debug", NULL, "set-acl-entry/dw", + (intmax_t)dwResult); + goto fail; + } + + d->pSD = (PSECURITY_DESCRIPTOR)LocalAlloc( + LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH); + if (!InitializeSecurityDescriptor(d->pSD, SECURITY_DESCRIPTOR_REVISION)) { + DWORD gle = GetLastError(); + trace2_data_intmax("ipc-debug", NULL, "init-sd/gle", (intmax_t)gle); + goto fail; + } + + if (!SetSecurityDescriptorDacl(d->pSD, TRUE, d->pACL, FALSE)) { + DWORD gle = GetLastError(); + trace2_data_intmax("ipc-debug", NULL, "set-sd-dacl/gle", (intmax_t)gle); + goto fail; + } + + d->lpSA = (LPSECURITY_ATTRIBUTES)LocalAlloc(LPTR, sizeof(SECURITY_ATTRIBUTES)); + d->lpSA->nLength = sizeof(SECURITY_ATTRIBUTES); + d->lpSA->lpSecurityDescriptor = d->pSD; + d->lpSA->bInheritHandle = FALSE; + + return d->lpSA; + +fail: + release_sa(d); + return NULL; +} + static HANDLE create_new_pipe(wchar_t *wpath, int is_first) { HANDLE hPipe; DWORD dwOpenMode, dwPipeMode; - LPSECURITY_ATTRIBUTES lpsa = NULL; + struct my_sa_data my_sa_data; + + init_sa(&my_sa_data); dwOpenMode = PIPE_ACCESS_INBOUND | PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED; @@ -611,20 +734,15 @@ static HANDLE create_new_pipe(wchar_t *wpath, int is_first) * set the ACL / Security Attributes on the named * pipe; subsequent instances inherit and cannot * change them. - * - * TODO Should we allow the application layer to - * specify security attributes, such as `LocalService` - * or `LocalSystem`, when we create the named pipe? - * This question is probably not important when the - * daemon is started by a foreground user process and - * only needs to talk to the current user, but may be - * if the daemon is run via the Control Panel as a - * System Service. */ + get_sa(&my_sa_data); } hPipe = CreateNamedPipeW(wpath, dwOpenMode, dwPipeMode, - PIPE_UNLIMITED_INSTANCES, 1024, 1024, 0, lpsa); + PIPE_UNLIMITED_INSTANCES, 1024, 1024, 0, + my_sa_data.lpSA); + + release_sa(&my_sa_data); return hPipe; } From patchwork Wed Sep 15 20:36:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Hostetler X-Patchwork-Id: 12497409 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91407C433FE for ; Wed, 15 Sep 2021 20:36:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 73E4061155 for ; Wed, 15 Sep 2021 20:36:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231863AbhIOUhq (ORCPT ); Wed, 15 Sep 2021 16:37:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231803AbhIOUho (ORCPT ); Wed, 15 Sep 2021 16:37:44 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C34D6C0613C1 for ; Wed, 15 Sep 2021 13:36:23 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id d207-20020a1c1dd8000000b00307e2d1ec1aso2936617wmd.5 for ; Wed, 15 Sep 2021 13:36:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=hrvC+HIqL2XOQTHkljqsU9Y1itYKqJ4zzmtZdhx3Zas=; b=RRtVTrQ1dSFAcA28vh9Utg7qGhzCpIEJCiR0sYmqrQNRyPT6M0o0d2gQJiyrWx6g64 a1GRp5XEUyRG5DVINug5v8Je4wx56bZrTtrDGRmcP+/baEqYXHoM9BcfylOuJsV9CO2L KtULP54uD9qV6p74WpsdW8LNWYyUNwEPbZWcR9tOLbgETlSgSFq5lIsNytCRwH7ITwo2 1cYW6IvhoIfPxX3TithGvLZgMrTgvq8y/dO7tJbHuCOJbrF9QM7CyE4fByQiKiSCLSki NoFkWq2Oxe9Hj2czz+xN9r6awdTV8PILYB0siMKxaJt0q+6Bsgjc6xW3txJQQ2HGrqXD 9ryQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=hrvC+HIqL2XOQTHkljqsU9Y1itYKqJ4zzmtZdhx3Zas=; b=OMo/oOEqtoum1EO3y7MFVVuTWDNlNaKHtE/vpCU8EXqxED+070sSse52ds4O+Ew409 ShRk94s2IUjzLEcDmE0V0Z1NB6Z3hxE00SO6CilS7QfHFJtixj2h6O2np+8U3VbuVNfQ WlY62nawf7rRsXl+/cTH5Bm/IAWRsgEt6zV3Gdgq47LPMwC0A87IsOegVku35YyY+XdV teuVRSjkpYGx3EU+Q11GL4Akn248I9mP8k4TGX8DCw7OGypC2nqPiG/DiivYoTmKu7TY ygs/aCpa8L+uqIGM6beirURifh5f5HBV+A2bTIwEa3OFwFZkXISn11f4TnhHcKMnW1XO 9dQA== X-Gm-Message-State: AOAM532PEZMSjseOXd8bvP2bfJisdXhWhwoO/Nog9PDzNyMWMnmgDw59 Zk9r+d3OUhlyW1tJvKiLmRxaXGpaI/w= X-Google-Smtp-Source: ABdhPJxFYH/d/b4Jkfn77F0OJSM6l7zryvkmvFlKpL2iqx/kR1pD5iiwrifAisO7jorZIOmjAf8r8A== X-Received: by 2002:a7b:c385:: with SMTP id s5mr6341659wmj.149.1631738182413; Wed, 15 Sep 2021 13:36:22 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id o24sm5108672wmm.11.2021.09.15.13.36.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Sep 2021 13:36:22 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Wed, 15 Sep 2021 20:36:16 +0000 Subject: [PATCH 6/7] run-command: create start_bg_command Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff Hostetler , Jeff Hostetler Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Jeff Hostetler From: Jeff Hostetler Create a variation of `run_command()` and `start_command()` to launch a command into the background and optionally wait for it to become "ready" before returning. Signed-off-by: Jeff Hostetler --- run-command.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++ run-command.h | 48 ++++++++++++++++++++ 2 files changed, 171 insertions(+) diff --git a/run-command.c b/run-command.c index 3e4e082e94d..fe75fd08f74 100644 --- a/run-command.c +++ b/run-command.c @@ -1901,3 +1901,126 @@ void prepare_other_repo_env(struct strvec *env_array, const char *new_git_dir) } strvec_pushf(env_array, "%s=%s", GIT_DIR_ENVIRONMENT, new_git_dir); } + +enum start_bg_result start_bg_command(struct child_process *cmd, + start_bg_wait_cb *wait_cb, + void *cb_data, + unsigned int timeout_sec) +{ + enum start_bg_result sbgr = SBGR_ERROR; + int ret; + int wait_status; + pid_t pid_seen; + time_t time_limit; + + /* + * Silently disallow child cleanup -- even if requested. + * The child process should persist in the background + * and possibly/probably after this process exits. That + * is, don't kill the child during our atexit routine. + */ + cmd->clean_on_exit = 0; + + ret = start_command(cmd); + if (ret) { + /* + * We assume that if `start_command()` fails, we + * either get a complete `trace2_child_start() / + * trace2_child_exit()` pair or it fails before the + * `trace2_child_start()` is emitted, so we do not + * need to worry about it here. + * + * We also assume that `start_command()` does not add + * us to the cleanup list. And that it calls + * calls `child_process_clear()`. + */ + sbgr = SBGR_ERROR; + goto done; + } + + time(&time_limit); + time_limit += timeout_sec; + +wait: + pid_seen = waitpid(cmd->pid, &wait_status, WNOHANG); + + if (pid_seen == 0) { + /* + * The child is currently running. Ask the callback + * if the child is ready to do work or whether we + * should keep waiting for it to boot up. + */ + ret = (*wait_cb)(cb_data, cmd); + if (!ret) { + /* + * The child is running and "ready". + * + * NEEDSWORK: As we prepare to orphan (release to + * the background) this child, it is not appropriate + * to emit a `trace2_child_exit()` event. Should we + * create a new event for this case? + */ + sbgr = SBGR_READY; + goto done; + } else if (ret > 0) { + time_t now; + + time(&now); + if (now < time_limit) + goto wait; + + /* + * Our timeout has expired. We don't try to + * kill the child, but rather let it continue + * (hopefully) trying to startup. + * + * NEEDSWORK: Like the "ready" case, should we + * log a custom child-something Trace2 event here? + */ + sbgr = SBGR_TIMEOUT; + goto done; + } else { + /* + * The cb gave up on this child. + * + * NEEDSWORK: Like above, should we log a custom + * Trace2 child-something event here? + */ + sbgr = SBGR_CB_ERROR; + goto done; + } + } + + if (pid_seen == cmd->pid) { + int child_code = -1; + + /* + * The child started, but exited or was terminated + * before becoming "ready". + * + * We try to match the behavior of `wait_or_whine()` + * and convert the child's status to a return code for + * tracing purposes and emit the `trace2_child_exit()` + * event. + */ + if (WIFEXITED(wait_status)) + child_code = WEXITSTATUS(wait_status); + else if (WIFSIGNALED(wait_status)) + child_code = WTERMSIG(wait_status) + 128; + trace2_child_exit(cmd, child_code); + + sbgr = SBGR_DIED; + goto done; + } + + if (pid_seen < 0 && errno == EINTR) + goto wait; + + trace2_child_exit(cmd, -1); + sbgr = SBGR_ERROR; + +done: + child_process_clear(cmd); + invalidate_lstat_cache(); + return sbgr; +} diff --git a/run-command.h b/run-command.h index af1296769f9..58065197d1b 100644 --- a/run-command.h +++ b/run-command.h @@ -496,4 +496,52 @@ int run_processes_parallel_tr2(int n, get_next_task_fn, start_failure_fn, */ void prepare_other_repo_env(struct strvec *env_array, const char *new_git_dir); +/** + * Possible return values for `start_bg_command()`. + */ +enum start_bg_result { + /* child process is "ready" */ + SBGR_READY = 0, + + /* child process could not be started */ + SBGR_ERROR, + + /* callback error when testing for "ready" */ + SBGR_CB_ERROR, + + /* timeout expired waiting for child to become "ready" */ + SBGR_TIMEOUT, + + /* child process exited or was signalled before becomming "ready" */ + SBGR_DIED, +}; + +/** + * Callback used by `start_bg_command()` to ask whether the + * child process is ready or needs more time to become ready. + * + * Returns 1 is child needs more time (subject to the requested timeout). + * Returns 0 if child is ready. + * Returns -1 on any error and cause `start_bg_command()` to also error out. + */ +typedef int(start_bg_wait_cb)(void *cb_data, + const struct child_process *cmd); + +/** + * Start a command in the background. Wait long enough for the child to + * become "ready". Capture immediate errors (like failure to start) and + * any immediate exit status (such as a shutdown/signal before the child + * became "ready"). + * + * This is a combination of `start_command()` and `finish_command()`, but + * with a custom `wait_or_whine()` that allows the caller to define when + * the child is "ready". + * + * The caller does not need to call `finish_command()`. + */ +enum start_bg_result start_bg_command(struct child_process *cmd, + start_bg_wait_cb *wait_cb, + void *cb_data, + unsigned int timeout_sec); + #endif From patchwork Wed Sep 15 20:36:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Hostetler X-Patchwork-Id: 12497419 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B4018C433FE for ; Wed, 15 Sep 2021 20:36:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 95D526103E for ; Wed, 15 Sep 2021 20:36:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232052AbhIOUhw (ORCPT ); Wed, 15 Sep 2021 16:37:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231490AbhIOUho (ORCPT ); Wed, 15 Sep 2021 16:37:44 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D789C0613CF for ; Wed, 15 Sep 2021 13:36:24 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id i23so5876206wrb.2 for ; Wed, 15 Sep 2021 13:36:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=qbJv3yQE130l2dg1VoZZP6a509XxMSMZ7Jrxs69dQ6s=; b=pGWLuszBDBuHlXir0oSSbpSWVs9flRl8lrvgH03Hm7sb3jf9dXmQpSzwVtOmGNGrCV 1PD1yYy2MBWIt3oqtvYMiNFDfvMbeUfBvLRuyMJmU8H+WVCRuRDWgReSsY+P2H58Dc5b JxoYNGMIq03+emXVI7ABKD/HzgdBTb6mSnPN1MXQ47dtuPaamxdVVHptriHSyWZuL0uZ Ou8EYPHjuUExszJxlRVYyaU7oIyy953NPnk9pvysf/PSS6/VFNib2xPS/x2bjOv0mOSz M970cwCk8/rVVqjA6AfC2qd44G11uDV+Q3YfJsFfHz3nlt3N3fgcUEVsYSm+iC9cnRUF qD6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=qbJv3yQE130l2dg1VoZZP6a509XxMSMZ7Jrxs69dQ6s=; b=qbIFtiZqu6ClmnY0ybNIAyy6vpTy0SgLfWkGczvlySqgiPmA8QO5ZfhS9J8GdW20Bm 2M2NBY7PCABRTz67oWCvVX3EvOcAuRhZ35F+XjTTeApLZ/ORH7fd+ufEi1jNcFyg0s5r xZpLxToioRvw3rXNgu5Y991c7eItTs45G6WmwGO2eImmPM+4wrq78TDdpITdO9CXEL+T QBgaJx7RgDgZxBwx96v3QmDGKa+zWti6yFPgXocoMGOkUhACPO7kY7az+AOpkdmojdw4 7owa8MG5ErUxMfYS5ElXT2KSCI5R165cosC6yDdtmCR2lbsedXJpa2qepM44OO8aGzhZ UEqA== X-Gm-Message-State: AOAM5311MatuPaWm/50XXEksXezp+olRm1vHaVW7WNU87+L2wSvjh32c KPYVmhV08zUeUK2xruls8UOgnRVGG/8= X-Google-Smtp-Source: ABdhPJxYVlBCnXm9YKkK8m0L6H+tX2EPNHLWt5odNMQPCCpIE2TysL7L8o6ZwMhuzFArFuk//UkSkw== X-Received: by 2002:adf:816f:: with SMTP id 102mr2082076wrm.368.1631738182896; Wed, 15 Sep 2021 13:36:22 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id d5sm1103219wra.38.2021.09.15.13.36.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Sep 2021 13:36:22 -0700 (PDT) Message-Id: <57f29feaadb4a732892cd193b2a1d3c838f09421.1631738177.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 15 Sep 2021 20:36:17 +0000 Subject: [PATCH 7/7] t/helper/simple-ipc: convert test-simple-ipc to use start_bg_command Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff Hostetler , Jeff Hostetler Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Jeff Hostetler From: Jeff Hostetler Convert test helper to use `start_bg_command()` when spawning a server daemon in the background rather than blocks of platform-specific code. Signed-off-by: Jeff Hostetler --- t/helper/test-simple-ipc.c | 193 ++++++++----------------------------- 1 file changed, 40 insertions(+), 153 deletions(-) diff --git a/t/helper/test-simple-ipc.c b/t/helper/test-simple-ipc.c index 91345180750..59a950f3b00 100644 --- a/t/helper/test-simple-ipc.c +++ b/t/helper/test-simple-ipc.c @@ -9,6 +9,7 @@ #include "parse-options.h" #include "thread-utils.h" #include "strvec.h" +#include "run-command.h" #ifndef SUPPORTS_SIMPLE_IPC int cmd__simple_ipc(int argc, const char **argv) @@ -274,178 +275,64 @@ static int daemon__run_server(void) return ret; } -#ifndef GIT_WINDOWS_NATIVE -/* - * This is adapted from `daemonize()`. Use `fork()` to directly create and - * run the daemon in a child process. - */ -static int spawn_server(pid_t *pid) -{ - struct ipc_server_opts opts = { - .nr_threads = cl_args.nr_threads, - }; +static start_bg_wait_cb bg_wait_cb; - *pid = fork(); - - switch (*pid) { - case 0: - if (setsid() == -1) - error_errno(_("setsid failed")); - close(0); - close(1); - close(2); - sanitize_stdfds(); +static int bg_wait_cb(void *cb_data, const struct child_process *cp) +{ + int s = ipc_get_active_state(cl_args.path); - return ipc_server_run(cl_args.path, &opts, test_app_cb, - (void*)&my_app_data); + switch (s) { + case IPC_STATE__LISTENING: + /* child is "ready" */ + return 0; - case -1: - return error_errno(_("could not spawn daemon in the background")); + case IPC_STATE__NOT_LISTENING: + case IPC_STATE__PATH_NOT_FOUND: + /* give child more time */ + return 1; default: - return 0; + case IPC_STATE__INVALID_PATH: + case IPC_STATE__OTHER_ERROR: + /* all the time in world won't help */ + return -1; } } -#else -/* - * Conceptually like `daemonize()` but different because Windows does not - * have `fork(2)`. Spawn a normal Windows child process but without the - * limitations of `start_command()` and `finish_command()`. - */ -static int spawn_server(pid_t *pid) -{ - char test_tool_exe[MAX_PATH]; - struct strvec args = STRVEC_INIT; - int in, out; - - GetModuleFileNameA(NULL, test_tool_exe, MAX_PATH); - - in = open("/dev/null", O_RDONLY); - out = open("/dev/null", O_WRONLY); - - strvec_push(&args, test_tool_exe); - strvec_push(&args, "simple-ipc"); - strvec_push(&args, "run-daemon"); - strvec_pushf(&args, "--name=%s", cl_args.path); - strvec_pushf(&args, "--threads=%d", cl_args.nr_threads); - - *pid = mingw_spawnvpe(args.v[0], args.v, NULL, NULL, in, out, out); - close(in); - close(out); - - strvec_clear(&args); - if (*pid < 0) - return error(_("could not spawn daemon in the background")); - - return 0; -} -#endif - -/* - * This is adapted from `wait_or_whine()`. Watch the child process and - * let it get started and begin listening for requests on the socket - * before reporting our success. - */ -static int wait_for_server_startup(pid_t pid_child) +static int daemon__start_server(void) { - int status; - pid_t pid_seen; - enum ipc_active_state s; - time_t time_limit, now; + struct child_process cp = CHILD_PROCESS_INIT; + enum start_bg_result sbgr; - time(&time_limit); - time_limit += cl_args.max_wait_sec; + strvec_push(&cp.args, "test-tool"); + strvec_push(&cp.args, "simple-ipc"); + strvec_push(&cp.args, "run-daemon"); + strvec_pushf(&cp.args, "--name=%s", cl_args.path); + strvec_pushf(&cp.args, "--threads=%d", cl_args.nr_threads); - for (;;) { - pid_seen = waitpid(pid_child, &status, WNOHANG); + cp.no_stdin = 1; + cp.no_stdout = 1; + cp.no_stderr = 1; - if (pid_seen == -1) - return error_errno(_("waitpid failed")); + sbgr = start_bg_command(&cp, bg_wait_cb, NULL, cl_args.max_wait_sec); - else if (pid_seen == 0) { - /* - * The child is still running (this should be - * the normal case). Try to connect to it on - * the socket and see if it is ready for - * business. - * - * If there is another daemon already running, - * our child will fail to start (possibly - * after a timeout on the lock), but we don't - * care (who responds) if the socket is live. - */ - s = ipc_get_active_state(cl_args.path); - if (s == IPC_STATE__LISTENING) - return 0; - - time(&now); - if (now > time_limit) - return error(_("daemon not online yet")); - - continue; - } + switch (sbgr) { + case SBGR_READY: + return 0; - else if (pid_seen == pid_child) { - /* - * The new child daemon process shutdown while - * it was starting up, so it is not listening - * on the socket. - * - * Try to ping the socket in the odd chance - * that another daemon started (or was already - * running) while our child was starting. - * - * Again, we don't care who services the socket. - */ - s = ipc_get_active_state(cl_args.path); - if (s == IPC_STATE__LISTENING) - return 0; + default: + case SBGR_ERROR: + case SBGR_CB_ERROR: + return error("daemon failed to start"); - /* - * We don't care about the WEXITSTATUS() nor - * any of the WIF*(status) values because - * `cmd__simple_ipc()` does the `!!result` - * trick on all function return values. - * - * So it is sufficient to just report the - * early shutdown as an error. - */ - return error(_("daemon failed to start")); - } + case SBGR_TIMEOUT: + return error("daemon not online yet"); - else - return error(_("waitpid is confused")); + case SBGR_DIED: + return error("daemon terminated"); } } -/* - * This process will start a simple-ipc server in a background process and - * wait for it to become ready. This is like `daemonize()` but gives us - * more control and better error reporting (and makes it easier to write - * unit tests). - */ -static int daemon__start_server(void) -{ - pid_t pid_child; - int ret; - - /* - * Run the actual daemon in a background process. - */ - ret = spawn_server(&pid_child); - if (pid_child <= 0) - return ret; - - /* - * Let the parent wait for the child process to get started - * and begin listening for requests on the socket. - */ - ret = wait_for_server_startup(pid_child); - - return ret; -} - /* * This process will run a quick probe to see if a simple-ipc server * is active on this path.