summaryrefslogtreecommitdiffstats
path: root/cat
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2012-03-14 19:30:46 +0000
committerRichard W.M. Jones <rjones@redhat.com>2012-03-14 19:30:46 +0000
commit606732d02e678161ff433040a21d54fc2ea8bb43 (patch)
tree7549558e51d1dd45a45e71ce219084e368eb845d /cat
parent13e7a1b400b7e2a5e9335d25205b09e74c89d858 (diff)
downloadlibguestfs-606732d02e678161ff433040a21d54fc2ea8bb43.tar.gz
libguestfs-606732d02e678161ff433040a21d54fc2ea8bb43.tar.xz
libguestfs-606732d02e678161ff433040a21d54fc2ea8bb43.zip
Use O_CLOEXEC / SOCK_CLOEXEC for almost all file descriptors.
The presumption is that all file descriptors should be created with the close-on-exec flag set. The only exception are file descriptors that we want passed through to exec'd subprocesses (mainly pipes and stdin/stdout/stderr). For open calls, we pass O_CLOEXEC as an extra flag, eg: fd = open ("foo", O_RDONLY|O_CLOEXEC); This is a Linux-ism, but using a macro we can easily make it portable. For sockets, similarly: sock = socket (..., SOCK_STREAM|SOCK_CLOEXEC, ...); For accepted sockets, we use the Linux accept4 system call which allows flags to be supplied, but we use the Gnulib 'accept4' module to make this portable. For dup, dup2, we use the Linux dup3 system call, and the Gnulib modules 'dup3' and 'cloexec'.
Diffstat (limited to 'cat')
-rw-r--r--cat/virt-ls.c6
1 files changed, 5 insertions, 1 deletions
diff --git a/cat/virt-ls.c b/cat/virt-ls.c
index b79afa31..3d3699ee 100644
--- a/cat/virt-ls.c
+++ b/cat/virt-ls.c
@@ -36,6 +36,10 @@
#include "guestfs.h"
#include "options.h"
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
/* Currently open libguestfs handle. */
guestfs_h *g;
@@ -468,7 +472,7 @@ do_ls_R (const char *dir)
/* The output of find0 is a \0-separated file. Turn each \0 into
* a \n character.
*/
- fd = open (tmpfile, O_RDONLY);
+ fd = open (tmpfile, O_RDONLY|O_CLOEXEC);
if (fd == -1) {
perror (tmpfile);
exit (EXIT_FAILURE);