summaryrefslogtreecommitdiffstats
path: root/httpd-2.4.27-systemd.patch
blob: 26aac4873a6f7a6bc001a4ecc80283dfd20c7993 (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
183
184
185
186
187
188
diff -uap httpd-2.4.27/modules/arch/unix/config5.m4.systemd httpd-2.4.27/modules/arch/unix/config5.m4
diff -uap httpd-2.4.27/modules/arch/unix/config5.m4.systemd httpd-2.4.27/modules/arch/unix/config5.m4
diff -uap httpd-2.4.27/modules/arch/unix/config5.m4.systemd httpd-2.4.27/modules/arch/unix/config5.m4
diff -uap httpd-2.4.27/modules/arch/unix/config5.m4.systemd httpd-2.4.27/modules/arch/unix/config5.m4
--- httpd-2.4.27/modules/arch/unix/config5.m4.systemd
+++ httpd-2.4.27/modules/arch/unix/config5.m4
@@ -18,6 +18,16 @@
   fi
 ])
 
+APACHE_MODULE(systemd, Systemd support, , , all, [
+  if test "${ac_cv_header_systemd_sd_daemon_h}" = "no" || test -z "${SYSTEMD_LIBS}"; then
+    AC_MSG_WARN([Your system does not support systemd.])
+    enable_systemd="no"
+  else
+    APR_ADDTO(MOD_SYSTEMD_LDADD, [$SYSTEMD_LIBS])
+    enable_systemd="yes"
+  fi
+])
+
 APR_ADDTO(INCLUDES, [-I\$(top_srcdir)/$modpath_current])
 
 APACHE_MODPATH_FINISH
diff -uap httpd-2.4.27/modules/arch/unix/mod_systemd.c.systemd httpd-2.4.27/modules/arch/unix/mod_systemd.c
--- httpd-2.4.27/modules/arch/unix/mod_systemd.c.systemd
+++ httpd-2.4.27/modules/arch/unix/mod_systemd.c
@@ -0,0 +1,161 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ */
+
+#include <stdint.h>
+#include <ap_config.h>
+#include "ap_mpm.h"
+#include <http_core.h>
+#include <httpd.h>
+#include <http_log.h>
+#include <apr_version.h>
+#include <apr_pools.h>
+#include <apr_strings.h>
+#include "unixd.h"
+#include "scoreboard.h"
+#include "mpm_common.h"
+
+#include "systemd/sd-daemon.h"
+
+#if APR_HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+static int shutdown_timer = 0;
+static int shutdown_counter = 0;
+static unsigned long bytes_served;
+static pid_t mainpid;
+
+static int systemd_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
+                              apr_pool_t *ptemp)
+{
+    sd_notify(0,
+              "RELOADING=1\n"
+              "STATUS=Reading configuration...\n");
+    ap_extended_status = 1;
+    return OK;
+}
+
+static int systemd_pre_mpm(apr_pool_t *p, ap_scoreboard_e sb_type)
+{
+    int rv;
+
+    mainpid = getpid();
+
+    rv = sd_notifyf(0, "READY=1\n"
+                    "STATUS=Processing requests...\n"
+                    "MAINPID=%" APR_PID_T_FMT, mainpid);
+    if (rv < 0) {
+        ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p, APLOGNO(02395)
+                     "sd_notifyf returned an error %d", rv);
+    }
+
+    return OK;
+}
+
+static int systemd_monitor(apr_pool_t *p, server_rec *s)
+{
+    ap_sload_t sload;
+    apr_interval_time_t up_time;
+    char bps[5];
+    int rv;
+
+    if (!ap_extended_status) {
+        /* Nothing useful to report if ExtendedStatus disabled. */
+        return DECLINED;
+    }
+    
+    ap_get_sload(&sload);
+    /* up_time in seconds */
+    up_time = (apr_uint32_t) apr_time_sec(apr_time_now() -
+                               ap_scoreboard_image->global->restart_time);
+
+    apr_strfsize((unsigned long)((float) (sload.bytes_served)
+                                 / (float) up_time), bps);
+
+    rv = sd_notifyf(0, "READY=1\n"
+                    "STATUS=Total requests: %lu; Idle/Busy workers %d/%d;"
+                    "Requests/sec: %.3g; Bytes served/sec: %sB/sec\n",
+                    sload.access_count, sload.idle, sload.busy,
+                    ((float) sload.access_count) / (float) up_time, bps);
+
+    if (rv < 0) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02396)
+                     "sd_notifyf returned an error %d", rv);
+    }
+
+    /* Shutdown httpd when nothing is sent for shutdown_timer seconds. */
+    if (sload.bytes_served == bytes_served) {
+        /* mpm_common.c: INTERVAL_OF_WRITABLE_PROBES is 10 */
+        shutdown_counter += 10;
+        if (shutdown_timer > 0 && shutdown_counter >= shutdown_timer) {
+            rv = sd_notifyf(0, "READY=1\n"
+                            "STATUS=Stopped as result of IdleShutdown "
+                            "timeout.");
+            if (rv < 0) {
+                ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02804)
+                            "sd_notifyf returned an error %d", rv);
+            }
+            kill(mainpid, AP_SIG_GRACEFUL);
+        }
+    }
+    else {
+        shutdown_counter = 0;
+    }
+
+    bytes_served = sload.bytes_served;
+
+    return DECLINED;
+}
+
+static void systemd_register_hooks(apr_pool_t *p)
+{
+    /* Enable ap_extended_status. */
+    ap_hook_pre_config(systemd_pre_config, NULL, NULL, APR_HOOK_LAST);
+    /* We know the PID in this hook ... */
+    ap_hook_pre_mpm(systemd_pre_mpm, NULL, NULL, APR_HOOK_LAST);
+    /* Used to update httpd's status line using sd_notifyf */
+    ap_hook_monitor(systemd_monitor, NULL, NULL, APR_HOOK_MIDDLE);
+}
+
+static const char *set_shutdown_timer(cmd_parms *cmd, void *dummy,
+                                      const char *arg)
+{
+    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+    if (err != NULL) {
+        return err;
+    }
+
+    shutdown_timer = atoi(arg);
+    return NULL;
+}
+
+static const command_rec systemd_cmds[] =
+{
+AP_INIT_TAKE1("IdleShutdown", set_shutdown_timer, NULL, RSRC_CONF,
+     "Number of seconds in idle-state after which httpd is shutdown"),
+    {NULL}
+};
+
+AP_DECLARE_MODULE(systemd) = {
+    STANDARD20_MODULE_STUFF,
+    NULL,
+    NULL,
+    NULL,
+    NULL,
+    systemd_cmds,
+    systemd_register_hooks,
+};