Commit | Line | Data |
---|---|---|
1479465f GJ |
1 | /* |
2 | * libdpkg - Debian packaging suite library routines | |
3 | * mlib.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 <string.h> | |
28 | #include <fcntl.h> | |
29 | #include <unistd.h> | |
30 | #include <stdlib.h> | |
31 | #include <stdio.h> | |
32 | ||
33 | #include <dpkg/i18n.h> | |
34 | #include <dpkg/dpkg.h> | |
35 | ||
36 | static inline void * | |
37 | must_alloc(void *ptr) | |
38 | { | |
39 | if (ptr) | |
40 | return ptr; | |
41 | ||
42 | onerr_abort++; | |
43 | ohshite(_("failed to allocate memory")); | |
44 | } | |
45 | ||
46 | void *m_malloc(size_t amount) { | |
47 | #ifdef MDEBUG | |
48 | unsigned short *ptr_canary, canary; | |
49 | #endif | |
50 | void *ptr; | |
51 | ||
52 | ptr = must_alloc(malloc(amount)); | |
53 | ||
54 | #ifdef MDEBUG | |
55 | ptr_canary = ptr; | |
56 | canary = (unsigned short)amount ^ 0xf000; | |
57 | while (amount >= 2) { | |
58 | *ptr_canary++ = canary; | |
59 | amount -= 2; | |
60 | } | |
61 | #endif | |
62 | return ptr; | |
63 | } | |
64 | ||
65 | void * | |
66 | m_calloc(size_t nmemb, size_t size) | |
67 | { | |
68 | return must_alloc(calloc(nmemb, size)); | |
69 | } | |
70 | ||
71 | void *m_realloc(void *r, size_t amount) { | |
72 | return must_alloc(realloc(r, amount)); | |
73 | } | |
74 | ||
75 | char * | |
76 | m_strdup(const char *str) | |
77 | { | |
78 | return must_alloc(strdup(str)); | |
79 | } | |
80 | ||
81 | char * | |
82 | m_strndup(const char *str, size_t n) | |
83 | { | |
84 | return must_alloc(strndup(str, n)); | |
85 | } | |
86 | ||
87 | int | |
88 | m_vasprintf(char **strp, const char *fmt, va_list args) | |
89 | { | |
90 | int n; | |
91 | ||
92 | n = vasprintf(strp, fmt, args); | |
93 | if (n >= 0) | |
94 | return n; | |
95 | ||
96 | onerr_abort++; | |
97 | ohshite(_("failed to allocate memory")); | |
98 | } | |
99 | ||
100 | int | |
101 | m_asprintf(char **strp, const char *fmt, ...) | |
102 | { | |
103 | va_list args; | |
104 | int n; | |
105 | ||
106 | va_start(args, fmt); | |
107 | n = m_vasprintf(strp, fmt, args); | |
108 | va_end(args); | |
109 | ||
110 | return n; | |
111 | } | |
112 | ||
113 | void m_dup2(int oldfd, int newfd) { | |
114 | const char *const stdstrings[]= { "in", "out", "err" }; | |
115 | ||
116 | if (dup2(oldfd,newfd) == newfd) return; | |
117 | ||
118 | onerr_abort++; | |
119 | if (newfd < 3) ohshite(_("failed to dup for std%s"),stdstrings[newfd]); | |
120 | ohshite(_("failed to dup for fd %d"),newfd); | |
121 | } | |
122 | ||
123 | void m_pipe(int *fds) { | |
124 | if (!pipe(fds)) return; | |
125 | onerr_abort++; | |
126 | ohshite(_("failed to create pipe")); | |
127 | } | |
128 | ||
129 | void | |
130 | m_output(FILE *f, const char *name) | |
131 | { | |
132 | fflush(f); | |
133 | if (ferror(f)) | |
134 | ohshite(_("error writing to '%s'"), name); | |
135 | } | |
136 | ||
137 | void | |
138 | setcloexec(int fd, const char *fn) | |
139 | { | |
140 | int f; | |
141 | ||
142 | f = fcntl(fd, F_GETFD); | |
143 | if (f == -1) | |
144 | ohshite(_("unable to read filedescriptor flags for %.250s"),fn); | |
145 | if (fcntl(fd, F_SETFD, (f|FD_CLOEXEC))==-1) | |
146 | ohshite(_("unable to set close-on-exec flag for %.250s"),fn); | |
147 | } |