From: Pierre Habouzit Date: Sun, 12 Nov 2006 14:01:31 +0000 (+0100) Subject: exit system.c, mutt_system goes into lib-sys/ X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=commitdiff_plain;h=97677c08933e16e25ecb3c12473ef1efdab7962b exit system.c, mutt_system goes into lib-sys/ that function is convoluted with imap things (eeek) Signed-off-by: Pierre Habouzit --- diff --git a/Makefile.am b/Makefile.am index ed8d667..a31ca6b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -25,7 +25,7 @@ madmutt_SOURCES = $(BUILT_SOURCES) \ main.c mbox.c mbyte.c mh.c muttlib.c mutt_idna.c mx.c \ pager.c pattern.c postpone.c query.c \ recvattach.c recvcmd.c rfc1524.c rfc3676.c \ - score.c send.c sendlib.c sidebar.c sort.c state.c status.c system.c \ + score.c send.c sendlib.c sidebar.c sort.c state.c status.c \ thread.c url.c utf8.c wcwidth.c account.c madmutt_LDADD = @MUTT_LIB_OBJECTS@ @LIBOBJS@ \ diff --git a/attach.c b/attach.c index 67f2aa4..a36fdf4 100644 --- a/attach.c +++ b/attach.c @@ -28,6 +28,8 @@ #include #include +#include + #include #include diff --git a/commands.c b/commands.c index 6a0dc10..622650a 100644 --- a/commands.c +++ b/commands.c @@ -30,6 +30,7 @@ #include #include +#include #include #include diff --git a/compose.c b/compose.c index 78a55a6..a37955e 100644 --- a/compose.c +++ b/compose.c @@ -15,12 +15,21 @@ # include "config.h" #endif +#include +#include +#include +#include +#include +#include + #include #include #include #include #include +#include + #include #include @@ -46,14 +55,6 @@ #include "nntp.h" #endif - -#include -#include -#include -#include -#include -#include - static const char *There_are_no_attachments = N_("There are no attachments."); #define CHECK_COUNT if (idxlen == 0) { mutt_error _(There_are_no_attachments); break; } diff --git a/compress.c b/compress.c index 456719a..aa453bb 100644 --- a/compress.c +++ b/compress.c @@ -19,6 +19,7 @@ #include #include +#include #include diff --git a/lib-crypt/pgpinvoke.c b/lib-crypt/pgpinvoke.c index f5d45e9..cdf4e58 100644 --- a/lib-crypt/pgpinvoke.c +++ b/lib-crypt/pgpinvoke.c @@ -31,6 +31,8 @@ #include #include +#include + #include #include diff --git a/lib-sys/mutt_socket.c b/lib-sys/mutt_socket.c index e3cd00d..82d9c4d 100644 --- a/lib-sys/mutt_socket.c +++ b/lib-sys/mutt_socket.c @@ -31,6 +31,7 @@ #include "mutt.h" #include "globals.h" +#include "unix.h" #include "mutt_socket.h" #include "mutt_tunnel.h" #include "mutt_signal.h" diff --git a/lib-sys/unix.c b/lib-sys/unix.c index 372ff06..f2141c0 100644 --- a/lib-sys/unix.c +++ b/lib-sys/unix.c @@ -8,13 +8,20 @@ * please see the file GPL in the top level source directory. */ +#include #include +#include +#include #include #include #include #include "unix.h" +#include "mutt_signal.h" + +#include /* for imap_wait_keepalive EEEEK */ + /* Extract the real name from /etc/passwd's GECOS field. * When set, honor the regular expression in rx, @@ -61,3 +68,91 @@ ssize_t mutt_gecos_name(char *dst, ssize_t n, struct passwd *pw, rx_t *rx) return len; } + +int _mutt_system(const char *cmd, int flags) +{ + int rc = -1; + struct sigaction act; + struct sigaction oldtstp; + struct sigaction oldcont; + sigset_t set; + pid_t pid; + + if (m_strisempty(cmd)) + return 0; + + /* must ignore SIGINT and SIGQUIT */ + mutt_block_signals_system(); + + /* also don't want to be stopped right now */ + if (flags & M_DETACH_PROCESS) { + sigemptyset(&set); + sigaddset(&set, SIGTSTP); + sigprocmask(SIG_BLOCK, &set, NULL); + } else { + act.sa_handler = SIG_DFL; + /* we want to restart the waitpid() below */ +#ifdef SA_RESTART + act.sa_flags = SA_RESTART; +#endif + sigemptyset(&act.sa_mask); + sigaction(SIGTSTP, &act, &oldtstp); + sigaction(SIGCONT, &act, &oldcont); + } + + pid = fork (); + if (pid == 0) { + act.sa_flags = 0; + + if (flags & M_DETACH_PROCESS) { + /* give up controlling terminal */ + setsid(); + + switch (fork()) { + int fd, nb_fds; + + case 0: + nb_fds = getdtablesize(); + for (fd = 0; fd < nb_fds; fd++) { + close(fd); + } + chdir ("/"); + act.sa_handler = SIG_DFL; + sigaction(SIGCHLD, &act, NULL); + break; + + case -1: + _exit(127); + + default: + _exit(0); + } + } + + /* reset signals for the child; not really needed, but... */ + mutt_unblock_signals_system(0); + act.sa_handler = SIG_DFL; + act.sa_flags = 0; + sigemptyset(&act.sa_mask); + sigaction(SIGTERM, &act, NULL); + sigaction(SIGTSTP, &act, NULL); + sigaction(SIGCONT, &act, NULL); + + execl("/bin/sh", "sh", "-c", cmd, NULL); + _exit(127); /* execl error */ + } else + if (pid != -1) { + rc = imap_wait_keepalive(pid); + } + + sigaction(SIGCONT, &oldcont, NULL); + sigaction(SIGTSTP, &oldtstp, NULL); + + /* reset SIGINT, SIGQUIT and SIGCHLD */ + mutt_unblock_signals_system(1); + if (flags & M_DETACH_PROCESS) { + sigprocmask (SIG_UNBLOCK, &set, NULL); + } + + return (pid > 0 && WIFEXITED(rc)) ? WEXITSTATUS(rc) : -1; +} diff --git a/lib-sys/unix.h b/lib-sys/unix.h index b43077a..0911521 100644 --- a/lib-sys/unix.h +++ b/lib-sys/unix.h @@ -27,4 +27,12 @@ ssize_t mutt_gecos_name(char *dst, ssize_t n, struct passwd *pw, rx_t *rx); + +/* flags for _mutt_system() */ +#define M_DETACH_PROCESS 1 /* detach subprocess from group */ + +#define mutt_system(x) _mutt_system(x,0) +int _mutt_system (const char *, int); + + #endif /* MUTT_LIB_SYS_UNIX_H */ diff --git a/lib-ui/curs_lib.c b/lib-ui/curs_lib.c index 56e796b..ee53578 100644 --- a/lib-ui/curs_lib.c +++ b/lib-ui/curs_lib.c @@ -35,6 +35,7 @@ #include #include +#include #include #include "curses.h" diff --git a/mutt.h b/mutt.h index 490989d..b1270ea 100644 --- a/mutt.h +++ b/mutt.h @@ -73,9 +73,6 @@ typedef struct { int op; /* function op */ } event_t; -/* flags for _mutt_system() */ -#define M_DETACH_PROCESS 1 /* detach subprocess from group */ - /* flags for mutt_FormatString() */ typedef enum { M_FORMAT_FORCESUBJ = (1 << 0), /* print the subject even if unchanged */ diff --git a/mx.c b/mx.c index 5f18ac1..4aef72f 100644 --- a/mx.c +++ b/mx.c @@ -30,6 +30,8 @@ #include #include +#include + #include #include "mutt.h" diff --git a/protos.h b/protos.h index ee8527d..c253db0 100644 --- a/protos.h +++ b/protos.h @@ -28,9 +28,6 @@ void _mutt_make_string (char *, size_t, const char *, CONTEXT *, HEADER *, format_flag); -#define mutt_system(x) _mutt_system(x,0) -int _mutt_system (const char *, int); - #define mutt_new_enter_state() p_new(ENTER_STATE, 1) typedef const char *format_t (char *, size_t, char, const char *, diff --git a/remailer.c b/remailer.c index 08b7a44..971f197 100644 --- a/remailer.c +++ b/remailer.c @@ -30,6 +30,8 @@ #include #include +#include + #include #include diff --git a/rfc1524.c b/rfc1524.c index cd089fc..6c6d317 100644 --- a/rfc1524.c +++ b/rfc1524.c @@ -37,6 +37,8 @@ #include #include +#include + #include "mutt.h" #include "rfc1524.h" #include "attach.h" diff --git a/system.c b/system.c deleted file mode 100644 index 384b7e7..0000000 --- a/system.c +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright notice from original mutt: - * Copyright (C) 1996-2000 Michael R. Elkins - * - * This file is part of mutt-ng, see http://www.muttng.org/. - * It's licensed under the GNU General Public License, - * please see the file GPL in the top level source directory. - */ - -#if HAVE_CONFIG_H -# include "config.h" -#endif - -#include -#include -#include -#include -#include -#include - -#include - -#include "mutt.h" -#include - -int _mutt_system (const char *cmd, int flags) -{ - int rc = -1; - struct sigaction act; - struct sigaction oldtstp; - struct sigaction oldcont; - sigset_t set; - pid_t thepid; - - if (!cmd || !*cmd) - return (0); - - /* must ignore SIGINT and SIGQUIT */ - - mutt_block_signals_system (); - - /* also don't want to be stopped right now */ - if (flags & M_DETACH_PROCESS) { - sigemptyset (&set); - sigaddset (&set, SIGTSTP); - sigprocmask (SIG_BLOCK, &set, NULL); - } - else { - act.sa_handler = SIG_DFL; - /* we want to restart the waitpid() below */ -#ifdef SA_RESTART - act.sa_flags = SA_RESTART; -#endif - sigemptyset (&act.sa_mask); - sigaction (SIGTSTP, &act, &oldtstp); - sigaction (SIGCONT, &act, &oldcont); - } - - if ((thepid = fork ()) == 0) { - act.sa_flags = 0; - - if (flags & M_DETACH_PROCESS) { - int fd; - - /* give up controlling terminal */ - setsid (); - - switch (fork ()) { - case 0: -#if defined(OPEN_MAX) - for (fd = 0; fd < OPEN_MAX; fd++) - close (fd); -#elif defined(_POSIX_OPEN_MAX) - for (fd = 0; fd < _POSIX_OPEN_MAX; fd++) - close (fd); -#else - close (0); - close (1); - close (2); -#endif - chdir ("/"); - act.sa_handler = SIG_DFL; - sigaction (SIGCHLD, &act, NULL); - break; - - case -1: - _exit (127); - - default: - _exit (0); - } - } - - /* reset signals for the child; not really needed, but... */ - mutt_unblock_signals_system (0); - act.sa_handler = SIG_DFL; - act.sa_flags = 0; - sigemptyset (&act.sa_mask); - sigaction (SIGTERM, &act, NULL); - sigaction (SIGTSTP, &act, NULL); - sigaction (SIGCONT, &act, NULL); - - execl ("/bin/sh", "sh", "-c", cmd, NULL); - _exit (127); /* execl error */ - } - else if (thepid != -1) { - rc = imap_wait_keepalive (thepid); - } - - sigaction (SIGCONT, &oldcont, NULL); - sigaction (SIGTSTP, &oldtstp, NULL); - - /* reset SIGINT, SIGQUIT and SIGCHLD */ - mutt_unblock_signals_system (1); - if (flags & M_DETACH_PROCESS) - sigprocmask (SIG_UNBLOCK, &set, NULL); - - rc = (thepid != -1) ? (WIFEXITED (rc) ? WEXITSTATUS (rc) : -1) : -1; - - return (rc); -}