From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from imap.gmail.com [173.194.76.109] by localhost with POP3 (fetchmail-6.3.26) for (single-drop); Wed, 22 May 2024 22:59:36 +0200 (CEST) Received: by 2002:a05:6a11:2489:b0:55f:c3c0:ed08 with SMTP id sg9csp910639pxb; Wed, 22 May 2024 13:59:26 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVGwIJqYL6AbvOocfdRvZOw1d2+GD38Ku1rz90HHB7diWnFY8QoTw2CbNGlj0NnjNuAXbfKlOXvh9ifaPLfX8Pdw829VDfVFds= X-Google-Smtp-Source: AGHT+IG0kuK/KTAkajTwkZiZfTZHObYwglEaPt7I21lyge2eeQe20ZARHFPa1kPUAE9pv1uSyQ6I X-Received: by 2002:a05:6214:598d:b0:6ab:631d:3267 with SMTP id 6a1803df08f44-6ab7f35fb2cmr32313966d6.20.1716411565937; Wed, 22 May 2024 13:59:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1716411565; cv=none; d=google.com; s=arc-20160816; b=WZfmgD8IQKaJ59uDC2IKR31DdjNtHDu9q4VuCVWCP7XVATpR6KQaaF9nZ2HJWhd+Xp WM0fO2W4feXbkjtfT6w5WBqNek6fZeOk/a4LxZkfNi93rDcQ9IgsJh90kGAjUI1tmj7i TEv1d3yym8/yJCf7izrxo3PbyuVds7ZvWhecuIPTb1xlDTxz/UNtOXGBsmXZcAGfBUQw Xq9sM4puFhBysG0cPmQ/Ms0YfhM+qExcFB/138zxHX9IgyWISLMtpZddaMdRMeCnvBym Ap0CzHBYHW1ZZOblJXCNzX7UHQenFqmOyB/xPLkmM5zDeWPNZqeLMSKLU2mvQx3U5cSK B7Gw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:list-unsubscribe:list-subscribe:list-post :list-owner:list-help:list-archive:list-archive:archived-at :archived-at:list-id:precedence:cc:message-id-hash:mime-version :references:in-reply-to:message-id:date:subject:to:from:delivered-to; bh=Un6gsX8OtubznOIhgFWWuJRc9xah9IIECW1stXZcHTs=; fh=Sda5+ix8pPMDNzAK4mfqRvxwOV3J+P0OHYKuDzXzUCA=; b=ryiuktoCHlMyD/nYzAx4l0wTGsPo88bvBWc5mybJLJckaIFp+R5HtN1F8bZvsXf2gM h5fx+PICPg15xq2H0UcJWVXLQQPX4c25rf5do4cTE6PhMTLPOyLwXYGSVnDw0L/iIM2z Dzx6z4oGdxEozIb5USoQrFIvGwz1fFuSe2MT0nnhMQkD8aoSiLJcXpXv0g/gJ+ExLgKg X424zRcsz7eoPoZsXZG6AxmMnnx2kQ8R/pJSZym4VTSPZcus+Ewy/LkPlH7+o9/EPNEG PVqGoUxMg+8Kpb6s8bwBvXxkkvCDwznamHKrmu5d4sXoQxiGrnGGCNeDh2XNiFvRvGPw Ukuw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of passt-dev-bounces@passt.top designates 88.198.0.164 as permitted sender) smtp.mailfrom=passt-dev-bounces@passt.top Return-Path: Received: from us-smtp-inbound-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com. [205.139.110.120]) by mx.google.com with ESMTPS id 6a1803df08f44-6ab873f72d8si19616316d6.61.2024.05.22.13.59.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 May 2024 13:59:25 -0700 (PDT) Received-SPF: pass (google.com: domain of passt-dev-bounces@passt.top designates 88.198.0.164 as permitted sender) client-ip=88.198.0.164; Authentication-Results: mx.google.com; spf=pass (google.com: domain of passt-dev-bounces@passt.top designates 88.198.0.164 as permitted sender) smtp.mailfrom=passt-dev-bounces@passt.top Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-82-IdvVTi2YOSC6g2xK4hhwLg-1; Wed, 22 May 2024 16:59:23 -0400 X-MC-Unique: IdvVTi2YOSC6g2xK4hhwLg-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0A4401955F29 for ; Wed, 22 May 2024 20:59:23 +0000 (UTC) Received: by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) id EFD3A197F039; Wed, 22 May 2024 20:59:22 +0000 (UTC) Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.49]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id ED417197F037 for ; Wed, 22 May 2024 20:59:22 +0000 (UTC) Received: from us-smtp-inbound-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A837E1955E8E for ; Wed, 22 May 2024 20:59:22 +0000 (UTC) Received: from passt.top (passt.top [88.198.0.164]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-472-l3SVZ4YMMga0OJiNrDvIrg-1; Wed, 22 May 2024 16:59:20 -0400 X-MC-Unique: l3SVZ4YMMga0OJiNrDvIrg-1 Received: from [127.0.0.1] (localhost.localdomain [127.0.0.1]) by passt.top (Postfix) with ESMTP id 010015A0319; Wed, 22 May 2024 22:59:15 +0200 (CEST) Received: by passt.top (Postfix, from userid 1000) id C06FE5A030B; Wed, 22 May 2024 22:59:11 +0200 (CEST) From: Stefano Brivio To: passt-dev@passt.top Subject: [PATCH 6/8] passt, util: Move opening of PID file to its own function Date: Wed, 22 May 2024 22:59:09 +0200 Message-ID: <20240522205911.261325-7-sbrivio@redhat.com> In-Reply-To: <20240522205911.261325-1-sbrivio@redhat.com> References: <20240522205911.261325-1-sbrivio@redhat.com> MIME-Version: 1.0 Message-ID-Hash: HFYC44Z2HED6HF3TH3YQLQO34XUQ75KR X-Message-ID-Hash: HFYC44Z2HED6HF3TH3YQLQO34XUQ75KR X-MailFrom: sbrivio@passt.top 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: David Gibson , "'Richard W . M . Jones'" , Minxi Hou 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: X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition;Similar Internal Domain=false;Similar Monitored External Domain=false;Custom External Domain=false;Mimecast External Domain=false;Newly Observed Domain=false;Internal User Name=false;Custom Display Name List=false;Reply-to Address Mismatch=false;Targeted Threat Dictionary=false;Mimecast Threat Dictionary=false;Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="US-ASCII"; x-default=true We won't call it from main() any longer: move it. Signed-off-by: Stefano Brivio --- passt.c | 11 ++--------- util.c | 22 ++++++++++++++++++++++ util.h | 1 + 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/passt.c b/passt.c index fb9773d..e2446fc 100644 --- a/passt.c +++ b/passt.c @@ -199,7 +199,7 @@ void exit_handler(int signal) */ int main(int argc, char **argv) { -=09int nfds, i, devnull_fd =3D -1, pidfile_fd =3D -1; +=09int nfds, i, devnull_fd =3D -1, pidfile_fd; =09struct epoll_event events[EPOLL_EVENTS]; =09char *log_name, argv0[PATH_MAX], *name; =09struct ctx c =3D { 0 }; @@ -299,14 +299,7 @@ int main(int argc, char **argv) =09=09} =09} =20 -=09if (*c.pid_file) { -=09=09if ((pidfile_fd =3D open(c.pid_file, -=09=09=09=09 O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, -=09=09=09=09 S_IRUSR | S_IWUSR)) < 0) { -=09=09=09perror("PID file open"); -=09=09=09exit(EXIT_FAILURE); -=09=09} -=09} +=09pidfile_fd =3D pidfile_open(c.pid_file); =20 =09if (isolate_prefork(&c)) =09=09die("Failed to sandbox process, exiting"); diff --git a/util.c b/util.c index 18c04ba..cf5a62b 100644 --- a/util.c +++ b/util.c @@ -402,6 +402,28 @@ void pidfile_write(int fd, pid_t pid) =09close(fd); } =20 +/** + * pidfile_open() - Open PID file if needed + * @path:=09Path for PID file, empty string if no PID file is requested + * + * Return: descriptor for PID file, -1 if path is NULL, won't return on fa= ilure + */ +int pidfile_open(const char *path) +{ +=09int fd; + +=09if (!*path) +=09=09return -1; + +=09if ((fd =3D open(path, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, +=09=09=09 S_IRUSR | S_IWUSR)) < 0) { +=09=09perror("PID file open"); +=09=09exit(EXIT_FAILURE); +=09} + +=09return fd; +} + /** * __daemon() - daemon()-like function writing PID file before parent exit= s * @pidfile_fd:=09Open PID file descriptor diff --git a/util.h b/util.h index f811975..8a430ca 100644 --- a/util.h +++ b/util.h @@ -156,6 +156,7 @@ char *line_read(char *buf, size_t len, int fd); void ns_enter(const struct ctx *c); bool ns_is_init(void); int open_in_ns(const struct ctx *c, const char *path, int flags); +int pidfile_open(const char *path); void pidfile_write(int fd, pid_t pid); int __daemon(int pidfile_fd, int devnull_fd); int fls(unsigned long x); --=20 2.43.0