fstests: Add an auxiliary program to create an AF_UNIX socket
authorDavid Howells <dhowells@redhat.com>
Mon, 10 Apr 2017 13:32:44 +0000 (14:32 +0100)
committerEryu Guan <eguan@redhat.com>
Tue, 11 Apr 2017 04:27:43 +0000 (12:27 +0800)
Add an auxiliary program to create an AF_UNIX socket at the
specified location so that tests can do things with it.

Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Eryu Guan <eguan@redhat.com>
.gitignore
src/Makefile
src/af_unix.c [new file with mode: 0644]

index 1ed2a92fbd9f6b659e76ce0def79e8a900b5db16..8a7c0523a8cfa432ba098c24887980efff7adfe4 100644 (file)
@@ -35,6 +35,7 @@
 /ltp/iogen
 
 # src/ binaries
+/src/af_unix
 /src/alloc
 /src/append_reader
 /src/append_writer
index a7f27f02f95f1e9a535a84432d4453ef3a4d0197..716c1783e6085b816e4942349aa3af73bcbddce9 100644 (file)
@@ -12,7 +12,7 @@ TARGETS = dirstress fill fill2 getpagesize holes lstat64 \
        godown resvtest writemod makeextents itrash rename \
        multi_open_unlink dmiperf unwritten_sync genhashnames t_holes \
        t_mmap_writev t_truncate_cmtime dirhash_collide t_rename_overwrite \
-       holetest t_truncate_self t_mmap_dio
+       holetest t_truncate_self t_mmap_dio af_unix
 
 LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
        preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \
diff --git a/src/af_unix.c b/src/af_unix.c
new file mode 100644 (file)
index 0000000..dc2368e
--- /dev/null
@@ -0,0 +1,66 @@
+/* Create an AF_UNIX socket.
+ *
+ * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells@redhat.com)
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public Licence
+ * as published by the Free Software Foundation; either version
+ * 2 of the Licence, or (at your option) any later version.
+ */
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
+
+int main(int argc, char *argv[])
+{
+       struct sockaddr_un sun;
+       struct stat st;
+       size_t len, max;
+       int fd;
+
+       if (argc != 2) {
+               fprintf(stderr, "Format: %s <socketpath>\n", argv[0]);
+               exit(2);
+       }
+
+       max = sizeof(sun.sun_path);
+       len = strlen(argv[1]);
+       if (len >= max) {
+               fprintf(stderr, "Filename too long (max %zu)\n", max);
+               exit(2);
+       }
+
+       fd = socket(AF_UNIX, SOCK_DGRAM, 0);
+       if (fd < 0) {
+               perror("socket");
+               exit(1);
+       }
+
+       memset(&sun, 0, sizeof(sun));
+       sun.sun_family = AF_UNIX;
+       strcpy(sun.sun_path, argv[1]);
+       if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
+               perror("bind");
+               exit(1);
+       }
+
+       if (stat(argv[1], &st)) {
+               fprintf(stderr, "Couldn't stat socket after creation: %m\n");
+               exit(1);
+       }
+
+       exit(0);
+}