2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
35 int _mutt_system (const char *cmd, int flags)
39 struct sigaction oldtstp;
40 struct sigaction oldcont;
47 /* must ignore SIGINT and SIGQUIT */
49 mutt_block_signals_system ();
51 /* also don't want to be stopped right now */
52 if (flags & M_DETACH_PROCESS)
55 sigaddset (&set, SIGTSTP);
56 sigprocmask (SIG_BLOCK, &set, NULL);
60 act.sa_handler = SIG_DFL;
61 /* we want to restart the waitpid() below */
63 act.sa_flags = SA_RESTART;
65 sigemptyset (&act.sa_mask);
66 sigaction (SIGTSTP, &act, &oldtstp);
67 sigaction (SIGCONT, &act, &oldcont);
70 if ((thepid = fork ()) == 0)
74 if (flags & M_DETACH_PROCESS)
78 /* give up controlling terminal */
85 for (fd = 0; fd < OPEN_MAX; fd++)
87 #elif defined(_POSIX_OPEN_MAX)
88 for (fd = 0; fd < _POSIX_OPEN_MAX; fd++)
96 act.sa_handler = SIG_DFL;
97 sigaction (SIGCHLD, &act, NULL);
108 /* reset signals for the child; not really needed, but... */
109 mutt_unblock_signals_system (0);
110 act.sa_handler = SIG_DFL;
112 sigemptyset (&act.sa_mask);
113 sigaction (SIGTERM, &act, NULL);
114 sigaction (SIGTSTP, &act, NULL);
115 sigaction (SIGCONT, &act, NULL);
117 execl (EXECSHELL, "sh", "-c", cmd, NULL);
118 _exit (127); /* execl error */
120 else if (thepid != -1)
123 /* wait for the (first) child process to finish */
124 waitpid (thepid, &rc, 0);
126 rc = imap_wait_keepalive (thepid);
130 sigaction (SIGCONT, &oldcont, NULL);
131 sigaction (SIGTSTP, &oldtstp, NULL);
133 /* reset SIGINT, SIGQUIT and SIGCHLD */
134 mutt_unblock_signals_system (1);
135 if (flags & M_DETACH_PROCESS)
136 sigprocmask (SIG_UNBLOCK, &set, NULL);
138 rc = (thepid != -1) ? (WIFEXITED (rc) ? WEXITSTATUS (rc) : -1) : -1;