Debian Codegraph @

src/util/unix/process.c

raw
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include <stdio.h>
9 #include <sys/wait.h>
10 #include <signal.h>
11 #include <git2.h>
12
13 #include "git2_util.h"
14 #include "vector.h"
15 #include "futils.h"
16 #include "process.h"
17 #include "strlist.h"
18
19 #ifdef __APPLE__
20 #include <crt_externs.h>
21 #define environ (*_NSGetEnviron())
22 #else
23 extern char **environ;
24 #endif
25
26 struct git_process {
27 char **args;
28 char **env;
29
30 char *cwd;
31
32 unsigned int use_shell : 1,
33 capture_in : 1,
34 capture_out : 1,
35 capture_err : 1;
36
37 pid_t pid;
38
39 int child_in;
40 int child_out;
41 int child_err;
42 git_process_result_status status;
43 };
44
45 GIT_INLINE(bool) is_delete_env(const char *env)
46 {
47 const char *c = strchr(env, '=');
48
49 if (c == NULL)
50 return false;
51
52 return *(c+1) == '\0';
53 }
54
55 GIT_INLINE(int) insert_dup(git_vector *v, const char *s)
56 {
57 char *dup = git__strdup(s);
58
59 GIT_ERROR_CHECK_ALLOC(dup);
60
61 return git_vector_insert(v, dup);
62 }
63
64 static int setup_args(
65 char ***out,
66 const char **args,
67 size_t args_len,
68 bool use_shell)
69 {
70 git_vector prefixed = GIT_VECTOR_INIT;
71 git_str first = GIT_STR_INIT;
72 size_t cnt;
73
74 GIT_ASSERT(args && args_len);
75
76 if (use_shell) {
77 if (git_str_puts(&first, args[0]) < 0 ||
78 git_str_puts(&first, " \"$@\"") < 0 ||
79 insert_dup(&prefixed, "/bin/sh") < 0 ||
80 insert_dup(&prefixed, "-c") < 0 ||
81 git_vector_insert(&prefixed, git_str_detach(&first)) < 0)
82 goto on_error;
83 }
84
85 for (cnt = 0; args && cnt < args_len; cnt++) {
86 if (insert_dup(&prefixed, args[cnt]) < 0)
87 goto on_error;
88 }
89
90 git_vector_insert(&prefixed, NULL);
91
92 *out = (char **)prefixed.contents;
93
94 return 0;
95
96 on_error:
97 git_str_dispose(&first);
98 git_vector_dispose_deep(&prefixed);
99 return -1;
100 }
101
102 static int merge_env(
103 char ***out,
104 const char **env,
105 size_t env_len,
106 bool exclude_env)
107 {
108 git_vector merged = GIT_VECTOR_INIT;
109 char **kv;
110 size_t max, cnt;
111 int error = 0;
112
113 for (max = env_len, kv = environ; !exclude_env && *kv; kv++)
114 max++;
115
116 if ((error = git_vector_init(&merged, max, NULL)) < 0)
117 goto on_error;
118
119 for (cnt = 0; env && cnt < env_len; cnt++) {
120 if (is_delete_env(env[cnt]))
121 continue;
122
123 if (insert_dup(&merged, env[cnt]) < 0)
124 goto on_error;
125 }
126
127 if (!exclude_env) {
128 for (kv = environ; *kv; kv++) {
129 if (env && git_strlist_contains_key(env, env_len, *kv, '='))
130 continue;
131
132 if (insert_dup(&merged, *kv) < 0)
133 goto on_error;
134 }
135 }
136
137 if (merged.length == 0) {
138 *out = NULL;
139 error = 0;
140 goto on_error;
141 }
142
143 git_vector_insert(&merged, NULL);
144
145 *out = (char **)merged.contents;
146
147 return 0;
148
149 on_error:
150 git_vector_dispose_deep(&merged);
151 return error;
152 }
153
154 int git_process_new(
155 git_process **out,
156 const char **args,
157 size_t args_len,
158 const char **env,
159 size_t env_len,
160 git_process_options *opts)
161 {
162 git_process *process;
163
164 GIT_ASSERT_ARG(out && args && args_len > 0);
165
166 *out = NULL;
167
168 process = git__calloc(sizeof(git_process), 1);
169 GIT_ERROR_CHECK_ALLOC(process);
170
171 if (setup_args(&process->args, args, args_len, opts ? opts->use_shell : false) < 0 ||
172 merge_env(&process->env, env, env_len, opts ? opts->exclude_env : false) < 0) {
173 git_process_free(process);
174 return -1;
175 }
176
177 if (opts) {
178 process->use_shell = opts->use_shell;
179 process->capture_in = opts->capture_in;
180 process->capture_out = opts->capture_out;
181 process->capture_err = opts->capture_err;
182
183 if (opts->cwd) {
184 process->cwd = git__strdup(opts->cwd);
185 GIT_ERROR_CHECK_ALLOC(process->cwd);
186 }
187 }
188
189 process->child_in = -1;
190 process->child_out = -1;
191 process->child_err = -1;
192 process->status = -1;
193
194 *out = process;
195 return 0;
196 }
197
198 extern int git_process_new_from_cmdline(
199 git_process **out,
200 const char *cmdline,
201 const char **env,
202 size_t env_len,
203 git_process_options *opts)
204 {
205 git_process_options merged_opts = {0};
206
207 memcpy(&merged_opts, opts, sizeof(git_process_options));
208 merged_opts.use_shell = 1;
209
210 return git_process_new(out, &cmdline, 1, env, env_len, &merged_opts);
211 }
212
213 #define CLOSE_FD(fd) \
214 if (fd >= 0) { \
215 close(fd); \
216 fd = -1; \
217 }
218
219 static int try_read_status(size_t *out, int fd, void *buf, size_t len)
220 {
221 size_t read_len = 0;
222 int ret = -1;
223
224 while (ret && read_len < len) {
225 ret = read(fd, buf + read_len, len - read_len);
226
227 if (ret < 0 && errno != EAGAIN && errno != EINTR) {
228 git_error_set(GIT_ERROR_OS, "could not read child status");
229 return -1;
230 }
231
232 read_len += ret;
233 }
234
235 *out = read_len;
236 return 0;
237 }
238
239
240 static int read_status(int fd)
241 {
242 size_t status_len = sizeof(int) * 3, read_len = 0;
243 char buffer[status_len], fn[128];
244 int error, fn_error, os_error, fn_len = 0;
245
246 if ((error = try_read_status(&read_len, fd, buffer, status_len)) < 0)
247 return error;
248
249 /* Immediate EOF indicates the exec succeeded. */
250 if (read_len == 0)
251 return 0;
252
253 if (read_len < status_len) {
254 git_error_set(GIT_ERROR_INVALID, "child status truncated");
255 return -1;
256 }
257
258 memcpy(&fn_error, &buffer[0], sizeof(int));
259 memcpy(&os_error, &buffer[sizeof(int)], sizeof(int));
260 memcpy(&fn_len, &buffer[sizeof(int) * 2], sizeof(int));
261
262 if (fn_len > 0) {
263 fn_len = min(fn_len, (int)(ARRAY_SIZE(fn) - 1));
264
265 if ((error = try_read_status(&read_len, fd, fn, fn_len)) < 0)
266 return error;
267
268 fn[fn_len] = '\0';
269 } else {
270 fn[0] = '\0';
271 }
272
273 if (fn_error) {
274 errno = os_error;
275 git_error_set(GIT_ERROR_OS, "could not %s", fn[0] ? fn : "(unknown)");
276 }
277
278 return fn_error;
279 }
280
281 static bool try_write_status(int fd, const void *buf, size_t len)
282 {
283 size_t write_len;
284 int ret;
285
286 for (write_len = 0; write_len < len; ) {
287 ret = write(fd, buf + write_len, len - write_len);
288
289 if (ret <= 0)
290 break;
291
292 write_len += ret;
293 }
294
295 return (len == write_len);
296 }
297
298 static void write_status(int fd, const char *fn, int error, int os_error)
299 {
300 size_t status_len = sizeof(int) * 3, fn_len;
301 char buffer[status_len];
302
303 fn_len = strlen(fn);
304
305 if (fn_len > INT_MAX)
306 fn_len = INT_MAX;
307
308 memcpy(&buffer[0], &error, sizeof(int));
309 memcpy(&buffer[sizeof(int)], &os_error, sizeof(int));
310 memcpy(&buffer[sizeof(int) * 2], &fn_len, sizeof(int));
311
312 /* Do our best effort to write all the status. */
313 if (!try_write_status(fd, buffer, status_len))
314 return;
315
316 if (fn_len)
317 try_write_status(fd, fn, fn_len);
318 }
319
320 static int resolve_path(git_process *process)
321 {
322 git_str full_path = GIT_STR_INIT;
323 int error = 0;
324
325 /* The shell will resolve the path for us */
326 if (process->use_shell)
327 goto done;
328
329 error = git_fs_path_find_executable(&full_path, process->args[0]);
330
331 if (error == GIT_ENOTFOUND) {
332 git_error_set(GIT_ERROR_SSH, "cannot run %s: No such file or directory", process->args[0]);
333 error = -1;
334 }
335
336 if (error)
337 goto done;
338
339 git__free(process->args[0]);
340 process->args[0] = git_str_detach(&full_path);
341
342 done:
343 git_str_dispose(&full_path);
344 return error;
345 }
346
347 int git_process_start(git_process *process)
348 {
349 int in[2] = { -1, -1 }, out[2] = { -1, -1 },
350 err[2] = { -1, -1 }, status[2] = { -1, -1 };
351 int fdflags, state, error;
352 pid_t pid;
353
354 /* Locate the path (unless we're letting the shell do it for us) */
355 if ((error = resolve_path(process)) < 0)
356 goto on_error;
357
358 /* Set up the pipes to read from/write to the process */
359 if ((process->capture_in && pipe(in) < 0) ||
360 (process->capture_out && pipe(out) < 0) ||
361 (process->capture_err && pipe(err) < 0)) {
362 git_error_set(GIT_ERROR_OS, "could not create pipe");
363 goto on_error;
364 }
365
366 /* Set up a self-pipe for status from the forked process. */
367 if (pipe(status) < 0 ||
368 (fdflags = fcntl(status[1], F_GETFD)) < 0 ||
369 fcntl(status[1], F_SETFD, fdflags | FD_CLOEXEC) < 0) {
370 git_error_set(GIT_ERROR_OS, "could not create pipe");
371 goto on_error;
372 }
373
374 switch (pid = fork()) {
375 case -1:
376 git_error_set(GIT_ERROR_OS, "could not fork");
377 goto on_error;
378
379 /* Child: start the process. */
380 case 0:
381 /* Close the opposing side of the pipes */
382 CLOSE_FD(status[0]);
383
384 if (process->capture_in) {
385 CLOSE_FD(in[1]);
386 dup2(in[0], STDIN_FILENO);
387 }
388
389 if (process->capture_out) {
390 CLOSE_FD(out[0]);
391 dup2(out[1], STDOUT_FILENO);
392 }
393
394 if (process->capture_err) {
395 CLOSE_FD(err[0]);
396 dup2(err[1], STDERR_FILENO);
397 }
398
399 if (process->cwd && (error = chdir(process->cwd)) < 0) {
400 write_status(status[1], "chdir", error, errno);
401 exit(0);
402 }
403
404 /*
405 * Exec the process and write the results back if the
406 * call fails. If it succeeds, we'll close the status
407 * pipe (via CLOEXEC) and the parent will know.
408 */
409 error = execve(process->args[0],
410 process->args,
411 process->env);
412
413 write_status(status[1], "execve", error, errno);
414 exit(0);
415
416 /* Parent: make sure the child process exec'd correctly. */
417 default:
418 /* Close the opposing side of the pipes */
419 CLOSE_FD(status[1]);
420
421 if (process->capture_in) {
422 CLOSE_FD(in[0]);
423 process->child_in = in[1];
424 }
425
426 if (process->capture_out) {
427 CLOSE_FD(out[1]);
428 process->child_out = out[0];
429 }
430
431 if (process->capture_err) {
432 CLOSE_FD(err[1]);
433 process->child_err = err[0];
434 }
435
436 /* Try to read the status */
437 process->status = status[0];
438 if ((error = read_status(status[0])) < 0) {
439 waitpid(process->pid, &state, 0);
440 goto on_error;
441 }
442
443 process->pid = pid;
444 return 0;
445 }
446
447 on_error:
448 CLOSE_FD(in[0]); CLOSE_FD(in[1]);
449 CLOSE_FD(out[0]); CLOSE_FD(out[1]);
450 CLOSE_FD(err[0]); CLOSE_FD(err[1]);
451 CLOSE_FD(status[0]); CLOSE_FD(status[1]);
452 return -1;
453 }
454
455 int git_process_id(p_pid_t *out, git_process *process)
456 {
457 GIT_ASSERT(out && process);
458
459 if (!process->pid) {
460 git_error_set(GIT_ERROR_INVALID, "process not running");
461 return -1;
462 }
463
464 *out = process->pid;
465 return 0;
466 }
467
468 static ssize_t process_read(int fd, void *buf, size_t count)
469 {
470 ssize_t ret;
471
472 if (count > SSIZE_MAX)
473 count = SSIZE_MAX;
474
475 if ((ret = read(fd, buf, count)) < 0) {
476 git_error_set(GIT_ERROR_OS, "could not read from child process");
477 return -1;
478 }
479
480 return ret;
481 }
482
483 ssize_t git_process_read(git_process *process, void *buf, size_t count)
484 {
485 GIT_ASSERT_ARG(process);
486 GIT_ASSERT(process->capture_out);
487
488 return process_read(process->child_out, buf, count);
489 }
490
491 ssize_t git_process_read_err(git_process *process, void *buf, size_t count)
492 {
493 GIT_ASSERT_ARG(process);
494 GIT_ASSERT(process->capture_err);
495
496 return process_read(process->child_err, buf, count);
497 }
498
499 #ifdef GIT_THREADS
500
501 # define signal_state sigset_t
502
503 /*
504 * Since signal-handling is process-wide, we cannot simply use
505 * SIG_IGN to avoid SIGPIPE. Instead: http://www.microhowto.info:80/howto/ignore_sigpipe_without_affecting_other_threads_in_a_process.html
506 */
507
508 GIT_INLINE(int) disable_signals(sigset_t *saved_mask)
509 {
510 sigset_t sigpipe_mask;
511
512 sigemptyset(&sigpipe_mask);
513 sigaddset(&sigpipe_mask, SIGPIPE);
514
515 if (pthread_sigmask(SIG_BLOCK, &sigpipe_mask, saved_mask) < 0) {
516 git_error_set(GIT_ERROR_OS, "could not configure signal mask");
517 return -1;
518 }
519
520 return 0;
521 }
522
523 GIT_INLINE(int) restore_signals(sigset_t *saved_mask)
524 {
525 sigset_t sigpipe_mask, pending;
526 int signal;
527
528 sigemptyset(&sigpipe_mask);
529 sigaddset(&sigpipe_mask, SIGPIPE);
530
531 if (sigpending(&pending) < 0) {
532 git_error_set(GIT_ERROR_OS, "could not examine pending signals");
533 return -1;
534 }
535
536 if (sigismember(&pending, SIGPIPE) == 1 &&
537 sigwait(&sigpipe_mask, &signal) < 0) {
538 git_error_set(GIT_ERROR_OS, "could not wait for (blocking) signal delivery");
539 return -1;
540 }
541
542 if (pthread_sigmask(SIG_SETMASK, saved_mask, 0) < 0) {
543 git_error_set(GIT_ERROR_OS, "could not configure signal mask");
544 return -1;
545 }
546
547 return 0;
548 }
549
550 #else
551
552 # define signal_state struct sigaction
553
554 GIT_INLINE(int) disable_signals(struct sigaction *saved_handler)
555 {
556 struct sigaction ign_handler = { 0 };
557
558 ign_handler.sa_handler = SIG_IGN;
559
560 if (sigaction(SIGPIPE, &ign_handler, saved_handler) < 0) {
561 git_error_set(GIT_ERROR_OS, "could not configure signal handler");
562 return -1;
563 }
564
565 return 0;
566 }
567
568 GIT_INLINE(int) restore_signals(struct sigaction *saved_handler)
569 {
570 if (sigaction(SIGPIPE, saved_handler, NULL) < 0) {
571 git_error_set(GIT_ERROR_OS, "could not configure signal handler");
572 return -1;
573 }
574
575 return 0;
576 }
577
578 #endif
579
580 ssize_t git_process_write(git_process *process, const void *buf, size_t count)
581 {
582 signal_state saved_signal;
583 ssize_t ret;
584
585 GIT_ASSERT_ARG(process);
586 GIT_ASSERT(process->capture_in);
587
588 if (count > SSIZE_MAX)
589 count = SSIZE_MAX;
590
591 if (disable_signals(&saved_signal) < 0)
592 return -1;
593
594 if ((ret = write(process->child_in, buf, count)) < 0)
595 git_error_set(GIT_ERROR_OS, "could not write to child process");
596
597 if (restore_signals(&saved_signal) < 0)
598 return -1;
599
600 return (ret < 0) ? -1 : ret;
601 }
602
603 int git_process_close_in(git_process *process)
604 {
605 if (!process->capture_in) {
606 git_error_set(GIT_ERROR_INVALID, "input is not open");
607 return -1;
608 }
609
610 CLOSE_FD(process->child_in);
611 return 0;
612 }
613
614 int git_process_close_out(git_process *process)
615 {
616 if (!process->capture_out) {
617 git_error_set(GIT_ERROR_INVALID, "output is not open");
618 return -1;
619 }
620
621 CLOSE_FD(process->child_out);
622 return 0;
623 }
624
625 int git_process_close_err(git_process *process)
626 {
627 if (!process->capture_err) {
628 git_error_set(GIT_ERROR_INVALID, "error is not open");
629 return -1;
630 }
631
632 CLOSE_FD(process->child_err);
633 return 0;
634 }
635
636 int git_process_close(git_process *process)
637 {
638 CLOSE_FD(process->child_in);
639 CLOSE_FD(process->child_out);
640 CLOSE_FD(process->child_err);
641
642 return 0;
643 }
644
645 int git_process_wait(git_process_result *result, git_process *process)
646 {
647 int state;
648
649 if (result)
650 memset(result, 0, sizeof(git_process_result));
651
652 if (!process->pid) {
653 git_error_set(GIT_ERROR_INVALID, "process is stopped");
654 return -1;
655 }
656
657 if (waitpid(process->pid, &state, 0) < 0) {
658 git_error_set(GIT_ERROR_OS, "could not wait for child");
659 return -1;
660 }
661
662 process->pid = 0;
663
664 if (result) {
665 if (WIFEXITED(state)) {
666 result->status = GIT_PROCESS_STATUS_NORMAL;
667 result->exitcode = WEXITSTATUS(state);
668 } else if (WIFSIGNALED(state)) {
669 result->status = GIT_PROCESS_STATUS_ERROR;
670 result->signal = WTERMSIG(state);
671 } else {
672 result->status = GIT_PROCESS_STATUS_ERROR;
673 }
674 }
675
676 return 0;
677 }
678
679 int git_process_result_msg(git_str *out, git_process_result *result)
680 {
681 if (result->status == GIT_PROCESS_STATUS_NONE) {
682 return git_str_puts(out, "process not started");
683 } else if (result->status == GIT_PROCESS_STATUS_NORMAL) {
684 return git_str_printf(out, "process exited with code %d",
685 result->exitcode);
686 } else if (result->signal) {
687 return git_str_printf(out, "process exited on signal %d",
688 result->signal);
689 }
690
691 return git_str_puts(out, "unknown error");
692 }
693
694 void git_process_free(git_process *process)
695 {
696 if (!process)
697 return;
698
699 if (process->pid)
700 git_process_close(process);
701
702 git__free(process->cwd);
703 git_strlist_free_with_null(process->args);
704 git_strlist_free_with_null(process->env);
705 git__free(process);
706 }