ctdb-scripts: Drop function ctdb_check_counter()
[Samba.git] / ctdb / common / system_util.c
blob91bd72eecbdeac1f289f2a3d449b05b8b7271210
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 "replace.h"
22 #include "system/filesys.h"
23 #include "system/shmem.h"
24 #include "system/network.h"
26 #include <talloc.h>
27 #include <libgen.h>
29 #include "lib/util/debug.h"
31 #include "protocol/protocol.h"
33 #include "common/logging.h"
34 #include "common/system.h"
36 #if HAVE_SCHED_H
37 #include <sched.h>
38 #endif
40 #if HAVE_PROCINFO_H
41 #include <procinfo.h>
42 #endif
45 if possible, make this task real time
47 bool set_scheduler(void)
49 #ifdef _AIX_
50 #if HAVE_THREAD_SETSCHED
51 struct thrdentry64 te;
52 tid64_t ti;
54 ti = 0ULL;
55 if (getthrds64(getpid(), &te, sizeof(te), &ti, 1) != 1) {
56 DEBUG(DEBUG_ERR, ("Unable to get thread information\n"));
57 return false;
60 if (thread_setsched(te.ti_tid, 0, SCHED_RR) == -1) {
61 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_RR (%s)\n",
62 strerror(errno)));
63 return false;
64 } else {
65 return true;
67 #endif
68 #else /* no AIX */
69 #if HAVE_SCHED_SETSCHEDULER
70 struct sched_param p;
71 int policy = SCHED_FIFO;
73 p.sched_priority = 1;
75 #ifdef SCHED_RESET_ON_FORK
76 policy |= SCHED_RESET_ON_FORK;
77 #endif
78 if (sched_setscheduler(0, policy, &p) == -1) {
79 DEBUG(DEBUG_CRIT,("Unable to set scheduler to SCHED_FIFO (%s)\n",
80 strerror(errno)));
81 return false;
82 } else {
83 return true;
85 #endif
86 #endif
87 DEBUG(DEBUG_CRIT,("No way to set real-time priority.\n"));
88 return false;
92 reset scheduler from real-time to normal scheduling
94 void reset_scheduler(void)
96 #ifdef _AIX_
97 #if HAVE_THREAD_SETSCHED
98 struct thrdentry64 te;
99 tid64_t ti;
101 ti = 0ULL;
102 if (getthrds64(getpid(), &te, sizeof(te), &ti, 1) != 1) {
103 DEBUG(DEBUG_ERR, ("Unable to get thread information\n"));
105 if (thread_setsched(te.ti_tid, 0, SCHED_OTHER) == -1) {
106 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n"));
108 #endif
109 #else /* no AIX */
110 #if HAVE_SCHED_SETSCHEDULER
111 #ifndef SCHED_RESET_ON_FORK
112 struct sched_param p;
114 p.sched_priority = 0;
115 if (sched_setscheduler(0, SCHED_OTHER, &p) == -1) {
116 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n"));
118 #endif
119 #endif
120 #endif
123 bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin)
125 sin->sin_family = AF_INET;
126 sin->sin_port = htons(port);
128 if (inet_pton(AF_INET, s, &sin->sin_addr) != 1) {
129 DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin_addr\n", s));
130 return false;
133 #ifdef HAVE_SOCK_SIN_LEN
134 sin->sin_len = sizeof(*sin);
135 #endif
136 return true;
139 static bool parse_ipv6(const char *s, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
141 saddr->ip6.sin6_family = AF_INET6;
142 saddr->ip6.sin6_port = htons(port);
143 saddr->ip6.sin6_flowinfo = 0;
144 saddr->ip6.sin6_scope_id = 0;
146 if (inet_pton(AF_INET6, s, &saddr->ip6.sin6_addr) != 1) {
147 DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin6_addr\n", s));
148 return false;
151 if (ifaces && IN6_IS_ADDR_LINKLOCAL(&saddr->ip6.sin6_addr)) {
152 if (strchr(ifaces, ',')) {
153 DEBUG(DEBUG_ERR, (__location__ " Link local address %s "
154 "is specified for multiple ifaces %s\n",
155 s, ifaces));
156 return false;
158 saddr->ip6.sin6_scope_id = if_nametoindex(ifaces);
161 #ifdef HAVE_SOCK_SIN_LEN
162 saddr->ip6.sin6_len = sizeof(*saddr);
163 #endif
164 return true;
168 parse an ip
170 bool parse_ip(const char *addr, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
172 char *p;
173 bool ret;
175 ZERO_STRUCTP(saddr); /* valgrind :-) */
177 /* now is this a ipv4 or ipv6 address ?*/
178 p = index(addr, ':');
179 if (p == NULL) {
180 ret = parse_ipv4(addr, port, &saddr->ip);
181 } else {
182 ret = parse_ipv6(addr, ifaces, port, saddr);
185 return ret;
189 parse a ip/mask pair
191 bool parse_ip_mask(const char *str, const char *ifaces, ctdb_sock_addr *addr, unsigned *mask)
193 char *p;
194 char s[64]; /* Much longer than INET6_ADDRSTRLEN */
195 char *endp = NULL;
196 ssize_t len;
197 bool ret;
199 ZERO_STRUCT(*addr);
201 len = strlen(str);
202 if (len >= sizeof(s)) {
203 DEBUG(DEBUG_ERR, ("Address %s is unreasonably long\n", str));
204 return false;
207 strncpy(s, str, len+1);
209 p = rindex(s, '/');
210 if (p == NULL) {
211 DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a mask\n", s));
212 return false;
215 *mask = strtoul(p+1, &endp, 10);
216 if (endp == NULL || *endp != 0) {
217 /* trailing garbage */
218 DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the mask in %s\n", s));
219 return false;
221 *p = 0;
224 /* now is this a ipv4 or ipv6 address ?*/
225 ret = parse_ip(s, ifaces, 0, addr);
227 return ret;
231 parse a ip:port pair
233 bool parse_ip_port(const char *addr, ctdb_sock_addr *saddr)
235 char *p;
236 char s[64]; /* Much longer than INET6_ADDRSTRLEN */
237 unsigned port;
238 char *endp = NULL;
239 ssize_t len;
240 bool ret;
242 len = strlen(addr);
243 if (len >= sizeof(s)) {
244 DEBUG(DEBUG_ERR, ("Address %s is unreasonably long\n", addr));
245 return false;
248 strncpy(s, addr, len+1);
250 p = rindex(s, ':');
251 if (p == NULL) {
252 DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a port number\n", s));
253 return false;
256 port = strtoul(p+1, &endp, 10);
257 if (endp == NULL || *endp != 0) {
258 /* trailing garbage */
259 DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the port in %s\n", s));
260 return false;
262 *p = 0;
264 /* now is this a ipv4 or ipv6 address ?*/
265 ret = parse_ip(s, NULL, port, saddr);
267 return ret;
270 /* we don't lock future pages here; it would increase the chance that
271 * we'd fail to mmap later on. */
272 void lockdown_memory(bool valgrinding)
274 #if defined(HAVE_MLOCKALL) && !defined(_AIX_)
275 /* Extra stack, please! */
276 char dummy[10000];
277 memset(dummy, 0, sizeof(dummy));
279 if (valgrinding) {
280 return;
283 /* Ignore when running in local daemons mode */
284 if (getuid() != 0) {
285 return;
288 /* Avoid compiler optimizing out dummy. */
289 mlock(dummy, sizeof(dummy));
290 if (mlockall(MCL_CURRENT) != 0) {
291 DEBUG(DEBUG_WARNING,("Failed to lockdown memory: %s'\n",
292 strerror(errno)));
294 #endif
297 int mkdir_p(const char *dir, int mode)
299 char t[PATH_MAX];
300 ssize_t len;
301 int ret;
303 if (strcmp(dir, "/") == 0) {
304 return 0;
307 if (strcmp(dir, ".") == 0) {
308 return 0;
311 /* Try to create directory */
312 ret = mkdir(dir, mode);
313 /* Succeed if that worked or if it already existed */
314 if (ret == 0 || errno == EEXIST) {
315 return 0;
317 /* Fail on anything else except ENOENT */
318 if (errno != ENOENT) {
319 return ret;
322 /* Create ancestors */
323 len = strlen(dir);
324 if (len >= PATH_MAX) {
325 errno = ENAMETOOLONG;
326 return -1;
328 strncpy(t, dir, len+1);
330 ret = mkdir_p(dirname(t), mode);
331 if (ret != 0) {
332 return ret;
335 /* Create directory */
336 ret = mkdir(dir, mode);
337 if ((ret == -1) && (errno == EEXIST)) {
338 ret = 0;
341 return ret;
344 void mkdir_p_or_die(const char *dir, int mode)
346 int ret;
348 ret = mkdir_p(dir, mode);
349 if (ret != 0) {
350 DEBUG(DEBUG_ALERT,
351 ("ctdb exiting with error: "
352 "failed to create directory \"%s\" (%s)\n",
353 dir, strerror(errno)));
354 exit(1);
358 /* A read wrapper that will deal with EINTR. For now, copied from
359 * source3/lib/system.c
361 ssize_t sys_read(int fd, void *buf, size_t count)
363 ssize_t ret;
365 do {
366 ret = read(fd, buf, count);
367 #if defined(EWOULDBLOCK)
368 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
369 #else
370 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
371 #endif
372 return ret;
375 /* A write wrapper that will deal with EINTR. For now, copied from
376 * source3/lib/system.c
378 ssize_t sys_write(int fd, const void *buf, size_t count)
380 ssize_t ret;
382 do {
383 ret = write(fd, buf, count);
384 #if defined(EWOULDBLOCK)
385 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
386 #else
387 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
388 #endif
389 return ret;
392 void ctdb_wait_for_process_to_exit(pid_t pid)
394 while (kill(pid, 0) == 0 || errno != ESRCH) {
395 sleep(5);
399 int ctdb_parse_connections(FILE *fp, TALLOC_CTX *mem_ctx,
400 int *num_conn, struct ctdb_connection **out)
402 struct ctdb_connection *conn = NULL;
403 char line[128], src[128], dst[128]; /* long enough for IPv6 */
404 int line_num, ret;
405 int num = 0, max = 0;
407 line_num = 0;
408 while (! feof(fp)) {
409 if (fgets(line, sizeof(line), fp) == NULL) {
410 break;
412 line_num += 1;
414 /* Skip empty lines */
415 if (line[0] == '\n') {
416 continue;
419 ret = sscanf(line, "%s %s\n", src, dst);
420 if (ret != 2) {
421 DEBUG(DEBUG_ERR, ("Bad line [%d]: %s\n",
422 line_num, line));
423 return EINVAL;
426 if (num >= max) {
427 max += 1024;
428 conn = talloc_realloc(mem_ctx, conn,
429 struct ctdb_connection, max);
430 if (conn == NULL) {
431 return ENOMEM;
435 if (! parse_ip_port(src, &conn[num].src)) {
436 DEBUG(DEBUG_ERR, ("Invalid IP address %s\n", src));
437 return EINVAL;
440 if (! parse_ip_port(dst, &conn[num].dst)) {
441 DEBUG(DEBUG_ERR, ("Invalid IP address %s\n", dst));
442 return EINVAL;
445 num += 1;
448 *num_conn = num;
449 *out = conn;
450 return 0;