summaryrefslogtreecommitdiff
path: root/socket.c
blob: 9f8718447d28d21fc3bc8bf7a921105bc681a217 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*-*- Mode: C; c-basic-offset: 8 -*-*/

#include "name.h"
#include "socket.h"

static int socket_load(Name *n) {
        Socket *s = SOCKET(n);

        exec_context_defaults(&s->exec_context);

        return name_load_fragment_and_dropin(n);
}

static void socket_dump(Name *n, FILE *f, const char *prefix) {

        static const char* const state_table[_SOCKET_STATE_MAX] = {
                [SOCKET_DEAD] = "dead",
                [SOCKET_START_PRE] = "start-pre",
                [SOCKET_START_POST] = "start-post",
                [SOCKET_LISTENING] = "listening",
                [SOCKET_RUNNING] = "running",
                [SOCKET_STOP_PRE] = "stop-pre",
                [SOCKET_STOP_POST] = "stop-post",
                [SOCKET_MAINTAINANCE] = "maintainance"
        };

        static const char* const command_table[_SOCKET_EXEC_MAX] = {
                [SOCKET_EXEC_START_PRE] = "StartPre",
                [SOCKET_EXEC_START_POST] = "StartPost",
                [SOCKET_EXEC_STOP_PRE] = "StopPre",
                [SOCKET_EXEC_STOP_POST] = "StopPost"
        };

        SocketExecCommand c;
        Socket *s = SOCKET(n);
        const char *t;
        int r;
        char *k;

        assert(s);

        if ((r = address_print(&n->socket.address, &k)) < 0)
                t = strerror(-r);
        else
                t = k;

        fprintf(f,
                "%sSocket State: %s\n"
                "%sAddress: %s\n",
                prefix, state_table[s->state],
                prefix, t);

        free(k);

        exec_context_dump(&s->exec_context, f, prefix);

        for (c = 0; c < _SOCKET_EXEC_MAX; c++) {
                ExecCommand *i;

                LIST_FOREACH(i, s->exec_command[c])
                        fprintf(f, "%s%s: %s\n", prefix, command_table[c], i->path);
        }
}

static NameActiveState socket_active_state(Name *n) {

        static const NameActiveState table[_SOCKET_STATE_MAX] = {
                [SOCKET_DEAD] = NAME_INACTIVE,
                [SOCKET_START_PRE] = NAME_ACTIVATING,
                [SOCKET_START_POST] = NAME_ACTIVATING,
                [SOCKET_LISTENING] = NAME_ACTIVE,
                [SOCKET_RUNNING] = NAME_ACTIVE,
                [SOCKET_STOP_PRE] = NAME_DEACTIVATING,
                [SOCKET_STOP_POST] = NAME_DEACTIVATING,
                [SOCKET_MAINTAINANCE] = NAME_INACTIVE,
        };

        return table[SOCKET(n)->state];
}

static void socket_free_hook(Name *n) {
        unsigned i;
        SocketExecCommand c;
        Socket *s = SOCKET(n);

        assert(s);

        for (i = 0; i < s->n_fds; i++)
                close_nointr(s->fds[i]);

        exec_context_free(&s->exec_context);

        for (c = 0; c < _SOCKET_EXEC_MAX; c++)
                exec_command_free_list(s->exec_command[c]);

        if (s->service)
                s->service->socket = NULL;
}

const NameVTable socket_vtable = {
        .suffix = ".socket",

        .load = socket_load,
        .dump = socket_dump,

        .start = NULL,
        .stop = NULL,
        .reload = NULL,

        .active_state = socket_active_state,

        .free_hook = socket_free_hook
};