Branch data Line data Source code
1 : : /*
2 : : * libdpkg - Debian packaging suite library routines
3 : : * mustlib.c - ‘must’ library: routines will succeed or longjmp
4 : : *
5 : : * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 : : * Copyright © 2006-2013, 2015 Guillem Jover <guillem@debian.org>
7 : : *
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.
12 : : *
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.
17 : : *
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/>.
20 : : */
21 : :
22 : : #include <config.h>
23 : : #include <compat.h>
24 : :
25 : : #include <sys/types.h>
26 : :
27 : : #include <errno.h>
28 : : #include <string.h>
29 : : #include <fcntl.h>
30 : : #include <unistd.h>
31 : : #include <stdlib.h>
32 : : #include <stdio.h>
33 : :
34 : : #include <dpkg/i18n.h>
35 : : #include <dpkg/dpkg.h>
36 : :
37 : : static inline void *
38 : 3801 : must_alloc(void *ptr)
39 : : {
40 [ + - ]: 3801 : if (ptr)
41 : 3801 : return ptr;
42 : :
43 : 0 : onerr_abort++;
44 : 0 : ohshite(_("failed to allocate memory"));
45 : : }
46 : :
47 : 1209 : void *m_malloc(size_t amount) {
48 : 1209 : return must_alloc(malloc(amount));
49 : : }
50 : :
51 : : void *
52 : 216 : m_calloc(size_t nmemb, size_t size)
53 : : {
54 : 216 : return must_alloc(calloc(nmemb, size));
55 : : }
56 : :
57 : 1031 : void *m_realloc(void *r, size_t amount) {
58 : 1031 : return must_alloc(realloc(r, amount));
59 : : }
60 : :
61 : : char *
62 : 1093 : m_strdup(const char *str)
63 : : {
64 : 1093 : return must_alloc(strdup(str));
65 : : }
66 : :
67 : : char *
68 : 252 : m_strndup(const char *str, size_t n)
69 : : {
70 : 252 : return must_alloc(strndup(str, n));
71 : : }
72 : :
73 : : int
74 : 1789 : m_vasprintf(char **strp, const char *fmt, va_list args)
75 : : {
76 : : int n;
77 : :
78 : 1789 : n = vasprintf(strp, fmt, args);
79 [ + - ]: 1789 : if (n >= 0)
80 : 1789 : return n;
81 : :
82 : 0 : onerr_abort++;
83 : 0 : ohshite(_("failed to allocate memory"));
84 : : }
85 : :
86 : : int
87 : 0 : m_asprintf(char **strp, const char *fmt, ...)
88 : : {
89 : : va_list args;
90 : : int n;
91 : :
92 : 0 : va_start(args, fmt);
93 : 0 : n = m_vasprintf(strp, fmt, args);
94 : 0 : va_end(args);
95 : :
96 : 0 : return n;
97 : : }
98 : :
99 : : int
100 : 0 : m_dup(int oldfd)
101 : : {
102 : : int newfd;
103 : :
104 : 0 : newfd = dup(oldfd);
105 [ # # ]: 0 : if (newfd >= 0)
106 : 0 : return newfd;
107 : :
108 : 0 : onerr_abort++;
109 : 0 : ohshite(_("failed to dup for fd %d"), oldfd);
110 : : }
111 : :
112 : 100 : void m_dup2(int oldfd, int newfd) {
113 : 100 : const char *const stdstrings[]= { "in", "out", "err" };
114 : :
115 [ + - ]: 100 : if (dup2(oldfd,newfd) == newfd) return;
116 : :
117 : 0 : onerr_abort++;
118 [ # # ]: 0 : if (newfd < 3) ohshite(_("failed to dup for std%s"),stdstrings[newfd]);
119 : 0 : ohshite(_("failed to dup for fd %d"),newfd);
120 : : }
121 : :
122 : : void
123 : 137 : m_pipe(int fds[2])
124 : : {
125 [ + - ]: 137 : if (!pipe(fds)) return;
126 : 0 : onerr_abort++;
127 : 0 : ohshite(_("failed to create pipe"));
128 : : }
129 : :
130 : : void
131 : 292 : m_output(FILE *f, const char *name)
132 : : {
133 : 292 : fflush(f);
134 [ - + - - ]: 292 : if (ferror(f) && errno != EPIPE)
135 : 0 : ohshite(_("error writing to '%s'"), name);
136 : 292 : }
137 : :
138 : : void
139 : 93 : setcloexec(int fd, const char *fn)
140 : : {
141 : : int f;
142 : :
143 : 93 : f = fcntl(fd, F_GETFD);
144 [ - + ]: 93 : if (f < 0)
145 : 0 : ohshite(_("unable to read filedescriptor flags for %.250s"),fn);
146 [ - + ]: 93 : if (fcntl(fd, F_SETFD, (f | FD_CLOEXEC)) < 0)
147 : 0 : ohshite(_("unable to set close-on-exec flag for %.250s"),fn);
148 : 93 : }
|