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 470
 		fifo_server( fifo_stream ); /* never retruns */
471 471
 	}
472 472
 	/* dad process */
473
-	pids[process_no]=fifo_pid;
473
+	pt[process_no].pid=fifo_pid;
474
+	strncpy(pt[process_no].desc, "fifo server", MAX_PT_DESC );
474 475
 	/* make sure the read fifo will not close */
475 476
 	fifo_write=open(fifo, O_WRONLY, 0);
476 477
 	if (fifo_write<0) {
... ...
@@ -546,7 +548,7 @@ static int which_fifo_cmd(FILE *stream, char *response_file )
546 546
 
547 547
 	reply_pipe=open_reply_pipe(response_file);
548 548
 	if (reply_pipe==NULL) {
549
-		LOG(L_ERR, "ERROR: opening reply pipe (%s) failed\n",
549
+		LOG(L_ERR, "ERROR: which_fifo_cmd: opening reply pipe (%s) failed\n",
550 550
 			response_file );
551 551
 		return -1;
552 552
 	}
... ...
@@ -560,23 +562,51 @@ static int which_fifo_cmd(FILE *stream, char *response_file )
560 560
 	return 1;
561 561
 }
562 562
 
563
+static int ps_fifo_cmd(FILE *stream, char *response_file )
564
+{
565
+	FILE *reply_pipe;
566
+	int p;
567
+
568
+	if (response_file==0 || *response_file==0 ) {
569
+		 LOG(L_ERR, "ERROR: ps_fifo_cmd: null file\n");
570
+		return -1;
571
+	}
572
+	reply_pipe=open_reply_pipe(response_file);
573
+	if (reply_pipe==NULL) {
574
+		LOG(L_ERR, "ERROR: ps_fifo_cmd: opening reply pipe (%s) failed\n",
575
+			response_file );
576
+		return -1;
577
+	}
578
+
579
+	for (p=0; p<process_count();p++) 
580
+		fprintf( reply_pipe, "%d\t%d\t%s\n",
581
+			p, pt[p].pid, pt[p].desc );
582
+
583
+	fclose(reply_pipe);
584
+	return 1;
585
+}
586
+
563 587
 
564 588
 int register_core_fifo()
565 589
 {
566 590
 	if (register_fifo_cmd(print_fifo_cmd, FIFO_PRINT, 0)<0) {
567
-		LOG(L_CRIT, "unable to register 'print' FIFO cmd\n");
591
+		LOG(L_CRIT, "unable to register '%s' FIFO cmd\n", FIFO_PRINT);
568 592
 		return -1;
569 593
 	}
570 594
 	if (register_fifo_cmd(uptime_fifo_cmd, FIFO_UPTIME, 0)<0) {
571
-		LOG(L_CRIT, "unable to register 'print' FIFO cmd\n");
595
+		LOG(L_CRIT, "unable to register '%s' FIFO cmd\n", FIFO_UPTIME);
572 596
 		return -1;
573 597
 	}
574 598
 	if (register_fifo_cmd(print_version_cmd, FIFO_VERSION, 0)<0) {
575
-		LOG(L_CRIT, "unable to register 'version' FIFO cmd\n");
599
+		LOG(L_CRIT, "unable to register '%s' FIFO cmd\n", FIFO_VERSION);
576 600
 		return -1;
577 601
 	}
578 602
 	if (register_fifo_cmd(which_fifo_cmd, FIFO_WHICH, 0)<0) {
579
-		LOG(L_CRIT, "unable to register 'version' FIFO cmd\n");
603
+		LOG(L_CRIT, "unable to register '%s' FIFO cmd\n", FIFO_WHICH);
604
+		return -1;
605
+	}
606
+	if (register_fifo_cmd(ps_fifo_cmd, FIFO_PS, 0)<0) {
607
+		LOG(L_CRIT, "unable to register '%s' FIFO cmd\n", FIFO_PS);
580 608
 		return -1;
581 609
 	}
582 610
 	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 525
 			goto error;
526 526
 		}
527 527
 		/* main process, receive loop */
528
-		pids[0]=getpid();
529
-		/* process_bit = 1; */
530 528
 		process_no=0; /*main process number*/
529
+		pt[process_no].pid=getpid();
530
+		snprintf(pt[process_no].desc, MAX_PT_DESC, 
531
+			"stand-alone receiver @ %s:%s", 
532
+			 bind_address->name.s, bind_address->port_no_str.s );
533
+		
531 534
 		
532 535
 		     /* We will call child_init even if we
533 536
 		      * do not fork
... ...
@@ -544,9 +548,7 @@ int main_loop()
544 544
 		return udp_rcv_loop();
545 545
 	}else{
546 546
 		/* 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
547
+		   as new processes are forked (while skipping 0 reserved for main )
550 548
 		*/
551 549
 		for(r=0;r<sock_no;r++){
552 550
 			/* create the listening socket (for each address)*/
... ...
@@ -581,7 +583,10 @@ int main_loop()
581 581
 #endif
582 582
 					return udp_rcv_loop();
583 583
 				}else{
584
-						pids[process_no]=pid; /*should be in shared mem.*/
584
+						pt[process_no].pid=pid; /*should be in shared mem.*/
585
+						snprintf(pt[process_no].desc, MAX_PT_DESC,
586
+							"receiver child=%d sock=%d @ %s:%s", i, r, 	
587
+							sock_info[r].name.s, sock_info[r].port_no_str.s );
585 588
 				}
586 589
 			}
