ctdb-scripts: Update statd-callout to try several configuration files
[vlendec/samba-autobuild/.git] / ctdb / common / system.c
index 53480dd0d6ce57e87e0a52d213dd4e603fded57d..ab1941124d7ed4c661cb8f1a163c8a3ae0636edb 100644 (file)
 #include "common/logging.h"
 #include "common/system.h"
 
-#if HAVE_SCHED_H
+#ifdef HAVE_SCHED_H
 #include <sched.h>
 #endif
 
-#if HAVE_PROCINFO_H
+#ifdef HAVE_PROCINFO_H
 #include <procinfo.h>
 #endif
 
@@ -49,7 +49,7 @@
 bool set_scheduler(void)
 {
 #ifdef _AIX_
-#if HAVE_THREAD_SETSCHED
+#ifdef HAVE_THREAD_SETSCHED
        struct thrdentry64 te;
        tid64_t ti;
 
@@ -68,7 +68,7 @@ bool set_scheduler(void)
        }
 #endif
 #else /* no AIX */
-#if HAVE_SCHED_SETSCHEDULER
+#ifdef HAVE_SCHED_SETSCHEDULER
        struct sched_param p;
 
        p.sched_priority = 1;
@@ -92,7 +92,7 @@ bool set_scheduler(void)
 void reset_scheduler(void)
 {
 #ifdef _AIX_
-#if HAVE_THREAD_SETSCHED
+#ifdef HAVE_THREAD_SETSCHED
        struct thrdentry64 te;
        tid64_t ti;
 
@@ -105,7 +105,7 @@ void reset_scheduler(void)
        }
 #endif
 #else /* no AIX */
-#if HAVE_SCHED_SETSCHEDULER
+#ifdef HAVE_SCHED_SETSCHEDULER
        struct sched_param p;
 
        p.sched_priority = 0;
@@ -143,23 +143,93 @@ void lockdown_memory(bool valgrinding)
 #endif
 }
 
-void mkdir_p_or_die(const char *dir, int mode)
+void ctdb_wait_for_process_to_exit(pid_t pid)
+{
+       while (kill(pid, 0) == 0 || errno != ESRCH) {
+               sleep(5);
+       }
+}
+
+#ifdef HAVE_AF_PACKET
+
+bool ctdb_sys_check_iface_exists(const char *iface)
+{
+       int s;
+       struct ifreq ifr;
+
+       s = socket(AF_PACKET, SOCK_RAW, 0);
+       if (s == -1){
+               /* We don't know if the interface exists, so assume yes */
+               DBG_ERR("Failed to open raw socket\n");
+               return true;
+       }
+
+       strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
+       if (ioctl(s, SIOCGIFINDEX, &ifr) < 0 && errno == ENODEV) {
+               DBG_ERR("Interface '%s' not found\n", iface);
+               close(s);
+               return false;
+       }
+       close(s);
+
+       return true;
+}
+
+#else /* HAVE_AF_PACKET */
+
+bool ctdb_sys_check_iface_exists(const char *iface)
+{
+       /* Not implemented: Interface always considered present */
+       return true;
+}
+
+#endif /* HAVE_AF_PACKET */
+
+#ifdef HAVE_PEERCRED
+
+int ctdb_get_peer_pid(const int fd, pid_t *peer_pid)
 {
+       struct ucred cr;
+       socklen_t crl = sizeof(struct ucred);
        int ret;
 
-       ret = mkdir_p(dir, mode);
-       if (ret != 0) {
-               DEBUG(DEBUG_ALERT,
-                     ("ctdb exiting with error: "
-                      "failed to create directory \"%s\" (%s)\n",
-                      dir, strerror(errno)));
-               exit(1);
+       ret = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &crl);
+       if (ret == 0) {
+               *peer_pid = cr.pid;
+       } else {
+               *peer_pid = -1;
        }
+       return ret;
 }
 
-void ctdb_wait_for_process_to_exit(pid_t pid)
+#else /* HAVE_PEERCRED */
+
+#ifdef _AIX_
+
+int ctdb_get_peer_pid(const int fd, pid_t *peer_pid)
 {
-       while (kill(pid, 0) == 0 || errno != ESRCH) {
-               sleep(5);
+       struct peercred_struct cr;
+       socklen_t crl = sizeof(struct peercred_struct);
+       int ret;
+
+       ret = getsockopt(fd, SOL_SOCKET, SO_PEERID, &cr, &crl);
+       if (ret == 0) {
+               *peer_pid = cr.pid;
+       } else {
+               *peer_pid = -1;
        }
+       return ret;
+}
+
+#else /* _AIX_ */
+
+int ctdb_get_peer_pid(const int fd, pid_t *peer_pid)
+{
+       /* Not implemented */
+       *peer_pid = -1;
+       return ENOSYS;
 }
+
+#endif /* _AIX_ */
+
+#endif /* HAVE_PEERCRED */