MCPcopy Create free account
hub / github.com/ElementsProject/lightning / subd

Function subd

lightningd/subd.c:202–301  ·  view source on GitHub ↗

We use sockets, not pipes, because fds are bidir. */

Source from the content-addressed store, hash-verified

200
201/* We use sockets, not pipes, because fds are bidir. */
202static int subd(const char *path, const char *name,
203 const char *debug_subdaemon,
204 int *msgfd,
205 bool io_logging,
206 va_list *ap)
207{
208 int childmsg[2], execfail[2];
209 pid_t childpid;
210 int err, *fd;
211
212 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, childmsg) != 0)
213 goto fail;
214
215 if (pipe(execfail) != 0)
216 goto close_msgfd_fail;
217
218 if (fcntl(execfail[1], F_SETFD, fcntl(execfail[1], F_GETFD)
219 | FD_CLOEXEC) < 0)
220 goto close_execfail_fail;
221
222 childpid = fork();
223 if (childpid < 0)
224 goto close_execfail_fail;
225
226 if (childpid == 0) {
227 size_t num_args;
228 char *args[] = { NULL, NULL, NULL, NULL };
229 int **fds = tal_arr(tmpctx, int *, 3);
230 int stdoutfd = STDOUT_FILENO, stderrfd = STDERR_FILENO;
231
232 close(childmsg[0]);
233 close(execfail[0]);
234
235 /* msg = STDIN (0) */
236 fds[0] = &childmsg[1];
237 /* These are untouched */
238 fds[1] = &stdoutfd;
239 fds[2] = &stderrfd;
240
241 while ((fd = va_arg(*ap, int *)) != NULL) {
242 assert(*fd != -1);
243 tal_arr_expand(&fds, fd);
244 }
245
246 /* Finally, the fd to report exec errors on */
247 tal_arr_expand(&fds, &execfail[1]);
248
249 if (!shuffle_fds(fds, tal_count(fds)))
250 goto child_errno_fail;
251
252 /* Make (fairly!) sure all other fds are closed. */
253 closefrom(tal_count(fds));
254
255 num_args = 0;
256 args[num_args++] = tal_strdup(NULL, path);
257 if (io_logging)
258 args[num_args++] = "--log-io";
259#if DEVELOPER

Callers 1

new_subdFunction · 0.70

Calls 5

shuffle_fdsFunction · 0.85
closefromFunction · 0.85
strendsFunction · 0.85
close_taken_fdsFunction · 0.85
close_noerrFunction · 0.85

Tested by

no test coverage detected