Browse code

process table introduced; rest of code aligned with proces_no bug_fix

Jiri Kuthan authored on 18/09/2002 07:49:31
Showing 7 changed files
... ...
@@ -48,6 +48,7 @@
48 48
 #include "fifo_server.h"
49 49
 #include "mem/mem.h"
50 50
 #include "sr_module.h"
51
+#include "pt.h"
51 52
 
52 53
 /* FIFO server vars */
53 54
 char *fifo="/tmp/ser_fifo"; /* FIFO name */
... ...
@@ -470,7 +471,8 @@ int open_fifo_server()
470 471
 		fifo_server( fifo_stream ); /* never retruns */
471 472
 	}
472 473
 	/* dad process */
473
-	pids[process_no]=fifo_pid;
474
+	pt[process_no].pid=fifo_pid;
475
+	strncpy(pt[process_no].desc, "fifo server", MAX_PT_DESC );
474 476
 	/* make sure the read fifo will not close */
475 477
 	fifo_write=open(fifo, O_WRONLY, 0);
476 478
 	if (fifo_write<0) {
... ...
@@ -546,7 +548,7 @@ static int which_fifo_cmd(FILE *stream, char *response_file )
546 548
 
547 549
 	reply_pipe=open_reply_pipe(response_file);
548 550
 	if (reply_pipe==NULL) {
549
-		LOG(L_ERR, "ERROR: opening reply pipe (%s) failed\n",
551
+		LOG(L_ERR, "ERROR: which_fifo_cmd: opening reply pipe (%s) failed\n",
550 552
 			response_file );
551 553
 		return -1;
552 554
 	}
... ...
@@ -560,23 +562,51 @@ static int which_fifo_cmd(FILE *stream, char *response_file )
560 562
 	return 1;
561 563
 }
562 564
 
565
+static int ps_fifo_cmd(FILE *stream, char *response_file )
566
+{
567
+	FILE *reply_pipe;
568
+	int p;
569
+
570
+	if (response_file==0 || *response_file==0 ) {
571
+		 LOG(L_ERR, "ERROR: ps_fifo_cmd: null file\n");
572
+		return -1;
573
+	}
574
+	reply_pipe=open_reply_pipe(response_file);
575
+	if (reply_pipe==NULL) {
576
+		LOG(L_ERR, "ERROR: ps_fifo_cmd: opening reply pipe (%s) failed\n",
577
+			response_file );
578
+		return -1;
579
+	}
580
+
581
+	for (p=0; p<process_count();p++) 
582
+		fprintf( reply_pipe, "%d\t%d\t%s\n",
583
+			p, pt[p].pid, pt[p].desc );
584
+
585
+	fclose(reply_pipe);
586
+	return 1;
587
+}
588
+
563 589
 
564 590
 int register_core_fifo()
565 591
 {
566 592
 	if (register_fifo_cmd(print_fifo_cmd, FIFO_PRINT, 0)<0) {
567
-		LOG(L_CRIT, "unable to register 'print' FIFO cmd\n");
593
+		LOG(L_CRIT, "unable to register '%s' FIFO cmd\n", FIFO_PRINT);
568 594
 		return -1;
569 595
 	}
570 596
 	if (register_fifo_cmd(uptime_fifo_cmd, FIFO_UPTIME, 0)<0) {
571
-		LOG(L_CRIT, "unable to register 'print' FIFO cmd\n");
597
+		LOG(L_CRIT, "unable to register '%s' FIFO cmd\n", FIFO_UPTIME);
572 598
 		return -1;
573 599
 	}
574 600
 	if (register_fifo_cmd(print_version_cmd, FIFO_VERSION, 0)<0) {
575
-		LOG(L_CRIT, "unable to register 'version' FIFO cmd\n");
601
+		LOG(L_CRIT, "unable to register '%s' FIFO cmd\n", FIFO_VERSION);
576 602
 		return -1;
577 603
 	}
578 604
 	if (register_fifo_cmd(which_fifo_cmd, FIFO_WHICH, 0)<0) {
579
-		LOG(L_CRIT, "unable to register 'version' FIFO cmd\n");
605
+		LOG(L_CRIT, "unable to register '%s' FIFO cmd\n", FIFO_WHICH);
606
+		return -1;
607
+	}
608
+	if (register_fifo_cmd(ps_fifo_cmd, FIFO_PS, 0)<0) {
609
+		LOG(L_CRIT, "unable to register '%s' FIFO cmd\n", FIFO_PS);
580 610
 		return -1;
581 611
 	}
582 612
 	return 1;
... ...
@@ -11,10 +11,16 @@
11 11
 #define CMD_SEPARATOR ':'
12 12
 
13 13
 /* core FIFO command set */
14
+/* echo input */
14 15
 #define FIFO_PRINT "print"
16
+/* print server's uptime */
15 17
 #define FIFO_UPTIME "uptime"
18
+/* print server's version */
16 19
 #define FIFO_VERSION "version"
20
+/* print available FIFO commands */
17 21
 #define FIFO_WHICH "which"
22
+/* print server's process table */
23
+#define FIFO_PS "ps"
18 24
 
19 25
 #define MAX_CTIME_LEN 128
20 26
 
... ...
@@ -45,7 +45,7 @@
45 45
 #include "fifo_server.h"
46 46
 #include "name_alias.h"
47 47
 #include "hash_func.h"
48
-#include "hash_func.h"
48
+#include "pt.h"
49 49
 
50 50
 
51 51
 #include "stats.h"
... ...
@@ -197,7 +197,7 @@ unsigned int maxbuffer = MAX_RECV_BUFFER_SIZE; /* maximum buffer size we do
197 197
 												  auto-probing procedure; may 
198 198
 												  be re-configured */
199 199
 int children_no = 0;			/* number of children processing requests */
200
-int *pids=0;					/*array with childrens pids, 0= main proc,
200
+struct process_table *pt=0;		/*array with childrens pids, 0= main proc,
201 201
 									alloc'ed in shared mem if possible*/
202 202
 int sig_flag = 0;              /* last signal received */
203 203
 int debug = 0;
... ...
@@ -390,14 +390,14 @@ void handle_sigs()
390 390
 				
391 391
 			destroy_modules();
392 392
 #ifdef PKG_MALLOC
393
-			LOG(L_INFO, "Memory status (pkg):\n");
393
+			LOG(memlog, "Memory status (pkg):\n");
394 394
 			pkg_status();
395 395
 #endif
396 396
 #ifdef SHM_MEM
397
-			LOG(L_INFO, "Memory status (shm):\n");
397
+			LOG(memlog, "Memory status (shm):\n");
398 398
 			shm_status();
399 399
 			/* zero all shmem alloc vars that we still use */
400
-			pids=0;
400
+			pt=0;
401 401
 			shm_mem_destroy();
402 402
 #endif
403 403
 			if (pid_file) unlink(pid_file);
... ...
@@ -412,11 +412,11 @@ void handle_sigs()
412 412
 			dump_all_statistic();
413 413
 #endif
414 414
 #ifdef PKG_MALLOC
415
-			LOG(L_INFO, "Memory status (pkg):\n");
415
+			LOG(memlog, "Memory status (pkg):\n");
416 416
 			pkg_status();
417 417
 #endif
418 418
 #ifdef SHM_MEM
419
-			LOG(L_INFO, "Memory status (shm):\n");
419
+			LOG(memlog, "Memory status (shm):\n");
420 420
 			shm_status();
421 421
 #endif
422 422
 			break;
... ...
@@ -515,7 +515,8 @@ int main_loop()
515 515
 						timer_ticker();
516 516
 					}
517 517
 				}else{
518
-						pids[process_no]=pid; /*should be shared mem anway*/
518
+						pt[process_no].pid=pid; /*should be shared mem anway*/
519
+						strncpy(pt[process_no].desc, "timer", MAX_PT_DESC );
519 520
 				}
