ctdb-common: Use documented names for protocol family in socket()
[samba.git] / ctdb / common / system_linux.c
index 97a57ac8b6596c47414185472defb13e9446e044..d523617086e989108ea008498f80d33910fb0017 100644 (file)
    along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "includes.h"
+#include "replace.h"
 #include "system/network.h"
 #include "system/filesys.h"
 #include "system/wait.h"
-#include "../include/ctdb_private.h"
+
+#include "lib/util/debug.h"
+
+#include "protocol/protocol.h"
+
 #include <netinet/if_ether.h>
 #include <netinet/ip6.h>
 #include <netinet/icmp6.h>
@@ -30,6 +34,9 @@
 #include <netpacket/packet.h>
 #include <sys/prctl.h>
 
+#include "common/logging.h"
+#include "common/system.h"
+
 #ifndef ETHERTYPE_IP6
 #define ETHERTYPE_IP6 0x86dd
 #endif
@@ -93,14 +100,14 @@ int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface)
 
        switch (addr->ip.sin_family) {
        case AF_INET:
-               s = socket(PF_PACKET, SOCK_RAW, htons(ETHERTYPE_ARP));
+               s = socket(AF_PACKET, SOCK_RAW, ETHERTYPE_ARP);
                if (s == -1){
                        DEBUG(DEBUG_CRIT,(__location__ " failed to open raw socket\n"));
                        return -1;
                }
 
                DEBUG(DEBUG_DEBUG, (__location__ " Created SOCKET FD:%d for sending arp\n", s));
-               strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name)-1);
+               strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
                if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
                        DEBUG(DEBUG_CRIT,(__location__ " interface '%s' not found\n", iface));
                        close(s);
@@ -187,7 +194,7 @@ int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface)
                close(s);
                break;
        case AF_INET6:
-               s = socket(PF_PACKET, SOCK_RAW, htons(ETHERTYPE_ARP));
+               s = socket(AF_PACKET, SOCK_RAW, ETHERTYPE_ARP);
                if (s == -1){
                        DEBUG(DEBUG_CRIT,(__location__ " failed to open raw socket\n"));
                        return -1;
@@ -240,7 +247,13 @@ int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface)
                ip6->ip6_hlim = 255;
                ip6->ip6_src  = addr->ip6.sin6_addr;
                /* all-nodes multicast */
-               inet_pton(AF_INET6, "ff02::1", &ip6->ip6_dst);
+
+               ret = inet_pton(AF_INET6, "ff02::1", &ip6->ip6_dst);
+               if (ret != 1) {
+                       close(s);
+                       DEBUG(DEBUG_CRIT,(__location__ " failed inet_pton\n"));
+                       return -1;
+               }
 
                nd_na = (struct nd_neighbor_advert *)(ip6+1);
                nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
