NeoMutt  2024-10-02-37-gfa9146
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
filter.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stdbool.h>
32#include <stdlib.h>
33#include <sys/wait.h>
34#include <unistd.h>
35#include "filter.h"
36#include "signal2.h"
37
62pid_t filter_create_fd(const char *cmd, FILE **fp_in, FILE **fp_out, FILE **fp_err,
63 int fdin, int fdout, int fderr, char **envlist)
64{
65 int pin[2], pout[2], perr[2], pid;
66
67 if (fp_in)
68 {
69 *fp_in = NULL;
70 if (pipe(pin) == -1)
71 return -1;
72 }
73
74 if (fp_out)
75 {
76 *fp_out = NULL;
77 if (pipe(pout) == -1)
78 {
79 if (fp_in)
80 {
81 close(pin[0]);
82 close(pin[1]);
83 }
84 return -1;
85 }
86 }
87
88 if (fp_err)
89 {
90 *fp_err = NULL;
91 if (pipe(perr) == -1)
92 {
93 if (fp_in)
94 {
95 close(pin[0]);
96 close(pin[1]);
97 }
98 if (fp_out)
99 {
100 close(pout[0]);
101 close(pout[1]);
102 }
103 return -1;
104 }
105 }
106
108
109 pid = fork();
110 if (pid == 0)
111 {
114
115 if (fp_in)
116 {
117 close(pin[1]);
118 dup2(pin[0], 0);
119 close(pin[0]);
120 }
121 else if (fdin != -1)
122 {
123 dup2(fdin, 0);
124 close(fdin);
125 }
126
127 if (fp_out)
128 {
129 close(pout[0]);
130 dup2(pout[1], 1);
131 close(pout[1]);
132 }
133 else if (fdout != -1)
134 {
135 dup2(fdout, 1);
136 close(fdout);
137 }
138
139 if (fp_err)
140 {
141 close(perr[0]);
142 dup2(perr[1], 2);
143 close(perr[1]);
144 }
145 else if (fderr != -1)
146 {
147 dup2(fderr, 2);
148 close(fderr);
149 }
150
151 execle(EXEC_SHELL, "sh", "-c", cmd, NULL, envlist);
152 _exit(127);
153 }
154 else if (pid == -1)
155 {
157
158 if (fp_in)
159 {
160 close(pin[0]);
161 close(pin[1]);
162 }
163
164 if (fp_out)
165 {
166 close(pout[0]);
167 close(pout[1]);
168 }
169
170 if (fp_err)
171 {
172 close(perr[0]);
173 close(perr[1]);
174 }
175
176 return -1;
177 }
178
179 if (fp_out)
180 {
181 close(pout[1]);
182 *fp_out = fdopen(pout[0], "r");
183 }
184
185 if (fp_in)
186 {
187 close(pin[0]);
188 *fp_in = fdopen(pin[1], "w");
189 }
190
191 if (fp_err)
192 {
193 close(perr[1]);
194 *fp_err = fdopen(perr[0], "r");
195 }
196
197 return pid;
198}
199
209pid_t filter_create(const char *cmd, FILE **fp_in, FILE **fp_out, FILE **fp_err, char **envlist)
210{
211 return filter_create_fd(cmd, fp_in, fp_out, fp_err, -1, -1, -1, envlist);
212}
213
220int filter_wait(pid_t pid)
221{
222 int rc = 0;
223
224 waitpid(pid, &rc, 0);
226 rc = WIFEXITED(rc) ? WEXITSTATUS(rc) : -1;
227
228 return rc;
229}
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:220
pid_t filter_create_fd(const char *cmd, FILE **fp_in, FILE **fp_out, FILE **fp_err, int fdin, int fdout, int fderr, char **envlist)
Run a command on a pipe (optionally connect stdin/stdout)
Definition: filter.c:62
pid_t filter_create(const char *cmd, FILE **fp_in, FILE **fp_out, FILE **fp_err, char **envlist)
Set up filter program.
Definition: filter.c:209
Pass files through external commands (filters)
#define EXEC_SHELL
Definition: filter.h:29
Signal handling.
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