aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2012-06-23 11:52:51 -0700
committerMike Frysinger <vapier@gentoo.org>2012-06-23 11:52:51 -0700
commita3ff1534945c3898332b2481c9fd355dfbd56e1f (patch)
tree6809e34021170105a1f5dee8ef5c8521bf735d80 /libsbutil
parentenviron: add set variants to env_is_{on,off} (diff)
downloadsandbox-a3ff1534945c3898332b2481c9fd355dfbd56e1f.tar.gz
sandbox-a3ff1534945c3898332b2481c9fd355dfbd56e1f.tar.bz2
sandbox-a3ff1534945c3898332b2481c9fd355dfbd56e1f.zip
libsandbox: clean up open file handles in parent tracing process
Currently, if a non-static app sets up a pipe (with cloexec enabled) and executes a static app, the handle to that pipe is left open in the parent process. This causes trouble when the parent is waiting for that to be closed immediately. Since none of the fds in the forked parent process matter to us, we can just go ahead and clean up all fds before we start tracing the child. URL: http://bugs.gentoo.org/364877 Reported-by: Victor Stinner <victor.stinner@haypocalc.com> Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Diffstat (limited to 'libsbutil')
-rw-r--r--libsbutil/sb_close.c26
-rw-r--r--libsbutil/sbutil.h1
2 files changed, 26 insertions, 1 deletions
diff --git a/libsbutil/sb_close.c b/libsbutil/sb_close.c
index 17a4560..5379197 100644
--- a/libsbutil/sb_close.c
+++ b/libsbutil/sb_close.c
@@ -3,7 +3,7 @@
*
* IO functions.
*
- * Copyright 1999-2008 Gentoo Foundation
+ * Copyright 1999-2012 Gentoo Foundation
* Licensed under the GPL-2
*/
@@ -29,3 +29,27 @@ int sb_close(int fd)
return res;
}
+
+/* Quickly close all the open fds (good for daemonization) */
+void sb_close_all_fds(void)
+{
+ DIR *dirp;
+ struct dirent *de;
+ int dfd, fd;
+ const char *fd_dir = sb_get_fd_dir();
+
+ dirp = opendir(fd_dir);
+ if (!dirp)
+ sb_ebort("could not process %s\n", fd_dir);
+ dfd = dirfd(dirp);
+
+ while ((de = readdir(dirp)) != NULL) {
+ if (de->d_name[0] == '.')
+ continue;
+ fd = atoi(de->d_name);
+ if (fd != dfd)
+ close(fd);
+ }
+
+ closedir(dirp);
+}
diff --git a/libsbutil/sbutil.h b/libsbutil/sbutil.h
index 02b88cb..479734b 100644
--- a/libsbutil/sbutil.h
+++ b/libsbutil/sbutil.h
@@ -97,6 +97,7 @@ int sb_open(const char *path, int flags, mode_t mode);
size_t sb_read(int fd, void *buf, size_t count);
size_t sb_write(int fd, const void *buf, size_t count);
int sb_close(int fd);
+void sb_close_all_fds(void);
int sb_copy_file_to_fd(const char *file, int ofd);
/* Reliable output */