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