forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpt.c
More file actions
195 lines (162 loc) · 4.13 KB
/
Copy pathpt.c
File metadata and controls
195 lines (162 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* $Id$
*
* Copyright (C) 2007 Voice Sistem SRL
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* 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
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History:
* --------
* 2007-06-07 - created to contain process handling functions (bogdan)
*/
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include "mem/shm_mem.h"
#include "socket_info.h"
#include "sr_module.h"
#include "dprint.h"
#include "timer.h"
#include "pt.h"
/* array with children pids, 0= main proc,
* alloc'ed in shared mem if possible */
struct process_table *pt=0;
/* variable keeping the number of created processes READONLY!! */
unsigned int counted_processes = 0;
int init_multi_proc_support(void)
{
unsigned short proc_no;
struct socket_info* si;
#ifdef USE_TCP
unsigned int i;
#endif
proc_no = 0;
/* count how many processes we will have in core */
if (dont_fork) {
/* only one UDP listener */
proc_no = 1;
} else {
#ifdef USE_SCTP
/* SCTP listeners */
for (si=sctp_listen; si; si=si->next)
proc_no+=children_no;
#endif
/* UDP listeners */
for (si=udp_listen; si; si=si->next)
proc_no+=children_no;
#ifdef USE_TCP
proc_no += ((!tcp_disable)?( 1/* tcp main */ + tcp_children_no ):0);
#endif
/* attendent */
proc_no++;
}
/* timer processes */
proc_no += count_timer_procs();
/* count the processes requested by modules */
proc_no += count_module_procs();
/* allocate the PID table */
pt = shm_malloc(sizeof(struct process_table)*proc_no);
if (pt==0){
LM_ERR("out of memory\n");
return -1;
}
memset(pt, 0, sizeof(struct process_table)*proc_no);
#ifdef USE_TCP
for( i=0 ; i<proc_no ; i++ ) {
pt[i].unix_sock = -1;
pt[i].idx = -1;
}
#endif
/* set the pid for the starter process */
set_proc_attrs("starter");
counted_processes = proc_no;
return 0;
}
void set_proc_attrs( char *fmt, ...)
{
va_list ap;
/* description */
va_start(ap, fmt);
vsnprintf( pt[process_no].desc, MAX_PT_DESC, fmt, ap);
va_end(ap);
/* pid */
pt[process_no].pid=getpid();
}
/* This function is to be called only by the main process!
* */
pid_t internal_fork(char *proc_desc)
{
#define CHILD_COUNTER_STOP 656565656
static int process_counter = 1;
pid_t pid;
unsigned int seed;
#ifdef USE_TCP
int sockfd[2];
#endif
if (process_counter==CHILD_COUNTER_STOP) {
LM_CRIT("buggy call from non-main process!!!");
return -1;
}
seed = rand();
LM_DBG("forking new process \"%s\"\n",proc_desc);
#ifdef USE_TCP
if(!tcp_disable){
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd)<0){
LM_ERR("socketpair failed: %s\n", strerror(errno));
return -1;
}
}
#endif
if ( (pid=fork())<0 ){
LM_CRIT("cannot fork \"%s\" process\n",proc_desc);
return -1;
}
if (pid==0){
/* child process */
is_main = 0; /* a child is not main process */
/* set uid and pid */
process_no = process_counter;
pt[process_no].pid = getpid();
process_counter = CHILD_COUNTER_STOP;
/* each children need a unique seed */
seed_child(seed);
/* set attributes */
set_proc_attrs(proc_desc);
/* set TCP communication */
#ifdef USE_TCP
if (!tcp_disable){
close(sockfd[0]);
unix_tcp_sock=sockfd[1];
pt[process_no].unix_sock=sockfd[0];
}
#endif
return 0;
}else{
/* parent process */
pt[process_counter].pid = pid;
#ifdef USE_TCP
if (!tcp_disable) {
close(sockfd[1]);
/* set the fd also in parent to be eliminate any
* races between the parent and child */
pt[process_counter].unix_sock=sockfd[0];
}
#endif
process_counter++;
return pid;
}
}