summaryrefslogtreecommitdiff
path: root/socket.c
blob: 9b694872bde0704939607f74eea6c2642ac747cd (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*-*- 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);
        s->backlog = SOMAXCONN;

        return name_load_fragment_and_dropin(n);
}

static const char* listen_lookup(int type) {

        if (type == SOCK_STREAM)
                return "ListenStream";
        else if (type == SOCK_DGRAM)
                return "ListenDatagram";
        else if (type == SOCK_SEQPACKET)
                return "ListenSequentialPacket";

        assert_not_reached("Unkown socket type");
        return NULL;
}

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);
        SocketPort *p;

        assert(s);

        fprintf(f,
                "%sSocket State: %s\n"
                "%sBindIPv6Only: %s\n"
                "%sBacklog: %u\n",
                prefix, state_table[s->state],
                prefix, yes_no(s->bind_ipv6_only),
                prefix, s->backlog);

        LIST_FOREACH(p, s->ports) {

                if (p->type == SOCKET_SOCKET) {
                        const char *t;
                        int r;
                        char *k;

                        if ((r = socket_address_print(&p->address, &k)) < 0)
                                t = strerror(-r);
                        else
                                t = k;

                        fprintf(f, "%s%s: %s\n", prefix, listen_lookup(p->address.type), k);
                        free(k);
                } else
                        fprintf(f, "%sListenFIFO: %s\n", prefix, p->path);
        }

        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 int socket_start(Name *n) {
        return 0;
}

static int socket_stop(Name *n) {
        return 0;
}

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) {
        SocketExecCommand c;
        Socket *s = SOCKET(n);
        SocketPort *p;

        assert(s);

        while ((p = s->ports)) {
                LIST_REMOVE(SocketPort, s->ports, p);

                if (p->fd >= 0)
                        close_nointr(p->fd);
                free(p->path);
                free(p);
        }

        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 = socket_start,
        .stop = socket_stop,
        .reload = NULL,

        .active_state = socket_active_state,

        .free_hook = socket_free_hook
};