NeoMutt  2020-06-26-30-g76c339
Teaching an old dog new tricks
DOXYGEN
system.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <signal.h>
31 #include <stdbool.h>
32 #include <stdlib.h>
33 #include <sys/wait.h> // IWYU pragma: keep
34 #include <unistd.h>
35 #include "mutt/lib.h"
36 #include "protos.h" // IWYU pragma: keep
37 #ifdef USE_IMAP
38 #include "imap/lib.h"
39 #endif
40 
51 int mutt_system(const char *cmd)
52 {
53  int rc = -1;
54  struct sigaction act;
55  struct sigaction oldtstp;
56  struct sigaction oldcont;
57  pid_t pid;
58 
59  if (!cmd || (*cmd == '\0'))
60  return 0;
61 
62  /* must ignore SIGINT and SIGQUIT */
63 
65 
66  act.sa_handler = SIG_DFL;
67 /* we want to restart the waitpid() below */
68 #ifdef SA_RESTART
69  act.sa_flags = SA_RESTART;
70 #endif
71  sigemptyset(&act.sa_mask);
72  sigaction(SIGTSTP, &act, &oldtstp);
73  sigaction(SIGCONT, &act, &oldcont);
74 
75  pid = fork();
76  if (pid == 0)
77  {
78  act.sa_flags = 0;
79 
80  /* reset signals for the child; not really needed, but... */
82  act.sa_handler = SIG_DFL;
83  act.sa_flags = 0;
84  sigemptyset(&act.sa_mask);
85  sigaction(SIGTERM, &act, NULL);
86  sigaction(SIGTSTP, &act, NULL);
87  sigaction(SIGCONT, &act, NULL);
88 
89  execle(EXEC_SHELL, "sh", "-c", cmd, NULL, mutt_envlist_getlist());
90  _exit(127); /* execl error */
91  }
92  else if (pid != -1)
93  {
94 #ifdef USE_IMAP
95  rc = imap_wait_keepalive(pid);
96 #endif
97  }
98 
99  sigaction(SIGCONT, &oldcont, NULL);
100  sigaction(SIGTSTP, &oldtstp, NULL);
101 
102  /* reset SIGINT, SIGQUIT and SIGCHLD */
104 
105  rc = (pid != -1) ? (WIFEXITED(rc) ? WEXITSTATUS(rc) : -1) : -1;
106 
107  return rc;
108 }
IMAP network mailbox.
void mutt_sig_block_system(void)
Block signals before calling exec()
Definition: signal.c:183
#define EXEC_SHELL
Definition: filter.h:27
void mutt_sig_unblock_system(bool restore)
Restore previously blocked signals.
Definition: signal.c:207
Prototypes for many functions.
int mutt_system(const char *cmd)
Run an external command.
Definition: system.c:51
int imap_wait_keepalive(pid_t pid)
Wait for a process to change state.
Definition: util.c:1112
Convenience wrapper for the library headers.
char ** mutt_envlist_getlist(void)
Get the private environment.
Definition: envlist.c:169