NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
system.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <signal.h>
32#include <stdbool.h>
33#include <stdlib.h>
34#include <sys/types.h>
35#include <sys/wait.h> // IWYU pragma: keep
36#include <unistd.h>
37#include "mutt/lib.h"
38#include "imap/lib.h"
39#include "globals.h"
40#include "protos.h"
41
52int mutt_system(const char *cmd)
53{
54 int rc = -1;
55 struct sigaction act = { 0 };
56 struct sigaction oldtstp = { 0 };
57 struct sigaction oldcont = { 0 };
58 pid_t pid;
59
60 if (!cmd || (*cmd == '\0'))
61 return 0;
62
63 /* must ignore SIGINT and SIGQUIT */
64
66
67 act.sa_handler = SIG_DFL;
68/* we want to restart the waitpid() below */
69#ifdef SA_RESTART
70 act.sa_flags = SA_RESTART;
71#endif
72 sigemptyset(&act.sa_mask);
73 sigaction(SIGTSTP, &act, &oldtstp);
74 sigaction(SIGCONT, &act, &oldcont);
75
76 pid = fork();
77 if (pid == 0)
78 {
79 act.sa_flags = 0;
80
83
84 execle(EXEC_SHELL, "sh", "-c", cmd, NULL, EnvList);
85 _exit(127); /* execl error */
86 }
87 else if (pid != -1)
88 {
89 rc = imap_wait_keep_alive(pid);
90 }
91
92 sigaction(SIGCONT, &oldcont, NULL);
93 sigaction(SIGTSTP, &oldtstp, NULL);
94
95 /* reset SIGINT, SIGQUIT and SIGCHLD */
97
98 rc = (pid != -1) ? (WIFEXITED(rc) ? WEXITSTATUS(rc) : -1) : -1;
99
100 return rc;
101}
char ** EnvList
Private copy of the environment variables.
Definition: globals.c:75
Global variables.
IMAP network mailbox.
int imap_wait_keep_alive(pid_t pid)
Wait for a process to change state.
Definition: util.c:1018
#define EXEC_SHELL
Definition: filter.h:29
Convenience wrapper for the library headers.
Prototypes for many functions.
void mutt_sig_reset_child_signals(void)
Reset ignored signals back to the default.
Definition: signal.c:321
void mutt_sig_block_system(void)
Block signals before calling exec()
Definition: signal.c:245
void mutt_sig_unblock_system(bool restore)
Restore previously blocked signals.
Definition: signal.c:269
int mutt_system(const char *cmd)
Run an external command.
Definition: system.c:52