00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <sys/types.h>
00034 #include <unistd.h>
00035 #include <stdio.h>
00036 #include "mem/shm_mem.h"
00037 #include "socket_info.h"
00038 #include "sr_module.h"
00039 #include "dprint.h"
00040 #include "timer.h"
00041 #include "pt.h"
00042
00043
00044
00045
00046 struct process_table *pt=0;
00047
00048
00049 unsigned int counted_processes = 0;
00050
00051
00052
00053
00054
00055 int init_multi_proc_support(void)
00056 {
00057 unsigned short proc_no;
00058 struct socket_info* si;
00059 #ifdef USE_TCP
00060 unsigned int i;
00061 #endif
00062
00063 proc_no = 0;
00064
00065
00066 if (dont_fork) {
00067
00068 proc_no = 1;
00069 } else {
00070 #ifdef USE_SCTP
00071
00072 for (si=sctp_listen; si; si=si->next)
00073 proc_no+=children_no;
00074 #endif
00075
00076 for (si=udp_listen; si; si=si->next)
00077 proc_no+=children_no;
00078 #ifdef USE_TCP
00079 proc_no += ((!tcp_disable)?( 1 + tcp_children_no ):0);
00080 #endif
00081
00082 proc_no++;
00083 }
00084
00085 proc_no += count_timer_procs();
00086
00087
00088 proc_no += count_module_procs();
00089
00090
00091 pt = shm_malloc(sizeof(struct process_table)*proc_no);
00092 if (pt==0){
00093 LM_ERR("out of memory\n");
00094 return -1;
00095 }
00096 memset(pt, 0, sizeof(struct process_table)*proc_no);
00097
00098 #ifdef USE_TCP
00099 for( i=0 ; i<proc_no ; i++ ) {
00100 pt[i].unix_sock = -1;
00101 pt[i].idx = -1;
00102 }
00103 #endif
00104
00105
00106 set_proc_attrs("starter");
00107
00108 counted_processes = proc_no;
00109
00110 return 0;
00111 }
00112
00113
00114
00115
00116
00117 void set_proc_attrs( char *fmt, ...)
00118 {
00119 va_list ap;
00120
00121
00122 va_start(ap, fmt);
00123 vsnprintf( pt[process_no].desc, MAX_PT_DESC, fmt, ap);
00124 va_end(ap);
00125
00126
00127 pt[process_no].pid=getpid();
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137 pid_t internal_fork(char *proc_desc)
00138 {
00139 #define CHILD_COUNTER_STOP 656565656
00140 static int process_counter = 1;
00141 pid_t pid;
00142 unsigned int seed;
00143 #ifdef USE_TCP
00144 int sockfd[2];
00145 #endif
00146
00147 if (process_counter==CHILD_COUNTER_STOP) {
00148 LM_CRIT("buggy call from non-main process!!!");
00149 return -1;
00150 }
00151
00152 seed = rand();
00153
00154 LM_DBG("forking new process \"%s\"\n",proc_desc);
00155
00156 #ifdef USE_TCP
00157 if(!tcp_disable){
00158 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd)<0){
00159 LM_ERR("socketpair failed: %s\n", strerror(errno));
00160 return -1;
00161 }
00162 }
00163 #endif
00164
00165 if ( (pid=fork())<0 ){
00166 LM_CRIT("cannot fork \"%s\" process\n",proc_desc);
00167 return -1;
00168 }
00169
00170 if (pid==0){
00171
00172 is_main = 0;
00173
00174 process_no = process_counter;
00175 pt[process_no].pid = getpid();
00176 process_counter = CHILD_COUNTER_STOP;
00177
00178 seed_child(seed);
00179
00180 set_proc_attrs(proc_desc);
00181
00182 #ifdef USE_TCP
00183 if (!tcp_disable){
00184 close(sockfd[0]);
00185 unix_tcp_sock=sockfd[1];
00186 pt[process_no].unix_sock=sockfd[0];
00187 }
00188 #endif
00189 return 0;
00190 }else{
00191
00192 pt[process_counter].pid = pid;
00193 #ifdef USE_TCP
00194 if (!tcp_disable) {
00195 close(sockfd[1]);
00196
00197
00198 pt[process_counter].unix_sock=sockfd[0];
00199 }
00200 #endif
00201 process_counter++;
00202 return pid;
00203 }
00204 }