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/>.
22 #include "system/filesys.h"
23 #include "system/shmem.h"
27 #include "ctdb_private.h"
38 if possible, make this task real time
40 void set_scheduler(void)
43 #if HAVE_THREAD_SETSCHED
44 struct thrdentry64 te
;
48 if (getthrds64(getpid(), &te
, sizeof(te
), &ti
, 1) != 1) {
49 DEBUG(DEBUG_ERR
, ("Unable to get thread information\n"));
53 if (thread_setsched(te
.ti_tid
, 0, SCHED_RR
) == -1) {
54 DEBUG(DEBUG_ERR
, ("Unable to set scheduler to SCHED_RR (%s)\n",
57 DEBUG(DEBUG_NOTICE
, ("Set scheduler to SCHED_RR\n"));
61 #if HAVE_SCHED_SETSCHEDULER
63 int policy
= SCHED_FIFO
;
67 #ifdef SCHED_RESET_ON_FORK
68 policy
|= SCHED_RESET_ON_FORK
;
70 if (sched_setscheduler(0, policy
, &p
) == -1) {
71 DEBUG(DEBUG_CRIT
,("Unable to set scheduler to SCHED_FIFO (%s)\n",
74 DEBUG(DEBUG_NOTICE
,("Set scheduler to SCHED_FIFO\n"));
81 reset scheduler from real-time to normal scheduling
83 void reset_scheduler(void)
86 #if HAVE_THREAD_SETSCHED
87 struct thrdentry64 te
;
91 if (getthrds64(getpid(), &te
, sizeof(te
), &ti
, 1) != 1) {
92 DEBUG(DEBUG_ERR
, ("Unable to get thread information\n"));
94 if (thread_setsched(te
.ti_tid
, 0, SCHED_OTHER
) == -1) {
95 DEBUG(DEBUG_ERR
, ("Unable to set scheduler to SCHED_OTHER\n"));
99 #if HAVE_SCHED_SETSCHEDULER
100 #ifndef SCHED_RESET_ON_FORK
101 struct sched_param p
;
103 p
.sched_priority
= 0;
104 if (sched_setscheduler(0, SCHED_OTHER
, &p
) == -1) {
105 DEBUG(DEBUG_ERR
, ("Unable to set scheduler to SCHED_OTHER\n"));
112 void set_nonblocking(int fd
)
116 v
= fcntl(fd
, F_GETFL
, 0);
118 DEBUG(DEBUG_WARNING
, ("Failed to get file status flags - %s\n",
122 if (fcntl(fd
, F_SETFL
, v
| O_NONBLOCK
) == -1) {
123 DEBUG(DEBUG_WARNING
, ("Failed to set non_blocking on fd - %s\n",
128 void set_close_on_exec(int fd
)
132 v
= fcntl(fd
, F_GETFD
, 0);
134 DEBUG(DEBUG_WARNING
, ("Failed to get file descriptor flags - %s\n",
138 if (fcntl(fd
, F_SETFD
, v
| FD_CLOEXEC
) != 0) {
139 DEBUG(DEBUG_WARNING
, ("Failed to set close_on_exec on fd - %s\n",
145 bool parse_ipv4(const char *s
, unsigned port
, struct sockaddr_in
*sin
)
147 sin
->sin_family
= AF_INET
;
148 sin
->sin_port
= htons(port
);
150 if (inet_pton(AF_INET
, s
, &sin
->sin_addr
) != 1) {
151 DEBUG(DEBUG_ERR
, (__location__
" Failed to translate %s into sin_addr\n", s
));
158 static bool parse_ipv6(const char *s
, const char *ifaces
, unsigned port
, ctdb_sock_addr
*saddr
)
160 saddr
->ip6
.sin6_family
= AF_INET6
;
161 saddr
->ip6
.sin6_port
= htons(port
);
162 saddr
->ip6
.sin6_flowinfo
= 0;
163 saddr
->ip6
.sin6_scope_id
= 0;
165 if (inet_pton(AF_INET6
, s
, &saddr
->ip6
.sin6_addr
) != 1) {
166 DEBUG(DEBUG_ERR
, (__location__
" Failed to translate %s into sin6_addr\n", s
));
170 if (ifaces
&& IN6_IS_ADDR_LINKLOCAL(&saddr
->ip6
.sin6_addr
)) {
171 if (strchr(ifaces
, ',')) {
172 DEBUG(DEBUG_ERR
, (__location__
" Link local address %s "
173 "is specified for multiple ifaces %s\n",
177 saddr
->ip6
.sin6_scope_id
= if_nametoindex(ifaces
);
186 bool parse_ip(const char *addr
, const char *ifaces
, unsigned port
, ctdb_sock_addr
*saddr
)
191 ZERO_STRUCTP(saddr
); /* valgrind :-) */
193 /* now is this a ipv4 or ipv6 address ?*/
194 p
= index(addr
, ':');
196 ret
= parse_ipv4(addr
, port
, &saddr
->ip
);
198 ret
= parse_ipv6(addr
, ifaces
, port
, saddr
);
207 bool parse_ip_mask(const char *str
, const char *ifaces
, ctdb_sock_addr
*addr
, unsigned *mask
)
210 char s
[64]; /* Much longer than INET6_ADDRSTRLEN */
218 if (len
>= sizeof(s
)) {
219 DEBUG(DEBUG_ERR
, ("Address %s is unreasonably long\n", str
));
223 strncpy(s
, str
, len
+1);
227 DEBUG(DEBUG_ERR
, (__location__
" This addr: %s does not contain a mask\n", s
));
231 *mask
= strtoul(p
+1, &endp
, 10);
232 if (endp
== NULL
|| *endp
!= 0) {
233 /* trailing garbage */
234 DEBUG(DEBUG_ERR
, (__location__
" Trailing garbage after the mask in %s\n", s
));
240 /* now is this a ipv4 or ipv6 address ?*/
241 ret
= parse_ip(s
, ifaces
, 0, addr
);
249 bool parse_ip_port(const char *addr
, ctdb_sock_addr
*saddr
)
252 char s
[64]; /* Much longer than INET6_ADDRSTRLEN */
259 if (len
>= sizeof(s
)) {
260 DEBUG(DEBUG_ERR
, ("Address %s is unreasonably long\n", addr
));
264 strncpy(s
, addr
, len
+1);
268 DEBUG(DEBUG_ERR
, (__location__
" This addr: %s does not contain a port number\n", s
));
272 port
= strtoul(p
+1, &endp
, 10);
273 if (endp
== NULL
|| *endp
!= 0) {
274 /* trailing garbage */
275 DEBUG(DEBUG_ERR
, (__location__
" Trailing garbage after the port in %s\n", s
));
280 /* now is this a ipv4 or ipv6 address ?*/
281 ret
= parse_ip(s
, NULL
, port
, saddr
);
286 /* we don't lock future pages here; it would increase the chance that
287 * we'd fail to mmap later on. */
288 void lockdown_memory(bool valgrinding
)
290 #if defined(HAVE_MLOCKALL) && !defined(_AIX_)
291 /* Extra stack, please! */
293 memset(dummy
, 0, sizeof(dummy
));
299 /* Ignore when running in local daemons mode */
304 /* Avoid compiler optimizing out dummy. */
305 mlock(dummy
, sizeof(dummy
));
306 if (mlockall(MCL_CURRENT
) != 0) {
307 DEBUG(DEBUG_WARNING
,("Failed to lockdown memory: %s'\n",
313 int mkdir_p(const char *dir
, int mode
)
319 if (strcmp(dir
, "/") == 0) {
323 if (strcmp(dir
, ".") == 0) {
327 /* Try to create directory */
328 ret
= mkdir(dir
, mode
);
329 /* Succeed if that worked or if it already existed */
330 if (ret
== 0 || errno
== EEXIST
) {
333 /* Fail on anything else except ENOENT */
334 if (errno
!= ENOENT
) {
338 /* Create ancestors */
340 if (len
>= PATH_MAX
) {
341 errno
= ENAMETOOLONG
;
344 strncpy(t
, dir
, len
+1);
346 ret
= mkdir_p(dirname(t
), mode
);
351 /* Create directory */
352 ret
= mkdir(dir
, mode
);
353 if ((ret
== -1) && (errno
== EEXIST
)) {
360 void mkdir_p_or_die(const char *dir
, int mode
)
364 ret
= mkdir_p(dir
, mode
);
367 ("ctdb exiting with error: "
368 "failed to create directory \"%s\" (%s)\n",
369 dir
, strerror(errno
)));
374 /* A read wrapper that will deal with EINTR. For now, copied from
375 * source3/lib/system.c
377 ssize_t
sys_read(int fd
, void *buf
, size_t count
)
382 ret
= read(fd
, buf
, count
);
383 #if defined(EWOULDBLOCK)
384 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
|| errno
== EWOULDBLOCK
));
386 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
391 /* A write wrapper that will deal with EINTR. For now, copied from
392 * source3/lib/system.c
394 ssize_t
sys_write(int fd
, const void *buf
, size_t count
)
399 ret
= write(fd
, buf
, count
);
400 #if defined(EWOULDBLOCK)
401 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
|| errno
== EWOULDBLOCK
));
403 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));