aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 15eec9e651ba415059df541f1e9376668bc98aba (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#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);
#ifndef DUMMY
    sp_save();
#endif
}

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

#ifndef DUMMY
void run_tests(void)
{
    crypto_test();
    tm_test();
    sp_test();
}
#endif

int main(int argc, char *argv[])
{
#ifndef DUMMY
    //run_tests();
#endif

    bool overwrite = false;

    /* TODO: real parsing */
    const char *dbpath = "csaa.db";
    int logleaves = 10;
    if(argc >= 2)
        logleaves = atol(argv[1]);
    if(argc >= 3)
        dbpath = argv[2];
    if(argc >= 4)
        if(!strcmp(argv[3], "--overwrite"))
            overwrite = true;

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

    printf("Listening on socket '%s'...\n", socket_name);

    cleanup_socket = socket_name;

    atexit(cleanup);
    signal(SIGINT, signal_handler);
    signal(SIGTERM, signal_handler);
    signal(SIGSEGV, signal_handler);
    signal(SIGABRT, signal_handler);

    sp_main(sockfd, logleaves, dbpath, overwrite);
}