From: Michal Privoznik <mprivozn@redhat.com>
To: passt-dev@passt.top
Subject: [PATCH 1/2] util: Introduce set_cloexec()
Date: Tue, 6 Jun 2023 13:41:29 +0200 [thread overview]
Message-ID: <99c455f027b5d94f45e34abb8ce875814e4dc534.1686037337.git.mprivozn@redhat.com> (raw)
In-Reply-To: <cover.1686037337.git.mprivozn@redhat.com>
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 <mprivozn@redhat.com>
---
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 */
--
@@ -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
next prev parent reply other threads:[~2023-06-06 11:41 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-06 11:41 [PATCH 0/2] Introduce --log-fd option Michal Privoznik
2023-06-06 11:41 ` Michal Privoznik [this message]
2023-06-06 20:59 ` [PATCH 1/2] util: Introduce set_cloexec() Stefano Brivio
2023-06-06 11:41 ` [PATCH 2/2] conf, log: Introduce --log-fd option Michal Privoznik
2023-06-06 20:59 ` Stefano Brivio
2023-06-06 20:58 ` [PATCH 0/2] " Stefano Brivio
2023-06-07 11:38 ` Michal Prívozník
2023-06-07 14:42 ` Stefano Brivio
2023-06-08 11:51 ` Michal Prívozník
2023-06-13 3:12 ` Stefano Brivio
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=99c455f027b5d94f45e34abb8ce875814e4dc534.1686037337.git.mprivozn@redhat.com \
--to=mprivozn@redhat.com \
--cc=passt-dev@passt.top \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).