tools: Target and matches preparation function moves in iptables_test
[framework/connectivity/connman.git] / tools / alg-test.c
index e10e11d..a7404b7 100644 (file)
 #include <config.h>
 #endif
 
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <string.h>
 #include <sys/stat.h>
-#include <sys/mman.h>
+#include <sys/sendfile.h>
 #include <sys/socket.h>
 #include <linux/if_alg.h>
 
-static void build_hash(int sk, char *map, size_t size, const char *pathname)
+static void build_hash(int sk, int fd, size_t size, const char *pathname)
 {
        unsigned char hash[20];
        ssize_t written, length;
        int i;
 
-       written = send(sk, map, size, 0);
+       written = sendfile(sk, fd, NULL, size);
        if (written < 0)
                perror("Failed to write data");
 
@@ -58,10 +59,9 @@ static void build_hash(int sk, char *map, size_t size, const char *pathname)
 static int create_hash(int sk, const char *pathname)
 {
        struct stat st;
-       char *map;
        int fd;
 
-       fd = open(pathname, O_RDONLY);
+       fd = open(pathname, O_RDONLY | O_CLOEXEC);
        if (fd < 0)
                return -1;
 
@@ -70,15 +70,7 @@ static int create_hash(int sk, const char *pathname)
                return -1;
        }
 
-       map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
-       if (map == NULL || map == MAP_FAILED) {
-               close(fd);
-               return -1;
-       }
-
-       build_hash(sk, map, st.st_size, pathname);
-
-       munmap(map, st.st_size);
+       build_hash(sk, fd, st.st_size, pathname);
 
        close(fd);
 
@@ -94,7 +86,7 @@ static int create_socket(void)
        };
        int sk, nsk;
 
-       sk = socket(PF_ALG, SOCK_SEQPACKET, 0);
+       sk = socket(PF_ALG, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
        if (sk < 0) {
                perror("Failed to create socket");
                return -1;