src/open_by_handle: verify dir content only with -r flag
[xfstests-dev.git] / src / af_unix.c
1 /* Create an AF_UNIX socket.
2  *
3  * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  */
11
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdbool.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24
25 #define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)
26
27 int main(int argc, char *argv[])
28 {
29         struct sockaddr_un sun;
30         struct stat st;
31         size_t len, max;
32         int fd;
33
34         if (argc != 2) {
35                 fprintf(stderr, "Format: %s <socketpath>\n", argv[0]);
36                 exit(2);
37         }
38
39         max = sizeof(sun.sun_path);
40         len = strlen(argv[1]);
41         if (len >= max) {
42                 fprintf(stderr, "Filename too long (max %zu)\n", max);
43                 exit(2);
44         }
45
46         fd = socket(AF_UNIX, SOCK_DGRAM, 0);
47         if (fd < 0) {
48                 perror("socket");
49                 exit(1);
50         }
51
52         memset(&sun, 0, sizeof(sun));
53         sun.sun_family = AF_UNIX;
54         strcpy(sun.sun_path, argv[1]);
55         if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
56                 perror("bind");
57                 exit(1);
58         }
59
60         if (stat(argv[1], &st)) {
61                 fprintf(stderr, "Couldn't stat socket after creation: %m\n");
62                 exit(1);
63         }
64
65         exit(0);
66 }