selftest: Add a variable to indicate that selftest is running
[ambi/samba-autobuild/.git] / ctdb / common / system_util.c
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 <talloc.h>
27 #include <libgen.h>
28
29 #include "lib/util/debug.h"
30
31 #include "protocol/protocol.h"
32
33 #include "common/logging.h"
34 #include "common/system.h"
35
36 #if HAVE_SCHED_H
37 #include <sched.h>
38 #endif
39
40 #if HAVE_PROCINFO_H
41 #include <procinfo.h>
42 #endif
43
44 /*
45   if possible, make this task real time
46  */
47 bool set_scheduler(void)
48 {
49 #ifdef _AIX_
50 #if HAVE_THREAD_SETSCHED
51         struct thrdentry64 te;
52         tid64_t ti;
53
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;
58         }
59
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;
66         }
67 #endif
68 #else /* no AIX */
69 #if HAVE_SCHED_SETSCHEDULER
70         struct sched_param p;
71
72         p.sched_priority = 1;
73
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;
80         }
81 #endif
82 #endif
83         DEBUG(DEBUG_CRIT,("No way to set real-time priority.\n"));
84         return false;
85 }
86
87 /*
88   reset scheduler from real-time to normal scheduling
89  */
90 void reset_scheduler(void)
91 {
92 #ifdef _AIX_
93 #if HAVE_THREAD_SETSCHED
94         struct thrdentry64 te;
95         tid64_t ti;
96
97         ti = 0ULL;
98         if (getthrds64(getpid(), &te, sizeof(te), &ti, 1) != 1) {
99                 DEBUG(DEBUG_ERR, ("Unable to get thread information\n"));
100         }
101         if (thread_setsched(te.ti_tid, 0, SCHED_OTHER) == -1) {
102                 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n"));
103         }
104 #endif
105 #else /* no AIX */
106 #if HAVE_SCHED_SETSCHEDULER
107         struct sched_param p;
108
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"));
112         }
113 #endif
114 #endif
115 }
116
117 bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin)
118 {
119         sin->sin_family = AF_INET;
120         sin->sin_port   = htons(port);
121
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;
125         }
126
127 #ifdef HAVE_SOCK_SIN_LEN
128         sin->sin_len = sizeof(*sin);
129 #endif
130         return true;
131 }
132
133 static bool parse_ipv6(const char *s, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
134 {
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;
139
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;
143         }
144
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;
151                 }
152                 saddr->ip6.sin6_scope_id = if_nametoindex(ifaces);
153         }
154
155 #ifdef HAVE_SOCK_SIN_LEN
156         saddr->ip6.sin6_len = sizeof(*saddr);
157 #endif
158         return true;
159 }
160
161 /*
162   parse an ip
163  */
164 bool parse_ip(const char *addr, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
165 {
166         char *p;
167         bool ret;
168
169         ZERO_STRUCTP(saddr); /* valgrind :-) */
170
171         /* now is this a ipv4 or ipv6 address ?*/
172         p = index(addr, ':');
173         if (p == NULL) {
174                 ret = parse_ipv4(addr, port, &saddr->ip);
175         } else {
176                 ret = parse_ipv6(addr, ifaces, port, saddr);
177         }
178
179         return ret;
180 }
181
182 /*
183   parse a ip/mask pair
184  */
185 bool parse_ip_mask(const char *str, const char *ifaces, ctdb_sock_addr *addr, unsigned *mask)
186 {
187         char *p;
188         char s[64]; /* Much longer than INET6_ADDRSTRLEN */
189         char *endp = NULL;
190         ssize_t len;
191         bool ret;
192
193         ZERO_STRUCT(*addr);
194
195         len = strlen(str);
196         if (len >= sizeof(s)) {
197                 DEBUG(DEBUG_ERR, ("Address %s is unreasonably long\n", str));
198                 return false;
199         }
200
201         strncpy(s, str, len+1);
202
203         p = rindex(s, '/');
204         if (p == NULL) {
205                 DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a mask\n", s));
206                 return false;
207         }
208
209         *mask = strtoul(p+1, &endp, 10);
210         if (endp == NULL || *endp != 0) {
211                 /* trailing garbage */
212                 DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the mask in %s\n", s));
213                 return false;
214         }
215         *p = 0;
216
217
218         /* now is this a ipv4 or ipv6 address ?*/
219         ret = parse_ip(s, ifaces, 0, addr);
220
221         return ret;
222 }
223
224 /*
225   parse a ip:port pair
226  */
227 bool parse_ip_port(const char *addr, ctdb_sock_addr *saddr)
228 {
229         char *p;
230         char s[64]; /* Much longer than INET6_ADDRSTRLEN */
231         unsigned port;
232         char *endp = NULL;
233         ssize_t len;
234         bool ret;
235
236         len = strlen(addr);
237         if (len >= sizeof(s)) {
238                 DEBUG(DEBUG_ERR, ("Address %s is unreasonably long\n", addr));
239                 return false;
240         }
241
242         strncpy(s, addr, len+1);
243
244         p = rindex(s, ':');
245         if (p == NULL) {
246                 DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a port number\n", s));
247                 return false;
248         }
249
250         port = strtoul(p+1, &endp, 10);
251         if (endp == NULL || *endp != 0) {
252                 /* trailing garbage */
253                 DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the port in %s\n", s));
254                 return false;
255         }
256         *p = 0;
257
258         /* now is this a ipv4 or ipv6 address ?*/
259         ret = parse_ip(s, NULL, port, saddr);
260
261         return ret;
262 }
263
264 /* we don't lock future pages here; it would increase the chance that
265  * we'd fail to mmap later on. */
266 void lockdown_memory(bool valgrinding)
267 {
268 #if defined(HAVE_MLOCKALL) && !defined(_AIX_)
269         /* Extra stack, please! */
270         char dummy[10000];
271         memset(dummy, 0, sizeof(dummy));
272
273         if (valgrinding) {
274                 return;
275         }
276
277         /* Ignore when running in local daemons mode */
278         if (getuid() != 0) {
279                 return;
280         }
281
282         /* Avoid compiler optimizing out dummy. */
283         mlock(dummy, sizeof(dummy));
284         if (mlockall(MCL_CURRENT) != 0) {
285                 DEBUG(DEBUG_WARNING,("Failed to lockdown memory: %s'\n",
286                                      strerror(errno)));
287         }
288 #endif
289 }
290
291 int mkdir_p(const char *dir, int mode)
292 {
293         char t[PATH_MAX];
294         ssize_t len;
295         int ret;
296
297         if (strcmp(dir, "/") == 0) {
298                 return 0;
299         }
300
301         if (strcmp(dir, ".") == 0) {
302                 return 0;
303         }
304
305         /* Try to create directory */
306         ret = mkdir(dir, mode);
307         /* Succeed if that worked or if it already existed */
308         if (ret == 0 || errno == EEXIST) {
309                 return 0;
310         }
311         /* Fail on anything else except ENOENT */
312         if (errno != ENOENT) {
313                 return ret;
314         }
315
316         /* Create ancestors */
317         len = strlen(dir);
318         if (len >= PATH_MAX) {
319                 errno = ENAMETOOLONG;
320                 return -1;
321         }
322         strncpy(t, dir, len+1);
323
324         ret = mkdir_p(dirname(t), mode);
325         if (ret != 0) {
326                 return ret;
327         }
328
329         /* Create directory */
330         ret = mkdir(dir, mode);
331         if ((ret == -1) && (errno == EEXIST)) {
332                 ret = 0;
333         }
334
335         return ret;
336 }
337
338 void mkdir_p_or_die(const char *dir, int mode)
339 {
340         int ret;
341
342         ret = mkdir_p(dir, mode);
343         if (ret != 0) {
344                 DEBUG(DEBUG_ALERT,
345                       ("ctdb exiting with error: "
346                        "failed to create directory \"%s\" (%s)\n",
347                        dir, strerror(errno)));
348                 exit(1);
349         }
350 }
351
352 void ctdb_wait_for_process_to_exit(pid_t pid)
353 {
354         while (kill(pid, 0) == 0 || errno != ESRCH) {
355                 sleep(5);
356         }
357 }
358
359 int ctdb_parse_connections(FILE *fp, TALLOC_CTX *mem_ctx,
360                            int *num_conn, struct ctdb_connection **out)
361 {
362         struct ctdb_connection *conn = NULL;
363         char line[128], src[128], dst[128]; /* long enough for IPv6 */
364         int line_num, ret;
365         int num = 0, max = 0;
366
367         line_num = 0;
368         while (! feof(fp)) {
369                 if (fgets(line, sizeof(line), fp) == NULL) {
370                         break;
371                 }
372                 line_num += 1;
373
374                 /* Skip empty lines */
375                 if (line[0] == '\n') {
376                         continue;
377                 }
378
379                 ret = sscanf(line, "%s %s\n", src, dst);
380                 if (ret != 2) {
381                         DEBUG(DEBUG_ERR, ("Bad line [%d]: %s\n",
382                                           line_num, line));
383                         talloc_free(conn);
384                         return EINVAL;
385                 }
386
387                 if (num >= max) {
388                         max += 1024;
389                         conn = talloc_realloc(mem_ctx, conn,
390                                               struct ctdb_connection, max);
391                         if (conn == NULL) {
392                                 return ENOMEM;
393                         }
394                 }
395
396                 if (! parse_ip_port(src, &conn[num].src)) {
397                         DEBUG(DEBUG_ERR, ("Invalid IP address %s\n", src));
398                         talloc_free(conn);
399                         return EINVAL;
400                 }
401
402                 if (! parse_ip_port(dst, &conn[num].dst)) {
403                         DEBUG(DEBUG_ERR, ("Invalid IP address %s\n", dst));
404                         talloc_free(conn);
405                         return EINVAL;
406                 }
407
408                 num += 1;
409         }
410
411         *num_conn = num;
412         *out = conn;
413         return 0;
414 }