520 521
 		}
521 522
 
... ...
@@ -525,9 +526,12 @@ int main_loop()
525 526
 			goto error;
526 527
 		}
527 528
 		/* main process, receive loop */
528
-		pids[0]=getpid();
529
-		/* process_bit = 1; */
530 529
 		process_no=0; /*main process number*/
530
+		pt[process_no].pid=getpid();
531
+		snprintf(pt[process_no].desc, MAX_PT_DESC, 
532
+			"stand-alone receiver @ %s:%s", 
533
+			 bind_address->name.s, bind_address->port_no_str.s );
534
+		
531 535
 		
532 536
 		     /* We will call child_init even if we
533 537
 		      * do not fork
... ...
@@ -544,9 +548,7 @@ int main_loop()
544 548
 		return udp_rcv_loop();
545 549
 	}else{
546 550
 		/* process_no now initialized to zero -- increase from now on
547
-		   as new processes are forked (while skipping 0 reserved for main ;
548
-		   not that with multiple listeners, more children processes will
549
-		   share the same process_no and the pids array will be rewritten
551
+		   as new processes are forked (while skipping 0 reserved for main )
550 552
 		*/
551 553
 		for(r=0;r<sock_no;r++){
552 554
 			/* create the listening socket (for each address)*/
... ...
@@ -581,7 +583,10 @@ int main_loop()
581 583
 #endif
582 584
 					return udp_rcv_loop();
583 585
 				}else{
584
-						pids[process_no]=pid; /*should be in shared mem.*/
586
+						pt[process_no].pid=pid; /*should be in shared mem.*/
587
+						snprintf(pt[process_no].desc, MAX_PT_DESC,
588
+							"receiver child=%d sock=%d @ %s:%s", i, r, 	
589
+							sock_info[r].name.s, sock_info[r].port_no_str.s );
585 590
 				}
