[Asterisk-code-review] asterisk.c: When astcanary dies on linux, reset priority on ... (asterisk[11])

Walter Doekes asteriskteam at digium.com
Fri Sep 16 10:14:32 CDT 2016


Walter Doekes has uploaded a new change for review.

  https://gerrit.asterisk.org/3919

Change subject: asterisk.c: When astcanary dies on linux, reset priority on all threads.
......................................................................

asterisk.c: When astcanary dies on linux, reset priority on all threads.

Previously only the canary checking thread itself had its priority set
to SCHED_OTHER. Now all threads are traversed and adjusted.

ASTERISK-19867 #close
Reported by: Xavier Hienne

Change-Id: Ie0dd02a3ec42f66a78303e9c1aac28f7ed9aae39
---
M main/asterisk.c
1 file changed, 36 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/19/3919/1

diff --git a/main/asterisk.c b/main/asterisk.c
index 1e1cb6e..b6a4d30 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -1698,6 +1698,41 @@
 		fprintf(stdout, "\033]1;%s\007", text);
 }
 
+/*! \brief Set priority on all known threads. */
+static int set_priority_all(int pri)
+{
+#ifdef __linux__
+	struct thread_list_t *cur;
+	struct sched_param sched;
+	char const *policy_str;
+	int policy;
+	memset(&sched, 0, sizeof(sched));
+	if (pri) {
+		policy = SCHED_RR;
+		policy_str = "realtime";
+		sched.sched_priority = 10;
+	} else {
+		policy = SCHED_OTHER;
+		policy_str = "regular";
+		sched.sched_priority = 0;
+	}
+	if (sched_setscheduler(getpid(), policy, &sched)) {
+		ast_log(LOG_WARNING, "Unable to set %s thread priority on main thread\n", policy_str);
+		return -1;
+	}
+	ast_verb(1, "Setting %s thread priority on all threads\n", policy_str);
+	AST_RWLIST_RDLOCK(&thread_list);
+	AST_RWLIST_TRAVERSE(&thread_list, cur, list) {
+		/* Don't care about the return value. It should work. */
+		sched_setscheduler(cur->lwp, policy, &sched);
+	}
+	AST_RWLIST_UNLOCK(&thread_list);
+	return 0;
+#else
+	return ast_set_priority(0);
+#endif
+}
+
 /*! \brief We set ourselves to a high priority, that we might pre-empt everything
    else.  If your PBX has heavy activity on it, this is a good thing.  */
 int ast_set_priority(int pri)
@@ -3586,7 +3621,7 @@
 				"He's kicked the bucket.  He's shuffled off his mortal coil, "
 				"run down the curtain, and joined the bleeding choir invisible!!  "
 				"THIS is an EX-CANARY.  (Reducing priority)\n");
-			ast_set_priority(0);
+			set_priority_all(0);
 			pthread_exit(NULL);
 		}
 

-- 
To view, visit https://gerrit.asterisk.org/3919
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie0dd02a3ec42f66a78303e9c1aac28f7ed9aae39
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 11
Gerrit-Owner: Walter Doekes <walter+asterisk at wjd.nu>



More information about the asterisk-code-review mailing list