From 6b91a6a966690af87dc77c8df3d0f7d6a7f62e37 Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Tue, 19 Nov 2019 12:47:52 +0100 Subject: [PATCH] TT#36285 support sd_notify() * added ngcp-kamailio-systemd-modules Change-Id: I91ff7f18fdbee0a9bac47cead9a84ed2374076e5 --- debian/control | 24 +- debian/ngcp-kamailio.kamailio-lb.service | 2 +- debian/ngcp-kamailio.kamailio-proxy.service | 2 +- debian/patches/series | 7 + ...upport-for-basic-void-core-callbacks.patch | 60 +++ ...es-callbacks-on-app-ready-and-shutdo.patch | 33 ++ ...ups-added-systemdops-module-to-syste.patch | 21 ++ ...odule-to-facilitate-integration-with.patch | 347 ++++++++++++++++++ ...stemdops-proper-check-for-null-value.patch | 25 ++ ...s-set-core-callbacks-in-mod_register.patch | 28 ++ debian/rules | 1 + 11 files changed, 547 insertions(+), 3 deletions(-) create mode 100644 debian/patches/upstream/core-events-support-for-basic-void-core-callbacks.patch create mode 100644 debian/patches/upstream/core-main-executes-callbacks-on-app-ready-and-shutdo.patch create mode 100644 debian/patches/upstream/src-Makefile.groups-added-systemdops-module-to-syste.patch create mode 100644 debian/patches/upstream/systemdops-new-module-to-facilitate-integration-with.patch create mode 100644 debian/patches/upstream/systemdops-proper-check-for-null-value.patch create mode 100644 debian/patches/upstream/systemdops-set-core-callbacks-in-mod_register.patch diff --git a/debian/control b/debian/control index 34d39f61c..874ef5333 100644 --- a/debian/control +++ b/debian/control @@ -39,6 +39,7 @@ Build-Depends: libsnmp-dev, libsqlite3-dev, libssl-dev, + libsystemd-dev, libunistring-dev, libxml2-dev, ngcp-libswrate-dev (>= 1.1.2+0~1364315461.svn13284.127), @@ -93,8 +94,8 @@ Suggests: Breaks: ngcp-kamailio-carrierroute-modules, Provides: - sip-router, ngcp-kamailio-carrierroute-modules, + sip-router, Description: very fast and configurable SIP proxy Kamailio is a very fast and flexible SIP (RFC3261) proxy server. Written entirely in C, Kamailio can handle thousands calls @@ -767,6 +768,27 @@ Description: erlang modules for Kamailio This module provides interact with Erlang node. The module allows sending, receiving Erlang messages and RPC calls between each other. +Package: ngcp-kamailio-systemd-modules +Architecture: linux-any +Multi-Arch: same +Pre-Depends: + ${misc:Pre-Depends}, +Depends: + ngcp-kamailio (= ${binary:Version}), + ${misc:Depends}, + ${shlibs:Depends}, +Conflicts: + kamailio-systemd-modules, +Replaces: + kamailio-systemd-modules, +Description: systemd logging modules for the Kamailio SIP server + Kamailio is a very fast and flexible SIP (RFC3261) + server. Written entirely in C, Kamailio can handle thousands calls + per second even on low-budget hardware. + . + This package provides logging to systemd journal directly from the + Kamailio configuration and routing scripts. + Package: ngcp-kamailio-extra-modules Architecture: any Multi-Arch: same diff --git a/debian/ngcp-kamailio.kamailio-lb.service b/debian/ngcp-kamailio.kamailio-lb.service index a99252c52..1ffa5b248 100644 --- a/debian/ngcp-kamailio.kamailio-lb.service +++ b/debian/ngcp-kamailio.kamailio-lb.service @@ -10,7 +10,7 @@ Requires=redis-server.service Wants=remote-fs.target [Service] -Type=simple +Type=notify User=kamailio Group=kamailio PIDFile=/run/kamailio/kamailio.lb.pid diff --git a/debian/ngcp-kamailio.kamailio-proxy.service b/debian/ngcp-kamailio.kamailio-proxy.service index 0290036b4..700ab1119 100644 --- a/debian/ngcp-kamailio.kamailio-proxy.service +++ b/debian/ngcp-kamailio.kamailio-proxy.service @@ -10,7 +10,7 @@ Wants=remote-fs.target Requires=mariadb.service [Service] -Type=simple +Type=notify User=kamailio Group=kamailio RuntimeDirectory=kamailio diff --git a/debian/patches/series b/debian/patches/series index 6e6953353..300b0cdcf 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -33,6 +33,13 @@ sipwise/sca-fix-notify-after-bye.patch ## Ongoing Patches sipwise/rtpengine-balancing.patch sipwise/openssl-locking.patch +## backport from kamailio trunk (5.4) +upstream/core-events-support-for-basic-void-core-callbacks.patch +upstream/core-main-executes-callbacks-on-app-ready-and-shutdo.patch +upstream/systemdops-new-module-to-facilitate-integration-with.patch +upstream/src-Makefile.groups-added-systemdops-module-to-syste.patch +upstream/systemdops-set-core-callbacks-in-mod_register.patch +upstream/systemdops-proper-check-for-null-value.patch ## backport from kamailio upstream (5.2) sipwise/tt41650.patch sipwise/rtpengine-trickle-ice-sdp.patch diff --git a/debian/patches/upstream/core-events-support-for-basic-void-core-callbacks.patch b/debian/patches/upstream/core-events-support-for-basic-void-core-callbacks.patch new file mode 100644 index 000000000..eb82a3724 --- /dev/null +++ b/debian/patches/upstream/core-events-support-for-basic-void-core-callbacks.patch @@ -0,0 +1,60 @@ +From 99403c83f2163350eda3b76180947b1fddcdf500 Mon Sep 17 00:00:00 2001 +From: Daniel-Constantin Mierla +Date: Thu, 21 Nov 2019 13:40:21 +0100 +Subject: [PATCH] core: events - support for basic void core callbacks + +- can be used to allow modules execute their code from core for specific needs +--- + src/core/events.c | 13 +++++++++++++ + src/core/events.h | 16 ++++++++++++++++ + 2 files changed, 29 insertions(+) + +diff --git a/src/core/events.c b/src/core/events.c +index 5f71cd8f2..70d717969 100644 +--- a/src/core/events.c ++++ b/src/core/events.c +@@ -362,3 +362,16 @@ int sr_event_enabled(int type) + return 0; + } + ++ ++/** ++ * ++ */ ++static sr_corecb_t _ksr_corecb = {0}; ++ ++/** ++ * ++ */ ++sr_corecb_t *sr_corecb_get(void) ++{ ++ return &_ksr_corecb; ++} +diff --git a/src/core/events.h b/src/core/events.h +index 9c4c54a2e..6282e65a5 100644 +--- a/src/core/events.h ++++ b/src/core/events.h +@@ -79,4 +79,20 @@ int sr_event_enabled(int type); + void sr_core_ert_init(void); + void sr_core_ert_run(sip_msg_t *msg, int e); + ++typedef void (*sr_corecb_void_f)(void); ++typedef struct sr_corecb { ++ sr_corecb_void_f app_ready; ++ sr_corecb_void_f app_shutdown; ++} sr_corecb_t; ++ ++sr_corecb_t *sr_corecb_get(void); ++ ++#define sr_corecb_void_exec(fname) \ ++ do { \ ++ sr_corecb_t *__cbp = sr_corecb_get(); \ ++ if(__cbp && __cbp->fname) { \ ++ __cbp->fname(); \ ++ } \ ++ } while(0); ++ + #endif +-- +2.20.1 + diff --git a/debian/patches/upstream/core-main-executes-callbacks-on-app-ready-and-shutdo.patch b/debian/patches/upstream/core-main-executes-callbacks-on-app-ready-and-shutdo.patch new file mode 100644 index 000000000..c544816c2 --- /dev/null +++ b/debian/patches/upstream/core-main-executes-callbacks-on-app-ready-and-shutdo.patch @@ -0,0 +1,33 @@ +From 03aa6556ef59c0b274c78f2edc2231c6d2ed333b Mon Sep 17 00:00:00 2001 +From: Daniel-Constantin Mierla +Date: Thu, 21 Nov 2019 13:41:38 +0100 +Subject: [PATCH] core: main - executes callbacks on app ready and shutdown + +--- + src/main.c | 3 +++ + 1 file changed, 3 insertions(+) + +diff --git a/src/main.c b/src/main.c +index c8ee3ad11..a5a007053 100644 +--- a/src/main.c ++++ b/src/main.c +@@ -684,6 +684,8 @@ static void sig_alarm_abort(int signo) + + static void shutdown_children(int sig, int show_status) + { ++ sr_corecb_void_exec(app_shutdown); ++ + kill_all_children(sig); + if (set_sig_h(SIGALRM, sig_alarm_kill) == SIG_ERR ) { + LM_ERR("could not install SIGALARM handler\n"); +@@ -1788,6 +1790,7 @@ int main_loop(void) + cfg_ok=1; + + *_sr_instance_started = 1; ++ sr_corecb_void_exec(app_ready); + + #ifdef EXTRA_DEBUG + for (r=0; r<*process_count; r++){ +-- +2.20.1 + diff --git a/debian/patches/upstream/src-Makefile.groups-added-systemdops-module-to-syste.patch b/debian/patches/upstream/src-Makefile.groups-added-systemdops-module-to-syste.patch new file mode 100644 index 000000000..42fb455e8 --- /dev/null +++ b/debian/patches/upstream/src-Makefile.groups-added-systemdops-module-to-syste.patch @@ -0,0 +1,21 @@ +From: Daniel-Constantin Mierla +Date: Thu, 21 Nov 2019 13:44:35 +0100 +Subject: src/Makefile.groups: added systemdops module to systemd group + +--- + src/Makefile.groups | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/src/Makefile.groups b/src/Makefile.groups +index 33cbf0a..53d8cc6 100644 +--- a/src/Makefile.groups ++++ b/src/Makefile.groups +@@ -199,7 +199,7 @@ mod_list_cnxcc=cnxcc + mod_list_erlang=erlang + + # - modules depending on systemd library +-mod_list_systemd=log_systemd ++mod_list_systemd=log_systemd systemdops + + # - modules depending on libtcap library + mod_list_tcap=tcap diff --git a/debian/patches/upstream/systemdops-new-module-to-facilitate-integration-with.patch b/debian/patches/upstream/systemdops-new-module-to-facilitate-integration-with.patch new file mode 100644 index 000000000..d72978923 --- /dev/null +++ b/debian/patches/upstream/systemdops-new-module-to-facilitate-integration-with.patch @@ -0,0 +1,347 @@ +From 0c48e1621c88bf1d390e4cfb82e52b2d6339b837 Mon Sep 17 00:00:00 2001 +From: Daniel-Constantin Mierla +Date: Thu, 21 Nov 2019 13:42:14 +0100 +Subject: [PATCH] systemdops: new module to facilitate integration with systemd + +--- + src/modules/systemdops/Makefile | 23 ++++++ + src/modules/systemdops/README | 77 +++++++++++++++++++ + src/modules/systemdops/doc/Makefile | 4 + + src/modules/systemdops/doc/systemdops.xml | 36 +++++++++ + .../systemdops/doc/systemdops_admin.xml | 76 ++++++++++++++++++ + src/modules/systemdops/systemdops_mod.c | 72 +++++++++++++++++ + 6 files changed, 288 insertions(+) + create mode 100644 src/modules/systemdops/Makefile + create mode 100644 src/modules/systemdops/README + create mode 100644 src/modules/systemdops/doc/Makefile + create mode 100644 src/modules/systemdops/doc/systemdops.xml + create mode 100644 src/modules/systemdops/doc/systemdops_admin.xml + create mode 100644 src/modules/systemdops/systemdops_mod.c + +diff --git a/src/modules/systemdops/Makefile b/src/modules/systemdops/Makefile +new file mode 100644 +index 000000000..26e15cd39 +--- /dev/null ++++ b/src/modules/systemdops/Makefile +@@ -0,0 +1,23 @@ ++# ++# WARNING: do not run this directly, it should be run by the master Makefile ++ ++include ../../Makefile.defs ++auto_gen= ++NAME=systemdops.so ++ ++ifeq ($(CROSS_COMPILE),) ++SYSTEMD_BUILDER=$(shell \ ++ if pkg-config --exists libsystemd; then \ ++ echo 'pkg-config libsystemd'; \ ++ fi) ++endif ++ ++ifneq ($(SYSTEMD_BUILDER),) ++ DEFS += $(shell $(SYSTEMD_BUILDER) --cflags) ++ LIBS += $(shell $(SYSTEMD_BUILDER) --libs) ++else ++ DEFS += -I$(LOCALBASE)/include ++ LIBS= -L$(LOCALBASE)/lib -lsystemd ++endif ++ ++include ../../Makefile.modules +diff --git a/src/modules/systemdops/README b/src/modules/systemdops/README +new file mode 100644 +index 000000000..c93df6102 +--- /dev/null ++++ b/src/modules/systemdops/README +@@ -0,0 +1,77 @@ ++SYSTEMDOPS Module ++ ++Daniel-Constantin Mierla ++ ++ ++ ++Edited by ++ ++Daniel-Constantin Mierla ++ ++ ++ ++ Copyright © 2019 asipto.com ++ __________________________________________________________________ ++ ++ Table of Contents ++ ++ 1. Admin Guide ++ ++ 1. Overview ++ 2. Dependencies ++ ++ 2.1. Kamailio Modules ++ 2.2. External Libraries or Applications ++ ++ 3. Systemd Notifications ++ ++ List of Examples ++ ++ 1.1. Systemd Notifications ++ ++Chapter 1. Admin Guide ++ ++ Table of Contents ++ ++ 1. Overview ++ 2. Dependencies ++ ++ 2.1. Kamailio Modules ++ 2.2. External Libraries or Applications ++ ++ 3. Systemd Notifications ++ ++1. Overview ++ ++ It provides a collection of features to make easier the integration ++ with systemd. ++ ++2. Dependencies ++ ++ 2.1. Kamailio Modules ++ 2.2. External Libraries or Applications ++ ++2.1. Kamailio Modules ++ ++ The following modules must be loaded before this module: ++ * none. ++ ++2.2. External Libraries or Applications ++ ++ The following libraries or applications must be installed before ++ running Kamailio with this module loaded: ++ * libsystemd ++ ++3. Systemd Notifications ++ ++ If loaded, the module will generated systemd notifications when ++ Kamailio starts and when Kamailio shuts down. The content of these ++ notifications (sent with sd_notify(...)) are shown in the next example ++ (_PID_ is replaced by the PID of the main process). ++ ++ Example 1.1. Systemd Notifications ++... ++READY=1\nMAINPID=_PID_ ++... ++STOPPING=1 ++... +diff --git a/src/modules/systemdops/doc/Makefile b/src/modules/systemdops/doc/Makefile +new file mode 100644 +index 000000000..51a2fbb9d +--- /dev/null ++++ b/src/modules/systemdops/doc/Makefile +@@ -0,0 +1,4 @@ ++docs = systemdops.xml ++ ++docbook_dir = ../../../../doc/docbook ++include $(docbook_dir)/Makefile.module +diff --git a/src/modules/systemdops/doc/systemdops.xml b/src/modules/systemdops/doc/systemdops.xml +new file mode 100644 +index 000000000..bf15611d2 +--- /dev/null ++++ b/src/modules/systemdops/doc/systemdops.xml +@@ -0,0 +1,36 @@ ++ ++ ++ ++%docentities; ++ ++]> ++ ++ ++ ++ SYSTEMDOPS Module ++ kamailio.org ++ ++ ++ Daniel-Constantin ++ Mierla ++ miconda@gmail.com ++ ++ ++ Daniel-Constantin ++ Mierla ++ miconda@gmail.com ++ ++ ++ ++ 2019 ++ asipto.com ++ ++ ++ ++ ++ ++ ++ +diff --git a/src/modules/systemdops/doc/systemdops_admin.xml b/src/modules/systemdops/doc/systemdops_admin.xml +new file mode 100644 +index 000000000..7af4cc3d9 +--- /dev/null ++++ b/src/modules/systemdops/doc/systemdops_admin.xml +@@ -0,0 +1,76 @@ ++ ++ ++ ++%docentities; ++ ++]> ++ ++ ++ ++ ++ &adminguide; ++ ++
++ Overview ++ ++ It provides a collection of features to make easier the integration ++ with systemd. ++ ++
++ ++
++ Dependencies ++
++ &kamailio; Modules ++ ++ The following modules must be loaded before this module: ++ ++ ++ ++ none. ++ ++ ++ ++ ++
++
++ External Libraries or Applications ++ ++ The following libraries or applications must be installed before running ++ &kamailio; with this module loaded: ++ ++ ++ ++ libsystemd ++ ++ ++ ++ ++
++
++ ++
++ Systemd Notifications ++ ++ If loaded, the module will generated systemd notifications when &kamailio; ++ starts and when &kamailio; shuts down. The content of these notifications ++ (sent with sd_notify(...)) are shown in the next example (_PID_ is ++ replaced by the PID of the main process). ++ ++ ++ <function>Systemd Notifications</function> ++ ++... ++READY=1\nMAINPID=_PID_ ++... ++STOPPING=1 ++... ++ ++ ++
++ ++
++ +diff --git a/src/modules/systemdops/systemdops_mod.c b/src/modules/systemdops/systemdops_mod.c +new file mode 100644 +index 000000000..58d7a2b24 +--- /dev/null ++++ b/src/modules/systemdops/systemdops_mod.c +@@ -0,0 +1,72 @@ ++/** ++ * Copyright (C) 2019 Daniel-Constantin Mierla (asipto.com) ++ * ++ * This file is part of Kamailio, a free SIP server. ++ * ++ * This file is free software; you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation; either version 2 of the License, or ++ * (at your option) any later version ++ * ++ * ++ * This file is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with this program; if not, write to the Free Software ++ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ++ * ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++ ++#include "../../core/sr_module.h" ++#include "../../core/dprint.h" ++#include "../../core/events.h" ++#include "../../core/globals.h" ++ ++ ++MODULE_VERSION ++ ++struct module_exports exports = { ++ "systemdops", /* module name */ ++ DEFAULT_DLFLAGS, /* dlopen flags */ ++ 0, /* cmd (cfg function) exports */ ++ 0, /* param exports */ ++ 0, /* RPC method exports */ ++ 0, /* pseudo-variables exports */ ++ 0, /* response handling function */ ++ 0, /* module init function */ ++ 0, /* per-child init function */ ++ 0 /* module destroy function */ ++}; ++ ++/** ++ * ++ */ ++void ksr_sd_app_ready(void) ++{ ++ sd_notifyf(0, "READY=1\nMAINPID=%lu", (unsigned long)creator_pid); ++} ++ ++/** ++ * ++ */ ++void ksr_sd_app_shutdown(void) ++{ ++ sd_notify(0, "STOPPING=1"); ++} ++ ++/** ++ * module registration function ++ */ ++int mod_register(char *path, int *dlflags, void *p1, void *p2) ++{ ++ return 0; ++} +-- +2.20.1 + diff --git a/debian/patches/upstream/systemdops-proper-check-for-null-value.patch b/debian/patches/upstream/systemdops-proper-check-for-null-value.patch new file mode 100644 index 000000000..d1c1d16d8 --- /dev/null +++ b/debian/patches/upstream/systemdops-proper-check-for-null-value.patch @@ -0,0 +1,25 @@ +From fffd892b7d18734c8f60d2eebec5d4a15a0fdc26 Mon Sep 17 00:00:00 2001 +From: Daniel-Constantin Mierla +Date: Fri, 22 Nov 2019 09:42:08 +0100 +Subject: [PATCH] systemdops: proper check for null value + +--- + src/modules/systemdops/systemdops_mod.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/src/modules/systemdops/systemdops_mod.c b/src/modules/systemdops/systemdops_mod.c +index 0ac15426d..8c24ac186 100644 +--- a/src/modules/systemdops/systemdops_mod.c ++++ b/src/modules/systemdops/systemdops_mod.c +@@ -69,7 +69,7 @@ void ksr_sd_app_shutdown(void) + int mod_register(char *path, int *dlflags, void *p1, void *p2) + { + sr_corecb_t *cbp = sr_corecb_get(); +- if(cbp) { ++ if(cbp==NULL) { + return -1; + } + cbp->app_ready = ksr_sd_app_ready; +-- +2.20.1 + diff --git a/debian/patches/upstream/systemdops-set-core-callbacks-in-mod_register.patch b/debian/patches/upstream/systemdops-set-core-callbacks-in-mod_register.patch new file mode 100644 index 000000000..fb24a85c9 --- /dev/null +++ b/debian/patches/upstream/systemdops-set-core-callbacks-in-mod_register.patch @@ -0,0 +1,28 @@ +From d4fc8b0168ba3de78e29deb7c7d7ed9b3fd29a36 Mon Sep 17 00:00:00 2001 +From: Daniel-Constantin Mierla +Date: Fri, 22 Nov 2019 09:14:06 +0100 +Subject: [PATCH] systemdops: set core callbacks in mod_register() + +--- + src/modules/systemdops/systemdops_mod.c | 6 ++++++ + 1 file changed, 6 insertions(+) + +diff --git a/src/modules/systemdops/systemdops_mod.c b/src/modules/systemdops/systemdops_mod.c +index 58d7a2b24..0ac15426d 100644 +--- a/src/modules/systemdops/systemdops_mod.c ++++ b/src/modules/systemdops/systemdops_mod.c +@@ -68,5 +68,11 @@ void ksr_sd_app_shutdown(void) + */ + int mod_register(char *path, int *dlflags, void *p1, void *p2) + { ++ sr_corecb_t *cbp = sr_corecb_get(); ++ if(cbp) { ++ return -1; ++ } ++ cbp->app_ready = ksr_sd_app_ready; ++ cbp->app_shutdown = ksr_sd_app_shutdown; + return 0; + } +-- +2.20.1 + diff --git a/debian/rules b/debian/rules index f12693016..f264b3a6f 100755 --- a/debian/rules +++ b/debian/rules @@ -83,6 +83,7 @@ endif # FTBFS on kfreebsd LINUX_MODULES += json LINUX_MODULES += kazoo +LINUX_MODULES += systemd ifeq ($(DEB_HOST_ARCH_OS),linux) PACKAGE_GROUPS += $(LINUX_MODULES) else