forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.c
109 lines (95 loc) · 2.62 KB
/
system.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* @file
* Execute external programs
*
* @authors
* Copyright (C) 1996-2000,2013 Michael R. Elkins <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* 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/>.
*/
/**
* @page system Execute external programs
*
* Execute external programs
*/
#include "config.h"
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/wait.h> // IWYU pragma: keep
#include <unistd.h>
#include "mutt/mutt.h"
#include "mutt.h"
#include "protos.h" // IWYU pragma: keep
#ifdef USE_IMAP
#include "imap/imap.h"
#endif
/**
* mutt_system - Run an external command
* @param cmd Command and arguments
* @retval -1 Error
* @retval >=0 Success (command's return code)
*
* Fork and run an external command with arguments.
*
* @note This function won't return until the command finishes.
*/
int mutt_system(const char *cmd)
{
int rc = -1;
struct sigaction act;
struct sigaction oldtstp;
struct sigaction oldcont;
pid_t pid;
if (!cmd || !*cmd)
return 0;
/* must ignore SIGINT and SIGQUIT */
mutt_sig_block_system();
act.sa_handler = SIG_DFL;
/* we want to restart the waitpid() below */
#ifdef SA_RESTART
act.sa_flags = SA_RESTART;
#endif
sigemptyset(&act.sa_mask);
sigaction(SIGTSTP, &act, &oldtstp);
sigaction(SIGCONT, &act, &oldcont);
pid = fork();
if (pid == 0)
{
act.sa_flags = 0;
/* reset signals for the child; not really needed, but... */
mutt_sig_unblock_system(false);
act.sa_handler = SIG_DFL;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGTERM, &act, NULL);
sigaction(SIGTSTP, &act, NULL);
sigaction(SIGCONT, &act, NULL);
execle(EXEC_SHELL, "sh", "-c", cmd, NULL, mutt_envlist_getlist());
_exit(127); /* execl error */
}
else if (pid != -1)
{
#ifdef USE_IMAP
rc = imap_wait_keepalive(pid);
#endif
}
sigaction(SIGCONT, &oldcont, NULL);
sigaction(SIGTSTP, &oldtstp, NULL);
/* reset SIGINT, SIGQUIT and SIGCHLD */
mutt_sig_unblock_system(true);
rc = (pid != -1) ? (WIFEXITED(rc) ? WEXITSTATUS(rc) : -1) : -1;
return rc;
}