Browse code

- updated mi modules to use the module interface in order to properly create the additional processes

git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@2622 689a6050-402a-0410-94f2-e92a70836424

Bogdan-Andrei Iancu authored on 21/08/2007 16:06:58
Showing 2 changed files
... ...
@@ -73,6 +73,8 @@
73 73
 static int mi_mod_init(void);
74 74
 static int mi_child_init(int rank);
75 75
 static int mi_destroy(void);
76
+static int pre_datagram_process();
77
+static int post_datagram_process();
76 78
 static void datagram_process(int rank);
77 79
 
78 80
 /* local variables */
... ...
@@ -100,7 +102,8 @@ MODULE_VERSION
100 102
 
101 103
 
102 104
 static proc_export_t mi_procs[] = {
103
-	{"MI Datagram", datagram_process, MI_CHILD_NO },
105
+	{"MI Datagram",  pre_datagram_process,  post_datagram_process,
106
+			datagram_process, MI_CHILD_NO },
104 107
 	{0,0,0}
105 108
 };
106 109
 
... ...
@@ -143,7 +146,6 @@ static int mi_mod_init(void)
143 146
 	struct hostent * host;
144 147
 	char *p, *host_s;
145 148
 	str port_str;
146
-	int res;
147 149
 
148 150
 	/* checking the mi_socket module param */
149 151
 	DBG("DBG: mi_datagram: mi_mod_init: testing socket existance ...\n");
... ...
@@ -261,19 +263,6 @@ static int mi_mod_init(void)
261 263
 			mi_socket, strlen(mi_socket));
262 264
 	}
263 265
 
264
-	/*create the sockets*/
265
-	res = mi_init_datagram_server(&mi_dtgram_addr, mi_socket_domain, &sockets,
266
-								mi_unix_socket_mode, mi_unix_socket_uid, 
267
-								mi_unix_socket_gid);
268
-
269
-	if ( res ) {
270
-		LOG(L_CRIT, "CRITICAL:mi_datagram:mi_init: The function "
271
-			"mi_init_datagram_server returned with error!!!\n");
272
-		return -1;
273
-	}
274
-	/* FIXME - the sockets will remain open in all processes and not only
275
-	 * in the mi_datagram ones .... */
276
-
277 266
 	return 0;
278 267
 }
279 268
 
... ...
@@ -292,24 +281,43 @@ static int mi_child_init(int rank)
292 281
 }
293 282
 
294 283
 
284
+static int pre_datagram_process()
285
+{
286
+	int res;
287
+
288
+	/*create the sockets*/
289
+	res = mi_init_datagram_server(&mi_dtgram_addr, mi_socket_domain, &sockets,
290
+								mi_unix_socket_mode, mi_unix_socket_uid, 
291
+								mi_unix_socket_gid);
292
+
293
+	if ( res ) {
294
+		LOG(L_CRIT, "CRITICAL:mi_datagram:mi_init: The function "
295
+			"mi_init_datagram_server returned with error!!!\n");
296
+		return -1;
297
+	}
298
+
299
+	return 0;
300
+}
301
+
302
+
295 303
 static void datagram_process(int rank)
