]> Sergey Matveev's repositories - bfs.git/blob - tests/mksock.c
Skip mtab
[bfs.git] / tests / mksock.c
1 // Copyright © Tavian Barnes <tavianator@tavianator.com>
2 // SPDX-License-Identifier: 0BSD
3
4 /**
5  * There's no standard Unix utility that creates a socket file, so this small
6  * program does the job.
7  */
8
9 #include "../src/bfstd.h"
10 #include <errno.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/socket.h>
15 #include <sys/un.h>
16 #include <unistd.h>
17
18 /**
19  * Print an error message.
20  */
21 static void errmsg(const char *cmd, const char *path) {
22         fprintf(stderr, "%s: '%s': %s.\n", cmd, path, xstrerror(errno));
23 }
24
25 /**
26  * struct sockaddr_un::sun_path is very short, so we chdir() into the target
27  * directory before creating sockets in case the full path is too long but the
28  * file name is not.
29  */
30 static int chdir_parent(const char *path) {
31         char *dir = xdirname(path);
32         if (!dir) {
33                 return -1;
34         }
35
36         int ret = chdir(dir);
37         free(dir);
38         return ret;
39 }
40
41 /**
42  * Initialize a struct sockaddr_un with the right filename.
43  */
44 static int init_sun(struct sockaddr_un *sock, const char *path) {
45         size_t len = strlen(path);
46         if (len == 0 || path[len - 1] == '/') {
47                 errno = ENOENT;
48                 return -1;
49         }
50
51         char *base = xbasename(path);
52         if (!base) {
53                 return -1;
54         }
55
56         len = strlen(base);
57         if (len >= sizeof(sock->sun_path)) {
58                 free(base);
59                 errno = ENAMETOOLONG;
60                 return -1;
61         }
62
63         sock->sun_family = AF_UNIX;
64         memcpy(sock->sun_path, base, len + 1);
65         free(base);
66         return 0;
67 }
68
69 int main(int argc, char *argv[]) {
70         const char *cmd = argc > 0 ? argv[0] : "mksock";
71
72         if (argc != 2) {
73                 fprintf(stderr, "Usage: %s NAME\n", cmd);
74                 return EXIT_FAILURE;
75         }
76
77         const char *path = argv[1];
78
79         if (chdir_parent(path) != 0) {
80                 errmsg(cmd, path);
81                 return EXIT_FAILURE;
82         }
83
84         struct sockaddr_un sock;
85         if (init_sun(&sock, path) != 0) {
86                 errmsg(cmd, path);
87                 return EXIT_FAILURE;
88         }
89
90         int fd = socket(AF_UNIX, SOCK_STREAM, 0);
91         if (fd < 0) {
92                 errmsg(cmd, path);
93                 return EXIT_FAILURE;
94         }
95
96         int ret = EXIT_SUCCESS;
97
98         if (bind(fd, (struct sockaddr *)&sock, sizeof(sock)) != 0) {
99                 errmsg(cmd, path);
100                 ret = EXIT_FAILURE;
101         }
102
103         if (xclose(fd) != 0) {
104                 errmsg(cmd, path);
105                 ret = EXIT_FAILURE;
106         }
107
108         return ret;
109 }