1 | /*
|
---|
2 | common system utilities
|
---|
3 |
|
---|
4 | Copyright (C) Amitay Isaacs 2014
|
---|
5 | Copyright (C) Martin Schwenke 2014
|
---|
6 |
|
---|
7 | This program is free software; you can redistribute it and/or modify
|
---|
8 | it under the terms of the GNU General Public License as published by
|
---|
9 | the Free Software Foundation; either version 3 of the License, or
|
---|
10 | (at your option) any later version.
|
---|
11 |
|
---|
12 | This program is distributed in the hope that it will be useful,
|
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | GNU General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU General Public License
|
---|
18 | along with this program; if not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "replace.h"
|
---|
22 | #include "system/filesys.h"
|
---|
23 | #include "system/shmem.h"
|
---|
24 | #include "system/network.h"
|
---|
25 |
|
---|
26 | #include <libgen.h>
|
---|
27 |
|
---|
28 | #include "lib/util/debug.h"
|
---|
29 |
|
---|
30 | #include "protocol/protocol.h"
|
---|
31 |
|
---|
32 | #include "common/logging.h"
|
---|
33 | #include "common/system.h"
|
---|
34 |
|
---|
35 | #if HAVE_SCHED_H
|
---|
36 | #include <sched.h>
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | #if HAVE_PROCINFO_H
|
---|
40 | #include <procinfo.h>
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | /*
|
---|
44 | if possible, make this task real time
|
---|
45 | */
|
---|
46 | bool set_scheduler(void)
|
---|
47 | {
|
---|
48 | #ifdef _AIX_
|
---|
49 | #if HAVE_THREAD_SETSCHED
|
---|
50 | struct thrdentry64 te;
|
---|
51 | tid64_t ti;
|
---|
52 |
|
---|
53 | ti = 0ULL;
|
---|
54 | if (getthrds64(getpid(), &te, sizeof(te), &ti, 1) != 1) {
|
---|
55 | DEBUG(DEBUG_ERR, ("Unable to get thread information\n"));
|
---|
56 | return false;
|
---|
57 | }
|
---|
58 |
|
---|
59 | if (thread_setsched(te.ti_tid, 0, SCHED_RR) == -1) {
|
---|
60 | DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_RR (%s)\n",
|
---|
61 | strerror(errno)));
|
---|
62 | return false;
|
---|
63 | } else {
|
---|
64 | return true;
|
---|
65 | }
|
---|
66 | #endif
|
---|
67 | #else /* no AIX */
|
---|
68 | #if HAVE_SCHED_SETSCHEDULER
|
---|
69 | struct sched_param p;
|
---|
70 | int policy = SCHED_FIFO;
|
---|
71 |
|
---|
72 | p.sched_priority = 1;
|
---|
73 |
|
---|
74 | #ifdef SCHED_RESET_ON_FORK
|
---|
75 | policy |= SCHED_RESET_ON_FORK;
|
---|
76 | #endif
|
---|
77 | if (sched_setscheduler(0, policy, &p) == -1) {
|
---|
78 | DEBUG(DEBUG_CRIT,("Unable to set scheduler to SCHED_FIFO (%s)\n",
|
---|
79 | strerror(errno)));
|
---|
80 | return false;
|
---|
81 | } else {
|
---|
82 | return true;
|
---|
83 | }
|
---|
84 | #endif
|
---|
85 | #endif
|
---|
86 | DEBUG(DEBUG_CRIT,("No way to set real-time priority.\n"));
|
---|
87 | return false;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /*
|
---|
91 | reset scheduler from real-time to normal scheduling
|
---|
92 | */
|
---|
93 | void reset_scheduler(void)
|
---|
94 | {
|
---|
95 | #ifdef _AIX_
|
---|
96 | #if HAVE_THREAD_SETSCHED
|
---|
97 | struct thrdentry64 te;
|
---|
98 | tid64_t ti;
|
---|
99 |
|
---|
100 | ti = 0ULL;
|
---|
101 | if (getthrds64(getpid(), &te, sizeof(te), &ti, 1) != 1) {
|
---|
102 | DEBUG(DEBUG_ERR, ("Unable to get thread information\n"));
|
---|
103 | }
|
---|
104 | if (thread_setsched(te.ti_tid, 0, SCHED_OTHER) == -1) {
|
---|
105 | DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n"));
|
---|
106 | }
|
---|
107 | #endif
|
---|
108 | #else /* no AIX */
|
---|
109 | #if HAVE_SCHED_SETSCHEDULER
|
---|
110 | #ifndef SCHED_RESET_ON_FORK
|
---|
111 | struct sched_param p;
|
---|
112 |
|
---|
113 | p.sched_priority = 0;
|
---|
114 | if (sched_setscheduler(0, SCHED_OTHER, &p) == -1) {
|
---|
115 | DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n"));
|
---|
116 | }
|
---|
117 | #endif
|
---|
118 | #endif
|
---|
119 | #endif
|
---|
120 | }
|
---|
121 |
|
---|
122 | void set_nonblocking(int fd)
|
---|
123 | {
|
---|
124 | int v;
|
---|
125 |
|
---|
126 | v = fcntl(fd, F_GETFL, 0);
|
---|
127 | if (v == -1) {
|
---|
128 | DEBUG(DEBUG_WARNING, ("Failed to get file status flags - %s\n",
|
---|
129 | strerror(errno)));
|
---|
130 | return;
|
---|
131 | }
|
---|
132 | if (fcntl(fd, F_SETFL, v | O_NONBLOCK) == -1) {
|
---|
133 | DEBUG(DEBUG_WARNING, ("Failed to set non_blocking on fd - %s\n",
|
---|
134 | strerror(errno)));
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | void set_close_on_exec(int fd)
|
---|
139 | {
|
---|
140 | int v;
|
---|
141 |
|
---|
142 | v = fcntl(fd, F_GETFD, 0);
|
---|
143 | if (v == -1) {
|
---|
144 | DEBUG(DEBUG_WARNING, ("Failed to get file descriptor flags - %s\n",
|
---|
145 | strerror(errno)));
|
---|
146 | return;
|
---|
147 | }
|
---|
148 | if (fcntl(fd, F_SETFD, v | FD_CLOEXEC) != 0) {
|
---|
149 | DEBUG(DEBUG_WARNING, ("Failed to set close_on_exec on fd - %s\n",
|
---|
150 | strerror(errno)));
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin)
|
---|
156 | {
|
---|
157 | sin->sin_family = AF_INET;
|
---|
158 | sin->sin_port = htons(port);
|
---|
159 |
|
---|
160 | if (inet_pton(AF_INET, s, &sin->sin_addr) != 1) {
|
---|
161 | DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin_addr\n", s));
|
---|
162 | return false;
|
---|
163 | }
|
---|
164 |
|
---|
165 | #ifdef HAVE_SOCK_SIN_LEN
|
---|
166 | sin->sin_len = sizeof(*sin);
|
---|
167 | #endif
|
---|
168 | return true;
|
---|
169 | }
|
---|
170 |
|
---|
171 | static bool parse_ipv6(const char *s, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
|
---|
172 | {
|
---|
173 | saddr->ip6.sin6_family = AF_INET6;
|
---|
174 | saddr->ip6.sin6_port = htons(port);
|
---|
175 | saddr->ip6.sin6_flowinfo = 0;
|
---|
176 | saddr->ip6.sin6_scope_id = 0;
|
---|
177 |
|
---|
178 | if (inet_pton(AF_INET6, s, &saddr->ip6.sin6_addr) != 1) {
|
---|
179 | DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin6_addr\n", s));
|
---|
180 | return false;
|
---|
181 | }
|
---|
182 |
|
---|
183 | if (ifaces && IN6_IS_ADDR_LINKLOCAL(&saddr->ip6.sin6_addr)) {
|
---|
184 | if (strchr(ifaces, ',')) {
|
---|
185 | DEBUG(DEBUG_ERR, (__location__ " Link local address %s "
|
---|
186 | "is specified for multiple ifaces %s\n",
|
---|
187 | s, ifaces));
|
---|
188 | return false;
|
---|
189 | }
|
---|
190 | saddr->ip6.sin6_scope_id = if_nametoindex(ifaces);
|
---|
191 | }
|
---|
192 |
|
---|
193 | #ifdef HAVE_SOCK_SIN_LEN
|
---|
194 | saddr->ip6.sin6_len = sizeof(*saddr);
|
---|
195 | #endif
|
---|
196 | return true;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /*
|
---|
200 | parse an ip
|
---|
201 | */
|
---|
202 | bool parse_ip(const char *addr, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
|
---|
203 | {
|
---|
204 | char *p;
|
---|
205 | bool ret;
|
---|
206 |
|
---|
207 | ZERO_STRUCTP(saddr); /* valgrind :-) */
|
---|
208 |
|
---|
209 | /* now is this a ipv4 or ipv6 address ?*/
|
---|
210 | p = index(addr, ':');
|
---|
211 | if (p == NULL) {
|
---|
212 | ret = parse_ipv4(addr, port, &saddr->ip);
|
---|
213 | } else {
|
---|
214 | ret = parse_ipv6(addr, ifaces, port, saddr);
|
---|
215 | }
|
---|
216 |
|
---|
217 | return ret;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /*
|
---|
221 | parse a ip/mask pair
|
---|
222 | */
|
---|
223 | bool parse_ip_mask(const char *str, const char *ifaces, ctdb_sock_addr *addr, unsigned *mask)
|
---|
224 | {
|
---|
225 | char *p;
|
---|
226 | char s[64]; /* Much longer than INET6_ADDRSTRLEN */
|
---|
227 | char *endp = NULL;
|
---|
228 | ssize_t len;
|
---|
229 | bool ret;
|
---|
230 |
|
---|
231 | ZERO_STRUCT(*addr);
|
---|
232 |
|
---|
233 | len = strlen(str);
|
---|
234 | if (len >= sizeof(s)) {
|
---|
235 | DEBUG(DEBUG_ERR, ("Address %s is unreasonably long\n", str));
|
---|
236 | return false;
|
---|
237 | }
|
---|
238 |
|
---|
239 | strncpy(s, str, len+1);
|
---|
240 |
|
---|
241 | p = rindex(s, '/');
|
---|
242 | if (p == NULL) {
|
---|
243 | DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a mask\n", s));
|
---|
244 | return false;
|
---|
245 | }
|
---|
246 |
|
---|
247 | *mask = strtoul(p+1, &endp, 10);
|
---|
248 | if (endp == NULL || *endp != 0) {
|
---|
249 | /* trailing garbage */
|
---|
250 | DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the mask in %s\n", s));
|
---|
251 | return false;
|
---|
252 | }
|
---|
253 | *p = 0;
|
---|
254 |
|
---|
255 |
|
---|
256 | /* now is this a ipv4 or ipv6 address ?*/
|
---|
257 | ret = parse_ip(s, ifaces, 0, addr);
|
---|
258 |
|
---|
259 | return ret;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /*
|
---|
263 | parse a ip:port pair
|
---|
264 | */
|
---|
265 | bool parse_ip_port(const char *addr, ctdb_sock_addr *saddr)
|
---|
266 | {
|
---|
267 | char *p;
|
---|
268 | char s[64]; /* Much longer than INET6_ADDRSTRLEN */
|
---|
269 | unsigned port;
|
---|
270 | char *endp = NULL;
|
---|
271 | ssize_t len;
|
---|
272 | bool ret;
|
---|
273 |
|
---|
274 | len = strlen(addr);
|
---|
275 | if (len >= sizeof(s)) {
|
---|
276 | DEBUG(DEBUG_ERR, ("Address %s is unreasonably long\n", addr));
|
---|
277 | return false;
|
---|
278 | }
|
---|
279 |
|
---|
280 | strncpy(s, addr, len+1);
|
---|
281 |
|
---|
282 | p = rindex(s, ':');
|
---|
283 | if (p == NULL) {
|
---|
284 | DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a port number\n", s));
|
---|
285 | return false;
|
---|
286 | }
|
---|
287 |
|
---|
288 | port = strtoul(p+1, &endp, 10);
|
---|
289 | if (endp == NULL || *endp != 0) {
|
---|
290 | /* trailing garbage */
|
---|
291 | DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the port in %s\n", s));
|
---|
292 | return false;
|
---|
293 | }
|
---|
294 | *p = 0;
|
---|
295 |
|
---|
296 | /* now is this a ipv4 or ipv6 address ?*/
|
---|
297 | ret = parse_ip(s, NULL, port, saddr);
|
---|
298 |
|
---|
299 | return ret;
|
---|
300 | }
|
---|
301 |
|
---|
302 | /* we don't lock future pages here; it would increase the chance that
|
---|
303 | * we'd fail to mmap later on. */
|
---|
304 | void lockdown_memory(bool valgrinding)
|
---|
305 | {
|
---|
306 | #if defined(HAVE_MLOCKALL) && !defined(_AIX_)
|
---|
307 | /* Extra stack, please! */
|
---|
308 | char dummy[10000];
|
---|
309 | memset(dummy, 0, sizeof(dummy));
|
---|
310 |
|
---|
311 | if (valgrinding) {
|
---|
312 | return;
|
---|
313 | }
|
---|
314 |
|
---|
315 | /* Ignore when running in local daemons mode */
|
---|
316 | if (getuid() != 0) {
|
---|
317 | return;
|
---|
318 | }
|
---|
319 |
|
---|
320 | /* Avoid compiler optimizing out dummy. */
|
---|
321 | mlock(dummy, sizeof(dummy));
|
---|
322 | if (mlockall(MCL_CURRENT) != 0) {
|
---|
323 | DEBUG(DEBUG_WARNING,("Failed to lockdown memory: %s'\n",
|
---|
324 | strerror(errno)));
|
---|
325 | }
|
---|
326 | #endif
|
---|
327 | }
|
---|
328 |
|
---|
329 | int mkdir_p(const char *dir, int mode)
|
---|
330 | {
|
---|
331 | char t[PATH_MAX];
|
---|
332 | ssize_t len;
|
---|
333 | int ret;
|
---|
334 |
|
---|
335 | if (strcmp(dir, "/") == 0) {
|
---|
336 | return 0;
|
---|
337 | }
|
---|
338 |
|
---|
339 | if (strcmp(dir, ".") == 0) {
|
---|
340 | return 0;
|
---|
341 | }
|
---|
342 |
|
---|
343 | /* Try to create directory */
|
---|
344 | ret = mkdir(dir, mode);
|
---|
345 | /* Succeed if that worked or if it already existed */
|
---|
346 | if (ret == 0 || errno == EEXIST) {
|
---|
347 | return 0;
|
---|
348 | }
|
---|
349 | /* Fail on anything else except ENOENT */
|
---|
350 | if (errno != ENOENT) {
|
---|
351 | return ret;
|
---|
352 | }
|
---|
353 |
|
---|
354 | /* Create ancestors */
|
---|
355 | len = strlen(dir);
|
---|
356 | if (len >= PATH_MAX) {
|
---|
357 | errno = ENAMETOOLONG;
|
---|
358 | return -1;
|
---|
359 | }
|
---|
360 | strncpy(t, dir, len+1);
|
---|
361 |
|
---|
362 | ret = mkdir_p(dirname(t), mode);
|
---|
363 | if (ret != 0) {
|
---|
364 | return ret;
|
---|
365 | }
|
---|
366 |
|
---|
367 | /* Create directory */
|
---|
368 | ret = mkdir(dir, mode);
|
---|
369 | if ((ret == -1) && (errno == EEXIST)) {
|
---|
370 | ret = 0;
|
---|
371 | }
|
---|
372 |
|
---|
373 | return ret;
|
---|
374 | }
|
---|
375 |
|
---|
376 | void mkdir_p_or_die(const char *dir, int mode)
|
---|
377 | {
|
---|
378 | int ret;
|
---|
379 |
|
---|
380 | ret = mkdir_p(dir, mode);
|
---|
381 | if (ret != 0) {
|
---|
382 | DEBUG(DEBUG_ALERT,
|
---|
383 | ("ctdb exiting with error: "
|
---|
384 | "failed to create directory \"%s\" (%s)\n",
|
---|
385 | dir, strerror(errno)));
|
---|
386 | exit(1);
|
---|
387 | }
|
---|
388 | }
|
---|
389 |
|
---|
390 | /* A read wrapper that will deal with EINTR. For now, copied from
|
---|
391 | * source3/lib/system.c
|
---|
392 | */
|
---|
393 | ssize_t sys_read(int fd, void *buf, size_t count)
|
---|
394 | {
|
---|
395 | ssize_t ret;
|
---|
396 |
|
---|
397 | do {
|
---|
398 | ret = read(fd, buf, count);
|
---|
399 | #if defined(EWOULDBLOCK)
|
---|
400 | } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
|
---|
401 | #else
|
---|
402 | } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
|
---|
403 | #endif
|
---|
404 | return ret;
|
---|
405 | }
|
---|
406 |
|
---|
407 | /* A write wrapper that will deal with EINTR. For now, copied from
|
---|
408 | * source3/lib/system.c
|
---|
409 | */
|
---|
410 | ssize_t sys_write(int fd, const void *buf, size_t count)
|
---|
411 | {
|
---|
412 | ssize_t ret;
|
---|
413 |
|
---|
414 | do {
|
---|
415 | ret = write(fd, buf, count);
|
---|
416 | #if defined(EWOULDBLOCK)
|
---|
417 | } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
|
---|
418 | #else
|
---|
419 | } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
|
---|
420 | #endif
|
---|
421 | return ret;
|
---|
422 | }
|
---|