瀏覽代碼

process table introduced; rest of code aligned with proces_no bug_fix

Jiri Kuthan 23 年之前
父節點
當前提交
f51155cfd6
共有 7 個文件被更改,包括 160 次插入88 次删除
  1. 36 6
      fifo_server.c
  2. 6 0
      fifo_server.h
  3. 31 22
      main.c
  4. 2 1
      msg_translator.c
  5. 49 0
      pt.h
  6. 34 59
      scripts/sc
  7. 2 0
      ut.h

+ 36 - 6
fifo_server.c

@@ -48,6 +48,7 @@
 #include "fifo_server.h"
 #include "mem/mem.h"
 #include "sr_module.h"
+#include "pt.h"
 
 /* FIFO server vars */
 char *fifo="/tmp/ser_fifo"; /* FIFO name */
@@ -470,7 +471,8 @@ int open_fifo_server()
 		fifo_server( fifo_stream ); /* never retruns */
 	}
 	/* dad process */
-	pids[process_no]=fifo_pid;
+	pt[process_no].pid=fifo_pid;
+	strncpy(pt[process_no].desc, "fifo server", MAX_PT_DESC );
 	/* make sure the read fifo will not close */
 	fifo_write=open(fifo, O_WRONLY, 0);
 	if (fifo_write<0) {
@@ -546,7 +548,7 @@ static int which_fifo_cmd(FILE *stream, char *response_file )
 
 	reply_pipe=open_reply_pipe(response_file);
 	if (reply_pipe==NULL) {
-		LOG(L_ERR, "ERROR: opening reply pipe (%s) failed\n",
+		LOG(L_ERR, "ERROR: which_fifo_cmd: opening reply pipe (%s) failed\n",
 			response_file );
 		return -1;
 	}
@@ -560,23 +562,51 @@ static int which_fifo_cmd(FILE *stream, char *response_file )
 	return 1;
 }
 
+static int ps_fifo_cmd(FILE *stream, char *response_file )
+{
+	FILE *reply_pipe;
+	int p;
+
+	if (response_file==0 || *response_file==0 ) {
+		 LOG(L_ERR, "ERROR: ps_fifo_cmd: null file\n");
+		return -1;
+	}
+	reply_pipe=open_reply_pipe(response_file);
+	if (reply_pipe==NULL) {
+		LOG(L_ERR, "ERROR: ps_fifo_cmd: opening reply pipe (%s) failed\n",
+			response_file );
+		return -1;
+	}
+
+	for (p=0; p<process_count();p++) 
+		fprintf( reply_pipe, "%d\t%d\t%s\n",
+			p, pt[p].pid, pt[p].desc );
+
+	fclose(reply_pipe);
+	return 1;
+}
+
 
 int register_core_fifo()
 {
 	if (register_fifo_cmd(print_fifo_cmd, FIFO_PRINT, 0)<0) {
-		LOG(L_CRIT, "unable to register 'print' FIFO cmd\n");
+		LOG(L_CRIT, "unable to register '%s' FIFO cmd\n", FIFO_PRINT);
 		return -1;
 	}
 	if (register_fifo_cmd(uptime_fifo_cmd, FIFO_UPTIME, 0)<0) {
-		LOG(L_CRIT, "unable to register 'print' FIFO cmd\n");
+		LOG(L_CRIT, "unable to register '%s' FIFO cmd\n", FIFO_UPTIME);
 		return -1;
 	}
 	if (register_fifo_cmd(print_version_cmd, FIFO_VERSION, 0)<0) {
-		LOG(L_CRIT, "unable to register 'version' FIFO cmd\n");
+		LOG(L_CRIT, "unable to register '%s' FIFO cmd\n", FIFO_VERSION);
 		return -1;
 	}
 	if (register_fifo_cmd(which_fifo_cmd, FIFO_WHICH, 0)<0) {
-		LOG(L_CRIT, "unable to register 'version' FIFO cmd\n");
+		LOG(L_CRIT, "unable to register '%s' FIFO cmd\n", FIFO_WHICH);
+		return -1;
+	}
+	if (register_fifo_cmd(ps_fifo_cmd, FIFO_PS, 0)<0) {
+		LOG(L_CRIT, "unable to register '%s' FIFO cmd\n", FIFO_PS);
 		return -1;
 	}
 	return 1;

+ 6 - 0
fifo_server.h

@@ -11,10 +11,16 @@
 #define CMD_SEPARATOR ':'
 
 /* core FIFO command set */
+/* echo input */
 #define FIFO_PRINT "print"
+/* print server's uptime */
 #define FIFO_UPTIME "uptime"
+/* print server's version */
 #define FIFO_VERSION "version"
+/* print available FIFO commands */
 #define FIFO_WHICH "which"
+/* print server's process table */
+#define FIFO_PS "ps"
 
 #define MAX_CTIME_LEN 128
 

+ 31 - 22
main.c

@@ -45,7 +45,7 @@
 #include "fifo_server.h"
 #include "name_alias.h"
 #include "hash_func.h"
-#include "hash_func.h"
+#include "pt.h"
 
 
 #include "stats.h"
@@ -197,7 +197,7 @@ unsigned int maxbuffer = MAX_RECV_BUFFER_SIZE; /* maximum buffer size we do
 												  auto-probing procedure; may 
 												  be re-configured */
 int children_no = 0;			/* number of children processing requests */
-int *pids=0;					/*array with childrens pids, 0= main proc,
+struct process_table *pt=0;		/*array with childrens pids, 0= main proc,
 									alloc'ed in shared mem if possible*/
 int sig_flag = 0;              /* last signal received */
 int debug = 0;
@@ -390,14 +390,14 @@ void handle_sigs()
 				
 			destroy_modules();
 #ifdef PKG_MALLOC
-			LOG(L_INFO, "Memory status (pkg):\n");
+			LOG(memlog, "Memory status (pkg):\n");
 			pkg_status();
 #endif
 #ifdef SHM_MEM
-			LOG(L_INFO, "Memory status (shm):\n");
+			LOG(memlog, "Memory status (shm):\n");
 			shm_status();
 			/* zero all shmem alloc vars that we still use */
-			pids=0;
+			pt=0;
 			shm_mem_destroy();
 #endif
 			if (pid_file) unlink(pid_file);
@@ -412,11 +412,11 @@ void handle_sigs()
 			dump_all_statistic();
 #endif
 #ifdef PKG_MALLOC
-			LOG(L_INFO, "Memory status (pkg):\n");
+			LOG(memlog, "Memory status (pkg):\n");
 			pkg_status();
 #endif
 #ifdef SHM_MEM
-			LOG(L_INFO, "Memory status (shm):\n");
+			LOG(memlog, "Memory status (shm):\n");
 			shm_status();
 #endif
 			break;
@@ -515,7 +515,8 @@ int main_loop()
 						timer_ticker();
 					}
 				}else{
-						pids[process_no]=pid; /*should be shared mem anway*/
+						pt[process_no].pid=pid; /*should be shared mem anway*/
+						strncpy(pt[process_no].desc, "timer", MAX_PT_DESC );
 				}
 		}
 
