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 7CBB35A0262 for ; Tue, 6 Jun 2023 13:41:35 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686051694; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Ff2JpMmrviF4QpUI/10ITPiUNYAktdmUDKyftYNFGLg=; b=eSQT7zXR7dBcghqGooKwmaYW/mj43lj1w/jJgFjFb+9fjvIDccb3c3tsYVltEaNj0at42U SK+XNgjuMDDSgBQrED133sPHuH8X80xRhT1z6OBAqSJbuApHujuHC2OdWXQy0MWx5bFe+8 ykCR9VyvijzHOmWBAj4PujeVbGnMAvI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-613-OFohauoBNr61UU0mxgtVLg-1; Tue, 06 Jun 2023 07:41:33 -0400 X-MC-Unique: OFohauoBNr61UU0mxgtVLg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B7A72101A53B for ; Tue, 6 Jun 2023 11:41:32 +0000 (UTC) Received: from localhost.localdomain (unknown [10.43.2.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 622B3C1603B for ; Tue, 6 Jun 2023 11:41:32 +0000 (UTC) From: Michal Privoznik To: passt-dev@passt.top Subject: [PATCH 1/2] util: Introduce set_cloexec() Date: Tue, 6 Jun 2023 13:41:29 +0200 Message-Id: <99c455f027b5d94f45e34abb8ce875814e4dc534.1686037337.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: BALR2XKEV4GRHDQD2XHTR2YNAWP464BE X-Message-ID-Hash: BALR2XKEV4GRHDQD2XHTR2YNAWP464BE X-MailFrom: mprivozn@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 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: This function is going to be used on FDs inherited from parent. Parent can't set O_CLOEXEC obviously, so we have to set it ourselves. Signed-off-by: Michal Privoznik --- util.c | 17 +++++++++++++++++ util.h | 1 + 2 files changed, 18 insertions(+) diff --git a/util.c b/util.c index 1d00404..9817c74 100644 --- a/util.c +++ b/util.c @@ -526,6 +526,23 @@ int write_file(const char *path, const char *buf) return len == 0 ? 0 : -1; } +/** + * set_cloexec() - Set Close-on-exec flag on given FD + * @fd: FD to set the flag on + * + * Return: 0 on success, -1 on any error + */ +int set_cloexec(int fd) +{ + int fflags; + if ((fflags = fcntl(fd, F_GETFD)) < 0) + return -1; + fflags |= FD_CLOEXEC; + if ((fcntl(fd, F_SETFD, fflags)) < 0) + return -1; + return 0; +} + #ifdef __ia64__ /* Needed by do_clone() below: glibc doesn't export the prototype of __clone2(), * use the description from clone(2). diff --git a/util.h b/util.h index 26892aa..3db901d 100644 --- a/util.h +++ b/util.h @@ -222,5 +222,6 @@ void write_pidfile(int fd, pid_t pid); int __daemon(int pidfile_fd, int devnull_fd); int fls(unsigned long x); int write_file(const char *path, const char *buf); +int set_cloexec(int fd); #endif /* UTIL_H */ -- 2.39.3