[Asterisk-code-review] res pjsip log forwarder: Add ast sip get pjproject buildopt (asterisk[13])

George Joseph asteriskteam at digium.com
Thu Jan 14 14:44:29 CST 2016


George Joseph has uploaded a new change for review.

  https://gerrit.asterisk.org/2012

Change subject: res_pjsip_log_forwarder:  Add ast_sip_get_pjproject_buildopt
......................................................................

res_pjsip_log_forwarder:  Add ast_sip_get_pjproject_buildopt

As a follow-on to the recent 'Add CLI "pjsip show buildopts"' patch,
a new ast_sip_get_pjproject_buildopt function has been added.  It
allows the caller to get the value of one of the buildopts.

The initial use case is retrieving the runtime value of
PJ_MAX_HOSTNAME to insure we don't send a hostname greater
than pjproject can handle.  Since it can differ between
the version of pjproject that Asterisk was compiled against
and the version of pjproject that Asterisk is running against,
we can't use the PJ_MAX_HOSTNAME macro directly in Asterisk
source code.

Since this function parses the output of pj_dump_config,
it should never be used during call processing.  If a
buildopt is needed during cal processing, the value
should be retrieved at startup and saved for later use.

Change-Id: Icd23c289e1190edf9c6f07cabf382144ccef5b31
---
M include/asterisk/res_pjsip.h
M res/res_pjsip_log_forwarder.c
2 files changed, 85 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/12/2012/1

diff --git a/include/asterisk/res_pjsip.h b/include/asterisk/res_pjsip.h
index 5dc16bc..a893502 100644
--- a/include/asterisk/res_pjsip.h
+++ b/include/asterisk/res_pjsip.h
@@ -2172,4 +2172,34 @@
  */
 long ast_sip_threadpool_queue_size(void);
 
+/*!
+ * \brief Retrieve a pjproject build option
+ *
+ * \param option The build option requested
+ * \param format_string A scanf-style format string to parse the option value into
+ * \param ... Pointers to variables to receive the values parsed
+ *
+ * \retval The number of values parsed
+ *
+ * \since 13.8.0
+ *
+ * \note The option requested must be from those returned by pj_dump_config()
+ * which can be displayed with the 'pjsip show buildopts' CLI command.
+ *
+ * \warning \b This function calls pj_dump_config and intercepts and parses the resulting
+ * log messages.  It could be used for configuration validation, debugging, or saving
+ * the value for later use.   It should never be directly used during call processing.
+ *
+ *   <b>Sample Usage:</b>
+ *   \code
+ *
+ *   int max_hostname;
+ *
+ *   ast_sip_get_pjproject_buildopt("PJ_MAX_HOSTNAME", "%d", &max_hostname);
+ *
+ *   \endcode
+ *
+ */
+int ast_sip_get_pjproject_buildopt(char *option, char *format_string, ...);
+
 #endif /* _RES_PJSIP_H */
diff --git a/res/res_pjsip_log_forwarder.c b/res/res_pjsip_log_forwarder.c
index 7a71280..5881ca2 100644
--- a/res/res_pjsip_log_forwarder.c
+++ b/res/res_pjsip_log_forwarder.c
@@ -41,12 +41,14 @@
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
+#include <stdarg.h>
 #include <pjsip.h>
 #include <pj/log.h>
 
 #include "asterisk/logger.h"
 #include "asterisk/module.h"
 #include "asterisk/cli.h"
+#include "asterisk/res_pjsip.h"
 
 static pj_log_func *log_cb_orig;
 static unsigned decor_orig;
@@ -57,12 +59,25 @@
 struct pjsip_show_buildopts {
 	pthread_t thread;
 	int fd;
+	const char *option;
+	const char *format_string;
+	va_list *arg_ptr;
+	int sscanf_result;
 };
 
 static struct pjsip_show_buildopts show_buildopts = {
 	.thread = AST_PTHREADT_NULL,
 	.fd = -1,
 };
+
+/**\
+ * The pragma is needed to prevent the compiler from emitting a warning
+ * that suggests the use of 'attribute format' because of vsscanf.
+ *
+ * Clang does respect pragma GCC
+ */
+#pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
+static void log_cb(int level, const char *data, int len);
 
 static void log_cb(int level, const char *data, int len)
 {
@@ -72,6 +87,19 @@
 	int log_line = 0;
 	const char *log_func = "<?>";
 	int mod_level;
+	char *format_temp;
+
+	if (show_buildopts.fd == -2) {
+		/* If we already found a match, don't bother processing the rest of the log messages from the dump */
+		if (show_buildopts.sscanf_result) {
+			return;
+		}
+
+		format_temp = ast_alloca(strlen(" config.c ") + strlen(show_buildopts.option) + strlen(" : ") + strlen(show_buildopts.format_string) + 1);
+		sprintf(format_temp, " config.c %s : %s", show_buildopts.option, show_buildopts.format_string);
+		show_buildopts.sscanf_result = vsscanf(data, format_temp, *show_buildopts.arg_ptr);
+		return;
+	}
 
 	if (show_buildopts.fd != -1 && show_buildopts.thread == pthread_self()) {
 		/*
@@ -109,6 +137,33 @@
 	ast_log(ast_level, log_source, log_line, log_func, "\t%s\n", data);
 }
 
+int ast_sip_get_pjproject_buildopt(char *option, char *format_string, ...)
+{
+	va_list arg_ptr;
+	int res;
+
+	va_start(arg_ptr, format_string);
+
+	/* Protect from other CLI instances trying to do this at the same time. */
+	ast_mutex_lock(&show_buildopts_lock);
+
+	show_buildopts.thread = pthread_self();
+	show_buildopts.fd = -2;
+	show_buildopts.option = option;
+	show_buildopts.format_string = format_string;
+	show_buildopts.arg_ptr = &arg_ptr;
+	show_buildopts.sscanf_result = 0;
+	pj_dump_config();
+	show_buildopts.fd = -1;
+	show_buildopts.thread = AST_PTHREADT_NULL;
+	res = show_buildopts.sscanf_result;
+	va_end(arg_ptr);
+
+	ast_mutex_unlock(&show_buildopts_lock);
+
+	return res;
+}
+
 static char *handle_pjsip_show_buildopts(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 	switch(cmd) {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Icd23c289e1190edf9c6f07cabf382144ccef5b31
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: George Joseph <george.joseph at fairview5.com>



More information about the asterisk-code-review mailing list