httpd-2.4.3-mod_systemd.patch 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. --- httpd-2.4.3/modules/arch/unix/config5.m4.systemd
  2. +++ httpd-2.4.3/modules/arch/unix/config5.m4
  3. @@ -18,6 +18,19 @@ APACHE_MODULE(privileges, Per-virtualhos
  4. fi
  5. ])
  6. +
  7. +APACHE_MODULE(systemd, Systemd support, , , $unixd_mods_enabled, [
  8. + AC_CHECK_LIB(systemd-daemon, sd_notify, SYSTEMD_LIBS="-lsystemd-daemon")
  9. + AC_CHECK_HEADERS(systemd/sd-daemon.h, [ap_HAVE_SD_DAEMON_H="yes"], [ap_HAVE_SD_DAEMON_H="no"])
  10. + if test $ap_HAVE_SD_DAEMON_H = "no" || test -z "${SYSTEMD_LIBS}"; then
  11. + AC_MSG_WARN([Your system does not support systemd.])
  12. + enable_systemd="no"
  13. + else
  14. + APR_ADDTO(MOD_SYSTEMD_LDADD, [$SYSTEMD_LIBS])
  15. + enable_systemd="yes"
  16. + fi
  17. +])
  18. +
  19. APR_ADDTO(INCLUDES, [-I\$(top_srcdir)/$modpath_current])
  20. APACHE_MODPATH_FINISH
  21. --- httpd-2.4.3/modules/arch/unix/mod_systemd.c.systemd
  22. +++ httpd-2.4.3/modules/arch/unix/mod_systemd.c
  23. @@ -0,0 +1,138 @@
  24. +/* Licensed to the Apache Software Foundation (ASF) under one or more
  25. + * contributor license agreements. See the NOTICE file distributed with
  26. + * this work for additional information regarding copyright ownership.
  27. + * The ASF licenses this file to You under the Apache License, Version 2.0
  28. + * (the "License"); you may not use this file except in compliance with
  29. + * the License. You may obtain a copy of the License at
  30. + *
  31. + * http://www.apache.org/licenses/LICENSE-2.0
  32. + *
  33. + * Unless required by applicable law or agreed to in writing, software
  34. + * distributed under the License is distributed on an "AS IS" BASIS,
  35. + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  36. + * See the License for the specific language governing permissions and
  37. + * limitations under the License.
  38. + *
  39. + */
  40. +
  41. +#include <stdint.h>
  42. +#include <ap_config.h>
  43. +#include "ap_mpm.h"
  44. +#include <http_core.h>
  45. +#include <http_log.h>
  46. +#include <apr_version.h>
  47. +#include <apr_pools.h>
  48. +#include <apr_strings.h>
  49. +#include "unixd.h"
  50. +#include "scoreboard.h"
  51. +#include "mpm_common.h"
  52. +
  53. +#include "systemd/sd-daemon.h"
  54. +
  55. +#if APR_HAVE_UNISTD_H
  56. +#include <unistd.h>
  57. +#endif
  58. +
  59. +#define KBYTE 1024
  60. +
  61. +static pid_t pid; /* PID of the main httpd instance */
  62. +static int server_limit, thread_limit, threads_per_child, max_servers;
  63. +static time_t last_update_time;
  64. +static unsigned long last_update_access;
  65. +static unsigned long last_update_kbytes;
  66. +
  67. +static int systemd_pre_mpm(apr_pool_t *p, ap_scoreboard_e sb_type)
  68. +{
  69. + int rv;
  70. + last_update_time = time(0);
  71. +
  72. + ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit);
  73. + ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &server_limit);
  74. + ap_mpm_query(AP_MPMQ_MAX_THREADS, &threads_per_child);
  75. + /* work around buggy MPMs */
  76. + if (threads_per_child == 0)
  77. + threads_per_child = 1;
  78. + ap_mpm_query(AP_MPMQ_MAX_DAEMONS, &max_servers);
  79. +
  80. + pid = getpid();
  81. +
  82. + rv = sd_notifyf(0, "READY=1\n"
  83. + "STATUS=Processing requests...\n"
  84. + "MAINPID=%lu",
  85. + (unsigned long) pid);
  86. + if (rv < 0) {
  87. + ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p,
  88. + "sd_notifyf returned an error %d", rv);
  89. + }
  90. +
  91. + return OK;
  92. +}
  93. +
  94. +static int systemd_monitor(apr_pool_t *p, server_rec *s)
  95. +{
  96. + int i, j, res, rv;
  97. + process_score *ps_record;
  98. + worker_score *ws_record;
  99. + unsigned long access = 0;
  100. + unsigned long bytes = 0;
  101. + unsigned long kbytes = 0;
  102. + char bps[5];
  103. + time_t now = time(0);
  104. + time_t elapsed = now - last_update_time;
  105. +
  106. + for (i = 0; i < server_limit; ++i) {
  107. + ps_record = ap_get_scoreboard_process(i);
  108. + for (j = 0; j < thread_limit; ++j) {
  109. + ws_record = ap_get_scoreboard_worker_from_indexes(i, j);
  110. + if (ap_extended_status && !ps_record->quiescing && ps_record->pid) {
  111. + res = ws_record->status;
  112. + if (ws_record->access_count != 0 ||
  113. + (res != SERVER_READY && res != SERVER_DEAD)) {
  114. + access += ws_record->access_count;
  115. + bytes += ws_record->bytes_served;
  116. + if (bytes >= KBYTE) {
  117. + kbytes += (bytes >> 10);
  118. + bytes = bytes & 0x3ff;
  119. + }
  120. + }
  121. + }
  122. + }
  123. + }
  124. +
  125. + apr_strfsize((unsigned long)(KBYTE *(float) (kbytes - last_update_kbytes)
  126. + / (float) elapsed), bps);
  127. +
  128. + rv = sd_notifyf(0, "READY=1\n"
  129. + "STATUS=Total requests: %lu; Current requests/sec: %.3g; "
  130. + "Current traffic: %sB/sec\n", access,
  131. + ((float)access - last_update_access) / (float) elapsed, bps);
  132. + if (rv < 0) {
  133. + ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00000)
  134. + "sd_notifyf returned an error %d", rv);
  135. + }
  136. +
  137. + last_update_access = access;
  138. + last_update_kbytes = kbytes;
  139. + last_update_time = now;
  140. +
  141. + return DECLINED;
  142. +}
  143. +
  144. +static void systemd_register_hooks(apr_pool_t *p)
  145. +{
  146. + /* We know the PID in this hook ... */
  147. + ap_hook_pre_mpm(systemd_pre_mpm, NULL, NULL, APR_HOOK_LAST);
  148. + /* Used to update httpd's status line using sd_notifyf */
  149. + ap_hook_monitor(systemd_monitor, NULL, NULL, APR_HOOK_MIDDLE);
  150. +}
  151. +
  152. +module AP_MODULE_DECLARE_DATA systemd_module =
  153. +{
  154. + STANDARD20_MODULE_STUFF,
  155. + NULL,
  156. + NULL,
  157. + NULL,
  158. + NULL,
  159. + NULL,
  160. + systemd_register_hooks,
  161. +};