a95f314e66fc3606ffe6b71a0b07de31634df9e2
[vlendec/samba-autobuild/.git] / ctdb / common / system.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 #include "lib/util/mkdir_p.h"
45
46 /*
47   if possible, make this task real time
48  */
49 bool set_scheduler(void)
50 {
51 #ifdef _AIX_
52 #if HAVE_THREAD_SETSCHED
53         struct thrdentry64 te;
54         tid64_t ti;
55
56         ti = 0ULL;
57         if (getthrds64(getpid(), &te, sizeof(te), &ti, 1) != 1) {
58                 DEBUG(DEBUG_ERR, ("Unable to get thread information\n"));
59                 return false;
60         }
61
62         if (thread_setsched(te.ti_tid, 0, SCHED_RR) == -1) {
63                 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_RR (%s)\n",
64                                   strerror(errno)));
65                 return false;
66         } else {
67                 return true;
68         }
69 #endif
70 #else /* no AIX */
71 #if HAVE_SCHED_SETSCHEDULER
72         struct sched_param p;
73
74         p.sched_priority = 1;
75
76         if (sched_setscheduler(0, SCHED_FIFO, &p) == -1) {
77                 DEBUG(DEBUG_CRIT,("Unable to set scheduler to SCHED_FIFO (%s)\n",
78                          strerror(errno)));
79                 return false;
80         } else {
81                 return true;
82         }
83 #endif
84 #endif
85         DEBUG(DEBUG_CRIT,("No way to set real-time priority.\n"));
86         return false;
87 }
88
89 /*
90   reset scheduler from real-time to normal scheduling
91  */
92 void reset_scheduler(void)
93 {
94 #ifdef _AIX_
95 #if HAVE_THREAD_SETSCHED
96         struct thrdentry64 te;
97         tid64_t ti;
98
99         ti = 0ULL;
100         if (getthrds64(getpid(), &te, sizeof(te), &ti, 1) != 1) {
101                 DEBUG(DEBUG_ERR, ("Unable to get thread information\n"));
102         }
103         if (thread_setsched(te.ti_tid, 0, SCHED_OTHER) == -1) {
104                 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n"));
105         }
106 #endif
107 #else /* no AIX */
108 #if HAVE_SCHED_SETSCHEDULER
109         struct sched_param p;
110
111         p.sched_priority = 0;
112         if (sched_setscheduler(0, SCHED_OTHER, &p) == -1) {
113                 DEBUG(DEBUG_ERR, ("Unable to set scheduler to SCHED_OTHER\n"));
114         }
115 #endif
116 #endif
117 }
118
119 /* we don't lock future pages here; it would increase the chance that
120  * we'd fail to mmap later on. */
121 void lockdown_memory(bool valgrinding)
122 {
123 #if defined(HAVE_MLOCKALL) && !defined(_AIX_)
124         /* Extra stack, please! */
125         char dummy[10000];
126         memset(dummy, 0, sizeof(dummy));
127
128         if (valgrinding) {
129                 return;
130         }
131
132         /* Ignore when running in local daemons mode */
133         if (getuid() != 0) {
134                 return;
135         }
136
137         /* Avoid compiler optimizing out dummy. */
138         mlock(dummy, sizeof(dummy));
139         if (mlockall(MCL_CURRENT) != 0) {
140                 DEBUG(DEBUG_WARNING,("Failed to lockdown memory: %s'\n",
141                                      strerror(errno)));
142         }
143 #endif
144 }
145
146 void mkdir_p_or_die(const char *dir, int mode)
147 {
148         int ret;
149
150         ret = mkdir_p(dir, mode);
151         if (ret != 0) {
152                 DEBUG(DEBUG_ALERT,
153                       ("ctdb exiting with error: "
154                        "failed to create directory \"%s\" (%s)\n",
155                        dir, strerror(errno)));
156                 exit(1);
157         }
158 }
159
160 void ctdb_wait_for_process_to_exit(pid_t pid)
161 {
162         while (kill(pid, 0) == 0 || errno != ESRCH) {
163                 sleep(5);
164         }
165 }
166
167 #ifdef HAVE_AF_PACKET
168
169 bool ctdb_sys_check_iface_exists(const char *iface)
170 {
171         int s;
172         struct ifreq ifr;
173
174         s = socket(AF_PACKET, SOCK_RAW, 0);
175         if (s == -1){
176                 /* We don't know if the interface exists, so assume yes */
177                 DBG_ERR("Failed to open raw socket\n");
178                 return true;
179         }
180
181         strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
182         if (ioctl(s, SIOCGIFINDEX, &ifr) < 0 && errno == ENODEV) {
183                 DBG_ERR("Interface '%s' not found\n", iface);
184                 close(s);
185                 return false;
186         }
187         close(s);
188
189         return true;
190 }
191
192 #else /* HAVE_AF_PACKET */
193
194 bool ctdb_sys_check_iface_exists(const char *iface)
195 {
196         /* Not implemented: Interface always considered present */
197         return true;
198 }
199
200 #endif /* HAVE_AF_PACKET */
201
202 #ifdef HAVE_PEERCRED
203
204 int ctdb_get_peer_pid(const int fd, pid_t *peer_pid)
205 {
206         struct ucred cr;
207         socklen_t crl = sizeof(struct ucred);
208         int ret;
209
210         ret = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &crl);
211         if (ret == 0) {
212                 *peer_pid = cr.pid;
213         } else {
214                 *peer_pid = -1;
215         }
216         return ret;
217 }
218
219 #else /* HAVE_PEERCRED */
220
221 #ifdef _AIX_
222
223 int ctdb_get_peer_pid(const int fd, pid_t *peer_pid)
224 {
225         struct peercred_struct cr;
226         socklen_t crl = sizeof(struct peercred_struct);
227         int ret;
228
229         ret = getsockopt(fd, SOL_SOCKET, SO_PEERID, &cr, &crl);
230         if (ret == 0) {
231                 *peer_pid = cr.pid;
232         } else {
233                 *peer_pid = -1;
234         }
235         return ret;
236 }
237
238 #else /* _AIX_ */
239
240 int ctdb_get_peer_pid(const int fd, pid_t *peer_pid)
241 {
242         /* Not implemented */
243         *peer_pid = -1;
244         return ENOSYS;
245 }
246
247 #endif /* _AIX_ */
248
249 #endif /* HAVE_PEERCRED */