1 /* Copyright (C) 2021 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 3 of the License, or
6 (at your option) any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 #include "knot/common/systemd.h"
21 #include "contrib/strtonum.h"
22
23 #ifdef ENABLE_SYSTEMD
24 #include <systemd/sd-daemon.h>
25
26 #define ZONE_LOAD_TIMEOUT_DEFAULT 60
27
28 static int zone_load_timeout_s;
29
systemd_zone_load_timeout(void)30 static int systemd_zone_load_timeout(void)
31 {
32 const char *timeout = getenv("KNOT_ZONE_LOAD_TIMEOUT_SEC");
33
34 int out;
35 if (timeout != NULL && timeout[0] != '\0' &&
36 str_to_int(timeout, &out, 0, 24 * 3600) == KNOT_EOK) {
37 return out;
38 } else {
39 return ZONE_LOAD_TIMEOUT_DEFAULT;
40 }
41 }
42 #endif
43
systemd_zone_load_timeout_notify(void)44 void systemd_zone_load_timeout_notify(void)
45 {
46 #ifdef ENABLE_SYSTEMD
47 if (zone_load_timeout_s == 0) {
48 zone_load_timeout_s = systemd_zone_load_timeout();
49 }
50 sd_notifyf(0, "EXTEND_TIMEOUT_USEC=%d000000", zone_load_timeout_s);
51 #endif
52 }
53
systemd_tasks_status_notify(int tasks)54 void systemd_tasks_status_notify(int tasks)
55 {
56 #ifdef ENABLE_SYSTEMD
57 if (tasks > 0) {
58 sd_notifyf(0, "STATUS=Waiting for %d tasks to finish...", tasks);
59 } else {
60 sd_notify(0, "STATUS=");
61 }
62 #endif
63 }
64
systemd_ready_notify(void)65 void systemd_ready_notify(void)
66 {
67 #ifdef ENABLE_SYSTEMD
68 sd_notify(0, "READY=1\nSTATUS=");
69 #endif
70 }
71
systemd_reloading_notify(void)72 void systemd_reloading_notify(void)
73 {
74 #ifdef ENABLE_SYSTEMD
75 sd_notify(0, "RELOADING=1\nSTATUS=");
76 #endif
77 }
78
systemd_stopping_notify(void)79 void systemd_stopping_notify(void)
80 {
81 #ifdef ENABLE_SYSTEMD
82 sd_notify(0, "STOPPING=1\nSTATUS=");
83 #endif
84 }
85