586 591
 			}
587 592
 			/*parent*/
... ...
@@ -615,18 +620,22 @@ int main_loop()
615 620
 				timer_ticker();
616 621
 			}
617 622
 		}else{
618
-			pids[process_no]=pid;
623
+			pt[process_no].pid=pid;
624
+			strncpy(pt[process_no].desc, "timer", MAX_PT_DESC );
619 625
 		}
620 626
 	}
621 627
 
622 628
 	/* main */
623
-	pids[0]=getpid();
629
+	pt[0].pid=getpid();
630
+	strncpy(pt[0].desc, "attendant", MAX_PT_DESC );
624 631
 	/*DEBUG- remove it*/
632
+#ifdef DEBUG
625 633
 	printf("\n% 3d processes, % 3d children * % 3d listening addresses + main + fifo %s\n",
626 634
 			process_no+1, children_no, sock_no, (timer_list)?"+ timer":"");
627 635
 	for (r=0; r<=process_no; r++){
628
-		printf("% 3d   % 5d\n", r, pids[r]);
636
+		printf("% 3d   % 5d\n", r, pt[r].pid);
629 637
 	}
638
+#endif
630 639
 	process_no=0; 
631 640
 	/* process_bit = 0; */
632 641
 	is_main=1;
... ...
@@ -670,7 +679,7 @@ static void sig_usr(int signo)
670 679
 			case SIGTERM:
671 680
 					/* print memory stats for non-main too */
672 681
 					#ifdef PKG_MALLOC
673
-					LOG(L_INFO, "Memory status (pkg):\n");
682
+					LOG(memlog, "Memory status (pkg):\n");
674 683
 					pkg_status();
675 684
 					#endif
676 685
 					exit(0);
... ...
@@ -1242,15 +1251,15 @@ int main(int argc, char** argv)
1242 1251
 	
1243 1252
 	/*alloc pids*/
1244 1253
 #ifdef SHM_MEM
1245
-	pids=shm_malloc(sizeof(int)*(children_no*sock_no+1/*main*/+1/*timer */+1/*fifo*/));
1254
+	pt=shm_malloc(sizeof(struct process_table)*process_count());
1246 1255
 #else
1247
-	pids=malloc(sizeof(int)*(children_no*sock_no+1+1+1));
1256
+	pt=malloc(sizeof(struct process_table)*process_count());
1248 1257
 #endif
