summaryrefslogtreecommitdiff
path: root/service.c
blob: ac9c39c3c83d16dfd88b69c045e0483b6771b945 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*-*- Mode: C; c-basic-offset: 8 -*-*/

#include <errno.h>

#include "name.h"
#include "service.h"
#include "load-fragment.h"
#include "load-dropin.h"

static int service_load_sysv(Service *s) {
        assert(s);

        /* Load service data from SysV init scripts, preferably with
         * LSB headers ... */

        return -ENOENT;
}

static int service_load(Name *n) {
        int r;
        Service *s = SERVICE(n);

        assert(s);

        exec_context_defaults(&s->exec_context);

        /* Load a .service file */
        r = name_load_fragment(n);

        /* Load a classic init script as a fallback */
        if (r == -ENOENT)
                r = service_load_sysv(s);

        if (r < 0)
                return r;

        /* Load dropin directory data */
        if ((r = name_load_dropin(n)) < 0)
                return r;

        return 0;
}

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

        static const char* const state_table[_SERVICE_STATE_MAX] = {
                [SERVICE_DEAD] = "dead",
                [SERVICE_START_PRE] = "start-pre",
                [SERVICE_START] = "start",
                [SERVICE_START_POST] = "post",
                [SERVICE_RUNNING] = "running",
                [SERVICE_RELOAD_PRE] = "reload-pre",
                [SERVICE_RELOAD] = "reload",
                [SERVICE_RELOAD_POST] = "reload-post",
                [SERVICE_STOP_PRE] = "stop-pre",
                [SERVICE_STOP] = "stop",
                [SERVICE_SIGTERM] = "sigterm",
                [SERVICE_SIGKILL] = "sigkill",
                [SERVICE_STOP_POST] = "stop-post",
                [SERVICE_MAINTAINANCE] = "maintainance"
        };

        static const char* const command_table[_SERVICE_EXEC_MAX] = {
                [SERVICE_EXEC_START_PRE] = "StartPre",
                [SERVICE_EXEC_START] = "Start",
                [SERVICE_EXEC_START_POST] = "StartPost",
                [SERVICE_EXEC_RELOAD_PRE] = "ReloadPre",
                [SERVICE_EXEC_RELOAD] = "Reload",
                [SERVICE_EXEC_RELOAD_POST] = "ReloadPost",
                [SERVICE_EXEC_STOP_PRE] = "StopPre",
                [SERVICE_EXEC_STOP] = "Stop",
                [SERVICE_EXEC_STOP_POST] = "StopPost",
        };

        ServiceExecCommand c;
        Service *s = SERVICE(n);

        assert(s);

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

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

        for (c = 0; c < _SERVICE_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 service_set_state(Service *s, ServiceState state) {
        assert(s);

        s->state = state;
        return 0;
}

static int service_start(Name *n) {
        Service *s = SERVICE(n);

        assert(s);

        /* We cannot fulfill this request right now */
        if (s->state == SERVICE_STOP_PRE ||
            s->state == SERVICE_STOP ||
            s->state == SERVICE_SIGTERM ||
            s->state == SERVICE_SIGKILL ||
            s->state == SERVICE_STOP_POST)
                return -EAGAIN;

        assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE);

        return service_set_state(s, SERVICE_START_PRE);
}

static int service_stop(Name *n) {
        Service *s = SERVICE(n);

        assert(s);


        return 0;
}

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

static NameActiveState service_active_state(Name *n) {

        static const NameActiveState table[_SERVICE_STATE_MAX] = {
                [SERVICE_DEAD] = NAME_INACTIVE,
                [SERVICE_START_PRE] = NAME_ACTIVATING,
                [SERVICE_START] = NAME_ACTIVATING,
                [SERVICE_START_POST] = NAME_ACTIVATING,
                [SERVICE_RUNNING] = NAME_ACTIVE,
                [SERVICE_RELOAD_PRE] = NAME_ACTIVE_RELOADING,
                [SERVICE_RELOAD] = NAME_ACTIVE_RELOADING,
                [SERVICE_RELOAD_POST] = NAME_ACTIVE_RELOADING,
                [SERVICE_STOP_PRE] = NAME_DEACTIVATING,
                [SERVICE_STOP] = NAME_DEACTIVATING,
                [SERVICE_SIGTERM] = NAME_DEACTIVATING,
                [SERVICE_SIGKILL] = NAME_DEACTIVATING,
                [SERVICE_STOP_POST] = NAME_DEACTIVATING,
                [SERVICE_MAINTAINANCE] = NAME_INACTIVE,
        };

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

static void service_free_hook(Name *n) {
        Service *s = SERVICE(n);
        unsigned c;

        assert(s);

        exec_context_free(&s->exec_context);

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

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

const NameVTable service_vtable = {
        .suffix = ".service",

        .load = service_load,
        .dump = service_dump,

        .start = service_start,
        .stop = service_stop,
        .reload = service_reload,

        .active_state = service_active_state,

        .free_hook = service_free_hook
};