2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
15 #include <imap/imap.h>
24 int _mutt_system (const char *cmd, int flags)
28 struct sigaction oldtstp;
29 struct sigaction oldcont;
36 /* must ignore SIGINT and SIGQUIT */
38 mutt_block_signals_system ();
40 /* also don't want to be stopped right now */
41 if (flags & M_DETACH_PROCESS) {
43 sigaddset (&set, SIGTSTP);
44 sigprocmask (SIG_BLOCK, &set, NULL);
47 act.sa_handler = SIG_DFL;
48 /* we want to restart the waitpid() below */
50 act.sa_flags = SA_RESTART;
52 sigemptyset (&act.sa_mask);
53 sigaction (SIGTSTP, &act, &oldtstp);
54 sigaction (SIGCONT, &act, &oldcont);
57 if ((thepid = fork ()) == 0) {
60 if (flags & M_DETACH_PROCESS) {
63 /* give up controlling terminal */
69 for (fd = 0; fd < OPEN_MAX; fd++)
71 #elif defined(_POSIX_OPEN_MAX)
72 for (fd = 0; fd < _POSIX_OPEN_MAX; fd++)
80 act.sa_handler = SIG_DFL;
81 sigaction (SIGCHLD, &act, NULL);
92 /* reset signals for the child; not really needed, but... */
93 mutt_unblock_signals_system (0);
94 act.sa_handler = SIG_DFL;
96 sigemptyset (&act.sa_mask);
97 sigaction (SIGTERM, &act, NULL);
98 sigaction (SIGTSTP, &act, NULL);
99 sigaction (SIGCONT, &act, NULL);
101 execl ("/bin/sh", "sh", "-c", cmd, NULL);
102 _exit (127); /* execl error */
104 else if (thepid != -1) {
105 rc = imap_wait_keepalive (thepid);
108 sigaction (SIGCONT, &oldcont, NULL);
109 sigaction (SIGTSTP, &oldtstp, NULL);
111 /* reset SIGINT, SIGQUIT and SIGCHLD */
112 mutt_unblock_signals_system (1);
113 if (flags & M_DETACH_PROCESS)
114 sigprocmask (SIG_UNBLOCK, &set, NULL);
116 rc = (thepid != -1) ? (WIFEXITED (rc) ? WEXITSTATUS (rc) : -1) : -1;