587 590
 			/*parent*/
... ...
@@ -615,18 +620,22 @@ int main_loop()
615 615
 				timer_ticker();
616 616
 			}
617 617
 		}else{
618
-			pids[process_no]=pid;
618
+			pt[process_no].pid=pid;
619
+			strncpy(pt[process_no].desc, "timer", MAX_PT_DESC );
619 620
 		}
620 621
 	}
621 622
 
622 623
 	/* main */
623
-	pids[0]=getpid();
624
+	pt[0].pid=getpid();
625
+	strncpy(pt[0].desc, "attendant", MAX_PT_DESC );
624 626
 	/*DEBUG- remove it*/
627
+#ifdef DEBUG
625 628
 	printf("\n% 3d processes, % 3d children * % 3d listening addresses + main + fifo %s\n",
626 629
 			process_no+1, children_no, sock_no, (timer_list)?"+ timer":"");
627 630
 	for (r=0; r<=process_no; r++){
628
-		printf("% 3d   % 5d\n", r, pids[r]);
631
+		printf("% 3d   % 5d\n", r, pt[r].pid);
629 632
 	}
633
+#endif
630 634
 	process_no=0; 
631 635
 	/* process_bit = 0; */
632 636
 	is_main=1;
... ...
@@ -670,7 +679,7 @@ static void sig_usr(int signo)
670 670
 			case SIGTERM:
671 671
 					/* print memory stats for non-main too */
672 672
 					#ifdef PKG_MALLOC
673
-					LOG(L_INFO, "Memory status (pkg):\n");
673
+					LOG(memlog, "Memory status (pkg):\n");
674 674
 					pkg_status();
675 675
 					#endif
676 676
 					exit(0);
... ...
@@ -1242,15 +1251,15 @@ int main(int argc, char** argv)
1242 1242
 	
1243 1243
 	/*alloc pids*/
1244 1244
 #ifdef SHM_MEM
1245
-	pids=shm_malloc(sizeof(int)*(children_no*sock_no+1/*main*/+1/*timer */+1/*fifo*/));
1245
+	pt=shm_malloc(sizeof(struct process_table)*process_count());
1246 1246
 #else
1247
-	pids=malloc(sizeof(int)*(children_no*sock_no+1+1+1));
1247
+	pt=malloc(sizeof(struct process_table)*process_count());
1248 1248
 #endif
