From f4ec61340fbbcc2640a1014a7d5cdb2065c137b0 Mon Sep 17 00:00:00 2001 From: o9000 Date: Sun, 16 Jul 2017 10:14:40 +0200 Subject: [PATCH] Fix fd leak to children --- src/execplugin/execplugin.c | 1 + src/util/common.c | 12 +++++++++++- src/util/common.h | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/execplugin/execplugin.c b/src/execplugin/execplugin.c index 34c942b..7b79406 100644 --- a/src/execplugin/execplugin.c +++ b/src/execplugin/execplugin.c @@ -644,6 +644,7 @@ void execp_timer_callback(void *arg) close(pipe_fd_stderr[0]); dup2(pipe_fd_stderr[1], 2); // 2 is stderr close(pipe_fd_stderr[1]); + close_all_fds(); setpgid(0, 0); execl("/bin/sh", "/bin/sh", "-c", execp->backend->command, NULL); // This should never happen! diff --git a/src/util/common.c b/src/util/common.c index 66cf4a6..76b95bf 100644 --- a/src/util/common.c +++ b/src/util/common.c @@ -230,6 +230,7 @@ pid_t tint_exec(const char *command, const char *dir, const char *tooltip, Time // Run the command if (dir) chdir(dir); + close_all_fds(); execl("/bin/sh", "/bin/sh", "-c", command, NULL); fprintf(stderr, "Failed to execlp %s\n", command); #if HAVE_SN @@ -754,7 +755,8 @@ void get_text_size2(const PangoFontDescription *font, available_height = MAX(0, available_height); Pixmap pmap = XCreatePixmap(server.display, server.root_win, available_height, available_width, server.depth); - cairo_surface_t *cs = cairo_xlib_surface_create(server.display, pmap, server.visual, available_height, available_width); + cairo_surface_t *cs = + cairo_xlib_surface_create(server.display, pmap, server.visual, available_height, available_width); cairo_t *c = cairo_create(cs); PangoLayout *layout = pango_cairo_create_layout(c); @@ -848,3 +850,11 @@ gint cmp_ptr(gconstpointer a, gconstpointer b) else return 1; } + +void close_all_fds() +{ + long maxfd = sysconf(_SC_OPEN_MAX); + for (int fd = 3; fd < maxfd; fd++) { + close(fd); + } +} diff --git a/src/util/common.h b/src/util/common.h index 4a8978f..6552553 100644 --- a/src/util/common.h +++ b/src/util/common.h @@ -116,6 +116,8 @@ void draw_rect_on_sides(cairo_t *c, double x, double y, double w, double h, doub // Clears the pixmap (with transparent color) void clear_pixmap(Pixmap p, int x, int y, int w, int h); +void close_all_fds(); + // Appends to the list locations all the directories contained in the environment variable var (split by ":"). // Optional suffixes are added to each directory. The suffix arguments MUST end with NULL. // Returns the new value of the list.