1249
-	if (pids==0){
1258
+	if (pt==0){
1250 1259
 		fprintf(stderr, "ERROR: out  of memory\n");
1251 1260
 		goto error;
1252 1261
 	}
1253
-	memset(pids, 0, sizeof(int)*(children_no+1));
1262
+	memset(pt, 0, sizeof(struct process_table)*process_count());
1254 1263
 	
1255 1264
 	/* init_daemon? */
1256 1265
 	if (!dont_fork){
... ...
@@ -22,6 +22,7 @@
22 22
 #include "ip_addr.h"
23 23
 #include "resolve.h"
24 24
 #include "ut.h"
25
+#include "pt.h"
25 26
 
26 27
 
27 28
 #define append_str(_dest,_src,_len,_msg) \
... ...
@@ -121,7 +122,7 @@ char * warning_builder( struct sip_msg *msg, unsigned int *returned_len)
121 122
 		foo=&(msg->first_line.u.request.uri);
122 123
 	print_len=snprintf(buf+fix_len, MAX_WARNING_LEN-fix_len,
123 124
 		"pid=%d req_src_ip=%s in_uri=%.*s out_uri=%.*s via_cnt%c=%d\"",
124
-		pids?pids[process_no]:0,
125
+		my_pid(),
125 126
 		ip_addr2a(&msg->src_ip),
126 127
 		msg->first_line.u.request.uri.len, msg->first_line.u.request.uri.s,
127 128
 		foo->len, foo->s, 
128 129
new file mode 100644
... ...
@@ -0,0 +1,49 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * Process Table
5
+ *
6
+ *
7
+ */
8
+
9
+#ifndef _PT_H
10
+
11
+#include <sys/types.h>
12
+#include <unistd.h>
13
+
14
+#include "globals.h"
15
+#include "timer.h"
16
+
17
+#define MAX_PT_DESC	128
18
+
19
+struct process_table {
20
+	int pid;
21
+	char desc[MAX_PT_DESC];
22
+};
23
+
24
+extern struct process_table *pt;
25
+extern int process_no;
26
+
27
+/* get number of process started by main with
28
+   given configuration
29
+*/
30
+inline static int process_count()
31
+{
32
+    return 
33
+		/* receivers and attendant */
34
+		(dont_fork ? 1 : children_no*sock_no + 1)
35
+		/* timer process */
36
+		+ (timer_list ? 1 : 0 )
37
+		/* fifo server */
38
+		+((fifo==NULL || strlen(fifo)==0) ? 0 : 1 );
39
+}
40
+
41
+
42
+/* retun processes's pid */
43
+inline static int my_pid()
44
+{
45
+	return pt ? pt[process_no].pid : getpid();
46
+}
47
+
48
+
49
+#endif
... ...
@@ -1,4 +1,4 @@
1
-#!/bin/sh 
1
+#!/bin/sh
2 2
 #
3 3
 # $Id$
4 4
 #
... ...
@@ -96,8 +96,8 @@ usage:
96 96
  $CMD perm <user> <uri> ............... introduce a permanent UrLoc entry
97 97
            < server health >
98 98
  $CMD monitor ......................... show internal status
99
- $CMD stat ............................ show runnig processes 
100
-                                        + $CMD show
99
+ $CMD ps .............................. show runnig processes 
100
+ $CMD fifo ............................ send raw commands to FIFO
101 101
 
102 102
    commands labeled with (*) will prompt for a MySQL password
103 103
    if the variable PW is set, the password will not be prompted"
... ...
@@ -116,8 +116,13 @@ prompt_pw() {
116 116
 	fi
117 117
 }
118 118
 
119
-ul_dump()
119
+
120
+fifo_cmd()
120 121
 {
122
+	if [ "$#" -lt 1 -o "$#" -gt 3 ]; then
123
+		echo "ERROR: fifo_cmd takes 1..3 parameters and not $#"
124
+		exit
125
+	fi
121 126
 	name=ser_receiver_$$
122 127
 	path=/tmp/$name
123 128
 	if [ ! -w $SER_FIFO ]; then
... ...
@@ -130,57 +135,28 @@ ul_dump()
130 135
 		echo "error opening read fifo $path"
131 136
 		exit 1
132 137
 	fi
133
-	cat > $SER_FIFO <<EOF
134
-:ul_dump:$name
135 138
 
136
-EOF
137
-	cat < $path
138
-	rm $path
139
-}
139
+	if [ "$#" -eq 1 ] ; then
140
+		cat > $SER_FIFO <<EOF
141
+:$1:$name
140 142
 
141
-ul_show_contact() # params: <table> <username>
142
-{
143
-	name=ser_receiver_$$
144
-	path=/tmp/$name
145
-	if [ ! -w $SER_FIFO ]; then
146
-		echo "Error opening ser's FIFO $SER_FIFO"
147
-		exit 1
148
-	fi
149
-	mkfifo $path
150
-	if [ $? -ne 0 ] ; then
151
-		echo "error opening read fifo $path"
152
-		exit 1
153
-	fi
154
-	cat > $SER_FIFO <<EOF
155
-:ul_show_contact:$name
156
-$1
143
+EOF
144
+	elif [ "$#" -eq 2 ] ; then 
145
+		cat > $SER_FIFO <<EOF
146
+:$1:$name
157 147
 $2
158 148
 
159 149
 EOF
160
-	cat < $path
161
-	rm $path
162
-}
163
-
164
-ul_rm() # params: <table> <username>
165
-{
166
-	name=ser_receiver_$$
167
-	path=/tmp/$name
168
-	if [ ! -w $SER_FIFO ]; then
169
-		echo "Error opening ser's FIFO $SER_FIFO"
170
-		echo "Make sure you have line fifo=$SER_FIFO in your config"
171
-		exit 1
172
-	fi
173
-	mkfifo $path
174
-	if [ $? -ne 0 ] ; then
175
-		echo "error opening read fifo $path"
176
-		exit 1
177
-	fi
178
-	cat > $SER_FIFO <<EOF
179
-:ul_rm:$name
180
-$1
150
+	elif [ "$#" -eq 3 ] ; then 
151
+		cat > $SER_FIFO <<EOF
152
+:$1:$name
181 153
 $2
154
+$3
182 155
 
183 156
 EOF
157
+	fi
158
+
159
+
184 160
 	cat < $path
185 161
 	rm $path
186 162
 }
... ...
@@ -505,7 +481,7 @@ case $1 in
505 481
 			usage
506 482
 			exit 1
507 483
 		fi
508
-		ul_rm $1 $2
484
+		fifo_cmd ul_rm $1 $2
509 485
 		exit $?
510 486
 		;;
511 487
 
... ...
@@ -563,15 +539,15 @@ case $1 in
563 539
 		shift
564 540
 	
565 541
 		if [ $# -eq 1 ] ; then
566
-			ul_show_contact $USRLOC $1
542
+			fifo_cmd ul_show_contact $USRLOC $1
567 543
 		else
568
-			ul_dump
544
+			fifo_cmd ul_dump
569 545
 		fi
570 546
 		exit $?
571 547
 		;;
572 548
 
573 549
 	online)
574
-		ul_dump grep aor| awk '{print $3}' | sort | sort -mu
550
+		fifo_cmd ul_dump |grep aor| awk '{print $3}' | sort | sort -mu
575 551
 		exit $?
576 552
 		;;
577 553
 
... ...
@@ -630,14 +606,8 @@ case $1 in
630 606
 		$0 dul $1   > /dev/null 2>&1
631 607
         ;;
632 608
 			
633
-	stat)
634
-        if [ $# -ne 1 ] ; then
635
-            usage
636
-            exit 1
637
-        fi
638
-		ps -o "pid,user,args" $i -C $SER
639
-		$0 show
640
-
609
+	ps)
610
+		fifo_cmd ps
641 611
 		;;
642 612
 
643 613
 	acl)
... ...
@@ -649,6 +619,11 @@ case $1 in
649 619
 		shift
650 620
 		ser_alias "$@"
651 621
 		;;
622
+	
623
+	fifo)
624
+		shift
625
+		fifo_cmd "$@"
626
+		;;
652 627
 		
653 628
 	*)
654 629
 		usage
... ...
@@ -190,6 +190,8 @@ inline static int string2hex(
190 190
 	return orig_len-len;
191 191
 }
192 192
 
193
+/* portable sleep in microseconds (no interrupt handling now) */
194
+
193 195
 inline static void sleep_us( unsigned int nusecs )
194 196
 {
195 197
 	struct timeval tval;