-
Notifications
You must be signed in to change notification settings - Fork 0
/
tAF_LOCAL+SO_REUSEADDR.c
52 lines (40 loc) · 1.03 KB
/
tAF_LOCAL+SO_REUSEADDR.c
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* $ cc -o test test.c
* $ ./test /tmp/foo.sock
* $ ./test /tmp/foo.sock
* test: bind(): Address already in use
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int, char **);
int
main(int argc, char **argv)
{
struct sockaddr_un sun;
int s, opt;
if (argc != 2)
errx(EXIT_FAILURE, "Usage: %s <path>", argv[0]);
if ((s = socket(PF_LOCAL, SOCK_DGRAM, 0)) == -1)
err(EXIT_FAILURE, "socket()");
opt = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)) == -1)
err(EXIT_FAILURE, "setsockopt(SO_REUSEADDR)");
sun.sun_family = AF_UNIX;
sun.sun_len = sizeof(sun);
(void)strlcpy(sun.sun_path, argv[1], sizeof(sun.sun_path));
if (bind(s, (struct sockaddr *)&sun, sun.sun_len) != 0)
err(EXIT_FAILURE, "bind()");
/*
* XXX note an unlink(argv[1]) will _immediately_ remove the socket
* before the close() call below!
*/
(void)close(s);
exit(EXIT_SUCCESS);
/* NOTREACHED */
}