@@ -525,9 +526,12 @@ int main_loop()
 			goto error;
 		}
 		/* main process, receive loop */
-		pids[0]=getpid();
-		/* process_bit = 1; */
 		process_no=0; /*main process number*/
+		pt[process_no].pid=getpid();
+		snprintf(pt[process_no].desc, MAX_PT_DESC, 
+			"stand-alone receiver @ %s:%s", 
+			 bind_address->name.s, bind_address->port_no_str.s );
+		
 		
 		     /* We will call child_init even if we
 		      * do not fork
@@ -544,9 +548,7 @@ int main_loop()
 		return udp_rcv_loop();
 	}else{
 		/* process_no now initialized to zero -- increase from now on
-		   as new processes are forked (while skipping 0 reserved for main ;
-		   not that with multiple listeners, more children processes will
-		   share the same process_no and the pids array will be rewritten
+		   as new processes are forked (while skipping 0 reserved for main )
 		*/
 		for(r=0;r<sock_no;r++){
 			/* create the listening socket (for each address)*/
@@ -581,7 +583,10 @@ int main_loop()
 #endif
 					return udp_rcv_loop();
 				}else{
-						pids[process_no]=pid; /*should be in shared mem.*/
+						pt[process_no].pid=pid; /*should be in shared mem.*/
+						snprintf(pt[process_no].desc, MAX_PT_DESC,
+							"receiver child=%d sock=%d @ %s:%s", i, r, 	
+							sock_info[r].name.s, sock_info[r].port_no_str.s );
 				}
 			}
 			/*parent*/