@@ -357,7 +370,7 @@ int ctdb_sys_send_tcp(const ctdb_sock_addr *dest,
                ip4pkt.tcp.check    = tcp_checksum((uint16_t *)&ip4pkt.tcp, sizeof(ip4pkt.tcp), &ip4pkt.ip);
 
                /* open a raw socket to send this segment from */
-               s = socket(AF_INET, SOCK_RAW, htons(IPPROTO_RAW));
+               s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
                if (s == -1) {
                        DEBUG(DEBUG_CRIT,(__location__ " failed to open raw socket (%s)\n",
                                 strerror(errno)));
@@ -406,13 +419,13 @@ int ctdb_sys_send_tcp(const ctdb_sock_addr *dest,
                ip6pkt.tcp.window   = htons(1234);
                ip6pkt.tcp.check    = tcp_checksum6((uint16_t *)&ip6pkt.tcp, sizeof(ip6pkt.tcp), &ip6pkt.ip6);
 
-               s = socket(PF_INET6, SOCK_RAW, IPPROTO_RAW);
+               s = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
                if (s == -1) {
                        DEBUG(DEBUG_CRIT, (__location__ " Failed to open sending socket\n"));
                        return -1;
 
                }
-               /* sendto() dont like if the port is set and the socket is
+               /* sendto() don't like if the port is set and the socket is
                   in raw mode.
                */
                tmpdest = discard_const(dest);
@@ -447,7 +460,7 @@ int ctdb_sys_open_capture_socket(const char *iface, void **private_data)
        int s;
 
        /* Open a socket to capture all traffic */
-       s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
+       s = socket(AF_PACKET, SOCK_RAW, ETH_P_ALL);
        if (s == -1) {
                DEBUG(DEBUG_CRIT,(__location__ " failed to open raw socket\n"));
                return -1;
@@ -568,9 +581,9 @@ bool ctdb_sys_check_iface_exists(const char *iface)
        int s;
        struct ifreq ifr;
 
-       s = socket(PF_PACKET, SOCK_RAW, 0);
+       s = socket(AF_PACKET, SOCK_RAW, 0);
        if (s == -1){
-               /* We dont know if the interface exists, so assume yes */
+               /* We don't know if the interface exists, so assume yes */
                DEBUG(DEBUG_CRIT,(__location__ " failed to open raw socket\n"));
                return true;
        }
@@ -596,192 +609,3 @@ int ctdb_get_peer_pid(const int fd, pid_t *peer_pid)
        }
        return ret;
 }
-
-/*
- * Find the process name from process ID
- */
-char *ctdb_get_process_name(pid_t pid)
-{
-       char path[32];
-       char buf[PATH_MAX];
-       char *ptr;
-       int n;
-
-       snprintf(path, sizeof(path), "/proc/%d/exe", pid);
-       n = readlink(path, buf, sizeof(buf)-1);
-       if (n < 0) {
-               return NULL;
-       }
-
-       /* Remove any extra fields */
-       buf[n] = '\0';
-       ptr = strtok(buf, " ");
-       return (ptr == NULL ? ptr : strdup(ptr));
-}
-
-/*
- * Set process name
- */
-int ctdb_set_process_name(const char *name)
-{
-       char procname[16];
-
-       strncpy(procname, name, 15);
-       procname[15] = '\0';
-       return prctl(PR_SET_NAME, (unsigned long)procname, 0, 0, 0);
-}
-
-/*
- * Parsing a line from /proc/locks,
- */
-static bool parse_proc_locks_line(char *line, pid_t *pid,
-                                 struct ctdb_lock_info *curlock)
-{
-       char *ptr, *saveptr;
-
-       /* output of /proc/locks
-        *
-        * lock assigned
-        * 1: POSIX  ADVISORY  WRITE 25945 fd:00:6424820 212 212
-        *
-        * lock waiting
-        * 1: -> POSIX  ADVISORY  WRITE 25946 fd:00:6424820 212 212
-        */
-
-       /* Id: */
-       ptr = strtok_r(line, " ", &saveptr);
-       if (ptr == NULL) return false;
-
-       /* -> */
-       ptr = strtok_r(NULL, " ", &saveptr);
-       if (ptr == NULL) return false;
-       if (strcmp(ptr, "->") == 0) {
-               curlock->waiting = true;
-               ptr = strtok_r(NULL, " ", &saveptr);
-       } else {
-               curlock->waiting = false;
-       }
-
-       /* POSIX */
-       if (ptr == NULL || strcmp(ptr, "POSIX") != 0) {
-               return false;
-       }
-
-       /* ADVISORY */
-       ptr = strtok_r(NULL, " ", &saveptr);
-       if (ptr == NULL) return false;
-
-       /* WRITE */
-       ptr = strtok_r(NULL, " ", &saveptr);
-       if (ptr == NULL) return false;
-       if (strcmp(ptr, "READ") == 0) {
-               curlock->read_only = true;
-       } else if (strcmp(ptr, "WRITE") == 0) {
-               curlock->read_only = false;
-       } else {
-               return false;
-       }
-
-       /* PID */
-       ptr = strtok_r(NULL, " ", &saveptr);
-       if (ptr == NULL) return false;
-       *pid = atoi(ptr);
-
-       /* MAJOR:MINOR:INODE */
-       ptr = strtok_r(NULL, " :", &saveptr);
-       if (ptr == NULL) return false;
-       ptr = strtok_r(NULL, " :", &saveptr);
-       if (ptr == NULL) return false;
-       ptr = strtok_r(NULL, " :", &saveptr);
-       if (ptr == NULL) return false;
-       curlock->inode = atol(ptr);
-
-       /* START OFFSET */
-       ptr = strtok_r(NULL, " ", &saveptr);
-       if (ptr == NULL) return false;
-       curlock->start = atol(ptr);
-
-       /* END OFFSET */
-       ptr = strtok_r(NULL, " ", &saveptr);
-       if (ptr == NULL) return false;
-       if (strncmp(ptr, "EOF", 3) == 0) {
-               curlock->end = (off_t)-1;
-       } else {
-               curlock->end = atol(ptr);
-       }
-
-       return true;
-}
-
-/*
- * Find information of lock being waited on for given process ID
- */
-bool ctdb_get_lock_info(pid_t req_pid, struct ctdb_lock_info *lock_info)
-{
-       FILE *fp;
-       struct ctdb_lock_info curlock;
-       pid_t pid;
-       char buf[1024];
-       bool status = false;
-
-       if ((fp = fopen("/proc/locks", "r")) == NULL) {
-               DEBUG(DEBUG_ERR, ("Failed to read locks information"));
-               return false;
-       }
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
-               if (! parse_proc_locks_line(buf, &pid, &curlock)) {
-                       continue;
-               }
-               if (pid == req_pid && curlock.waiting) {
-                       *lock_info = curlock;
-                       status = true;
-                       break;
-               }
-       }
-       fclose(fp);
-
-       return status;
-}
-
-/*
- * Find process ID which holds an overlapping byte lock for required
- * inode and byte range.
- */
-bool ctdb_get_blocker_pid(struct ctdb_lock_info *reqlock, pid_t *blocker_pid)
-{
-       FILE *fp;
-       struct ctdb_lock_info curlock;
-       pid_t pid;
-       char buf[1024];
-       bool status = false;
-
-       if ((fp = fopen("/proc/locks", "r")) == NULL) {
-               DEBUG(DEBUG_ERR, ("Failed to read locks information"));
-               return false;
-       }
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
-               if (! parse_proc_locks_line(buf, &pid, &curlock)) {
-                       continue;
-               }
-
-               if (curlock.waiting) {
-                       continue;
-               }
-
-               if (curlock.inode != reqlock->inode) {
-                       continue;
-               }
-
-               if (curlock.start > reqlock->end ||
-                   curlock.end < reqlock->start) {
-                       /* Outside the required range */
-                       continue;
-               }
-               *blocker_pid = pid;
-               status = true;
-               break;
-       }
-       fclose(fp);
-
-       return status;
-}