فهرست منبع

exec: clang format the code

Daniel-Constantin Mierla 7 سال پیش
والد
کامیت
34ec1fc381
7فایلهای تغییر یافته به همراه486 افزوده شده و 467 حذف شده
  1. 113 113
      src/modules/exec/exec.c
  2. 4 5
      src/modules/exec/exec.h
  3. 212 193
      src/modules/exec/exec_hf.c
  4. 29 22
      src/modules/exec/exec_hf.h
  5. 72 75
      src/modules/exec/exec_mod.c
  6. 48 50
      src/modules/exec/kill.c
  7. 8 9
      src/modules/exec/kill.h

+ 113 - 113
src/modules/exec/exec.c

@@ -13,8 +13,8 @@
  * 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 
+ * 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
  *
  */
@@ -31,21 +31,19 @@
  * @brief Kamailio exec module
  *
  * The exec module allows external commands to be executed from a Kamailio script.
- * The commands may be any valid shell commands--the command string is passed to the 
+ * The commands may be any valid shell commands--the command string is passed to the
  * shell using “popen” command. Kamailio passes additional information about the request
  * in environment variables.
  *
  */
 
 
-
-
 #include <stdio.h>
 #include <strings.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <sys/types.h>
-/* 
+/*
 #include <sys/resource.h>
 */
 #include <sys/wait.h>
@@ -60,153 +58,156 @@
 
 #include "exec.h"
 
