summaryrefslogtreecommitdiffstats
path: root/src/socket_test.c
blob: ac6ebeccd1738c55c4e613f32f6d6d9ce76b42a6 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
 * Creates named and anonymous sockets to test fuser
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>

int main(int argc, char *argv[])
{
    int skt;
    struct sockaddr_un name;

    if (argc < 2)
    {
        fprintf(stderr, "Usage:\n%s <socketpath>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    if ( (skt = socket(AF_UNIX, SOCK_SEQPACKET, 0)) < 0)
    {
        perror("socket");
        exit(EXIT_FAILURE);
    }

    memset(&name, 0, sizeof(name));

    name.sun_family = AF_UNIX;
    strncpy(name.sun_path, argv[1], sizeof(name.sun_path) -1);
    
    if ( bind(skt, (const struct sockaddr *) &name, sizeof name) < 0)
    {
        perror("bind");
        exit(EXIT_FAILURE);
    }

    if ( listen(skt, 5) < 0)
    {
        perror("listen");
        exit(EXIT_FAILURE);
    }

    if ( accept(skt, NULL, NULL) < 0)
    {
        perror("accept");
        exit(EXIT_FAILURE);
    }

//    sleep(100);
    exit(EXIT_SUCCESS);
}