ctdb-common: Move mkdir_p_or_die to system utilities
[metze/samba/wip.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
28 #if HAVE_SCHED_H
29 #include <sched.h>
30 #endif
31
32 #if HAVE_PROCINFO_H
33 #include <procinfo.h>
34 #endif
35
36 /*
37   if possible, make this task real time
38  */
39 void set_scheduler(void)
40 {
41 #ifdef _AIX_
42 #if HAVE_THREAD_SETSCHED
43         struct thrdentry64 te;
44         tid64_t ti;
45
46         ti = 0ULL;
47         if (getthrds64(getpid(), &te, sizeof(te), &ti, 1) != 1) {
48                 DEBUG(DEBUG_ERR, ("Unable to get thread information\n"));
49                 return;
50         }
51
52         if (thread_setsched(te.ti_tid, 0, SCHED_RR) == -1) {
53                 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_RR (%s)\n",
54                                   strerror(errno)));
55         } else {
56                 DEBUG(DEBUG_NOTICE, ("Set scheduler to SCHED_RR\n"));
57         }
58 #endif
59 #else /* no AIX */
60 #if HAVE_SCHED_SETSCHEDULER
61         struct sched_param p;
62
63         p.sched_priority = 1;
64
65         if (sched_setscheduler(0, SCHED_FIFO, &p) == -1) {
66                 DEBUG(DEBUG_CRIT,("Unable to set scheduler to SCHED_FIFO (%s)\n",
67                          strerror(errno)));
68         } else {
69                 DEBUG(DEBUG_NOTICE,("Set scheduler to SCHED_FIFO\n"));
70         }
71 #endif
72 #endif
73 }
74
75 /*
76   reset scheduler from real-time to normal scheduling
77  */
78 void reset_scheduler(void)
79 {
80 #ifdef _AIX_
81 #if HAVE_THREAD_SETSCHED
82         struct thrdentry64 te;
83         tid64_t ti;
84
85         ti = 0ULL;
86         if (getthrds64(getpid(), &te, sizeof(te), &ti, 1) != 1) {
87                 DEBUG(DEBUG_ERR, ("Unable to get thread information\n"));
88         }
89         if (thread_setsched(te.ti_tid, 0, SCHED_OTHER) == -1) {
90                 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n"));
91         }
92 #endif
93 #else /* no AIX */
94 #if HAVE_SCHED_SETSCHEDULER
95         struct sched_param p;
96
97         p.sched_priority = 0;
98         if (sched_setscheduler(0, SCHED_OTHER, &p) == -1) {
99                 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n"));
100         }
101 #endif
102 #endif
103 }
104
105 void set_nonblocking(int fd)
106 {
107         int v;
108
109         v = fcntl(fd, F_GETFL, 0);
110         if (v == -1) {
111                 DEBUG(DEBUG_WARNING, ("Failed to get file status flags - %s\n",
112                                       strerror(errno)));
113                 return;
114         }
115         if (fcntl(fd, F_SETFL, v | O_NONBLOCK) == -1) {
116                 DEBUG(DEBUG_WARNING, ("Failed to set non_blocking on fd - %s\n",
117                                       strerror(errno)));
118         }
119 }
120
121 void set_close_on_exec(int fd)
122 {
123         int v;
124
125         v = fcntl(fd, F_GETFD, 0);
126         if (v == -1) {
127                 DEBUG(DEBUG_WARNING, ("Failed to get file descriptor flags - %s\n",
128                                       strerror(errno)));
129                 return;
130         }
131         if (fcntl(fd, F_SETFD, v | FD_CLOEXEC) != 0) {
132                 DEBUG(DEBUG_WARNING, ("Failed to set close_on_exec on fd - %s\n",
133                                       strerror(errno)));
134         }
135 }
136
137
138 bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin)
139 {
140         sin->sin_family = AF_INET;
141         sin->sin_port   = htons(port);
142
143         if (inet_pton(AF_INET, s, &sin->sin_addr) != 1) {
144                 DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin_addr\n", s));
145                 return false;
146         }
147
148         return true;
149 }
150
151 static bool parse_ipv6(const char *s, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
152 {
153         saddr->ip6.sin6_family   = AF_INET6;
154         saddr->ip6.sin6_port     = htons(port);
155         saddr->ip6.sin6_flowinfo = 0;
156         saddr->ip6.sin6_scope_id = 0;
157
158         if (inet_pton(AF_INET6, s, &saddr->ip6.sin6_addr) != 1) {
159                 DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin6_addr\n", s));
160                 return false;
161         }
162
163         if (ifaces && IN6_IS_ADDR_LINKLOCAL(&saddr->ip6.sin6_addr)) {
164                 if (strchr(ifaces, ',')) {
165                         DEBUG(DEBUG_ERR, (__location__ " Link local address %s "
166                                           "is specified for multiple ifaces %s\n",
167                                           s, ifaces));
168                         return false;
169                 }
170                 saddr->ip6.sin6_scope_id = if_nametoindex(ifaces);
171         }
172
173         return true;
174 }
175
176 /*
177   parse an ip
178  */
179 bool parse_ip(const char *addr, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
180 {
181         char *p;
182         bool ret;
183
184         ZERO_STRUCTP(saddr); /* valgrind :-) */
185
186         /* now is this a ipv4 or ipv6 address ?*/
187         p = index(addr, ':');
188         if (p == NULL) {
189                 ret = parse_ipv4(addr, port, &saddr->ip);
190         } else {
191                 ret = parse_ipv6(addr, ifaces, port, saddr);
192         }
193
194         return ret;
195 }
196
197 /*
198   parse a ip/mask pair
199  */
200 bool parse_ip_mask(const char *str, const char *ifaces, ctdb_sock_addr *addr, unsigned *mask)
201 {
202         char *p;
203         char s[64]; /* Much longer than INET6_ADDRSTRLEN */
204         char *endp = NULL;
205         ssize_t len;
206         bool ret;
207
208         ZERO_STRUCT(*addr);
209
210         len = strlen(str);
211         if (len >= sizeof(s)) {
212                 DEBUG(DEBUG_ERR, ("Address %s is unreasonably long\n", str));
213                 return false;
214         }
215
216         strncpy(s, str, len+1);
217
218         p = rindex(s, '/');
219         if (p == NULL) {
220                 DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a mask\n", s));
221                 return false;
222         }
223
224         *mask = strtoul(p+1, &endp, 10);
225         if (endp == NULL || *endp != 0) {
226                 /* trailing garbage */
227                 DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the mask in %s\n", s));
228                 return false;
229         }
230         *p = 0;
231
232
233         /* now is this a ipv4 or ipv6 address ?*/
234         ret = parse_ip(s, ifaces, 0, addr);
235
236         return ret;
237 }
238
239 /*
240   parse a ip:port pair
241  */
242 bool parse_ip_port(const char *addr, ctdb_sock_addr *saddr)
243 {
244         char *p;
245         char s[64]; /* Much longer than INET6_ADDRSTRLEN */
246         unsigned port;
247         char *endp = NULL;
248         ssize_t len;
249         bool ret;
250
251         len = strlen(addr);
252         if (len >= sizeof(s)) {
253                 DEBUG(DEBUG_ERR, ("Address %s is unreasonably long\n", addr));
254                 return false;
255         }
256
257         strncpy(s, addr, len+1);
258
259         p = rindex(s, ':');
260         if (p == NULL) {
261                 DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a port number\n", s));
262                 return false;
263         }
264
265         port = strtoul(p+1, &endp, 10);
266         if (endp == NULL || *endp != 0) {
267                 /* trailing garbage */
268                 DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the port in %s\n", s));
269                 return false;
270         }
271         *p = 0;
272
273         /* now is this a ipv4 or ipv6 address ?*/
274         ret = parse_ip(s, NULL, port, saddr);
275
276         return ret;
277 }
278
279 /* we don't lock future pages here; it would increase the chance that
280  * we'd fail to mmap later on. */
281 void lockdown_memory(bool valgrinding)
282 {
283 #if defined(HAVE_MLOCKALL) && !defined(_AIX_)
284         /* Extra stack, please! */
285         char dummy[10000];
286         memset(dummy, 0, sizeof(dummy));
287
288         if (valgrinding) {
289                 return;
290         }
291
292         /* Ignore when running in local daemons mode */
293         if (getuid() != 0) {
294                 return;
295         }
296
297         /* Avoid compiler optimizing out dummy. */
298         mlock(dummy, sizeof(dummy));
299         if (mlockall(MCL_CURRENT) != 0) {
300                 DEBUG(DEBUG_WARNING,("Failed to lockdown memory: %s'\n",
301                                      strerror(errno)));
302         }
303 #endif
304 }
305
306 int mkdir_p(const char *dir, int mode)
307 {
308         char t[PATH_MAX];
309         ssize_t len;
310         int ret;
311
312         if (strcmp(dir, "/") == 0) {
313                 return 0;
314         }
315
316         if (strcmp(dir, ".") == 0) {
317                 return 0;
318         }
319
320         /* Try to create directory */
321         ret = mkdir(dir, mode);
322         /* Succeed if that worked or if it already existed */
323         if (ret == 0 || errno == EEXIST) {
324                 return 0;
325         }
326         /* Fail on anything else except ENOENT */
327         if (errno != ENOENT) {
328                 return ret;
329         }
330
331         /* Create ancestors */
332         len = strlen(dir);
333         if (len >= PATH_MAX) {
334                 errno = ENAMETOOLONG;
335                 return -1;
336         }
337         strncpy(t, dir, len+1);
338
339         ret = mkdir_p(dirname(t), mode);
340         if (ret != 0) {
341                 return ret;
342         }
343
344         /* Create directory */
345         ret = mkdir(dir, mode);
346         if ((ret == -1) && (errno == EEXIST)) {
347                 ret = 0;
348         }
349
350         return ret;
351 }
352
353 void mkdir_p_or_die(const char *dir, int mode)
354 {
355         int ret;
356
357         ret = mkdir_p(dir, mode);
358         if (ret != 0) {
359                 DEBUG(DEBUG_ALERT,
360                       ("ctdb exiting with error: "
361                        "failed to create directory \"%s\" (%s)\n",
362                        dir, strerror(errno)));
363                 exit(1);
364         }
365 }