ctdb-common: Add some missing #includes
[Samba.git] / ctdb / common / system_util.c
blob692bc25623a325d5d46f4ed1ca01a6ee2f511728
1 /*
2 common system utilities
4 Copyright (C) Amitay Isaacs 2014
5 Copyright (C) Martin Schwenke 2014
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.
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.
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/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "system/shmem.h"
25 #include <libgen.h>
27 #include "ctdb_private.h"
29 #if HAVE_SCHED_H
30 #include <sched.h>
31 #endif
33 #if HAVE_PROCINFO_H
34 #include <procinfo.h>
35 #endif
38 if possible, make this task real time
40 void set_scheduler(void)
42 #ifdef _AIX_
43 #if HAVE_THREAD_SETSCHED
44 struct thrdentry64 te;
45 tid64_t ti;
47 ti = 0ULL;
48 if (getthrds64(getpid(), &te, sizeof(te), &ti, 1) != 1) {
49 DEBUG(DEBUG_ERR, ("Unable to get thread information\n"));
50 return;
53 if (thread_setsched(te.ti_tid, 0, SCHED_RR) == -1) {
54 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_RR (%s)\n",
55 strerror(errno)));
56 } else {
57 DEBUG(DEBUG_NOTICE, ("Set scheduler to SCHED_RR\n"));
59 #endif
60 #else /* no AIX */
61 #if HAVE_SCHED_SETSCHEDULER
62 struct sched_param p;
63 int policy = SCHED_FIFO;
65 p.sched_priority = 1;
67 #ifdef SCHED_RESET_ON_FORK
68 policy |= SCHED_RESET_ON_FORK;
69 #endif
70 if (sched_setscheduler(0, policy, &p) == -1) {
71 DEBUG(DEBUG_CRIT,("Unable to set scheduler to SCHED_FIFO (%s)\n",
72 strerror(errno)));
73 } else {
74 DEBUG(DEBUG_NOTICE,("Set scheduler to SCHED_FIFO\n"));
76 #endif
77 #endif
81 reset scheduler from real-time to normal scheduling
83 void reset_scheduler(void)
85 #ifdef _AIX_
86 #if HAVE_THREAD_SETSCHED
87 struct thrdentry64 te;
88 tid64_t ti;
90 ti = 0ULL;
91 if (getthrds64(getpid(), &te, sizeof(te), &ti, 1) != 1) {
92 DEBUG(DEBUG_ERR, ("Unable to get thread information\n"));
94 if (thread_setsched(te.ti_tid, 0, SCHED_OTHER) == -1) {
95 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n"));
97 #endif
98 #else /* no AIX */
99 #if HAVE_SCHED_SETSCHEDULER
100 #ifndef SCHED_RESET_ON_FORK
101 struct sched_param p;
103 p.sched_priority = 0;
104 if (sched_setscheduler(0, SCHED_OTHER, &p) == -1) {
105 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n"));
107 #endif
108 #endif
109 #endif
112 void set_nonblocking(int fd)
114 int v;
116 v = fcntl(fd, F_GETFL, 0);
117 if (v == -1) {
118 DEBUG(DEBUG_WARNING, ("Failed to get file status flags - %s\n",
119 strerror(errno)));
120 return;
122 if (fcntl(fd, F_SETFL, v | O_NONBLOCK) == -1) {
123 DEBUG(DEBUG_WARNING, ("Failed to set non_blocking on fd - %s\n",
124 strerror(errno)));
128 void set_close_on_exec(int fd)
130 int v;
132 v = fcntl(fd, F_GETFD, 0);
133 if (v == -1) {
134 DEBUG(DEBUG_WARNING, ("Failed to get file descriptor flags - %s\n",
135 strerror(errno)));
136 return;
138 if (fcntl(fd, F_SETFD, v | FD_CLOEXEC) != 0) {
139 DEBUG(DEBUG_WARNING, ("Failed to set close_on_exec on fd - %s\n",
140 strerror(errno)));
145 bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin)
147 sin->sin_family = AF_INET;
148 sin->sin_port = htons(port);
150 if (inet_pton(AF_INET, s, &sin->sin_addr) != 1) {
151 DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin_addr\n", s));
152 return false;
155 return true;
158 static bool parse_ipv6(const char *s, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
160 saddr->ip6.sin6_family = AF_INET6;
161 saddr->ip6.sin6_port = htons(port);
162 saddr->ip6.sin6_flowinfo = 0;
163 saddr->ip6.sin6_scope_id = 0;
165 if (inet_pton(AF_INET6, s, &saddr->ip6.sin6_addr) != 1) {
166 DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin6_addr\n", s));
167 return false;
170 if (ifaces && IN6_IS_ADDR_LINKLOCAL(&saddr->ip6.sin6_addr)) {
171 if (strchr(ifaces, ',')) {
172 DEBUG(DEBUG_ERR, (__location__ " Link local address %s "
173 "is specified for multiple ifaces %s\n",
174 s, ifaces));
175 return false;
177 saddr->ip6.sin6_scope_id = if_nametoindex(ifaces);
180 return true;
184 parse an ip
186 bool parse_ip(const char *addr, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
188 char *p;
189 bool ret;
191 ZERO_STRUCTP(saddr); /* valgrind :-) */
193 /* now is this a ipv4 or ipv6 address ?*/
194 p = index(addr, ':');
195 if (p == NULL) {
196 ret = parse_ipv4(addr, port, &saddr->ip);
197 } else {
198 ret = parse_ipv6(addr, ifaces, port, saddr);
201 return ret;
205 parse a ip/mask pair
207 bool parse_ip_mask(const char *str, const char *ifaces, ctdb_sock_addr *addr, unsigned *mask)
209 char *p;
210 char s[64]; /* Much longer than INET6_ADDRSTRLEN */
211 char *endp = NULL;
212 ssize_t len;
213 bool ret;
215 ZERO_STRUCT(*addr);
217 len = strlen(str);
218 if (len >= sizeof(s)) {
219 DEBUG(DEBUG_ERR, ("Address %s is unreasonably long\n", str));
220 return false;
223 strncpy(s, str, len+1);
225 p = rindex(s, '/');
226 if (p == NULL) {
227 DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a mask\n", s));
228 return false;
231 *mask = strtoul(p+1, &endp, 10);
232 if (endp == NULL || *endp != 0) {
233 /* trailing garbage */
234 DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the mask in %s\n", s));
235 return false;
237 *p = 0;
240 /* now is this a ipv4 or ipv6 address ?*/
241 ret = parse_ip(s, ifaces, 0, addr);
243 return ret;
247 parse a ip:port pair
249 bool parse_ip_port(const char *addr, ctdb_sock_addr *saddr)
251 char *p;
252 char s[64]; /* Much longer than INET6_ADDRSTRLEN */
253 unsigned port;
254 char *endp = NULL;
255 ssize_t len;
256 bool ret;
258 len = strlen(addr);
259 if (len >= sizeof(s)) {
260 DEBUG(DEBUG_ERR, ("Address %s is unreasonably long\n", addr));
261 return false;
264 strncpy(s, addr, len+1);
266 p = rindex(s, ':');
267 if (p == NULL) {
268 DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a port number\n", s));
269 return false;
272 port = strtoul(p+1, &endp, 10);
273 if (endp == NULL || *endp != 0) {
274 /* trailing garbage */
275 DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the port in %s\n", s));
276 return false;
278 *p = 0;
280 /* now is this a ipv4 or ipv6 address ?*/
281 ret = parse_ip(s, NULL, port, saddr);
283 return ret;
286 /* we don't lock future pages here; it would increase the chance that
287 * we'd fail to mmap later on. */
288 void lockdown_memory(bool valgrinding)
290 #if defined(HAVE_MLOCKALL) && !defined(_AIX_)
291 /* Extra stack, please! */
292 char dummy[10000];
293 memset(dummy, 0, sizeof(dummy));
295 if (valgrinding) {
296 return;
299 /* Ignore when running in local daemons mode */
300 if (getuid() != 0) {
301 return;
304 /* Avoid compiler optimizing out dummy. */
305 mlock(dummy, sizeof(dummy));
306 if (mlockall(MCL_CURRENT) != 0) {
307 DEBUG(DEBUG_WARNING,("Failed to lockdown memory: %s'\n",
308 strerror(errno)));
310 #endif
313 int mkdir_p(const char *dir, int mode)
315 char t[PATH_MAX];
316 ssize_t len;
317 int ret;
319 if (strcmp(dir, "/") == 0) {
320 return 0;
323 if (strcmp(dir, ".") == 0) {
324 return 0;
327 /* Try to create directory */
328 ret = mkdir(dir, mode);
329 /* Succeed if that worked or if it already existed */
330 if (ret == 0 || errno == EEXIST) {
331 return 0;
333 /* Fail on anything else except ENOENT */
334 if (errno != ENOENT) {
335 return ret;
338 /* Create ancestors */
339 len = strlen(dir);
340 if (len >= PATH_MAX) {
341 errno = ENAMETOOLONG;
342 return -1;
344 strncpy(t, dir, len+1);
346 ret = mkdir_p(dirname(t), mode);
347 if (ret != 0) {
348 return ret;
351 /* Create directory */
352 ret = mkdir(dir, mode);
353 if ((ret == -1) && (errno == EEXIST)) {
354 ret = 0;
357 return ret;
360 void mkdir_p_or_die(const char *dir, int mode)
362 int ret;
364 ret = mkdir_p(dir, mode);
365 if (ret != 0) {
366 DEBUG(DEBUG_ALERT,
367 ("ctdb exiting with error: "
368 "failed to create directory \"%s\" (%s)\n",
369 dir, strerror(errno)));
370 exit(1);
374 /* A read wrapper that will deal with EINTR. For now, copied from
375 * source3/lib/system.c
377 ssize_t sys_read(int fd, void *buf, size_t count)
379 ssize_t ret;
381 do {
382 ret = read(fd, buf, count);
383 #if defined(EWOULDBLOCK)
384 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
385 #else
386 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
387 #endif
388 return ret;
391 /* A write wrapper that will deal with EINTR. For now, copied from
392 * source3/lib/system.c
394 ssize_t sys_write(int fd, const void *buf, size_t count)
396 ssize_t ret;
398 do {
399 ret = write(fd, buf, count);
400 #if defined(EWOULDBLOCK)
401 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
402 #else
403 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
404 #endif
405 return ret;