@@ -615,18 +620,22 @@ int main_loop()
 				timer_ticker();
 			}
 		}else{
-			pids[process_no]=pid;
+			pt[process_no].pid=pid;
+			strncpy(pt[process_no].desc, "timer", MAX_PT_DESC );
 		}
 	}
 
 	/* main */
-	pids[0]=getpid();
+	pt[0].pid=getpid();
+	strncpy(pt[0].desc, "attendant", MAX_PT_DESC );
 	/*DEBUG- remove it*/
+#ifdef DEBUG
 	printf("\n% 3d processes, % 3d children * % 3d listening addresses + main + fifo %s\n",
 			process_no+1, children_no, sock_no, (timer_list)?"+ timer":"");
 	for (r=0; r<=process_no; r++){
-		printf("% 3d   % 5d\n", r, pids[r]);
+		printf("% 3d   % 5d\n", r, pt[r].pid);
 	}
+#endif
 	process_no=0; 
 	/* process_bit = 0; */
 	is_main=1;
@@ -670,7 +679,7 @@ static void sig_usr(int signo)
 			case SIGTERM:
 					/* print memory stats for non-main too */
 					#ifdef PKG_MALLOC
-					LOG(L_INFO, "Memory status (pkg):\n");
+					LOG(memlog, "Memory status (pkg):\n");
 					pkg_status();
 					#endif
 					exit(0);
@@ -1242,15 +1251,15 @@ int main(int argc, char** argv)
 	
 	/*alloc pids*/
 #ifdef SHM_MEM
-	pids=shm_malloc(sizeof(int)*(children_no*sock_no+1/*main*/+1/*timer */+1/*fifo*/));
+	pt=shm_malloc(sizeof(struct process_table)*process_count());
 #else
-	pids=malloc(sizeof(int)*(children_no*sock_no+1+1+1));
+	pt=malloc(sizeof(struct process_table)*process_count());
 #endif
