NeoMutt  2018-07-16 +952-a2da0a
Teaching an old dog new tricks
DOXYGEN
system.c File Reference

Execute external programs. More...

#include "config.h"
#include <signal.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include "mutt/mutt.h"
#include "mutt.h"
#include "imap/imap.h"
+ Include dependency graph for system.c:

Go to the source code of this file.

Functions

int mutt_system (const char *cmd)
 Run an external command. More...
 

Detailed Description

Execute external programs.

Authors
  • Michael R. Elkins

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file system.c.

Function Documentation

int mutt_system ( const char *  cmd)

Run an external command.

Parameters
cmdCommand and argments
Return values
-1Error
>=0Success (command's return code)

Fork and run an external command with arguments.

Note
This function won't return until the command finishes.

Definition at line 50 of file system.c.

51 {
52  int rc = -1;
53  struct sigaction act;
54  struct sigaction oldtstp;
55  struct sigaction oldcont;
56  pid_t thepid;
57 
58  if (!cmd || !*cmd)
59  return 0;
60 
61  /* must ignore SIGINT and SIGQUIT */
62 
64 
65  act.sa_handler = SIG_DFL;
66 /* we want to restart the waitpid() below */
67 #ifdef SA_RESTART
68  act.sa_flags = SA_RESTART;
69 #endif
70  sigemptyset(&act.sa_mask);
71  sigaction(SIGTSTP, &act, &oldtstp);
72  sigaction(SIGCONT, &act, &oldcont);
73 
74  thepid = fork();
75  if (thepid == 0)
76  {
77  act.sa_flags = 0;
78 
79  /* reset signals for the child; not really needed, but... */
81  act.sa_handler = SIG_DFL;
82  act.sa_flags = 0;
83  sigemptyset(&act.sa_mask);
84  sigaction(SIGTERM, &act, NULL);
85  sigaction(SIGTSTP, &act, NULL);
86  sigaction(SIGCONT, &act, NULL);
87 
88  execle(EXECSHELL, "sh", "-c", cmd, NULL, mutt_envlist_getlist());
89  _exit(127); /* execl error */
90  }
91  else if (thepid != -1)
92  {
93 #ifdef USE_IMAP
94  rc = imap_wait_keepalive(thepid);
95 #endif
96  }
97 
98  sigaction(SIGCONT, &oldcont, NULL);
99  sigaction(SIGTSTP, &oldtstp, NULL);
100 
101  /* reset SIGINT, SIGQUIT and SIGCHLD */
103 
104  rc = (thepid != -1) ? (WIFEXITED(rc) ? WEXITSTATUS(rc) : -1) : -1;
105 
106  return rc;
107 }
void mutt_sig_unblock_system(bool catch)
Restore previously blocked signals.
Definition: signal.c:200
void mutt_sig_block_system(void)
Block signals before calling exec()
Definition: signal.c:176
#define EXECSHELL
Definition: mutt.h:205
int imap_wait_keepalive(pid_t pid)
Wait for a process to change state.
Definition: util.c:1086
char ** mutt_envlist_getlist(void)
Get the private environment.
Definition: envlist.c:165

+ Here is the call graph for this function:

+ Here is the caller graph for this function: