python: Port ntvfs posix bindings to Python 3 compatible form
[Samba.git] / ctdb / common / system_util.c
blob63dcd53795b1256280cdb4352353245ca1b636d7
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;
72 p.sched_priority = 1;
74 if (sched_setscheduler(0, SCHED_FIFO, &p) == -1) {
75 DEBUG(DEBUG_CRIT,("Unable to set scheduler to SCHED_FIFO (%s)\n",
76 strerror(errno)));
77 return false;
78 } else {
79 return true;
81 #endif
82 #endif
83 DEBUG(DEBUG_CRIT,("No way to set real-time priority.\n"));
84 return false;
88 reset scheduler from real-time to normal scheduling
90 void reset_scheduler(void)
92 #ifdef _AIX_
93 #if HAVE_THREAD_SETSCHED
94 struct thrdentry64 te;
95 tid64_t ti;
97 ti = 0ULL;
98 if (getthrds64(getpid(), &te, sizeof(te), &ti, 1) != 1) {
99 DEBUG(DEBUG_ERR, ("Unable to get thread information\n"));
101 if (thread_setsched(te.ti_tid, 0, SCHED_OTHER) == -1) {
102 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n"));
104 #endif
105 #else /* no AIX */
106 #if HAVE_SCHED_SETSCHEDULER
107 struct sched_param p;
109 p.sched_priority = 0;
110 if (sched_setscheduler(0, SCHED_OTHER, &p) == -1) {
111 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n"));
113 #endif
114 #endif
117 static bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin)
119 sin->sin_family = AF_INET;
120 sin->sin_port = htons(port);
122 if (inet_pton(AF_INET, s, &sin->sin_addr) != 1) {
123 DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin_addr\n", s));
124 return false;
127 #ifdef HAVE_SOCK_SIN_LEN
128 sin->sin_len = sizeof(*sin);
129 #endif
130 return true;
133 static bool parse_ipv6(const char *s, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
135 saddr->ip6.sin6_family = AF_INET6;
136 saddr->ip6.sin6_port = htons(port);
137 saddr->ip6.sin6_flowinfo = 0;
138 saddr->ip6.sin6_scope_id = 0;
140 if (inet_pton(AF_INET6, s, &saddr->ip6.sin6_addr) != 1) {
141 DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin6_addr\n", s));
142 return false;
145 if (ifaces && IN6_IS_ADDR_LINKLOCAL(&saddr->ip6.sin6_addr)) {
146 if (strchr(ifaces, ',')) {
147 DEBUG(DEBUG_ERR, (__location__ " Link local address %s "
148 "is specified for multiple ifaces %s\n",
149 s, ifaces));
150 return false;
152 saddr->ip6.sin6_scope_id = if_nametoindex(ifaces);
155 #ifdef HAVE_SOCK_SIN_LEN
156 saddr->ip6.sin6_len = sizeof(*saddr);
157 #endif
158 return true;
162 parse an ip
164 static bool parse_ip(const char *addr, const char *ifaces, unsigned port,
165 ctdb_sock_addr *saddr)
167 char *p;
168 bool ret;
170 ZERO_STRUCTP(saddr); /* valgrind :-) */
172 /* IPv4 or IPv6 address?
174 * Use rindex() because we need the right-most ':' below for
175 * IPv4-mapped IPv6 addresses anyway...
177 p = rindex(addr, ':');
178 if (p == NULL) {
179 ret = parse_ipv4(addr, port, &saddr->ip);
180 } else {
181 uint8_t ipv4_mapped_prefix[12] = {
182 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff
185 ret = parse_ipv6(addr, ifaces, port, saddr);
186 if (! ret) {
187 return ret;
191 * Check for IPv4-mapped IPv6 address
192 * (e.g. ::ffff:192.0.2.128) - reparse as IPv4 if
193 * necessary
195 if (memcmp(&saddr->ip6.sin6_addr.s6_addr[0],
196 ipv4_mapped_prefix,
197 sizeof(ipv4_mapped_prefix)) == 0) {
198 /* Reparse as IPv4 */
199 ret = parse_ipv4(p+1, port, &saddr->ip);
203 return ret;
207 parse a ip/mask pair
209 bool parse_ip_mask(const char *str, const char *ifaces, ctdb_sock_addr *addr, unsigned *mask)
211 char *p;
212 char s[64]; /* Much longer than INET6_ADDRSTRLEN */
213 char *endp = NULL;
214 ssize_t len;
215 bool ret;
217 ZERO_STRUCT(*addr);
219 len = strlen(str);
220 if (len >= sizeof(s)) {
221 DEBUG(DEBUG_ERR, ("Address %s is unreasonably long\n", str));
222 return false;
225 strncpy(s, str, len+1);
227 p = rindex(s, '/');
228 if (p == NULL) {
229 DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a mask\n", s));
230 return false;
233 *mask = strtoul(p+1, &endp, 10);
234 if (endp == NULL || *endp != 0) {
235 /* trailing garbage */
236 DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the mask in %s\n", s));
237 return false;
239 *p = 0;
242 /* now is this a ipv4 or ipv6 address ?*/
243 ret = parse_ip(s, ifaces, 0, addr);
245 return ret;
248 /* we don't lock future pages here; it would increase the chance that
249 * we'd fail to mmap later on. */
250 void lockdown_memory(bool valgrinding)
252 #if defined(HAVE_MLOCKALL) && !defined(_AIX_)
253 /* Extra stack, please! */
254 char dummy[10000];
255 memset(dummy, 0, sizeof(dummy));
257 if (valgrinding) {
258 return;
261 /* Ignore when running in local daemons mode */
262 if (getuid() != 0) {
263 return;
266 /* Avoid compiler optimizing out dummy. */
267 mlock(dummy, sizeof(dummy));
268 if (mlockall(MCL_CURRENT) != 0) {
269 DEBUG(DEBUG_WARNING,("Failed to lockdown memory: %s'\n",
270 strerror(errno)));
272 #endif
275 int mkdir_p(const char *dir, int mode)
277 char t[PATH_MAX];
278 ssize_t len;
279 int ret;
281 if (strcmp(dir, "/") == 0) {
282 return 0;
285 if (strcmp(dir, ".") == 0) {
286 return 0;
289 /* Try to create directory */
290 ret = mkdir(dir, mode);
291 /* Succeed if that worked or if it already existed */
292 if (ret == 0 || errno == EEXIST) {
293 return 0;
295 /* Fail on anything else except ENOENT */
296 if (errno != ENOENT) {
297 return ret;
300 /* Create ancestors */
301 len = strlen(dir);
302 if (len >= PATH_MAX) {
303 errno = ENAMETOOLONG;
304 return -1;
306 strncpy(t, dir, len+1);
308 ret = mkdir_p(dirname(t), mode);
309 if (ret != 0) {
310 return ret;
313 /* Create directory */
314 ret = mkdir(dir, mode);
315 if ((ret == -1) && (errno == EEXIST)) {
316 ret = 0;
319 return ret;
322 void mkdir_p_or_die(const char *dir, int mode)
324 int ret;
326 ret = mkdir_p(dir, mode);
327 if (ret != 0) {
328 DEBUG(DEBUG_ALERT,
329 ("ctdb exiting with error: "
330 "failed to create directory \"%s\" (%s)\n",
331 dir, strerror(errno)));
332 exit(1);
336 void ctdb_wait_for_process_to_exit(pid_t pid)
338 while (kill(pid, 0) == 0 || errno != ESRCH) {
339 sleep(5);