-	if (pids==0){
+	if (pt==0){
 		fprintf(stderr, "ERROR: out  of memory\n");
 		goto error;
 	}
-	memset(pids, 0, sizeof(int)*(children_no+1));
+	memset(pt, 0, sizeof(struct process_table)*process_count());
 	
 	/* init_daemon? */
 	if (!dont_fork){

+ 2 - 1
msg_translator.c

@@ -22,6 +22,7 @@
 #include "ip_addr.h"
 #include "resolve.h"
 #include "ut.h"
+#include "pt.h"
 
 
 #define append_str(_dest,_src,_len,_msg) \
@@ -121,7 +122,7 @@ char * warning_builder( struct sip_msg *msg, unsigned int *returned_len)
 		foo=&(msg->first_line.u.request.uri);
 	print_len=snprintf(buf+fix_len, MAX_WARNING_LEN-fix_len,
 		"pid=%d req_src_ip=%s in_uri=%.*s out_uri=%.*s via_cnt%c=%d\"",
-		pids?pids[process_no]:0,
+		my_pid(),
 		ip_addr2a(&msg->src_ip),
 		msg->first_line.u.request.uri.len, msg->first_line.u.request.uri.s,
 		foo->len, foo->s, 

+ 49 - 0
pt.h

@@ -0,0 +1,49 @@
+/*
+ * $Id$
+ *
+ * Process Table
+ *
+ *
+ */
+
+#ifndef _PT_H
+
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "globals.h"
+#include "timer.h"
+
+#define MAX_PT_DESC	128
+
+struct process_table {
+	int pid;
+	char desc[MAX_PT_DESC];
+};
+
+extern struct process_table *pt;
+extern int process_no;
+
+/* get number of process started by main with
+   given configuration
+*/
+inline static int process_count()
+{
+    return 
+		/* receivers and attendant */
+		(dont_fork ? 1 : children_no*sock_no + 1)
+		/* timer process */
+		+ (timer_list ? 1 : 0 )
+		/* fifo server */
+		+((fifo==NULL || strlen(fifo)==0) ? 0 : 1 );
+}
+
+
+/* retun processes's pid */
+inline static int my_pid()
+{
+	return pt ? pt[process_no].pid : getpid();
+}
+
+
+#endif

+ 34 - 59
scripts/sc

@@ -1,4 +1,4 @@
-#!/bin/sh 
+#!/bin/sh
 #
 # $Id$
 #
@@ -96,8 +96,8 @@ usage:
  $CMD perm <user> <uri> ............... introduce a permanent UrLoc entry
            < server health >
  $CMD monitor ......................... show internal status
- $CMD stat ............................ show runnig processes 
-                                        + $CMD show
+ $CMD ps .............................. show runnig processes 
+ $CMD fifo ............................ send raw commands to FIFO
 
    commands labeled with (*) will prompt for a MySQL password
    if the variable PW is set, the password will not be prompted"
@@ -116,8 +116,13 @@ prompt_pw() {
 	fi
 }
 
-ul_dump()
+
+fifo_cmd()
 {
+	if [ "$#" -lt 1 -o "$#" -gt 3 ]; then
+		echo "ERROR: fifo_cmd takes 1..3 parameters and not $#"
+		exit
+	fi
 	name=ser_receiver_$$
 	path=/tmp/$name
 	if [ ! -w $SER_FIFO ]; then
@@ -130,57 +135,28 @@ ul_dump()
 		echo "error opening read fifo $path"
 		exit 1
 	fi
-	cat > $SER_FIFO <<EOF
-:ul_dump:$name
 
-EOF
-	cat < $path
-	rm $path
-}
+	if [ "$#" -eq 1 ] ; then
+		cat > $SER_FIFO <<EOF
+:$1:$name
 
-ul_show_contact() # params: <table> <username>
-{
-	name=ser_receiver_$$
-	path=/tmp/$name
-	if [ ! -w $SER_FIFO ]; then
-		echo "Error opening ser's FIFO $SER_FIFO"
-		exit 1
-	fi
-	mkfifo $path
-	if [ $? -ne 0 ] ; then
-		echo "error opening read fifo $path"
-		exit 1
-	fi
-	cat > $SER_FIFO <<EOF
-:ul_show_contact:$name
-$1
+EOF
+	elif [ "$#" -eq 2 ] ; then 
+		cat > $SER_FIFO <<EOF
+:$1:$name
 $2
 
 EOF
-	cat < $path
-	rm $path
-}
-
-ul_rm() # params: <table> <username>
-{
-	name=ser_receiver_$$
-	path=/tmp/$name
-	if [ ! -w $SER_FIFO ]; then
-		echo "Error opening ser's FIFO $SER_FIFO"
-		echo "Make sure you have line fifo=$SER_FIFO in your config"
-		exit 1
-	fi
-	mkfifo $path
-	if [ $? -ne 0 ] ; then
-		echo "error opening read fifo $path"
-		exit 1
-	fi
-	cat > $SER_FIFO <<EOF
-:ul_rm:$name
-$1
+	elif [ "$#" -eq 3 ] ; then 
+		cat > $SER_FIFO <<EOF
+:$1:$name
 $2
+$3
 
 EOF
+	fi
+
+
 	cat < $path
 	rm $path
 }
@@ -505,7 +481,7 @@ case $1 in
 			usage
 			exit 1
 		fi
-		ul_rm $1 $2
+		fifo_cmd ul_rm $1 $2
 		exit $?
 		;;
 
@@ -563,15 +539,15 @@ case $1 in
 		shift
 	
 		if [ $# -eq 1 ] ; then
-			ul_show_contact $USRLOC $1
+			fifo_cmd ul_show_contact $USRLOC $1
 		else
-			ul_dump
+			fifo_cmd ul_dump
 		fi
 		exit $?
 		;;
 
 	online)
-		ul_dump grep aor| awk '{print $3}' | sort | sort -mu
+		fifo_cmd ul_dump |grep aor| awk '{print $3}' | sort | sort -mu
 		exit $?
 		;;
 
@@ -630,14 +606,8 @@ case $1 in
 		$0 dul $1   > /dev/null 2>&1
         ;;
 			
-	stat)
-        if [ $# -ne 1 ] ; then
-            usage
-            exit 1
-        fi
-		ps -o "pid,user,args" $i -C $SER
-		$0 show
-
+	ps)
+		fifo_cmd ps
 		;;
 
 	acl)
@@ -649,6 +619,11 @@ case $1 in
 		shift
 		ser_alias "$@"
 		;;
+	
+	fifo)
+		shift
+		fifo_cmd "$@"
+		;;
 		
 	*)
 		usage

+ 2 - 0
ut.h

@@ -190,6 +190,8 @@ inline static int string2hex(
 	return orig_len-len;
 }
 
+/* portable sleep in microseconds (no interrupt handling now) */
+
 inline static void sleep_us( unsigned int nusecs )
 {
 	struct timeval tval;