summaryrefslogtreecommitdiff
path: root/yarns/sockbind.c
blob: 2f656ba0b7243be9c3c2d98ad39b8565018eb7e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>

/*
 * There is currently no command within Busybox or otherwise to create named
 * UNIX sockets; this program is used to compensate for that.
 */

int main(int argc, char *argv[])
{
	int sfd, len;
	struct sockaddr_un sock = { .sun_family = AF_UNIX };

	if (argc != 2) {
		fprintf(stderr, "Usage: %s PATH\n", argv[0]);
		return EXIT_FAILURE;
	}

	len = sizeof(sock) - offsetof(struct sockaddr_un, sun_path);
	strncpy(sock.sun_path, argv[1], len);

	if ((sfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
		perror("socket");
		return EXIT_FAILURE;
	}

	if (bind(sfd, (struct sockaddr*)&sock, sizeof(sock)) == -1) {
		perror("bind");
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}