Andreas Krennmair:
[apps/madmutt.git] / system.c
1 /*
2  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3  * 
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.
8  * 
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.
13  * 
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.
17  */
18
19 #if HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include "mutt.h"
24 #ifdef USE_IMAP
25 # include "imap.h"
26 # include <errno.h>
27 #endif
28
29 #include <stdlib.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <sys/wait.h>
33 #include <unistd.h>
34
35 int _mutt_system (const char *cmd, int flags)
36 {
37   int rc = -1;
38   struct sigaction act;
39   struct sigaction oldtstp;
40   struct sigaction oldcont;
41   sigset_t set;
42   pid_t thepid;
43
44   if (!cmd || !*cmd)
45     return (0);
46
47   /* must ignore SIGINT and SIGQUIT */
48
49   mutt_block_signals_system ();
50
51   /* also don't want to be stopped right now */
52   if (flags & M_DETACH_PROCESS) {
53     sigemptyset (&set);
54     sigaddset (&set, SIGTSTP);
55     sigprocmask (SIG_BLOCK, &set, NULL);
56   }
57   else {
58     act.sa_handler = SIG_DFL;
59     /* we want to restart the waitpid() below */
60 #ifdef SA_RESTART
61     act.sa_flags = SA_RESTART;
62 #endif
63     sigemptyset (&act.sa_mask);
64     sigaction (SIGTSTP, &act, &oldtstp);
65     sigaction (SIGCONT, &act, &oldcont);
66   }
67
68   if ((thepid = fork ()) == 0) {
69     act.sa_flags = 0;
70
71     if (flags & M_DETACH_PROCESS) {
72       int fd;
73
74       /* give up controlling terminal */
75       setsid ();
76
77       switch (fork ()) {
78       case 0:
79 #if defined(OPEN_MAX)
80         for (fd = 0; fd < OPEN_MAX; fd++)
81           close (fd);
82 #elif defined(_POSIX_OPEN_MAX)
83         for (fd = 0; fd < _POSIX_OPEN_MAX; fd++)
84           close (fd);
85 #else
86         close (0);
87         close (1);
88         close (2);
89 #endif
90         chdir ("/");
91         act.sa_handler = SIG_DFL;
92         sigaction (SIGCHLD, &act, NULL);
93         break;
94
95       case -1:
96         _exit (127);
97
98       default:
99         _exit (0);
100       }
101     }
102
103     /* reset signals for the child; not really needed, but... */
104     mutt_unblock_signals_system (0);
105     act.sa_handler = SIG_DFL;
106     act.sa_flags = 0;
107     sigemptyset (&act.sa_mask);
108     sigaction (SIGTERM, &act, NULL);
109     sigaction (SIGTSTP, &act, NULL);
110     sigaction (SIGCONT, &act, NULL);
111
112     execl (EXECSHELL, "sh", "-c", cmd, NULL);
113     _exit (127);                /* execl error */
114   }
115   else if (thepid != -1) {
116 #ifndef USE_IMAP
117     /* wait for the (first) child process to finish */
118     waitpid (thepid, &rc, 0);
119 #else
120     rc = imap_wait_keepalive (thepid);
121 #endif
122   }
123
124   sigaction (SIGCONT, &oldcont, NULL);
125   sigaction (SIGTSTP, &oldtstp, NULL);
126
127   /* reset SIGINT, SIGQUIT and SIGCHLD */
128   mutt_unblock_signals_system (1);
129   if (flags & M_DETACH_PROCESS)
130     sigprocmask (SIG_UNBLOCK, &set, NULL);
131
132   rc = (thepid != -1) ? (WIFEXITED (rc) ? WEXITSTATUS (rc) : -1) : -1;
133
134   return (rc);
135 }