lib: Remove unused parmlist code
[Samba.git] / ctdb / common / system_util.c
blob663df6e6b579eabc7a5e254accc2130468157aac
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 bool 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 false;
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 return false;
57 } else {
58 return true;
60 #endif
61 #else /* no AIX */
62 #if HAVE_SCHED_SETSCHEDULER
63 struct sched_param p;
64 int policy = SCHED_FIFO;
66 p.sched_priority = 1;
68 #ifdef SCHED_RESET_ON_FORK
69 policy |= SCHED_RESET_ON_FORK;
70 #endif
71 if (sched_setscheduler(0, policy, &p) == -1) {
72 DEBUG(DEBUG_CRIT,("Unable to set scheduler to SCHED_FIFO (%s)\n",
73 strerror(errno)));
74 return false;
75 } else {
76 return true;
78 #endif
79 #endif
80 DEBUG(DEBUG_CRIT,("No way to set real-time priority.\n"));
81 return false;
85 reset scheduler from real-time to normal scheduling
87 void reset_scheduler(void)
89 #ifdef _AIX_
90 #if HAVE_THREAD_SETSCHED
91 struct thrdentry64 te;
92 tid64_t ti;
94 ti = 0ULL;
95 if (getthrds64(getpid(), &te, sizeof(te), &ti, 1) != 1) {
96 DEBUG(DEBUG_ERR, ("Unable to get thread information\n"));
98 if (thread_setsched(te.ti_tid, 0, SCHED_OTHER) == -1) {
99 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n"));
101 #endif
102 #else /* no AIX */
103 #if HAVE_SCHED_SETSCHEDULER
104 #ifndef SCHED_RESET_ON_FORK
105 struct sched_param p;
107 p.sched_priority = 0;
108 if (sched_setscheduler(0, SCHED_OTHER, &p) == -1) {
109 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n"));
111 #endif
112 #endif
113 #endif
116 void set_nonblocking(int fd)
118 int v;
120 v = fcntl(fd, F_GETFL, 0);
121 if (v == -1) {
122 DEBUG(DEBUG_WARNING, ("Failed to get file status flags - %s\n",
123 strerror(errno)));
124 return;
126 if (fcntl(fd, F_SETFL, v | O_NONBLOCK) == -1) {
127 DEBUG(DEBUG_WARNING, ("Failed to set non_blocking on fd - %s\n",
128 strerror(errno)));
132 void set_close_on_exec(int fd)
134 int v;
136 v = fcntl(fd, F_GETFD, 0);
137 if (v == -1) {
138 DEBUG(DEBUG_WARNING, ("Failed to get file descriptor flags - %s\n",
139 strerror(errno)));
140 return;
142 if (fcntl(fd, F_SETFD, v | FD_CLOEXEC) != 0) {
143 DEBUG(DEBUG_WARNING, ("Failed to set close_on_exec on fd - %s\n",
144 strerror(errno)));
149 bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin)
151 sin->sin_family = AF_INET;
152 sin->sin_port = htons(port);
154 if (inet_pton(AF_INET, s, &sin->sin_addr) != 1) {
155 DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin_addr\n", s));
156 return false;
159 #ifdef HAVE_SOCK_SIN_LEN
160 sin->sin_len = sizeof(*sin);
161 #endif
162 return true;
165 static bool parse_ipv6(const char *s, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
167 saddr->ip6.sin6_family = AF_INET6;
168 saddr->ip6.sin6_port = htons(port);
169 saddr->ip6.sin6_flowinfo = 0;
170 saddr->ip6.sin6_scope_id = 0;
172 if (inet_pton(AF_INET6, s, &saddr->ip6.sin6_addr) != 1) {
173 DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin6_addr\n", s));
174 return false;
177 if (ifaces && IN6_IS_ADDR_LINKLOCAL(&saddr->ip6.sin6_addr)) {
178 if (strchr(ifaces, ',')) {
179 DEBUG(DEBUG_ERR, (__location__ " Link local address %s "
180 "is specified for multiple ifaces %s\n",
181 s, ifaces));
182 return false;
184 saddr->ip6.sin6_scope_id = if_nametoindex(ifaces);
187 #ifdef HAVE_SOCK_SIN_LEN
188 saddr->ip6.sin6_len = sizeof(*saddr);
189 #endif
190 return true;
194 parse an ip
196 bool parse_ip(const char *addr, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
198 char *p;
199 bool ret;
201 ZERO_STRUCTP(saddr); /* valgrind :-) */
203 /* now is this a ipv4 or ipv6 address ?*/
204 p = index(addr, ':');
205 if (p == NULL) {
206 ret = parse_ipv4(addr, port, &saddr->ip);
207 } else {
208 ret = parse_ipv6(addr, ifaces, port, saddr);
211 return ret;
215 parse a ip/mask pair
217 bool parse_ip_mask(const char *str, const char *ifaces, ctdb_sock_addr *addr, unsigned *mask)
219 char *p;
220 char s[64]; /* Much longer than INET6_ADDRSTRLEN */
221 char *endp = NULL;
222 ssize_t len;
223 bool ret;
225 ZERO_STRUCT(*addr);
227 len = strlen(str);
228 if (len >= sizeof(s)) {
229 DEBUG(DEBUG_ERR, ("Address %s is unreasonably long\n", str));
230 return false;
233 strncpy(s, str, len+1);
235 p = rindex(s, '/');
236 if (p == NULL) {
237 DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a mask\n", s));
238 return false;
241 *mask = strtoul(p+1, &endp, 10);
242 if (endp == NULL || *endp != 0) {
243 /* trailing garbage */
244 DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the mask in %s\n", s));
245 return false;
247 *p = 0;
250 /* now is this a ipv4 or ipv6 address ?*/
251 ret = parse_ip(s, ifaces, 0, addr);
253 return ret;
257 parse a ip:port pair
259 bool parse_ip_port(const char *addr, ctdb_sock_addr *saddr)
261 char *p;
262 char s[64]; /* Much longer than INET6_ADDRSTRLEN */
263 unsigned port;
264 char *endp = NULL;
265 ssize_t len;
266 bool ret;
268 len = strlen(addr);
269 if (len >= sizeof(s)) {
270 DEBUG(DEBUG_ERR, ("Address %s is unreasonably long\n", addr));
271 return false;
274 strncpy(s, addr, len+1);
276 p = rindex(s, ':');
277 if (p == NULL) {
278 DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a port number\n", s));
279 return false;
282 port = strtoul(p+1, &endp, 10);
283 if (endp == NULL || *endp != 0) {
284 /* trailing garbage */
285 DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the port in %s\n", s));
286 return false;
288 *p = 0;
290 /* now is this a ipv4 or ipv6 address ?*/
291 ret = parse_ip(s, NULL, port, saddr);
293 return ret;
296 /* we don't lock future pages here; it would increase the chance that
297 * we'd fail to mmap later on. */
298 void lockdown_memory(bool valgrinding)
300 #if defined(HAVE_MLOCKALL) && !defined(_AIX_)
301 /* Extra stack, please! */
302 char dummy[10000];
303 memset(dummy, 0, sizeof(dummy));
305 if (valgrinding) {
306 return;
309 /* Ignore when running in local daemons mode */
310 if (getuid() != 0) {
311 return;
314 /* Avoid compiler optimizing out dummy. */
315 mlock(dummy, sizeof(dummy));
316 if (mlockall(MCL_CURRENT) != 0) {
317 DEBUG(DEBUG_WARNING,("Failed to lockdown memory: %s'\n",
318 strerror(errno)));
320 #endif
323 int mkdir_p(const char *dir, int mode)
325 char t[PATH_MAX];
326 ssize_t len;
327 int ret;
329 if (strcmp(dir, "/") == 0) {
330 return 0;
333 if (strcmp(dir, ".") == 0) {
334 return 0;
337 /* Try to create directory */
338 ret = mkdir(dir, mode);
339 /* Succeed if that worked or if it already existed */
340 if (ret == 0 || errno == EEXIST) {
341 return 0;
343 /* Fail on anything else except ENOENT */
344 if (errno != ENOENT) {
345 return ret;
348 /* Create ancestors */
349 len = strlen(dir);
350 if (len >= PATH_MAX) {
351 errno = ENAMETOOLONG;
352 return -1;
354 strncpy(t, dir, len+1);
356 ret = mkdir_p(dirname(t), mode);
357 if (ret != 0) {
358 return ret;
361 /* Create directory */
362 ret = mkdir(dir, mode);
363 if ((ret == -1) && (errno == EEXIST)) {
364 ret = 0;
367 return ret;
370 void mkdir_p_or_die(const char *dir, int mode)
372 int ret;
374 ret = mkdir_p(dir, mode);
375 if (ret != 0) {
376 DEBUG(DEBUG_ALERT,
377 ("ctdb exiting with error: "
378 "failed to create directory \"%s\" (%s)\n",
379 dir, strerror(errno)));
380 exit(1);
384 /* A read wrapper that will deal with EINTR. For now, copied from
385 * source3/lib/system.c
387 ssize_t sys_read(int fd, void *buf, size_t count)
389 ssize_t ret;
391 do {
392 ret = read(fd, buf, count);
393 #if defined(EWOULDBLOCK)
394 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
395 #else
396 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
397 #endif
398 return ret;
401 /* A write wrapper that will deal with EINTR. For now, copied from
402 * source3/lib/system.c
404 ssize_t sys_write(int fd, const void *buf, size_t count)
406 ssize_t ret;
408 do {
409 ret = write(fd, buf, count);
410 #if defined(EWOULDBLOCK)
411 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
412 #else
413 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
414 #endif
415 return ret;