Skip to content

Commit 5f3f6ec

Browse files
committed
patches: Prevent creation of duplicate Asterisk processes.
Slipstream the following patch, which was not merged when up on Gerrit, and is probably not really worth the effort of trying to upstream again due to the resistance it received before: * ASTERISK-30339 (Gerrit 19655) asterisk.c: Prevent creation of duplicate Asterisk processes (Dec 2022)
1 parent 1dd0cf5 commit 5f3f6ec

File tree

3 files changed

+93
-24
lines changed

3 files changed

+93
-24
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ PhreakScript installs:
1414
- Bug fixes for reading and writing configuration file templates
1515
- Bug fix for ConfBridge shutdown race condition
1616
- Enhances performance by completely removing Newexten AMI event
17+
- Prevents duplicate Asterisk process creation
1718
- Many additional features and improvements
1819
- Adds prefix capabilities to `include => `
1920
- Signaling enhancements
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
From 6a35b12e045da797ff030c85c1692b5585710d37 Mon Sep 17 00:00:00 2001
2+
From: Naveen Albert <asterisk@phreaknet.org>
3+
Date: Sun, 4 Dec 2022 14:58:35 +0000
4+
Subject: [PATCH] asterisk.c: Prevent duplicate Asterisk processes from
5+
starting.
6+
7+
During startup, there is a period of time between when the daemon
8+
is started and when Asterisk starts accepting socket connections
9+
for remote consoles, during which if another attempt is made to
10+
start the Asterisk daemon, it will succeed. This will lead to
11+
multiple Asterisk processes running concurrently, causing all
12+
sorts of issues.
13+
14+
A check is now added before starting the daemon to check if
15+
if the PID in the PID file is currently running, and if so,
16+
to abort to avoid starting a duplicate Asterisk process.
17+
18+
ASTERISK-30339 #close
19+
20+
Change-Id: I38d8d75567524ffd6b1779aa24e7f2bd6951fbb3
21+
---
22+
main/asterisk.c | 35 +++++++++++++++++++++++++++++++++++
23+
1 file changed, 35 insertions(+)
24+
25+
diff --git a/main/asterisk.c b/main/asterisk.c
26+
index dea849f10c..21da28c1fb 100644
27+
--- a/main/asterisk.c
28+
+++ b/main/asterisk.c
29+
@@ -1696,6 +1696,35 @@ static int ast_tryconnect(void)
30+
return 1;
31+
}
32+
33+
+static int ast_is_starting(void)
34+
+{
35+
+ /* Don't use kill since that only works if Asterisk was started as the same user. */
36+
+ struct stat st;
37+
+ FILE *f;
38+
+ long file_pid = 0;
39+
+ char procpath[PATH_MAX];
40+
+
41+
+ /* Get current value from PID file */
42+
+ f = fopen(ast_config_AST_PID, "r");
43+
+ if (!f) {
44+
+ return 0; /* PID file doesn't exist? No way to tell. */
45+
+ }
46+
+ fscanf(f, "%ld", &file_pid);
47+
+ fclose(f);
48+
+ if (!file_pid) {
49+
+ return 0;
50+
+ }
51+
+
52+
+ /* Check if such a process is running */
53+
+ snprintf(procpath, sizeof(procpath), "/proc/%ld", file_pid);
54+
+ if (stat(procpath, &st) == -1 && errno == ENOENT) {
55+
+ /* Process doesn't exist */
56+
+ return 0;
57+
+ }
58+
+ return 1;
59+
+}
60+
+
61+
+
62+
/*! \brief Urgent handler
63+
*
64+
* Called by soft_hangup to interrupt the poll, read, or other
65+
@@ -4021,6 +4050,12 @@ int main(int argc, char *argv[])
66+
exit(1);
67+
}
68+
69+
+ if (ast_is_starting()) {
70+
+ fprintf(stderr, "Asterisk is currently starting. Use 'asterisk -r' to connect momentarily.\n");
71+
+ printf("%s", term_quit());
72+
+ exit(1);
73+
+ }
74+
+
75+
#ifdef HAVE_CAP
76+
child_cap = cap_from_text("cap_net_admin-eip");
77+
#endif
78+
--
79+
2.30.2
80+

phreaknet.sh

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,12 +2255,8 @@ phreak_patches() { # $1 = $PATCH_DIR, $2 = $AST_SRC_DIR
22552255
phreak_tree_module "funcs/func_query.c"
22562256
phreak_tree_module "funcs/func_resonance.c"
22572257
phreak_tree_module "funcs/func_tech.c"
2258-
phreak_tree_module "res/res_coindetect.c"
2259-
2260-
if [ "$DEVMODE" = "1" ]; then
2261-
phreak_tree_module "res/res_deadlock.c" # this is not possibly useful to non-developers
2262-
fi
22632258

2259+
phreak_tree_module "res/res_coindetect.c"
22642260
phreak_tree_module "res/res_alarmsystem.c"
22652261
phreak_tree_module "res/res_digitmap.c"
22662262
phreak_tree_module "res/res_irc.c"
@@ -2270,6 +2266,9 @@ phreak_patches() { # $1 = $PATCH_DIR, $2 = $AST_SRC_DIR
22702266
phreak_tree_module "res/res_pjsip_presence.c"
22712267
phreak_tree_module "res/res_smdr_whozz.c"
22722268

2269+
if [ "$DEVMODE" = "1" ]; then
2270+
phreak_tree_module "res/res_deadlock.c" # this is not possibly useful to non-developers
2271+
fi
22732272
if [ -d /etc/dahdi ]; then
22742273
phreak_tree_module "apps/app_loopdisconnect.c"
22752274
if [ "$RTPULSING" = "1" ]; then
@@ -2326,6 +2325,11 @@ phreak_patches() { # $1 = $PATCH_DIR, $2 = $AST_SRC_DIR
23262325
asterisk_pr_unconditional 971 # config.c fix issues w/ whitespace in comments
23272326
asterisk_pr_unconditional 1030 # chan_dahdi: Fix wrong channel state when RINGING recieved
23282327

2328+
#asterisk_pr_unconditional 272 # Call Waiting Deluxe. This also now conflicts (with the latest revisions), so temp. disabled.
2329+
#asterisk_pr_unconditional 438 # Last Number Redial. This now conflicts with 272, so temp. disabled.
2330+
asterisk_pr_unconditional 292 # GROUP VARs
2331+
git_custom_patch "https://code.phreaknet.org/asterisk/dahdicleanup.diff"
2332+
23292333
if [ $AST_MAJOR_VER -lt 21 ]; then
23302334
if [ "$EXTERNAL_CODECS" = "1" ]; then
23312335
phreak_nontree_patch "main/translate.c" "translate.diff" "https://issues.asterisk.org/jira/secure/attachment/60464/translate.diff" # Bug fix to translation code
@@ -2336,39 +2340,23 @@ phreak_patches() { # $1 = $PATCH_DIR, $2 = $AST_SRC_DIR
23362340
fi
23372341
fi
23382342

2339-
# Unmerged patches
2340-
git_patch "app_confbridge_Fix_bridge_shutdown_race_condition.patch" # app_confbridge: Fix bridge shutdown race condition
2341-
2342-
## WIP
2343-
asterisk_pr_unconditional 292 # GROUP VARs
2344-
2345-
# Unmerged
2346-
#asterisk_pr_unconditional 272 # Call Waiting Deluxe. This also now conflicts (with the latest revisions), so temp. disabled.
2347-
#asterisk_pr_unconditional 438 # Last Number Redial. This now conflicts with 272, so temp. disabled.
2348-
2349-
### TODO: Include ASTERISK-30339 and ASTERISK-30374 once resubmitted on GitHub
2350-
23512343
if [ "$RTPULSING" = "1" ]; then
23522344
# Patches split up to make it easier to selectively redo the 2nd one if a patch conflict occurs and the patch needs to be rebased.
23532345
git_patch "ast_rtoutpulsing1.diff" # chan_dahdi: add rtoutpulsing
23542346
git_patch "ast_rtoutpulsing2.diff" # chan_dahdi: add rtoutpulsing
23552347
fi
23562348

2349+
# Out of tree patches
2350+
git_patch "app_confbridge_Fix_bridge_shutdown_race_condition.patch" # app_confbridge: Fix bridge shutdown race condition
23572351
git_patch "blueboxing.diff" # dsp: make blue boxing easier
23582352
git_patch "prefixinclude.diff" # pbx: prefix includes
23592353
git_patch "agi_record_noisefirst.diff" # res_agi: Add noise before silence detection option to Record AGI
2360-
2361-
git_custom_patch "https://code.phreaknet.org/asterisk/dahdicleanup.diff"
2354+
git_patch "19655-asterisk.c-Prevent-duplicate-Asterisk-processes-fro.patch" # Prevent duplicate Asterisk process creation
23622355

23632356
if [ "$EXPERIMENTAL_FEATURES" = "1" ] && [ $AST_MAJOR_VER -ge 21 ]; then
23642357
printf "Installing 21+ patches for experimental features\n"
23652358
add_experimental
23662359
fi
2367-
2368-
if [ "$DEVMODE" = "1" ]; then # highly experimental
2369-
# res_pbx_validate
2370-
:
2371-
fi
23722360
}
23732361

23742362
freebsd_port_patch() {

0 commit comments

Comments
 (0)