-int exec_msg(struct sip_msg *msg, char *cmd )
+int exec_msg(struct sip_msg *msg, char *cmd)
 {
 	FILE *pipe;
 	int exit_status;
 	int ret;
 
-	ret=-1; /* pessimist: assume error */
-	pipe=popen( cmd, "w" );
-	if (pipe==NULL) {
+	ret = -1; /* pessimist: assume error */
+	pipe = popen(cmd, "w");
+	if(pipe == NULL) {
 		LM_ERR("cannot open pipe: %s\n", cmd);
-		ser_error=E_EXEC;
+		ser_error = E_EXEC;
 		return -1;
 	}
 
-	if (fwrite(msg->buf, 1, msg->len, pipe)!=msg->len) {
+	if(fwrite(msg->buf, 1, msg->len, pipe) != msg->len) {
 		LM_ERR("failed to write to pipe\n");
-		ser_error=E_EXEC;
+		ser_error = E_EXEC;
 		goto error01;
 	}
 	/* success */
-	ret=1;
+	ret = 1;
 
 error01:
-	if (ferror(pipe)) {
+	if(ferror(pipe)) {
 		LM_ERR("pipe: %s\n", strerror(errno));
-		ser_error=E_EXEC;
-		ret=-1;
+		ser_error = E_EXEC;
+		ret = -1;
 	}
-	exit_status=pclose(pipe);
-	if (WIFEXITED(exit_status)) { /* exited properly .... */
+	exit_status = pclose(pipe);
+	if(WIFEXITED(exit_status)) { /* exited properly .... */
 		/* return false if script exited with non-zero status */
-		if (WEXITSTATUS(exit_status)!=0) ret=-1;
+		if(WEXITSTATUS(exit_status) != 0)
+			ret = -1;
 	} else { /* exited erroneously */
-		LM_ERR("cmd %s failed. exit_status=%d, errno=%d: %s\n",
-			cmd, exit_status, errno, strerror(errno) );
-		ret=-1;
+		LM_ERR("cmd %s failed. exit_status=%d, errno=%d: %s\n", cmd,
+				exit_status, errno, strerror(errno));
+		ret = -1;
 	}
 	return ret;
 }
 
-int exec_str(struct sip_msg *msg, char *cmd, char *param, int param_len) {
+int exec_str(struct sip_msg *msg, char *cmd, char *param, int param_len)
+{
 
 	struct action act;
 	struct run_act_ctx ra_ctx;
 	int cmd_len;
-	FILE *pipe;	
+	FILE *pipe;
 	char *cmd_line;
 	int ret;
 	int l1;
-	static char uri_line[MAX_URI_SIZE+1];
+	static char uri_line[MAX_URI_SIZE + 1];
 	int uri_cnt;
 	str uri;
 	int exit_status;
 
 	/* pessimist: assume error by default */
-	ret=-1;
-	
-	l1=strlen(cmd);
-	if(param_len>0)
-		cmd_len=l1+param_len+4;
+	ret = -1;
+
+	l1 = strlen(cmd);
+	if(param_len > 0)
+		cmd_len = l1 + param_len + 4;
 	else
-		cmd_len=l1+1;
-	cmd_line=pkg_malloc(cmd_len);
-	if (cmd_line==0) {
-		ret=ser_error=E_OUT_OF_MEM;
+		cmd_len = l1 + 1;
+	cmd_line = pkg_malloc(cmd_len);
+	if(cmd_line == 0) {
+		ret = ser_error = E_OUT_OF_MEM;
 		LM_ERR("no pkg mem for command\n");
 		goto error00;
 	}
 
 	/* 'command parameter \0' */
-	memcpy(cmd_line, cmd, l1); 
-	if(param_len>0)
-	{
-		cmd_line[l1]=' ';
-		cmd_line[l1+1]='\'';
-		memcpy(cmd_line+l1+2, param, param_len);
-		cmd_line[l1+param_len+2]='\'';
-		cmd_line[l1+param_len+3]=0;
+	memcpy(cmd_line, cmd, l1);
+	if(param_len > 0) {
+		cmd_line[l1] = ' ';
+		cmd_line[l1 + 1] = '\'';
+		memcpy(cmd_line + l1 + 2, param, param_len);
+		cmd_line[l1 + param_len + 2] = '\'';
+		cmd_line[l1 + param_len + 3] = 0;
 	} else {
 		cmd_line[l1] = 0;
 	}
-	
-	pipe=popen( cmd_line, "r" );
-	if (pipe==NULL) {
+
+	pipe = popen(cmd_line, "r");
+	if(pipe == NULL) {
 		LM_ERR("cannot open pipe: %s\n", cmd_line);
-		ser_error=E_EXEC;
+		ser_error = E_EXEC;
 		goto error01;
 	}
 
 	/* read now line by line */
-	uri_cnt=0;
-	while( fgets(uri_line, MAX_URI_SIZE, pipe)!=NULL){
+	uri_cnt = 0;
+	while(fgets(uri_line, MAX_URI_SIZE, pipe) != NULL) {
 		uri.s = uri_line;
-		uri.len=strlen(uri.s);
+		uri.len = strlen(uri.s);
 		/* trim from right */
-		while(uri.len && (uri.s[uri.len-1]=='\r' 
-				|| uri.s[uri.len-1]=='\n' 
-				|| uri.s[uri.len-1]=='\t'
-				|| uri.s[uri.len-1]==' ' )) {
+		while(uri.len
+				&& (uri.s[uri.len - 1] == '\r' || uri.s[uri.len - 1] == '\n'
+						   || uri.s[uri.len - 1] == '\t'
+						   || uri.s[uri.len - 1] == ' ')) {
 			LM_DBG("rtrim\n");
 			uri.len--;
 		}
 		/* skip empty line */
-		if (uri.len==0) continue;
+		if(uri.len == 0)
+			continue;
 		/* ZT */
-		uri.s[uri.len]=0;
-		if (uri_cnt==0) {
+		uri.s[uri.len] = 0;
+		if(uri_cnt == 0) {
 			memset(&act, 0, sizeof(act));
 			act.type = SET_URI_T;
 			act.val[0].type = STRING_ST;
 			act.val[0].u.string = uri.s;
 			init_run_actions_ctx(&ra_ctx);
-			if (do_action(&ra_ctx, &act, msg)<0) {
+			if(do_action(&ra_ctx, &act, msg) < 0) {
 				LM_ERR("the action for has failed\n");
-				ser_error=E_OUT_OF_MEM;
+				ser_error = E_OUT_OF_MEM;
 				goto error02;
 			}
 		} else {
-		    if (append_branch(msg, &uri, 0, 0, Q_UNSPECIFIED, 0, 0,
-				      0, 0, 0, 0) == -1) {
+			if(append_branch(msg, &uri, 0, 0, Q_UNSPECIFIED, 0, 0, 0, 0, 0, 0)
+					== -1) {
 				LM_ERR("append_branch failed; too many or too long URIs?\n");
 				goto error02;
 			}
 		}
 		uri_cnt++;
 	}
-	if (uri_cnt==0) {
-		LM_ERR("no uri from %s\n", cmd_line );
+	if(uri_cnt == 0) {
+		LM_ERR("no uri from %s\n", cmd_line);
 		goto error02;
 	}
 	/* success */
-	ret=1;
+	ret = 1;
 
 error02:
-	if (ferror(pipe)) {
+	if(ferror(pipe)) {
 		LM_ERR("in pipe: %s\n", strerror(errno));
-		ser_error=E_EXEC;
-		ret=-1;
+		ser_error = E_EXEC;
+		ret = -1;
 	}
-	exit_status=pclose(pipe);
-	if (WIFEXITED(exit_status)) { /* exited properly .... */
+	exit_status = pclose(pipe);
+	if(WIFEXITED(exit_status)) { /* exited properly .... */
 		/* return false if script exited with non-zero status */
-		if (WEXITSTATUS(exit_status)!=0) ret=-1;
+		if(WEXITSTATUS(exit_status) != 0)
+			ret = -1;
 	} else { /* exited erroneously */
-		LM_ERR("cmd %s failed. exit_status=%d, errno=%d: %s\n",
-			cmd, exit_status, errno, strerror(errno) );
-		ret=-1;
+		LM_ERR("cmd %s failed. exit_status=%d, errno=%d: %s\n", cmd,
+				exit_status, errno, strerror(errno));
+		ret = -1;
 	}
 error01:
 	pkg_free(cmd_line);
@@ -220,88 +221,87 @@ int exec_avp(struct sip_msg *msg, char *cmd, pvname_list_p avpl)
 	int_str avp_val;
 	int_str avp_name;
 	unsigned short avp_type;
-	FILE *pipe;	
+	FILE *pipe;
 	int ret;
-	char res_line[MAX_URI_SIZE+1];
+	char res_line[MAX_URI_SIZE + 1];
 	str res;
 	int exit_status;
 	int i;
-	pvname_list_t* crt;
+	pvname_list_t *crt;
 
 	/* pessimist: assume error by default */
-	ret=-1;
-	
-	pipe=popen( cmd, "r" );
-	if (pipe==NULL) {
+	ret = -1;
+
+	pipe = popen(cmd, "r");
+	if(pipe == NULL) {
 		LM_ERR("cannot open pipe: %s\n", cmd);
-		ser_error=E_EXEC;
+		ser_error = E_EXEC;
 		return ret;
 	}
 
 	/* read now line by line */
-	i=0;
+	i = 0;
 	crt = avpl;
-	while( fgets(res_line, MAX_URI_SIZE, pipe)!=NULL){
+	while(fgets(res_line, MAX_URI_SIZE, pipe) != NULL) {
 		res.s = res_line;
-		res.len=strlen(res.s);
+		res.len = strlen(res.s);
 		/* trim from right */
-		while(res.len && (res.s[res.len-1]=='\r' 
-				|| res.s[res.len-1]=='\n' 
-				|| res.s[res.len-1]=='\t'
-				|| res.s[res.len-1]==' ' )) {
+		while(res.len
+				&& (res.s[res.len - 1] == '\r' || res.s[res.len - 1] == '\n'
+						   || res.s[res.len - 1] == '\t'
+						   || res.s[res.len - 1] == ' ')) {
 			res.len--;
 		}
 		/* skip empty line */
-		if (res.len==0) continue;
+		if(res.len == 0)
+			continue;
 		/* ZT */
-		res.s[res.len]=0;
+		res.s[res.len] = 0;
 
 		avp_type = 0;
-		if(crt==NULL)
-		{
-			avp_name.n = i+1;
+		if(crt == NULL) {
+			avp_name.n = i + 1;
 		} else {
-			if(pv_get_avp_name(msg, &(crt->sname.pvp), &avp_name, &avp_type)!=0)
-			{
-				LM_ERR("can't get item name [%d]\n",i);
+			if(pv_get_avp_name(msg, &(crt->sname.pvp), &avp_name, &avp_type)
+					!= 0) {
+				LM_ERR("can't get item name [%d]\n", i);
 				goto error;
 			}
 		}
 
 		avp_type |= AVP_VAL_STR;
 		avp_val.s = res;
-	
-		if(add_avp(avp_type, avp_name, avp_val)!=0)
-		{
+
+		if(add_avp(avp_type, avp_name, avp_val) != 0) {
 			LM_ERR("unable to add avp\n");
 			goto error;
 		}
-	
+
 		if(crt)
 			crt = crt->next;
 
 		i++;
 	}
-	if (i==0)
+	if(i == 0)
 		LM_DBG("no result from %s\n", cmd);
 	/* success */
-	ret=1;
+	ret = 1;
 
 error:
-	if (ferror(pipe)) {
-		LM_ERR("pipe: %d/%s\n",	errno, strerror(errno));
-		ser_error=E_EXEC;
-		ret=-1;
+	if(ferror(pipe)) {
+		LM_ERR("pipe: %d/%s\n", errno, strerror(errno));
+		ser_error = E_EXEC;
+		ret = -1;
 	}
-	exit_status=pclose(pipe);
-	if (WIFEXITED(exit_status)) { /* exited properly .... */
+	exit_status = pclose(pipe);
+	if(WIFEXITED(exit_status)) { /* exited properly .... */
 		/* return false if script exited with non-zero status */
-		if (WEXITSTATUS(exit_status)!=0) ret=-1;
+		if(WEXITSTATUS(exit_status) != 0)
+			ret = -1;
 	} else { /* exited erroneously */
-		LM_ERR("cmd %s failed. exit_status=%d, errno=%d: %s\n",
-			cmd, exit_status, errno, strerror(errno) );
-		ret=-1;
+		LM_ERR("cmd %s failed. exit_status=%d, errno=%d: %s\n", cmd,
+				exit_status, errno, strerror(errno));
+		ret = -1;
 	}
 	return ret;
 }
-

+ 4 - 5
src/modules/exec/exec.h

@@ -13,14 +13,14 @@
  * 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 
+ * 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
  */
 /*!
  * \file
  * \brief Exec module:: Module interface
- * \ingroup exec 
+ * \ingroup exec
  * Module: \ref exec
  */
 
@@ -30,8 +30,7 @@
 #include "../../core/pvar.h"
 
 int exec_str(struct sip_msg *msg, char *cmd, char *param, int param_len);
-int exec_msg(struct sip_msg *msg, char *cmd );
+int exec_msg(struct sip_msg *msg, char *cmd);
 int exec_avp(struct sip_msg *msg, char *cmd, pvname_list_p avpl);
 
 #endif
-

+ 212 - 193
src/modules/exec/exec_hf.c

@@ -13,8 +13,8 @@
  * 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 
+ * 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
  *
  */
@@ -22,7 +22,7 @@
 /*!
  * \file
  * \brief Exec module:: Module interface
- * \ingroup exec 
+ * \ingroup exec
  * Module: \ref exec
  *
  * functions for creating environment variables out of a request's
@@ -30,9 +30,9 @@
  * canonical form; multiple header field occurrences are merged
  * into a single variable
  *
- * known limitations: 
- * - compact header field names unknown to parser will not be translated to 
- *   canonical form. Thus, environment variables may have either name and 
+ * known limitations:
+ * - compact header field names unknown to parser will not be translated to
+ *   canonical form. Thus, environment variables may have either name and
  *   users have to check for both of them.
  * - symbols in header field names will be translated to underscore
  *
@@ -53,142 +53,145 @@
 extern int exec_bash_safety;
 
 /* should be environment variables set by header fields ? */
-unsigned int setvars=1;
+unsigned int setvars = 1;
 
 /* insert a new header field into the structure; */
-static int insert_hf( struct hf_wrapper **list, struct hdr_field *hf )
+static int insert_hf(struct hf_wrapper **list, struct hdr_field *hf)
 {
 	struct hf_wrapper *w; /* new wrapper */
 	struct hf_wrapper *i;
 
-	w=(struct hf_wrapper *)pkg_malloc(sizeof(struct hf_wrapper));
-	if (!w) {
+	w = (struct hf_wrapper *)pkg_malloc(sizeof(struct hf_wrapper));
+	if(!w) {
 		LM_ERR("ran out of pkg mem\n");
 		return 0;
 	}
 	memset(w, 0, sizeof(struct hf_wrapper));
-	w->var_type=W_HF;w->u.hf=hf; 
-	w->prefix=HF_PREFIX; w->prefix_len=HF_PREFIX_LEN;
+	w->var_type = W_HF;
+	w->u.hf = hf;
+	w->prefix = HF_PREFIX;
+	w->prefix_len = HF_PREFIX_LEN;
 
 	/* is there another hf of the same type?... */
-	for(i=*list; i; i=i->next_other) {
-		if (i->var_type==W_HF && i->u.hf->type==hf->type) {
+	for(i = *list; i; i = i->next_other) {
+		if(i->var_type == W_HF && i->u.hf->type == hf->type) {
 			/* if it is OTHER, check name too */
-			if (hf->type==HDR_OTHER_T && (hf->name.len!=i->u.hf->name.len
-					|| strncasecmp(i->u.hf->name.s, hf->name.s, 
-					   hf->name.len)!=0))
+			if(hf->type == HDR_OTHER_T
+					&& (hf->name.len != i->u.hf->name.len
+							   || strncasecmp(i->u.hf->name.s, hf->name.s,
+										  hf->name.len)
+										  != 0))
 				continue;
 			/* yes, we found a hf of same type */
-			w->next_same=i->next_same;
-			w->next_other=i->next_other;
-			i->next_same=w;
+			w->next_same = i->next_same;
+			w->next_other = i->next_other;
+			i->next_same = w;
 			break;
 		}
 	}
 	/* ... no previous HF of the same type found */
-	if (i==0) {
-		w->next_other=*list;
-		*list=w;
+	if(i == 0) {
+		w->next_other = *list;
+		*list = w;
 	}
 	return 1;
 }
 
-static void release_hf_struct( struct hf_wrapper *list )
+static void release_hf_struct(struct hf_wrapper *list)
 {
 	struct hf_wrapper *i, *j, *nexts, *nexto;
 
-	i=list;
+	i = list;
 	while(i) {
-		nexto=i->next_other;
-		j=i->next_same;
+		nexto = i->next_other;
+		j = i->next_same;
 		pkg_free(i);
 		/* release list of same type hf */
 		while(j) {
-			nexts=j->next_same;
+			nexts = j->next_same;
 			pkg_free(j);
-			j=nexts;
+			j = nexts;
 		}
-		i=nexto;
+		i = nexto;
 	}
 }
 
 /* if that is some of well-known header fields which have compact
  * form, return canonical form ... returns 1 and sets params;
  * 0 is returned otherwise */
-static int compacthdr_type2str(hdr_types_t  type, char **hname, int *hlen )
+static int compacthdr_type2str(hdr_types_t type, char **hname, int *hlen)
 {
 	switch(type) {
 		/* HDR_CONTENT_ENCODING: 'e' -- unsupported by parser */
 		/* HDR_SUBJECT: 's' -- unsupported by parser */
-		case HDR_VIA_T /* v */ : 
-			*hname=VAR_VIA;
-			*hlen=VAR_VIA_LEN;
+		case HDR_VIA_T /* v */:
+			*hname = VAR_VIA;
+			*hlen = VAR_VIA_LEN;
 			break;
-		case HDR_CONTENTTYPE_T /* c */ : 
-			*hname=VAR_CTYPE;
-			*hlen=VAR_CTYPE_LEN;
+		case HDR_CONTENTTYPE_T /* c */:
+			*hname = VAR_CTYPE;
+			*hlen = VAR_CTYPE_LEN;
 			break;
-		case HDR_FROM_T /* f */: 
-			*hname=VAR_FROM;
-			*hlen=VAR_FROM_LEN;
+		case HDR_FROM_T /* f */:
+			*hname = VAR_FROM;
+			*hlen = VAR_FROM_LEN;
 			break;
-		case HDR_CALLID_T /* i */: 
-			*hname=VAR_CALLID;
-			*hlen=VAR_CALLID_LEN;
+		case HDR_CALLID_T /* i */:
+			*hname = VAR_CALLID;
+			*hlen = VAR_CALLID_LEN;
 			break;
-		case HDR_SUPPORTED_T /* k */: 
-			*hname=VAR_SUPPORTED;
-			*hlen=VAR_SUPPORTED_LEN;
+		case HDR_SUPPORTED_T /* k */:
+			*hname = VAR_SUPPORTED;
+			*hlen = VAR_SUPPORTED_LEN;
 			break;
-		case HDR_CONTENTLENGTH_T /* l */: 
-			*hname=VAR_CLEN;
-			*hlen=VAR_CLEN_LEN;
+		case HDR_CONTENTLENGTH_T /* l */:
+			*hname = VAR_CLEN;
+			*hlen = VAR_CLEN_LEN;
 			break;
-		case HDR_CONTACT_T /* m */: 
-			*hname=VAR_CONTACT;
-			*hlen=VAR_CONTACT_LEN;
+		case HDR_CONTACT_T /* m */:
+			*hname = VAR_CONTACT;
+			*hlen = VAR_CONTACT_LEN;
 			break;
-		case HDR_TO_T /* t */: 
-			*hname=VAR_TO;
-			*hlen=VAR_TO_LEN;
+		case HDR_TO_T /* t */:
+			*hname = VAR_TO;
+			*hlen = VAR_TO_LEN;
 			break;
-		case HDR_EVENT_T /* o */: 
-			*hname=VAR_EVENT;
-			*hlen=VAR_EVENT_LEN;
+		case HDR_EVENT_T /* o */:
+			*hname = VAR_EVENT;
+			*hlen = VAR_EVENT_LEN;
 			break;
-		default:	
+		default:
 			return 0;
 	}
 	return 1;
 }
 
 
-static int canonize_headername(str *orig, char **hname, int *hlen )
+static int canonize_headername(str *orig, char **hname, int *hlen)
 {
 	char *c;
 	int i;
 
-	*hlen=orig->len;
-	*hname=pkg_malloc(*hlen);
-	if (!*hname) {
+	*hlen = orig->len;
+	*hname = pkg_malloc(*hlen);
+	if(!*hname) {
 		LM_ERR("no pkg mem for hname\n");
 		return 0;
 	}
-	for (c=orig->s, i=0; i<*hlen; i++, c++) {
+	for(c = orig->s, i = 0; i < *hlen; i++, c++) {
 		/* lowercase to uppercase */
-		if (*c>='a' && *c<='z') 
-			*((*hname)+i)=*c-('a'-'A');
-			/* uppercase and numbers stay "as is" */
-		else if ((*c>='A' && *c<='Z')||(*c>='0' && *c<='9')) 
-			*((*hname)+i)=*c;
+		if(*c >= 'a' && *c <= 'z')
+			*((*hname) + i) = *c - ('a' - 'A');
+		/* uppercase and numbers stay "as is" */
+		else if((*c >= 'A' && *c <= 'Z') || (*c >= '0' && *c <= '9'))
+			*((*hname) + i) = *c;
 		/* legal symbols will be translated to underscore */
-		else if (strchr(UNRESERVED_MARK HNV_UNRESERVED, *c)
-				|| (*c==ESCAPE))
-			*((*hname)+i)=HFN_SYMBOL;
+		else if(strchr(UNRESERVED_MARK HNV_UNRESERVED, *c) || (*c == ESCAPE))
+			*((*hname) + i) = HFN_SYMBOL;
 		else {
-			LM_ERR("print_var unexpected char '%c' in hfname %.*s\n", 
-					*c, *hlen, orig->s );
-			*((*hname)+i)=HFN_SYMBOL;
+			LM_ERR("print_var unexpected char '%c' in hfname %.*s\n", *c, *hlen,
+					orig->s);
+			*((*hname) + i) = HFN_SYMBOL;
 		}
 	}
 	return 1;
@@ -201,18 +204,21 @@ static int print_av_var(struct hf_wrapper *w)
 	char *env;
 	char *c;
 
-	env_len=w->u.av.attr.len+1/*assignment*/+w->u.av.val.len+1/*ZT*/;
-	env=pkg_malloc(env_len);
-	if (!env) {
+	env_len = w->u.av.attr.len + 1 /*assignment*/ + w->u.av.val.len + 1 /*ZT*/;
+	env = pkg_malloc(env_len);
+	if(!env) {
 		LM_ERR("no pkg mem\n");
 		return 0;
 	}
-	c=env;
-	memcpy(c, w->u.av.attr.s, w->u.av.attr.len); c+=w->u.av.attr.len;
-	*c=EV_ASSIGN;c++;
-	memcpy(c, w->u.av.val.s, w->u.av.val.len);c+=w->u.av.val.len;
-	*c=0; /* zero termination */
-	w->envvar=env;
+	c = env;
+	memcpy(c, w->u.av.attr.s, w->u.av.attr.len);
+	c += w->u.av.attr.len;
+	*c = EV_ASSIGN;
+	c++;
+	memcpy(c, w->u.av.val.s, w->u.av.val.len);
+	c += w->u.av.val.len;
+	*c = 0; /* zero termination */
+	w->envvar = env;
 	return 1;
 }
 
@@ -229,85 +235,95 @@ static int print_hf_var(struct hf_wrapper *w, int offset)
 	char *c;
 
 	/* make -Wall happy */
-	hname=0;hlen=0;envvar=0;
+	hname = 0;
+	hlen = 0;
+	envvar = 0;
 
 	/* Make sure header names with possible compact forms
 	 * will be printed canonically
 	 */
-	canonical=compacthdr_type2str(w->u.hf->type, &hname, &hlen);
+	canonical = compacthdr_type2str(w->u.hf->type, &hname, &hlen);
 	/* header field has not been made canonical using a table;
 	 * do it now by uppercasing header-field name */
-	if (!canonical) {
-		if (!canonize_headername(&w->u.hf->name, &hname, &hlen)) {
+	if(!canonical) {
+		if(!canonize_headername(&w->u.hf->name, &hname, &hlen)) {
 			LM_ERR("canonize_hn error\n");
 			return 0;
 		}
-	} 
-	/* now we have a header name, let us generate the var */
-	envvar_len=w->u.hf->body.len;
-	for(wi=w->next_same; wi; wi=wi->next_same) { /* other values, separated */
-		envvar_len+=1 /* separator */ + wi->u.hf->body.len;
 	}
-	envvar=pkg_malloc(w->prefix_len+hlen+1/*assignment*/+envvar_len+1/*ZT*/);
-	if (!envvar) {
+	/* now we have a header name, let us generate the var */
+	envvar_len = w->u.hf->body.len;
+	for(wi = w->next_same; wi;
+			wi = wi->next_same) { /* other values, separated */
+		envvar_len += 1 /* separator */ + wi->u.hf->body.len;
+	}
+	envvar = pkg_malloc(
+			w->prefix_len + hlen + 1 /*assignment*/ + envvar_len + 1 /*ZT*/);
+	if(!envvar) {
 		LM_ERR("no pkg mem\n");
 		goto error00;
 	}
-	memcpy(envvar, w->prefix, w->prefix_len); c=envvar+w->prefix_len;
-	memcpy(c, hname, hlen ); c+=hlen;
-	*c=EV_ASSIGN;c++;
-	if (exec_bash_safety && w->u.hf->body.len>=4
+	memcpy(envvar, w->prefix, w->prefix_len);
+	c = envvar + w->prefix_len;
+	memcpy(c, hname, hlen);
+	c += hlen;
+	*c = EV_ASSIGN;
+	c++;
+	if(exec_bash_safety && w->u.hf->body.len >= 4
 			&& !strncmp(w->u.hf->body.s, "() {", 4)) {
-		memcpy(c, w->u.hf->body.s+offset+2, w->u.hf->body.len-2 );
-		c+=(w->u.hf->body.len-2);
+		memcpy(c, w->u.hf->body.s + offset + 2, w->u.hf->body.len - 2);
+		c += (w->u.hf->body.len - 2);
 	} else {
-		memcpy(c, w->u.hf->body.s+offset, w->u.hf->body.len );
-		c+=w->u.hf->body.len;
+		memcpy(c, w->u.hf->body.s + offset, w->u.hf->body.len);
+		c += w->u.hf->body.len;
 	}
-	for(wi=w->next_same; wi; wi=wi->next_same) {
-		*c=HF_SEPARATOR;c++;
-		if (exec_bash_safety && wi->u.hf->body.len>=4
+	for(wi = w->next_same; wi; wi = wi->next_same) {
+		*c = HF_SEPARATOR;
+		c++;
+		if(exec_bash_safety && wi->u.hf->body.len >= 4
 				&& !strncmp(wi->u.hf->body.s, "() {", 4)) {
-			memcpy(c, wi->u.hf->body.s+offset+2, wi->u.hf->body.len-2 );
-			c+=(wi->u.hf->body.len-2);
+			memcpy(c, wi->u.hf->body.s + offset + 2, wi->u.hf->body.len - 2);
+			c += (wi->u.hf->body.len - 2);
 		} else {
-			memcpy(c, wi->u.hf->body.s+offset, wi->u.hf->body.len );
-			c+=wi->u.hf->body.len;
+			memcpy(c, wi->u.hf->body.s + offset, wi->u.hf->body.len);
+			c += wi->u.hf->body.len;
 		}
 	}
-	*c=0; /* zero termination */
-	LM_DBG("%s\n", envvar );
-	
-	w->envvar=envvar;
-	if (!canonical) pkg_free(hname);
+	*c = 0; /* zero termination */
+	LM_DBG("%s\n", envvar);
+
+	w->envvar = envvar;
+	if(!canonical)
+		pkg_free(hname);
 	return 1;
 
 error00:
-	if (!canonical) pkg_free(hname);
+	if(!canonical)
+		pkg_free(hname);
 	return 0;
 }
 
 static int print_var(struct hf_wrapper *w, int offset)
 {
 	switch(w->var_type) {
-		case W_HF: 
+		case W_HF:
 			return print_hf_var(w, offset);
-		case W_AV: 
+		case W_AV:
 			return print_av_var(w);
 		default:
-		   	LM_CRIT("unknown type: %d\n", w->var_type );
+			LM_CRIT("unknown type: %d\n", w->var_type);
 			return 0;
 	}
 }
 
-static void release_vars(struct hf_wrapper *list) 
+static void release_vars(struct hf_wrapper *list)
 {
 	while(list) {
-		if (list->envvar) {
+		if(list->envvar) {
 			pkg_free(list->envvar);
-			list->envvar=0;
+			list->envvar = 0;
 		}
-		list=list->next_other;
+		list = list->next_other;
 	}
 }
 
@@ -316,10 +332,10 @@ static int build_hf_struct(struct sip_msg *msg, struct hf_wrapper **list)
 {
 	struct hdr_field *h;
 
-	*list=0;
+	*list = 0;
 	/* create ordered header-field structure */
-	for (h=msg->headers; h; h=h->next) {
-		if (!insert_hf(list,h)) {
+	for(h = msg->headers; h; h = h->next) {
+		if(!insert_hf(list, h)) {
 			LM_ERR("insert_hf failed\n");
 			goto error00;
 		}
@@ -327,9 +343,8 @@ static int build_hf_struct(struct sip_msg *msg, struct hf_wrapper **list)
 	return 1;
 error00:
 	release_hf_struct(*list);
-	*list=0;
+	*list = 0;
 	return 0;
-
 }
 
 /* create env vars in malloc memory */
@@ -339,9 +354,9 @@ static int create_vars(struct hf_wrapper *list, int offset)
 	struct hf_wrapper *w;
 
 	/* create variables now */
-	var_cnt=0;
-	for(w=list;w;w=w->next_other) {
-		if (!print_var(w, offset)) {
+	var_cnt = 0;
+	for(w = list; w; w = w->next_other) {
+		if(!print_var(w, offset)) {
 			LM_ERR("create_vars failed\n");
 			return 0;
 		}
@@ -360,38 +375,40 @@ environment_t *replace_env(struct hf_wrapper *list)
 	int i;
 	environment_t *backup_env;
 
-	backup_env=(environment_t *)pkg_malloc(sizeof(environment_t));
-	if (!backup_env) {
+	backup_env = (environment_t *)pkg_malloc(sizeof(environment_t));
+	if(!backup_env) {
 		LM_ERR("no pkg mem for backup env\n");
 		return 0;
 	}
 
 	/* count length of current env list */
-	var_cnt=0;
-	for (cp=environ; *cp; cp++) var_cnt++;
-	backup_env->old_cnt=var_cnt;
+	var_cnt = 0;
+	for(cp = environ; *cp; cp++)
+		var_cnt++;
+	backup_env->old_cnt = var_cnt;
 	/* count length of our extensions */
-	for(w=list;w;w=w->next_other) var_cnt++;
-	new_env=pkg_malloc((var_cnt+1)*sizeof(char *));
-	if (!new_env) {
+	for(w = list; w; w = w->next_other)
+		var_cnt++;
+	new_env = pkg_malloc((var_cnt + 1) * sizeof(char *));
+	if(!new_env) {
 		LM_ERR("no pkg mem\n");
 		pkg_free(backup_env);
 		return 0;
 	}
 	/* put all var pointers into new environment */
-	i=0;
-	for (cp=environ; *cp; cp++) { /* replicate old env */
-		new_env[i]=*cp;
+	i = 0;
+	for(cp = environ; *cp; cp++) { /* replicate old env */
+		new_env[i] = *cp;
 		i++;
 	}
-	for (w=list;w;w=w->next_other) { /* append new env */
-		new_env[i]=w->envvar;
+	for(w = list; w; w = w->next_other) { /* append new env */
+		new_env[i] = w->envvar;
 		i++;
 	}
-	new_env[i]=0; /* zero termination */
+	new_env[i] = 0; /* zero termination */
 	/* install new environment */
-	backup_env->env=environ;
-	environ=new_env;
+	backup_env->env = environ;
+	environ = new_env;
 	/* return previous environment */
 	return backup_env;
 }
@@ -402,13 +419,13 @@ void unset_env(environment_t *backup_env)
 	int i;
 
 	/* switch-over to backup environment */
-	cur_env0=cur_env=environ;
-	environ=backup_env->env;
-	i=0;
+	cur_env0 = cur_env = environ;
+	environ = backup_env->env;
+	i = 0;
 	/* release environment */
 	while(*cur_env) {
 		/* leave previously existing vars alone */
-		if (i>=backup_env->old_cnt) {
+		if(i >= backup_env->old_cnt) {
 			pkg_free(*cur_env);
 		}
 		cur_env++;
@@ -418,24 +435,25 @@ void unset_env(environment_t *backup_env)
 	pkg_free(backup_env);
 }
 
-static int append_var(char *name, char *value, int len, struct hf_wrapper **list)
+static int append_var(
+		char *name, char *value, int len, struct hf_wrapper **list)
 {
 	struct hf_wrapper *w;
 
-	w=(struct hf_wrapper *)pkg_malloc(sizeof(struct hf_wrapper));
-	if (!w) {
+	w = (struct hf_wrapper *)pkg_malloc(sizeof(struct hf_wrapper));
+	if(!w) {
 		LM_ERR("ran out of pkg mem\n");
 		return 0;
 	}
-	memset(w, 0, sizeof(struct hf_wrapper)); 
-	w->var_type=W_AV;
-	w->u.av.attr.s=name;
-	w->u.av.attr.len=strlen(name);
-	w->u.av.val.s=value;
+	memset(w, 0, sizeof(struct hf_wrapper));
+	w->var_type = W_AV;
+	w->u.av.attr.s = name;
+	w->u.av.attr.len = strlen(name);
+	w->u.av.val.s = value;
 	/* NULL strings considered empty, if len unknown, calculate it now */
-	w->u.av.val.len= value==0?0:(len==0? strlen(value) : len);
-	w->next_other=*list;
-	*list=w;
+	w->u.av.val.len = value == 0 ? 0 : (len == 0 ? strlen(value) : len);
+	w->next_other = *list;
+	*list = w;
 	return 1;
 }
 
@@ -448,66 +466,68 @@ static int append_fixed_vars(struct sip_msg *msg, struct hf_wrapper **list)
 	int val_len;
 
 	/* source ip */
-	if (!append_var(EV_SRCIP, ip_addr2a(&msg->rcv.src_ip), 0, list)) {
+	if(!append_var(EV_SRCIP, ip_addr2a(&msg->rcv.src_ip), 0, list)) {
 		LM_ERR("append_var SRCIP failed \n");
 		return 0;
 	}
 	/* request URI */
-	uri=msg->new_uri.s && msg->new_uri.len ? 
-		&msg->new_uri : &msg->first_line.u.request.uri;
-	if (!append_var(EV_RURI, uri->s, uri->len, list )) {
+	uri = msg->new_uri.s && msg->new_uri.len ? &msg->new_uri
+											 : &msg->first_line.u.request.uri;
+	if(!append_var(EV_RURI, uri->s, uri->len, list)) {
 		LM_ERR("append_var URI failed\n");
 		return 0;
 	}
 	/* userpart of request URI */
-	if (parse_uri(uri->s, uri->len, &parsed_uri)<0) {
+	if(parse_uri(uri->s, uri->len, &parsed_uri) < 0) {
 		LM_WARN("uri not parsed\n");
 	} else {
-		if (!append_var(EV_USER, parsed_uri.user.s, 
-					parsed_uri.user.len, list)) {
+		if(!append_var(EV_USER, parsed_uri.user.s, parsed_uri.user.len, list)) {
 			LM_ERR("append_var USER failed\n");
 			goto error;
 		}
 	}
 	/* original URI */
-	if (!append_var(EV_ORURI, msg->first_line.u.request.uri.s,
-				msg->first_line.u.request.uri.len, list)) {
+	if(!append_var(EV_ORURI, msg->first_line.u.request.uri.s,
+			   msg->first_line.u.request.uri.len, list)) {
 		LM_ERR("append_var O-URI failed\n");
 		goto error;
 	}
 	/* userpart of request URI */
-	if (parse_uri(msg->first_line.u.request.uri.s, 
-				msg->first_line.u.request.uri.len, 
-				&oparsed_uri)<0) {
+	if(parse_uri(msg->first_line.u.request.uri.s,
+			   msg->first_line.u.request.uri.len, &oparsed_uri)
+			< 0) {
 		LM_WARN("orig URI not parsed\n");
 	} else {
-		if (!append_var(EV_OUSER, oparsed_uri.user.s, 
-					oparsed_uri.user.len, list)) {
+		if(!append_var(
+				   EV_OUSER, oparsed_uri.user.s, oparsed_uri.user.len, list)) {
 			LM_ERR("append_var OUSER failed\n");
 			goto error;
 		}
 	}
 	/* tid, transaction id == via/branch */
-	if (!char_msg_val(msg, tid)) {
+	if(!char_msg_val(msg, tid)) {
 		LM_WARN("no tid can be determined\n");
-		val=0; val_len=0;
+		val = 0;
+		val_len = 0;
 	} else {
-		val=tid;val_len=MD5_LEN;
+		val = tid;
+		val_len = MD5_LEN;
 	}
-	if (!append_var(EV_TID, val,val_len, list)) {
+	if(!append_var(EV_TID, val, val_len, list)) {
 		LM_ERR("append_var TID failed\n");
 		goto error;
 	}
 
 	/* did, dialogue id == To-tag */
-	if (!(msg->to && get_to(msg) ))  {
+	if(!(msg->to && get_to(msg))) {
 		LM_ERR("no to-tag\n");
-		val=0; val_len=0;
+		val = 0;
+		val_len = 0;
 	} else {
-		val=get_to(msg)->tag_value.s;
-		val_len=get_to(msg)->tag_value.len;
+		val = get_to(msg)->tag_value.s;
+		val_len = get_to(msg)->tag_value.len;
 	}
-	if (!append_var(EV_DID, val, val_len, list)) {
+	if(!append_var(EV_DID, val, val_len, list)) {
 		LM_ERR("append_var DID failed\n");
 		goto error;
 	}
@@ -522,30 +542,30 @@ environment_t *set_env(struct sip_msg *msg)
 	environment_t *backup_env;
 
 	/* parse all so that we can pass all header fields to script */
-	if (parse_headers(msg, HDR_EOH_F, 0)==-1) {
+	if(parse_headers(msg, HDR_EOH_F, 0) == -1) {
 		LM_ERR("parsing failed\n");
 		return 0;
 	}
 
-	hf_list=0;
+	hf_list = 0;
 	/* create a temporary structure with ordered header fields
 	 * and create environment variables out of it */
-	if (!build_hf_struct(msg, &hf_list)) {
+	if(!build_hf_struct(msg, &hf_list)) {
 		LM_ERR("build_hf_struct failed\n");
 		return 0;
 	}
-	if (!append_fixed_vars(msg, &hf_list)) {
+	if(!append_fixed_vars(msg, &hf_list)) {
 		LM_ERR("append_fixed_vars failed\n");
 		goto error01;
 	}
 	/* create now the strings for environment variables */
-	if (!create_vars(hf_list, 0)) {
+	if(!create_vars(hf_list, 0)) {
 		LM_ERR("create_vars failed\n");
 		goto error00;
 	}
 	/* install the variables in current environment */
-	backup_env=replace_env(hf_list);
-	if (!backup_env) {
+	backup_env = replace_env(hf_list);
+	if(!backup_env) {
 		LM_ERR("replace_env failed\n");
 		goto error00;
 	}
@@ -559,4 +579,3 @@ error01:
 	release_hf_struct(hf_list); /* release temporary ordered HF struct */
 	return 0;
 }
-

+ 29 - 22
src/modules/exec/exec_hf.h

@@ -13,15 +13,15 @@
  * 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 
+ * 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
  */
 
 /*!
  * \file
  * \brief Exec module:: Header Field handling
- * \ingroup exec 
+ * \ingroup exec
  * Module: \ref exec
  */
 
@@ -33,7 +33,7 @@
 /* prefix prepended to header field name in env var name */
 #define SIP "SIP_"
 #define HF_PREFIX SIP "HF_"
-#define HF_PREFIX_LEN (sizeof(HF_PREFIX)-1)
+#define HF_PREFIX_LEN (sizeof(HF_PREFIX) - 1)
 /* well known variable names */
 #define EV_SRCIP SIP "SRCIP"
 #define EV_RURI SIP "RURI"
@@ -49,32 +49,31 @@
 /* RFC3261 -- characters legal in header names; a really
  * _bloated_ thing
  */
-#define UNRESERVED_MARK	"-_.!~*'()"
-#define HNV_UNRESERVED	"[]/?:+$"
+#define UNRESERVED_MARK "-_.!~*'()"
+#define HNV_UNRESERVED "[]/?:+$"
 #define ESCAPE '%'
 /* and this is what all such crazy symbols in header field
  * name will be replaced with in env vars */
 #define HFN_SYMBOL '_'
 
 #define VAR_VIA "VIA"
-#define VAR_VIA_LEN (sizeof(VAR_VIA)-1)
+#define VAR_VIA_LEN (sizeof(VAR_VIA) - 1)
 #define VAR_CTYPE "CONTENT_TYPE"
-#define VAR_CTYPE_LEN (sizeof(VAR_CTYPE)-1)
+#define VAR_CTYPE_LEN (sizeof(VAR_CTYPE) - 1)
 #define VAR_FROM "FROM"
-#define VAR_FROM_LEN (sizeof(VAR_FROM)-1)
+#define VAR_FROM_LEN (sizeof(VAR_FROM) - 1)
 #define VAR_CALLID "CALLID"
-#define VAR_CALLID_LEN (sizeof(VAR_CALLID)-1)
+#define VAR_CALLID_LEN (sizeof(VAR_CALLID) - 1)
 #define VAR_SUPPORTED "SUPPORTED"
-#define VAR_SUPPORTED_LEN (sizeof(VAR_SUPPORTED)-1)
+#define VAR_SUPPORTED_LEN (sizeof(VAR_SUPPORTED) - 1)
 #define VAR_CLEN "CONTENT_LENGTH"
-#define VAR_CLEN_LEN (sizeof(VAR_CLEN)-1)
+#define VAR_CLEN_LEN (sizeof(VAR_CLEN) - 1)
 #define VAR_CONTACT "CONTACT"
-#define VAR_CONTACT_LEN (sizeof(VAR_CONTACT)-1)
+#define VAR_CONTACT_LEN (sizeof(VAR_CONTACT) - 1)
 #define VAR_TO "TO"
-#define VAR_TO_LEN (sizeof(VAR_TO)-1)
+#define VAR_TO_LEN (sizeof(VAR_TO) - 1)
 #define VAR_EVENT "EVENT"
-#define VAR_EVENT_LEN (sizeof(VAR_EVENT)-1)
-
+#define VAR_EVENT_LEN (sizeof(VAR_EVENT) - 1)
 
 
 #if 0
@@ -88,21 +87,29 @@ struct hdr_field {
 };
 #endif
 
-typedef struct env {
-	char** env;
+typedef struct env
+{
+	char **env;
 	int old_cnt;
 } environment_t;
 
-struct attrval {
+struct attrval
+{
 	str attr;
 	str val;
 };
 
-enum wrapper_type { W_HF=1, W_AV };
+enum wrapper_type
+{
+	W_HF = 1,
+	W_AV
+};
 
-struct hf_wrapper {
+struct hf_wrapper
+{
 	enum wrapper_type var_type;
-	union {
+	union
+	{
 		struct hdr_field *hf;
 		struct attrval av;
 	} u;

+ 72 - 75
src/modules/exec/exec_mod.c

@@ -15,8 +15,8 @@
  * 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 
+ * 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
  *
  */
@@ -24,7 +24,7 @@
 /*!
  * \file
  * \brief Exec module:: Module interface
- * \ingroup exec 
+ * \ingroup exec
  * Module: \ref exec
  */
 
@@ -45,19 +45,20 @@
 
 MODULE_VERSION
 
-unsigned int time_to_kill=0;
-int exec_bash_safety=1;
+unsigned int time_to_kill = 0;
+int exec_bash_safety = 1;
 
-static int mod_init( void );
+static int mod_init(void);
 
-static int w_exec_dset(struct sip_msg* msg, char* cmd, char* foo);
-static int w_exec_msg(struct sip_msg* msg, char* cmd, char* foo);
-static int w_exec_avp(struct sip_msg* msg, char* cmd, char* avpl);
+static int w_exec_dset(struct sip_msg *msg, char *cmd, char *foo);
+static int w_exec_msg(struct sip_msg *msg, char *cmd, char *foo);
+static int w_exec_avp(struct sip_msg *msg, char *cmd, char *avpl);
 
-static int exec_avp_fixup(void** param, int param_no);
+static int exec_avp_fixup(void **param, int param_no);
 
 inline static void exec_shutdown(void);
 
+/* clang-format off */
 /*
  * Exported functions
  */
@@ -99,171 +100,167 @@ struct module_exports exports= {
 	exec_shutdown,  /* destroy function */
 	0               /* per-child init function */
 };
+/* clang-format on */
 
 void exec_shutdown(void)
 {
-	if (time_to_kill) destroy_kill();
+	if(time_to_kill)
+		destroy_kill();
 }
 
 
-static int mod_init( void )
+static int mod_init(void)
 {
-	if (time_to_kill) initialize_kill();
+	if(time_to_kill)
+		initialize_kill();
 	return 0;
 }
 
-static int ki_exec_dset(struct sip_msg* msg, str* cmd)
+static int ki_exec_dset(struct sip_msg *msg, str *cmd)
 {
 	str *uri;
 	environment_t *backup;
 	int ret;
-	
-	if(msg==0 || cmd==0)
+
+	if(msg == 0 || cmd == 0)
 		return -1;
-	
-	backup=0;
-	if (setvars) {
-		backup=set_env(msg);
-		if (!backup) {
+
+	backup = 0;
+	if(setvars) {
+		backup = set_env(msg);
+		if(!backup) {
 			LM_ERR("no env created\n");
 			return -1;
 		}
 	}
 
-	if (msg->new_uri.s && msg->new_uri.len)
-		uri=&msg->new_uri;
+	if(msg->new_uri.s && msg->new_uri.len)
+		uri = &msg->new_uri;
 	else
-		uri=&msg->first_line.u.request.uri;
-	
+		uri = &msg->first_line.u.request.uri;
+
 	LM_DBG("executing [%s]\n", cmd->s);
 
-	ret=exec_str(msg, cmd->s, uri->s, uri->len);
-	if (setvars) {
+	ret = exec_str(msg, cmd->s, uri->s, uri->len);
+	if(setvars) {
 		unset_env(backup);
 	}
 	return ret;
 }
 
-static int w_exec_dset(struct sip_msg* msg, char* cmd, char* foo)
+static int w_exec_dset(struct sip_msg *msg, char *cmd, char *foo)
 {
 	str command;
-	if(fixup_get_svalue(msg, (gparam_p)cmd, &command)!=0)
-	{
+	if(fixup_get_svalue(msg, (gparam_p)cmd, &command) != 0) {
 		LM_ERR("invalid command parameter");
 		return -1;
 	}
 	return ki_exec_dset(msg, &command);
 }
 
-static int ki_exec_msg(struct sip_msg* msg, str* cmd)
+static int ki_exec_msg(struct sip_msg *msg, str *cmd)
 {
 	environment_t *backup;
 	int ret;
-	
-	if(msg==0 || cmd==0)
+
+	if(msg == 0 || cmd == 0)
 		return -1;
 
-	backup=0;
-	if (setvars) {
-		backup=set_env(msg);
-		if (!backup) {
+	backup = 0;
+	if(setvars) {
+		backup = set_env(msg);
+		if(!backup) {
 			LM_ERR("no env created\n");
 			return -1;
 		}
 	}
-	
+
 	LM_DBG("executing [%s]\n", cmd->s);
-	
-	ret=exec_msg(msg, cmd->s);
-	if (setvars) {
+
+	ret = exec_msg(msg, cmd->s);
+	if(setvars) {
 		unset_env(backup);
 	}
 	return ret;
 }
 
-static int w_exec_msg(struct sip_msg* msg, char* cmd, char* foo)
+static int w_exec_msg(struct sip_msg *msg, char *cmd, char *foo)
 {
 	str command;
-	
-	if(fixup_get_svalue(msg, (gparam_p)cmd, &command)!=0)
-	{
+
+	if(fixup_get_svalue(msg, (gparam_p)cmd, &command) != 0) {
 		LM_ERR("invalid command parameter");
 		return -1;
 	}
 	return ki_exec_msg(msg, &command);
 }
 
-static int w_exec_avp_helper(sip_msg_t* msg, str* cmd, pvname_list_t* avpl)
+static int w_exec_avp_helper(sip_msg_t *msg, str *cmd, pvname_list_t *avpl)
 {
 	environment_t *backup;
 	int ret;
-	
-	if(msg==0 || cmd==0)
+
+	if(msg == 0 || cmd == 0)
 		return -1;
-	
-	backup=0;
-	if (setvars) {
-		backup=set_env(msg);
-		if (!backup) {
+
+	backup = 0;
+	if(setvars) {
+		backup = set_env(msg);
+		if(!backup) {
 			LM_ERR("no env created\n");
 			return -1;
 		}
 	}
-	
+
 	LM_DBG("executing [%s]\n", cmd->s);
 
-	ret=exec_avp(msg, cmd->s, avpl);
-	if (setvars) {
+	ret = exec_avp(msg, cmd->s, avpl);
+	if(setvars) {
 		unset_env(backup);
 	}
 	return ret;
 }
 
-static int w_exec_avp(struct sip_msg* msg, char* cmd, char* avpl)
+static int w_exec_avp(struct sip_msg *msg, char *cmd, char *avpl)
 {
 	str command;
 
-	if(fixup_get_svalue(msg, (gparam_p)cmd, &command)!=0)
-	{
+	if(fixup_get_svalue(msg, (gparam_p)cmd, &command) != 0) {
 		LM_ERR("invalid command parameter");
 		return -1;
 	}
-	return w_exec_avp_helper(msg, &command, (pvname_list_t*)avpl);
+	return w_exec_avp_helper(msg, &command, (pvname_list_t *)avpl);
 }
 
-static int ki_exec_avp(sip_msg_t* msg, str* cmd)
+static int ki_exec_avp(sip_msg_t *msg, str *cmd)
 {
 	return w_exec_avp_helper(msg, cmd, NULL);
 }
 
-static int exec_avp_fixup(void** param, int param_no)
+static int exec_avp_fixup(void **param, int param_no)
 {
 	pvname_list_t *anlist = NULL;
 	str s;
 
-	s.s = (char*)(*param);
-	if (param_no==1)
-	{
-		if(s.s==NULL)
-		{
+	s.s = (char *)(*param);
+	if(param_no == 1) {
+		if(s.s == NULL) {
 			LM_ERR("null format in P%d\n", param_no);
 			return E_UNSPEC;
 		}
 		return fixup_spve_null(param, 1);
-	} else if(param_no==2) {
-		if(s.s==NULL)
-		{
+	} else if(param_no == 2) {
+		if(s.s == NULL) {
 			LM_ERR("null format in P%d\n", param_no);
 			return E_UNSPEC;
 		}
-		s.len =  strlen(s.s);
+		s.len = strlen(s.s);
 		anlist = parse_pvname_list(&s, PVT_AVP);
-		if(anlist==NULL)
-		{
+		if(anlist == NULL) {
 			LM_ERR("bad format in P%d [%s]\n", param_no, s.s);
 			return E_UNSPEC;
 		}
-		*param = (void*)anlist;
+		*param = (void *)anlist;
 		return 0;
 	}
 
@@ -302,4 +299,4 @@ int mod_register(char *path, int *dlflags, void *p1, void *p2)
 {
 	sr_kemi_modules_add(sr_kemi_exec_exports);
 	return 0;
-}
+}

+ 48 - 50
src/modules/exec/kill.c

@@ -13,8 +13,8 @@
  * 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 
+ * 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
  *
  */
@@ -22,7 +22,7 @@
 /*!
  * \file
  * \brief Exec module:: Kill
- * \ingroup exec 
+ * \ingroup exec
  * Module: \ref exec
  *
  * in this file, we implement the ability to send a kill signal to
@@ -38,7 +38,8 @@
  * so we cannot close anyway
  *
  * From the README:
- *  (There is kill.c but it is not used along with the current mechanisms based on popen. Besides that kill.c is ugly).
+ *  (There is kill.c but it is not used along with the current mechanisms
+ *  based on popen. Besides that kill.c is ugly).
  */
 
 
@@ -46,7 +47,7 @@
 #include <sys/types.h>
 #include <signal.h>
 
-#include "../../core/mem/shm_mem.h" 
+#include "../../core/mem/shm_mem.h"
 #include "../../core/dprint.h"
 #include "../../core/timer.h"
 #include "../../core/locking.h"
@@ -54,41 +55,39 @@
 #include "kill.h"
 
 
-static gen_lock_t *kill_lock=NULL;
+static gen_lock_t *kill_lock = NULL;
 
 
 static struct timer_list kill_list;
 
 
-
 #define lock() lock_get(kill_lock)
 
 #define unlock() lock_release(kill_lock)
 
 
-
 /* copy and paste from TM -- might consider putting in better
-   in some utils part of core
-*/
-static void timer_routine(unsigned int ticks , void * attr)
+ * in some utils part of core
+ */
+static void timer_routine(unsigned int ticks, void *attr)
 {
 	struct timer_link *tl, *tmp_tl, *end, *ret;
 	int killr;
 
 	/* check if it worth entering the lock */
-	if (kill_list.first_tl.next_tl==&kill_list.last_tl 
-			|| kill_list.first_tl.next_tl->time_out > ticks )
+	if(kill_list.first_tl.next_tl == &kill_list.last_tl
+			|| kill_list.first_tl.next_tl->time_out > ticks)
 		return;
 
 	lock();
 	end = &kill_list.last_tl;
 	tl = kill_list.first_tl.next_tl;
-	while( tl!=end && tl->time_out <= ticks ) {
-		tl=tl->next_tl;
+	while(tl != end && tl->time_out <= ticks) {
+		tl = tl->next_tl;
 	}
 
 	/* nothing to delete found */
-	if (tl->prev_tl==&kill_list.first_tl) {
+	if(tl->prev_tl == &kill_list.first_tl) {
 		unlock();
 		return;
 	}
@@ -98,38 +97,38 @@ static void timer_routine(unsigned int ticks , void * attr)
 	tl->prev_tl->next_tl = 0;
 	/* the shortened list starts from where we suspended */
 	kill_list.first_tl.next_tl = tl;
-	tl->prev_tl = & kill_list.first_tl;
+	tl->prev_tl = &kill_list.first_tl;
 	unlock();
 
 	/* process the list now */
-	while (ret) {
-		tmp_tl=ret->next_tl;
-		ret->next_tl=ret->prev_tl=0;
-		if (ret->time_out>0) {
-			killr=kill(ret->pid, SIGTERM );
-			LM_DBG("child process (%d) kill status: %d\n", ret->pid, killr );
+	while(ret) {
+		tmp_tl = ret->next_tl;
+		ret->next_tl = ret->prev_tl = 0;
+		if(ret->time_out > 0) {
+			killr = kill(ret->pid, SIGTERM);
+			LM_DBG("child process (%d) kill status: %d\n", ret->pid, killr);
 		}
 		shm_free(ret);
-		ret=tmp_tl;
+		ret = tmp_tl;
 	}
 }
 
-int schedule_to_kill( int pid )
+int schedule_to_kill(int pid)
 {
 	struct timer_link *tl;
-	tl=shm_malloc( sizeof(struct timer_link) );
-	if (tl==0) {
+	tl = shm_malloc(sizeof(struct timer_link));
+	if(tl == 0) {
 		LM_ERR("no shmem\n");
 		return -1;
 	}
-	memset(tl, 0, sizeof(struct timer_link) );
+	memset(tl, 0, sizeof(struct timer_link));
 	lock();
-	tl->pid=pid;
-	tl->time_out=get_ticks()+time_to_kill;
-	tl->prev_tl=kill_list.last_tl.prev_tl;
-	tl->next_tl=&kill_list.last_tl;
-	kill_list.last_tl.prev_tl=tl;
-	tl->prev_tl->next_tl=tl;
+	tl->pid = pid;
+	tl->time_out = get_ticks() + time_to_kill;
+	tl->prev_tl = kill_list.last_tl.prev_tl;
+	tl->next_tl = &kill_list.last_tl;
+	kill_list.last_tl.prev_tl = tl;
+	tl->prev_tl->next_tl = tl;
 	unlock();
 	return 1;
 }
@@ -137,19 +136,18 @@ int schedule_to_kill( int pid )
 int initialize_kill(void)
 {
 	/* if disabled ... */
-	if (time_to_kill==0) return 1;
-    if ((register_timer( timer_routine,
-            0 /* param */, 1 /* period */)<0)) {
-        LM_ERR("no exec timer registered\n");
-        return -1;
-    }
-	kill_list.first_tl.next_tl=&kill_list.last_tl;
-	kill_list.last_tl.prev_tl=&kill_list.first_tl;
-	kill_list.first_tl.prev_tl=
-	kill_list.last_tl.next_tl = 0;
-	kill_list.last_tl.time_out=-1;
-	kill_lock=lock_alloc();
-	if (kill_lock==0) {
+	if(time_to_kill == 0)
+		return 1;
+	if((register_timer(timer_routine, 0 /* param */, 1 /* period */) < 0)) {
+		LM_ERR("no exec timer registered\n");
+		return -1;
+	}
+	kill_list.first_tl.next_tl = &kill_list.last_tl;
+	kill_list.last_tl.prev_tl = &kill_list.first_tl;
+	kill_list.first_tl.prev_tl = kill_list.last_tl.next_tl = 0;
+	kill_list.last_tl.time_out = -1;
+	kill_lock = lock_alloc();
+	if(kill_lock == 0) {
 		LM_ERR("no shm mem for mutex\n");
 		return -1;
 	}
@@ -161,9 +159,9 @@ int initialize_kill(void)
 void destroy_kill(void)
 {
 	/* if disabled ... */
-	if (time_to_kill==0) 
-		return; 
-	if (kill_lock) {
+	if(time_to_kill == 0)
+		return;
+	if(kill_lock) {
 		lock_destroy(kill_lock);
 		lock_dealloc(kill_lock);
 	}

+ 8 - 9
src/modules/exec/kill.h

@@ -13,15 +13,15 @@
  * 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 
+ * 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
  */
 
 /*!
  * \file
  * \brief Exec module:: Kill process
- * \ingroup exec 
+ * \ingroup exec
  * Module: \ref exec
  */
 
@@ -29,7 +29,8 @@
 #ifndef _KILL_H
 #define _KILL_H
 
-struct timer_link {
+struct timer_link
+{
 	struct timer_link *next_tl;
 	struct timer_link *prev_tl;
 	volatile unsigned int time_out;
@@ -38,16 +39,14 @@ struct timer_link {
 
 struct timer_list
 {
-	struct timer_link  first_tl;
-	struct timer_link  last_tl;
+	struct timer_link first_tl;
+	struct timer_link last_tl;
 };
 
 extern unsigned int time_to_kill;
 
 void destroy_kill(void);
 int initialize_kill(void);
-int schedule_to_kill( int pid );
-
+int schedule_to_kill(int pid);
 
 #endif
-