From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTP id 5C0E15A026D for ; Wed, 5 Apr 2023 13:58:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680695884; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=E/8lSLZkZc6I26R1bWLRLQbDW4PLPX4luYK/y+ENLeQ=; b=Sm/fKBnMgl4h7vSGnrImR2R9mKAijrvgqlpLTYbXkGPXTuhcKPobNTT+KegX6cH8p/kmA2 HjykUOnLeBbIZrjIXexTd3uxdnhOSG2+uSLHHKKffeGrt55YFMye5IqAqobZMidzatnySn czLhWI2OfuD5An2urHxLlqKG8Quj4Ug= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-511-dcZBysqdMUCijqwnQb42Rw-1; Wed, 05 Apr 2023 07:58:03 -0400 X-MC-Unique: dcZBysqdMUCijqwnQb42Rw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B0152382C968; Wed, 5 Apr 2023 11:58:02 +0000 (UTC) Received: from elisabeth (unknown [10.39.208.33]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1A3A21121314; Wed, 5 Apr 2023 11:58:01 +0000 (UTC) Date: Wed, 5 Apr 2023 13:58:00 +0200 From: Stefano Brivio To: David Gibson Subject: Re: [PATCH 04/14] nstool: Split some command line parsing and socket setup to subcommands Message-ID: <20230405135800.1c3705ca@elisabeth> In-Reply-To: <20230404014638.3225556-5-david@gibson.dropbear.id.au> References: <20230404014638.3225556-1-david@gibson.dropbear.id.au> <20230404014638.3225556-5-david@gibson.dropbear.id.au> Organization: Red Hat MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: VNKWPHE335GG7S545YZLLPET7U2CKWGP X-Message-ID-Hash: VNKWPHE335GG7S545YZLLPET7U2CKWGP X-MailFrom: sbrivio@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: On Tue, 4 Apr 2023 11:46:28 +1000 David Gibson wrote: > This will make it easier to differentiate the options to those commands > further in future. > > Signed-off-by: David Gibson > --- > test/nstool.c | 102 +++++++++++++++++++++++++++++++++----------------- > 1 file changed, 68 insertions(+), 34 deletions(-) > > diff --git a/test/nstool.c b/test/nstool.c > index 7e069b6..9ea7eeb 100644 > --- a/test/nstool.c > +++ b/test/nstool.c > @@ -11,6 +11,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -37,19 +38,55 @@ static void usage(void) > " terminate.\n"); > } > > -static void hold(int fd, const struct sockaddr_un *addr) > +static int connect_ctl(const char * sockpath, bool wait) > { > + int fd = socket(AF_UNIX, SOCK_STREAM, PF_UNIX); > + struct sockaddr_un addr = { > + .sun_family = AF_UNIX, > + }; > int rc; > > - rc = bind(fd, (struct sockaddr *)addr, sizeof(*addr)); > + if (fd < 0) > + die("socket(): %s\n", strerror(errno)); Unrelated: it would be nice if die() added newlines eventually. > + > + strncpy(addr.sun_path, sockpath, UNIX_PATH_MAX); > + > + do { > + rc = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); > + if (rc < 0 && > + (!wait || (errno != ENOENT && errno != ECONNREFUSED))) > + die("connect() to %s: %s\n", sockpath, strerror(errno)); A (1ms?) delay would be nice to have here -- it's almost a busyloop, connect() fails fast. > + } while (rc < 0); > + > + return fd; > +} > + > +static void cmd_hold(int argc, char *argv[]) > +{ > + int fd = socket(AF_UNIX, SOCK_STREAM, PF_UNIX); > + struct sockaddr_un addr = { > + .sun_family = AF_UNIX, > + }; > + const char *sockpath = argv[1]; > + int rc; > + > + if (argc != 2) > + usage(); > + > + if (fd < 0) > + die("socket(): %s\n", strerror(errno)); > + > + strncpy(addr.sun_path, sockpath, UNIX_PATH_MAX); > + > + rc = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); > if (rc < 0) > - die("bind(): %s\n", strerror(errno)); > + die("bind() to %s: %s\n", sockpath, strerror(errno)); > > rc = listen(fd, 0); > if (rc < 0) > - die("listen(): %s\n", strerror(errno)); > + die("listen() on %s: %s\n", sockpath, strerror(errno)); > > - printf("nstool: local PID=%d local UID=%u local GID=%u\n", > + printf("nstool hold: local PID=%d local UID=%u local GID=%u\n", > getpid(), getuid(), getgid()); > do { > int afd = accept(fd, NULL, NULL); > @@ -63,71 +100,68 @@ static void hold(int fd, const struct sockaddr_un *addr) > die("read(): %s\n", strerror(errno)); > } while (rc == 0); > > - unlink(addr->sun_path); > + unlink(sockpath); > } > > -static void pid(int fd, const struct sockaddr_un *addr) > +static void cmd_pid(int argc, char *argv[]) > { > - int rc; > + const char *sockpath = argv[1]; > struct ucred peercred; > socklen_t optlen = sizeof(peercred); > + int fd, rc; > > - do { > - rc = connect(fd, (struct sockaddr *)addr, sizeof(*addr)); > - if (rc < 0 && errno != ENOENT && errno != ECONNREFUSED) > - die("connect(): %s\n", strerror(errno)); > - } while (rc < 0); > + if (argc != 2) > + usage(); > + > + fd = connect_ctl(sockpath, true); I didn't spot this earlier, but... does it really make sense to wait in cmd_pid(), also on ENOENT, rather than making 'hold' return only once the socket is ready? I don't think it would be outrageous to have 'nstool pid' failing if the holding process doesn't exist. Admittely, I'm biased by the few hundreds of times I needed to 'killall -9 nsholder' in the past months. :) > > rc = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, > &peercred, &optlen); > if (rc < 0) > - die("getsockopet(SO_PEERCRED): %s\n", strerror(errno)); > + die("getsockopet(SO_PEERCRED) %s: %s\n", > + sockpath, strerror(errno)); > > close(fd); > > printf("%d\n", peercred.pid); > } > > -static void stop(int fd, const struct sockaddr_un *addr) > +static void cmd_stop(int argc, char *argv[]) > { > - int rc; > + const char *sockpath = argv[1]; > + int fd, rc; > char buf = 'Q'; > > - rc = connect(fd, (struct sockaddr *)addr, sizeof(*addr)); > - if (rc < 0) > - die("connect(): %s\n", strerror(errno)); > + if (argc != 2) > + usage(); > + > + fd = connect_ctl(sockpath, false); > > rc = write(fd, &buf, sizeof(buf)); Unrelated: a compound literal would make this more readable. -- Stefano