296 304
 {
297 305
 	LOG(L_INFO,"INFO:mi_datagram:mi_child_init: a new child %d/%d\n",
298 306
 		rank, getpid());
299 307
 
300 308
 	/*child's initial settings*/
301
-	if( init_mi_child()!=0) {
309
+	if ( init_mi_child()!=0) {
302 310
 		LOG(L_CRIT,"CRITICAL:mi_datagram:mi_child_init: failed to init"
303 311
 			"the mi process\n");
304 312
 		exit(-1);
305 313
 	}
306
-	if(mi_init_datagram_buffer()!=0){
314
+	if (mi_init_datagram_buffer()!=0){
307 315
 		LOG(L_CRIT,"CRITICAL:mi_datagram:mi_child_init: failed to "
308 316
 			"allocate datagram buffer\n");
309 317
 		exit(-1);
310 318
 	}
311 319
 
312
-	if(mi_datagram_writer_init( DATAGRAM_SOCK_BUF_SIZE ,
320
+	if (mi_datagram_writer_init( DATAGRAM_SOCK_BUF_SIZE ,
313 321
 	mi_reply_indent )!= 0){
314 322
 		LOG(L_CRIT, "CRITICAL:mi_datagram:mi_child_init: failed to "
315 323
 			"initiate mi_datagram_writer\n");
... ...
@@ -322,6 +330,15 @@ static void datagram_process(int rank)
322 330
 }
323 331
 
324 332
 
333
+static int post_datagram_process()
334
+{
335
+	/* close the sockets */
336
+	close(sockets.rx_sock);
337
+	close(sockets.tx_sock);
338
+	return 0;
339
+}
340
+
341
+
325 342
 static int mi_destroy(void)
326 343
 {
327 344
 	int n;
... ...
@@ -35,6 +35,7 @@
35 35
 #include <sys/signal.h>
36 36
 #include <sys/wait.h>
37 37
 #include <grp.h>
38
+#include <stdlib.h>
38 39
 #include "mi_xmlrpc.h"
39 40
 #include "xr_writer.h"
40 41
 #include "xr_parser.h"
... ...
@@ -53,22 +54,28 @@ int rpl_opt = 0;
53 54
 
54 55
 /* module functions */
55 56
 static int mod_init();
56
-static int mod_child_init(int rank);
57 57
 static int destroy(void);
58
+static void xmlrpc_process(int rank);
58 59
 
59
-static pid_t * mi_xmlrpc_pid = 0;
60 60
 static int port = 8080;
61 61
 static char *log_file = NULL; 
62 62
 static int read_buf_size = MAX_READ;
63 63
 static TServer srv;
64 64
 MODULE_VERSION
65 65
 
66
+
67
+static proc_export_t mi_procs[] = {
68
+	{"MI XMLRPC",  0,  0, xmlrpc_process, 1 },
69
+	{0,0,0}
70
+};
71
+
72
+
66 73
 /* module parameters */
67 74
 static param_export_t mi_params[] = {
68
-	{"port",        				INT_PARAM, &port},
69
-	{"log_file",       				STR_PARAM, &log_file},
70
-	{"reply_option",        		INT_PARAM, &rpl_opt},
71
-	{"buffer_size", 				INT_PARAM, &read_buf_size},
75
+	{"port",					INT_PARAM, &port},
76
+	{"log_file",				STR_PARAM, &log_file},
77
+	{"reply_option",			INT_PARAM, &rpl_opt},
78
+	{"buffer_size",				INT_PARAM, &read_buf_size},
72 79
 	{0,0,0}
73 80
 };
74 81
 
... ...
@@ -81,13 +88,14 @@ struct module_exports exports = {
81 88
 	0,                                  /* exported statistics */
82 89
 	0,                                  /* exported MI functions */
83 90
 	0,                                  /* exported PV */
84
-	0,                                  /* extra processes */
91
+	mi_procs,                           /* extra processes */
85 92
 	mod_init,                           /* module initialization function */
86 93
 	(response_function) 0,              /* response handling function */
87 94
 	(destroy_function) destroy,         /* destroy function */
88
-	mod_child_init                      /* per-child init function */
95
+	0                                   /* per-child init function */
89 96
 };
90 97
 
98
+
91 99
 static int mod_init(void)
92 100
 {
93 101
 	DBG("DBG: mi_xmlrpc: mod_init: Testing port number...\n");
... ...
@@ -97,13 +105,6 @@ static int mod_init(void)
97 105
 		port = 8080;
98 106
 	}
99 107
 
100
-	mi_xmlrpc_pid = (pid_t*) shm_malloc ( sizeof(pid_t) );
101
-	if ( mi_xmlrpc_pid == 0 ) {
102
-		LOG(L_ERR, "ERROR mi_xmlrpc: mod_init: failed to init shm mem for "
103
-			"mi_xmlrpc_pid\n");
104
-		return -1;
105
-	}
106
-
107 108
 	if (init_async_lock()!=0) {
108 109
 		LOG(L_ERR, "ERROR mi_xmlrpc: mod_init: failed to init async lock\n");
109 110
 		return -1;
... ...
@@ -135,28 +136,14 @@ static void xmlrpc_sigchld( int sig )
135 136
 	}
136 137
 }
137 138
 
138
-static int mod_child_init( int rank )
139
-{
140
-	if ( rank != 1 )
141
-		return 0;
142
-	
143
-	*mi_xmlrpc_pid = fork();
144
-	
145
-	if ( *mi_xmlrpc_pid < 0 ){
146
-		LOG(L_ERR, "ERROR: mi_xmlrpc: mod_child_init: The process cannot "
147
-			"fork!\n");
148
-		return -1;
149
-	} else if ( *mi_xmlrpc_pid ) {
150
-		LOG(L_INFO, "INFO: mi_xmlrpc: mod_child_init: XMLRPC listener "
151
-			"process created (pid: %d)\n", *mi_xmlrpc_pid);
152
-		return 0;
153
-	}
154 139
 
140
+static void xmlrpc_process(int rank)
141
+{
155 142
 	/* install handler to catch termination of child processes */
156 143
 	if (signal(SIGCHLD, xmlrpc_sigchld)==SIG_ERR) {
157 144
 		LOG(L_ERR,"ERROR: mi_xmlrpc: mod_child_init: failed to install "
158 145
 			"signal handler for SIGCHLD\n");
159
-		return -1;
146
+		goto error;
160 147
 	}
161 148
 
162 149
 	/* Server Abyss init */
... ...
@@ -167,13 +154,13 @@ static int mod_child_init( int rank )
167 154
 	if (!ServerCreate(&srv, "XmlRpcServer", port, "", log_file)) {
168 155
 		LOG(L_ERR,"ERROR: mi_xmlrpc: mod_child_init: failed to create XMLRPC "
169 156
 			"server\n");
170
-		return -1;
157
+		goto error;
171 158
 	}
172 159
 
173 160
 	if (!ServerAddHandler(&srv, xmlrpc_server_abyss_rpc2_handler)) {
174 161
 		LOG(L_ERR,"ERROR: mi_xmlrpc: mod_child_init: failed to add handler "
175 162
 			"to server\n");
176
-		return -1;
163
+		goto error;
177 164
 	}
178 165
 
179 166
 	ServerDefaultHandler(&srv, xmlrpc_server_abyss_default_handler);
... ...
@@ -182,13 +169,13 @@ static int mod_child_init( int rank )
182 169
 	if( init_mi_child() != 0 ) {
183 170
 		LOG(L_CRIT, "CRITICAL: mi_xmlrpc: mod_child_init: Failed to init "
184 171
 			"the mi process\n");
185
-		return -1;
172
+		goto error;
186 173
 	}
187 174
 
188 175
 	if ( xr_writer_init(read_buf_size) != 0 ) {
189 176
 		LOG(L_ERR, "ERROR: mi_xmlrpc: mod_child_init: Failed to init the "
190 177
 			"reply writer\n");
191
-		return -1;
178
+		goto error;
192 179
 	}
193 180
 
194 181
 	xmlrpc_env_init(&env);
... ...
@@ -212,41 +199,22 @@ static int mod_child_init( int rank )
212 199
 	LOG(L_INFO, "INFO: mi_xmlrpc: mod_child_init: Starting xmlrpc server "
213 200
 		"on (%d)\n", getpid());
214 201
 
215
-	ServerRun(&srv); 
202
+	ServerRun(&srv);
216 203
 
217 204
 	LOG(L_CRIT, "CRITICAL: mi_xmlrpc: mod_child_init: Server terminated!!!\n");
218 205
 
219 206
 cleanup:
220 207
 	xmlrpc_env_clean(&env);
221 208
 	if ( xr_response ) xmlrpc_DECREF(xr_response);
222
-	return -1;
209
+error:
210
+	exit(-1);
223 211
 }
224 212
 
213
+
225 214
 int destroy(void)
226 215
 {
227 216
 	DBG("DBG: mi_xmlrpc: destroy: Destroying module ...\n");
228 217
 
229
-	if ( mi_xmlrpc_pid == 0 ) {
230
-		LOG(L_INFO, "INFO: mi_xmlrpc: destroy: Process hasn't been created "
231
-			"-> nothing to kill\n");
232
-	} else {
233
-		if ( *mi_xmlrpc_pid != 0 ) {
234
-			if ( kill(*mi_xmlrpc_pid, SIGKILL) != 0 ) {
235
-				if ( errno == ESRCH ) {
236
-					LOG(L_INFO, "INFO: mi_xmlrpc: destroy: seems that xmlrpc"
237
-						" process is already dead!\n");
238
-				} else {
239
-					LOG(L_ERR, "ERROR: mi_xmlrpc: destroy: killing the aux. "
240
-						"process failed! kill said: %s\n", strerror(errno));
241
-				}
242
-			} else {
243
-				LOG(L_INFO, "INFO: mi_xmlrpc: destroy: xmlrpc child "
244
-					"successfully killed!");
245
-			}
246
-		}
247
-		shm_free(mi_xmlrpc_pid);
248
-	}
249
-
250 218
 	destroy_async_lock();
251 219
 
252 220
 	return 0;