aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 567a441965efde902a0a27ca0e5076c105398408 (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
56
57
58
59
60
61
62
63
64
65
#include "service_provider.h"
#include "test.h"

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

int bind_unix_socket(const char *fname)
{
    int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);

    struct sockaddr_un addr;
    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, fname, sizeof(addr.sun_path) - 1);
    if(bind(sockfd, (struct sockaddr*) &addr, sizeof(addr)) < 0)
        return -1;

    return sockfd;
}

static const char *cleanup_socket = NULL;
void cleanup(void)
{
    if(cleanup_socket)
        unlink(cleanup_socket);
}

void sigint(int sig)
{
    cleanup();
    exit(1);
}

void run_tests(void)
{
    crypto_test();
    tm_test();
    sp_test();
}

int main()
{
    run_tests();

    const char *socket_name = "socket";
    int sockfd;
    if((sockfd = bind_unix_socket(socket_name)) < 0)
    {
        perror("bind");
        return 1;
    }

    cleanup_socket = socket_name;

    atexit(cleanup);
    signal(SIGINT, sigint);

    sp_main(sockfd);
}