From 8f5bf736a9d3f8084c644dbf6ceefc4142715c4e Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Tue, 13 Mar 2012 00:45:13 +0100 Subject: Experimental code to limit imagemagick convert runtime (see #82) Problems so far: * leaks zombie processes * does not work when terminating feh with a signal (since the convert process is no longer in feh's process group) --- src/imlib.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'src/imlib.c') diff --git a/src/imlib.c b/src/imlib.c index c0252c3..1e4eb3f 100644 --- a/src/imlib.c +++ b/src/imlib.c @@ -59,6 +59,8 @@ int xinerama_screen; int num_xinerama_screens; #endif /* HAVE_LIBXINERAMA */ +int childpid; + static char *feh_http_load_image(char *url); static char *feh_magick_load_image(char *filename); @@ -253,7 +255,10 @@ static char *feh_magick_load_image(char *filename) char *tmpname; char *sfn; int fd = -1, devnull = -1; - int pid, status; + int status; + + if (opt.magick_timeout < 0) + return NULL; basename = strrchr(filename, '/'); @@ -277,26 +282,39 @@ static char *feh_magick_load_image(char *filename) snprintf(argv_fd, sizeof(argv_fd), "png:fd:%d", fd); - - if ((pid = fork()) == 0) { + if ((childpid = fork()) == 0) { /* discard convert output */ devnull = open("/dev/null", O_WRONLY); dup2(devnull, 1); dup2(devnull, 2); + /* + * convert only accepts SIGINT via killpg, a normal kill doesn't work + */ + if (opt.magick_timeout) + setpgid(0, 0); + execlp("convert", "convert", filename, argv_fd, NULL); exit(1); } else { - waitpid(pid, &status, 0); + alarm(opt.magick_timeout); + waitpid(childpid, &status, 0); + alarm(0); if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) { close(fd); unlink(sfn); sfn = NULL; - if (!opt.quiet) - weprintf("%s - No loader for that file format", filename); + if (!opt.quiet) { + if (WIFSIGNALED(status)) + weprintf("%s - Conversion took too long, skipping", + filename); + else + weprintf("%s - No loader for that file format", + filename); + } } } -- cgit v1.2.3