ctdb-util: Log an error if there is no way to set scheduler
[obnox/samba/samba-obnox.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 "includes.h"
22 #include "system/filesys.h"
23 #include "system/shmem.h"
24
25 #include <libgen.h>
26
27 #include "ctdb_private.h"
28
29 #if HAVE_SCHED_H
30 #include <sched.h>
31 #endif
32
33 #if HAVE_PROCINFO_H
34 #include <procinfo.h>
35 #endif
36
37 /*
38   if possible, make this task real time
39  */
40 bool set_scheduler(void)
41 {
42 #ifdef _AIX_
43 #if HAVE_THREAD_SETSCHED
44         struct thrdentry64 te;
45         tid64_t ti;
46
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;
51         }
52
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;
59         }
60 #endif
61 #else /* no AIX */
62 #if HAVE_SCHED_SETSCHEDULER
63         struct sched_param p;
64         int policy = SCHED_FIFO;
65
66         p.sched_priority = 1;
67
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;
77         }
78 #endif
79 #endif
80         DEBUG(DEBUG_CRIT,("No way to set real-time priority.\n"));
81         return false;
82 }
83
84 /*
85   reset scheduler from real-time to normal scheduling
86  */
87 void reset_scheduler(void)
88 {
89 #ifdef _AIX_
90 #if HAVE_THREAD_SETSCHED
91         struct thrdentry64 te;
92         tid64_t ti;
93
94         ti = 0ULL;
95         if (getthrds64(getpid(), &te, sizeof(te), &ti, 1) != 1) {
96                 DEBUG(DEBUG_ERR, ("Unable to get thread information\n"));
97         }
98         if (thread_setsched(te.ti_tid, 0, SCHED_OTHER) == -1) {
99                 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n"));
100         }
101 #endif
102 #else /* no AIX */
103 #if HAVE_SCHED_SETSCHEDULER
104 #ifndef SCHED_RESET_ON_FORK
105         struct sched_param p;
106
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"));
110         }
111 #endif
112 #endif
113 #endif
114 }
115
116 void set_nonblocking(int fd)
117 {
118         int v;
119
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;
125         }
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)));
129         }
130 }
131
132 void set_close_on_exec(int fd)
133 {
134         int v;
135
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;
141         }
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)));
145         }
146 }
147
148
149 bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin)
150 {
151         sin->sin_family = AF_INET;
152         sin->sin_port   = htons(port);
153
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;
157         }
158
159         return true;
160 }
161
162 static bool parse_ipv6(const char *s, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
163 {
164         saddr->ip6.sin6_family   = AF_INET6;
165         saddr->ip6.sin6_port     = htons(port);
166         saddr->ip6.sin6_flowinfo = 0;
167         saddr->ip6.sin6_scope_id = 0;
168
169         if (inet_pton(AF_INET6, s, &saddr->ip6.sin6_addr) != 1) {
170                 DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin6_addr\n", s));
171                 return false;
172         }
173
174         if (ifaces && IN6_IS_ADDR_LINKLOCAL(&saddr->ip6.sin6_addr)) {
175                 if (strchr(ifaces, ',')) {
176                         DEBUG(DEBUG_ERR, (__location__ " Link local address %s "
177                                           "is specified for multiple ifaces %s\n",
178                                           s, ifaces));
179                         return false;
180                 }
181                 saddr->ip6.sin6_scope_id = if_nametoindex(ifaces);
182         }
183
184         return true;
185 }
186
187 /*
188   parse an ip
189  */
190 bool parse_ip(const char *addr, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
191 {
192         char *p;
193         bool ret;
194
195         ZERO_STRUCTP(saddr); /* valgrind :-) */
196
197         /* now is this a ipv4 or ipv6 address ?*/
198         p = index(addr, ':');
199         if (p == NULL) {
200                 ret = parse_ipv4(addr, port, &saddr->ip);
201         } else {
202                 ret = parse_ipv6(addr, ifaces, port, saddr);
203         }
204
205         return ret;
206 }
207
208 /*
209   parse a ip/mask pair
210  */
211 bool parse_ip_mask(const char *str, const char *ifaces, ctdb_sock_addr *addr, unsigned *mask)
212 {
213         char *p;
214         char s[64]; /* Much longer than INET6_ADDRSTRLEN */
215         char *endp = NULL;
216         ssize_t len;
217         bool ret;
218
219         ZERO_STRUCT(*addr);
220
221         len = strlen(str);
222         if (len >= sizeof(s)) {
223                 DEBUG(DEBUG_ERR, ("Address %s is unreasonably long\n", str));
224                 return false;
225         }
226
227         strncpy(s, str, len+1);
228
229         p = rindex(s, '/');
230         if (p == NULL) {
231                 DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a mask\n", s));
232                 return false;
233         }
234
235         *mask = strtoul(p+1, &endp, 10);
236         if (endp == NULL || *endp != 0) {
237                 /* trailing garbage */
238                 DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the mask in %s\n", s));
239                 return false;
240         }
241         *p = 0;
242
243
244         /* now is this a ipv4 or ipv6 address ?*/
245         ret = parse_ip(s, ifaces, 0, addr);
246
247         return ret;
248 }
249
250 /*
251   parse a ip:port pair
252  */
253 bool parse_ip_port(const char *addr, ctdb_sock_addr *saddr)
254 {
255         char *p;
256         char s[64]; /* Much longer than INET6_ADDRSTRLEN */
257         unsigned port;
258         char *endp = NULL;
259         ssize_t len;
260         bool ret;
261
262         len = strlen(addr);
263         if (len >= sizeof(s)) {
264                 DEBUG(DEBUG_ERR, ("Address %s is unreasonably long\n", addr));
265                 return false;
266         }
267
268         strncpy(s, addr, len+1);
269
270         p = rindex(s, ':');
271         if (p == NULL) {
272                 DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a port number\n", s));
273                 return false;
274         }
275
276         port = strtoul(p+1, &endp, 10);
277         if (endp == NULL || *endp != 0) {
278                 /* trailing garbage */
279                 DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the port in %s\n", s));
280                 return false;
281         }
282         *p = 0;
283
284         /* now is this a ipv4 or ipv6 address ?*/
285         ret = parse_ip(s, NULL, port, saddr);
286
287         return ret;
288 }
289
290 /* we don't lock future pages here; it would increase the chance that
291  * we'd fail to mmap later on. */
292 void lockdown_memory(bool valgrinding)
293 {
294 #if defined(HAVE_MLOCKALL) && !defined(_AIX_)
295         /* Extra stack, please! */
296         char dummy[10000];
297         memset(dummy, 0, sizeof(dummy));
298
299         if (valgrinding) {
300                 return;
301         }
302
303         /* Ignore when running in local daemons mode */
304         if (getuid() != 0) {
305                 return;
306         }
307
308         /* Avoid compiler optimizing out dummy. */
309         mlock(dummy, sizeof(dummy));
310         if (mlockall(MCL_CURRENT) != 0) {
311                 DEBUG(DEBUG_WARNING,("Failed to lockdown memory: %s'\n",
312                                      strerror(errno)));
313         }
314 #endif
315 }
316
317 int mkdir_p(const char *dir, int mode)
318 {
319         char t[PATH_MAX];
320         ssize_t len;
321         int ret;
322
323         if (strcmp(dir, "/") == 0) {
324                 return 0;
325         }
326
327         if (strcmp(dir, ".") == 0) {
328                 return 0;
329         }
330
331         /* Try to create directory */
332         ret = mkdir(dir, mode);
333         /* Succeed if that worked or if it already existed */
334         if (ret == 0 || errno == EEXIST) {
335                 return 0;
336         }
337         /* Fail on anything else except ENOENT */
338         if (errno != ENOENT) {
339                 return ret;
340         }
341
342         /* Create ancestors */
343         len = strlen(dir);
344         if (len >= PATH_MAX) {
345                 errno = ENAMETOOLONG;
346                 return -1;
347         }
348         strncpy(t, dir, len+1);
349
350         ret = mkdir_p(dirname(t), mode);
351         if (ret != 0) {
352                 return ret;
353         }
354
355         /* Create directory */
356         ret = mkdir(dir, mode);
357         if ((ret == -1) && (errno == EEXIST)) {
358                 ret = 0;
359         }
360
361         return ret;
362 }
363
364 void mkdir_p_or_die(const char *dir, int mode)
365 {
366         int ret;
367
368         ret = mkdir_p(dir, mode);
369         if (ret != 0) {
370                 DEBUG(DEBUG_ALERT,
371                       ("ctdb exiting with error: "
372                        "failed to create directory \"%s\" (%s)\n",
373                        dir, strerror(errno)));
374                 exit(1);
375         }
376 }
377
378 /* A read wrapper that will deal with EINTR.  For now, copied from
379  * source3/lib/system.c
380  */
381 ssize_t sys_read(int fd, void *buf, size_t count)
382 {
383         ssize_t ret;
384
385         do {
386                 ret = read(fd, buf, count);
387 #if defined(EWOULDBLOCK)
388         } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
389 #else
390         } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
391 #endif
392         return ret;
393 }
394
395 /* A write wrapper that will deal with EINTR.  For now, copied from
396  * source3/lib/system.c
397  */
398 ssize_t sys_write(int fd, const void *buf, size_t count)
399 {
400         ssize_t ret;
401
402         do {
403                 ret = write(fd, buf, count);
404 #if defined(EWOULDBLOCK)
405         } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
406 #else
407         } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
408 #endif
409         return ret;
410 }