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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
31 int _mutt_system (const char *cmd, int flags)
35 struct sigaction oldtstp;
36 struct sigaction oldcont;
43 /* must ignore SIGINT and SIGQUIT */
45 mutt_block_signals_system ();
47 /* also don't want to be stopped right now */
48 if (flags & M_DETACH_PROCESS)
51 sigaddset (&set, SIGTSTP);
52 sigprocmask (SIG_BLOCK, &set, NULL);
56 act.sa_handler = SIG_DFL;
57 /* we want to restart the waitpid() below */
59 act.sa_flags = SA_RESTART;
61 sigemptyset (&act.sa_mask);
62 sigaction (SIGTSTP, &act, &oldtstp);
63 sigaction (SIGCONT, &act, &oldcont);
66 if ((thepid = fork ()) == 0)
70 if (flags & M_DETACH_PROCESS)
74 /* give up controlling terminal */
81 for (fd = 0; fd < OPEN_MAX; fd++)
83 #elif defined(_POSIX_OPEN_MAX)
84 for (fd = 0; fd < _POSIX_OPEN_MAX; fd++)
92 act.sa_handler = SIG_DFL;
93 sigaction (SIGCHLD, &act, NULL);
104 /* reset signals for the child; not really needed, but... */
105 mutt_unblock_signals_system (0);
106 act.sa_handler = SIG_DFL;
108 sigemptyset (&act.sa_mask);
109 sigaction (SIGTERM, &act, NULL);
110 sigaction (SIGTSTP, &act, NULL);
111 sigaction (SIGCONT, &act, NULL);
113 execl (EXECSHELL, "sh", "-c", cmd, NULL);
114 _exit (127); /* execl error */
116 else if (thepid != -1)
119 /* wait for the (first) child process to finish */
120 waitpid (thepid, &rc, 0);
122 rc = imap_wait_keepalive (thepid);
126 sigaction (SIGCONT, &oldcont, NULL);
127 sigaction (SIGTSTP, &oldtstp, NULL);
129 /* reset SIGINT, SIGQUIT and SIGCHLD */
130 mutt_unblock_signals_system (1);
131 if (flags & M_DETACH_PROCESS)
132 sigprocmask (SIG_UNBLOCK, &set, NULL);
134 rc = (thepid != -1) ? (WIFEXITED (rc) ? WEXITSTATUS (rc) : -1) : -1;