po: Update German man pages translation
[dpkg.git] / lib / dpkg / subproc.c
blobe91bb621520ba599277498d083a85077d12950e6
1 /*
2 * libdpkg - Debian packaging suite library routines
3 * subproc.c - subprocess helper routines
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2008-2014 Guillem Jover <guillem@debian.org>
8 * This is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 #include <config.h>
23 #include <compat.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <signal.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <stdio.h>
35 #include <dpkg/i18n.h>
36 #include <dpkg/dpkg.h>
37 #include <dpkg/subproc.h>
39 static int signo_ignores[] = { SIGQUIT, SIGINT };
40 static struct sigaction sa_save[array_count(signo_ignores)];
42 static void
43 subproc_reset_signal(int sig, struct sigaction *sa_old)
45 if (sigaction(sig, sa_old, NULL)) {
46 fprintf(stderr, _("error un-catching signal %s: %s\n"),
47 strsignal(sig), strerror(errno));
48 onerr_abort++;
52 static void
53 subproc_set_signal(int sig, struct sigaction *sa, struct sigaction *sa_old,
54 const char *name)
56 if (sigaction(sig, sa, sa_old))
57 ohshite(_("unable to ignore signal %s before running %.250s"),
58 strsignal(sig), name);
61 void
62 subproc_signals_ignore(const char *name)
64 struct sigaction sa;
65 size_t i;
67 onerr_abort++;
68 memset(&sa, 0, sizeof(sa));
69 sigemptyset(&sa.sa_mask);
70 sa.sa_handler = SIG_IGN;
71 sa.sa_flags = 0;
73 for (i = 0; i < array_count(signo_ignores); i++)
74 subproc_set_signal(signo_ignores[i], &sa, &sa_save[i], name);
76 push_cleanup(subproc_signals_cleanup, ~0, 0);
77 onerr_abort--;
80 void
81 subproc_signals_cleanup(int argc, void **argv)
83 size_t i;
85 for (i = 0; i < array_count(signo_ignores); i++)
86 subproc_reset_signal(signo_ignores[i], &sa_save[i]);
89 void
90 subproc_signals_restore(void)
92 pop_cleanup(ehflag_normaltidy);
95 static void
96 print_subproc_error(const char *emsg, const void *data)
98 fprintf(stderr, _("%s (subprocess): %s\n"), dpkg_get_progname(), emsg);
101 pid_t
102 subproc_fork(void)
104 pid_t pid;
106 pid = fork();
107 if (pid < 0) {
108 onerr_abort++;
109 ohshite(_("fork failed"));
111 if (pid > 0)
112 return pid;
114 /* Push a new error context, so that we don't do the other cleanups,
115 * because they'll be done by/in the parent process. */
116 push_error_context_func(catch_fatal_error, print_subproc_error, NULL);
118 return pid;
121 static int
122 subproc_check(int status, const char *desc, enum subproc_flags flags)
124 void (*out)(const char *fmt, ...) DPKG_ATTR_PRINTF(1);
125 int n;
127 if (flags & SUBPROC_WARN)
128 out = warning;
129 else
130 out = ohshit;
132 if (WIFEXITED(status)) {
133 n = WEXITSTATUS(status);
134 if (!n)
135 return 0;
136 if (flags & SUBPROC_RETERROR)
137 return n;
139 out(_("%s subprocess returned error exit status %d"), desc, n);
140 } else if (WIFSIGNALED(status)) {
141 n = WTERMSIG(status);
142 if (!n)
143 return 0;
144 if ((flags & SUBPROC_NOPIPE) && n == SIGPIPE)
145 return 0;
146 if (flags & SUBPROC_RETSIGNO)
147 return n;
149 if (n == SIGINT)
150 out(_("%s subprocess was interrupted"), desc);
151 else
152 out(_("%s subprocess was killed by signal (%s)%s"),
153 desc, strsignal(n),
154 WCOREDUMP(status) ? _(", core dumped") : "");
155 } else {
156 if (flags & SUBPROC_RETERROR)
157 return -1;
159 out(_("%s subprocess failed with wait status code %d"), desc,
160 status);
163 return -1;
166 static int
167 subproc_wait(pid_t pid, const char *desc)
169 pid_t dead_pid;
170 int status;
172 while ((dead_pid = waitpid(pid, &status, 0)) < 0 && errno == EINTR) ;
174 if (dead_pid != pid) {
175 onerr_abort++;
176 ohshite(_("wait for %s subprocess failed"), desc);
179 return status;
183 subproc_reap(pid_t pid, const char *desc, enum subproc_flags flags)
185 int status, rc;
187 status = subproc_wait(pid, desc);
189 if (flags & SUBPROC_NOCHECK)
190 rc = status;
191 else
192 rc = subproc_check(status, desc, flags);
194 return rc;