1249
-	if (pids==0){
1249
+	if (pt==0){
1250 1250
 		fprintf(stderr, "ERROR: out  of memory\n");
1251 1251
 		goto error;
1252 1252
 	}
1253
-	memset(pids, 0, sizeof(int)*(children_no+1));
1253
+	memset(pt, 0, sizeof(struct process_table)*process_count());
1254 1254
 	
1255 1255
 	/* init_daemon? */
1256 1256
 	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 121
 		foo=&(msg->first_line.u.request.uri);
122 122
 	print_len=snprintf(buf+fix_len, MAX_WARNING_LEN-fix_len,
123 123
 		"pid=%d req_src_ip=%s in_uri=%.*s out_uri=%.*s via_cnt%c=%d\"",
124
-		pids?pids[process_no]:0,
124
+		my_pid(),
125 125
 		ip_addr2a(&msg->src_ip),
126 126
 		msg->first_line.u.request.uri.len, msg->first_line.u.request.uri.s,
127 127
 		foo->len, foo->s, 
128 128
new file mode 100644
... ...
@@ -0,0 +1,49 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Process Table
4
+ *
5
+ *
6
+ */
7
+
8
+#ifndef _PT_H
9
+
10
+#include <sys/types.h>
11
+#include <unistd.h>
12
+
13
+#include "globals.h"
14
+#include "timer.h"
15
+
16
+#define MAX_PT_DESC	128
17
+
18
+struct process_table {
19
+	int pid;
20
+	char desc[MAX_PT_DESC];
21
+};
22
+
23
+extern struct process_table *pt;
24
+extern int process_no;
25
+
26
+/* get number of process started by main with
27
+   given configuration
28
+*/
29
+inline static int process_count()
30
+{
31
+    return 
32
+		/* receivers and attendant */
33
+		(dont_fork ? 1 : children_no*sock_no + 1)
34
+		/* timer process */
35
+		+ (timer_list ? 1 : 0 )
36
+		/* fifo server */
37
+		+((fifo==NULL || strlen(fifo)==0) ? 0 : 1 );
38
+}
39
+
40
+
41
+/* retun processes's pid */
42
+inline static int my_pid()
43
+{
44
+	return pt ? pt[process_no].pid : getpid();
45
+}
46
+
47
+
48
+#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 130
 		echo "error opening read fifo $path"
131 131
 		exit 1
132 132
 	fi
133
-	cat > $SER_FIFO <<EOF
134
-:ul_dump:$name
135 133
 
136
-EOF
137
-	cat < $path
138
-	rm $path
139
-}
134
+	if [ "$#" -eq 1 ] ; then
135
+		cat > $SER_FIFO <<EOF
136
+:$1:$name
140 137
 
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
138
+EOF
139
+	elif [ "$#" -eq 2 ] ; then 
140
+		cat > $SER_FIFO <<EOF
141
+:$1:$name
157 142
 $2
158 143
 
159 144
 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
145
+	elif [ "$#" -eq 3 ] ; then 
146
+		cat > $SER_FIFO <<EOF
147
+:$1:$name
181 148
 $2
149
+$3
182 150
 
183 151
 EOF
152
+	fi
153
+
154
+
184 155
 	cat < $path
185 156
 	rm $path
186 157
 }
... ...
@@ -505,7 +481,7 @@ case $1 in
505 505
 			usage
506 506
 			exit 1
507 507
 		fi
508
-		ul_rm $1 $2
508
+		fifo_cmd ul_rm $1 $2
509 509
 		exit $?
510 510
 		;;
511 511
 
... ...
@@ -563,15 +539,15 @@ case $1 in
563 563
 		shift
564 564
 	
565 565
 		if [ $# -eq 1 ] ; then
566
-			ul_show_contact $USRLOC $1
566
+			fifo_cmd ul_show_contact $USRLOC $1
567 567
 		else
568
-			ul_dump
568
+			fifo_cmd ul_dump
569 569
 		fi
570 570
 		exit $?
571 571
 		;;
572 572
 
573 573
 	online)
574
-		ul_dump grep aor| awk '{print $3}' | sort | sort -mu
574
+		fifo_cmd ul_dump |grep aor| awk '{print $3}' | sort | sort -mu
575 575
 		exit $?
576 576
 		;;
577 577
 
... ...
@@ -630,14 +606,8 @@ case $1 in
630 630
 		$0 dul $1   > /dev/null 2>&1
631 631
         ;;
632 632
 			
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
-
633
+	ps)
634
+		fifo_cmd ps
641 635
 		;;
642 636
 
643 637
 	acl)
... ...
@@ -649,6 +619,11 @@ case $1 in
649 649
 		shift
650 650
 		ser_alias "$@"
651 651
 		;;
652
+	
653
+	fifo)
654
+		shift
655
+		fifo_cmd "$@"
656
+		;;
652 657
 		
653 658
 	*)
654 659
 		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;