aboutsummaryrefslogtreecommitdiff
path: root/mail/dbmail-devel/files/patch-2.0.4_bug190_serverchild.c
blob: bf6da33dcfbdcee74f16d4164bcf88bdebaa58c4 (plain) (blame)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
Index: serverchild.c
===================================================================
--- serverchild.c	(revision 1600)
+++ serverchild.c	(revision 1758)
@@ -66,13 +66,13 @@
 void client_close(void)
 {
 	if (client.tx) {
-		trace(TRACE_DEBUG,"%s,%s: closing write stream", __FILE__,__FUNCTION__);
+		trace(TRACE_DEBUG,"%s,%s: closing write stream", __FILE__,__func__);
 		fflush(client.tx);
 		fclose(client.tx);	/* closes clientSocket as well */
 		client.tx = NULL;
 	}
 	if (client.rx) {
-		trace(TRACE_DEBUG,"%s,%s: closing read stream", __FILE__,__FUNCTION__);
+		trace(TRACE_DEBUG,"%s,%s: closing read stream", __FILE__,__func__);
 		shutdown(fileno(client.rx), SHUT_RDWR);
 		fclose(client.rx);
 		client.rx = NULL;
@@ -86,7 +86,7 @@
 	
 	trace(TRACE_DEBUG,
 		"%s,%s: database connection still open, closing",
-		__FILE__,__FUNCTION__);
+		__FILE__,__func__);
 	db_disconnect();
 	auth_disconnect();
 	connected = 0;		/* FIXME a signal between this line and the previous one 
@@ -99,18 +99,22 @@
 
 void noop_child_sig_handler(int sig, siginfo_t *info UNUSED, void *data UNUSED)
 {
-       trace(TRACE_DEBUG, "%s,%s: ignoring signal [%d]", __FILE__, __FUNCTION__, sig);
+	int saved_errno = errno;
+	trace(TRACE_DEBUG, "%s,%s: ignoring signal [%d]", __FILE__, __func__, sig);
+	errno = saved_errno;
 }
 
 void active_child_sig_handler(int sig, siginfo_t * info UNUSED, void *data UNUSED)
 {
+	int saved_errno = errno;
+	
 	static int triedDisconnect = 0;
 
 #ifdef _USE_STR_SIGNAL
-	trace(TRACE_ERROR, "%s,%s: got signal [%s]", __FILE__, __FUNCTION__,
+	trace(TRACE_ERROR, "%s,%s: got signal [%s]", __FILE__, __func__,
 	      strsignal(sig));
 #else
-	trace(TRACE_ERROR, "%s,%s: got signal [%d]", __FILE__, __FUNCTION__, sig);
+	trace(TRACE_ERROR, "%s,%s: got signal [%d]", __FILE__, __func__, sig);
 #endif
 
 	/* perform reinit at SIGHUP otherwise exit, but do nothing on
@@ -118,10 +122,10 @@
 	switch (sig) {
 	case SIGCHLD:
 		trace(TRACE_DEBUG, "%s,%s: SIGCHLD received... ignoring",
-			__FILE__, __FUNCTION__);
+			__FILE__, __func__);
 		break;
 	case SIGALRM:
-		trace(TRACE_DEBUG, "%s,%s: timeout received", __FILE__, __FUNCTION__);
+		trace(TRACE_DEBUG, "%s,%s: timeout received", __FILE__, __func__);
 		client_close();
 		break;
 
@@ -132,23 +136,23 @@
 		if (ChildStopRequested) {
 			trace(TRACE_DEBUG,
 				"%s,%s: already caught a stop request. Closing right now",
-				__FILE__,__FUNCTION__);
+				__FILE__,__func__);
 
 			/* already caught this signal, exit the hard way now */
 			client_close();
 			disconnect_all();
 			child_unregister();
-			trace(TRACE_DEBUG, "%s,%s: exit",__FILE__,__FUNCTION__);
+			trace(TRACE_DEBUG, "%s,%s: exit",__FILE__,__func__);
 			exit(1);
 		}
-		trace(TRACE_DEBUG, "%s,%s: setting stop request",__FILE__,__FUNCTION__);
+		trace(TRACE_DEBUG, "%s,%s: setting stop request",__FILE__,__func__);
 		DelChildSigHandler();
 	 	ChildStopRequested = 1;
 		break;
 	default:
 		/* bad shtuff, exit */
 		trace(TRACE_DEBUG,
-		      "%s,%s: cannot ignore this. Terminating",__FILE__,__FUNCTION__);
+		      "%s,%s: cannot ignore this. Terminating",__FILE__,__func__);
 
 		/*
 		 * For some reason i have not yet determined the process starts eating up
@@ -164,10 +168,12 @@
 			disconnect_all();
 		}
 
-		trace(TRACE_DEBUG, "%s,%s: exit", __FILE__, __FUNCTION__);
+		trace(TRACE_DEBUG, "%s,%s: exit", __FILE__, __func__);
 		child_unregister();
 		exit(1);
 	}
+
+	errno = saved_errno;
 }
 
 
@@ -239,16 +245,19 @@
 	if (! pid) {
 		if (child_register() == -1) {
 			trace(TRACE_FATAL, "%s,%s: child_register failed", 
-				__FILE__, __FUNCTION__);
+				__FILE__, __func__);
 			exit(0);
 
 		}
 	
  		ChildStopRequested = 0;
  		SetChildSigHandler();
- 		trace(TRACE_INFO, "%s,%s: signal handler placed, going to perform task now",
-			__FILE__, __FUNCTION__);
- 		PerformChildTask(info);
+ 		
+		trace(TRACE_INFO, "%s,%s: signal handler placed, going to perform task now",
+			__FILE__, __func__);
+		
+ 		if (PerformChildTask(info)== -1)
+			return -1;
  		child_unregister();
  		exit(0);
 	} else {
@@ -269,22 +278,22 @@
 	struct hostent *clientHost;
 
 	if (!info) {
-		trace(TRACE_ERROR,
-		      "PerformChildTask(): NULL info supplied");
+		trace(TRACE_ERROR, "%s,%s: NULL info supplied",
+				__FILE__, __func__);
 		return -1;
 	}
 
 	if (db_connect() != 0) {
-		trace(TRACE_ERROR,
-		      "PerformChildTask(): could not connect to database");
+		trace(TRACE_ERROR, "%s,%s: could not connect to database", 
+				__FILE__, __func__);
 		return -1;
 	}
 	if (db_check_version() != 0) 
 		return -1;
 
 	if (auth_connect() != 0) {
-		trace(TRACE_ERROR,
-		      "PerformChildTask(): could not connect to authentication");
+		trace(TRACE_ERROR, "%s,%s: could not connect to authentication",
+				__FILE__, __func__);
 		return -1;
 	}
 
@@ -294,12 +303,13 @@
 	for (i = 0; i < info->maxConnect && !ChildStopRequested; i++) {
 
 		if (db_check_connection()) {
-			trace(TRACE_ERROR, "%s,%s: database has gone away", __FILE__, __func__);
+			trace(TRACE_ERROR, "%s,%s: database has gone away", 
+					__FILE__, __func__);
 			return -1;
 		}
 	
-		trace(TRACE_INFO,
-		      "PerformChildTask(): waiting for connection");
+		trace(TRACE_INFO, "%s,%s: waiting for connection",
+				__FILE__, __func__);
 
 		child_reg_disconnected();
 
@@ -312,8 +322,8 @@
 	
 		if (clientSocket == -1) {
 			i--;	/* don't count this as a connect */
-			trace(TRACE_INFO,
-			      "PerformChildTask(): accept failed");
+			trace(TRACE_INFO, "%s,%s: accept failed",
+					__FILE__, __func__);
 			continue;	/* accept failed, refuse connection & continue */
 		}
 
@@ -337,30 +347,29 @@
 				strncpy(client.clientname,
 					clientHost->h_name, FIELDSIZE);
 
-			trace(TRACE_MESSAGE,
-			      "PerformChildTask(): incoming connection from [%s (%s)]",
+			trace(TRACE_MESSAGE, "%s,%s: incoming connection from [%s (%s)]",
+					__FILE__, __func__,
 			      client.ip,
 			      client.clientname[0] ? client.
 			      clientname : "Lookup failed");
 		} else {
-			trace(TRACE_MESSAGE,
-			      "PerformChildTask(): incoming connection from [%s]",
-			      client.ip);
+			trace(TRACE_MESSAGE, "%s,%s: incoming connection from [%s]",
+					__FILE__, __func__, client.ip);
 		}
 
 		/* make streams */
 		if (!(client.rx = fdopen(dup(clientSocket), "r"))) {
 			/* read-FILE opening failure */
-			trace(TRACE_ERROR,
-			      "PerformChildTask(): error opening read file stream");
+			trace(TRACE_ERROR, "%s,%s: error opening read file stream",
+					__FILE__, __func__);
 			close(clientSocket);
 			continue;
 		}
 
 		if (!(client.tx = fdopen(clientSocket, "w"))) {
 			/* write-FILE opening failure */
-			trace(TRACE_ERROR,
-			      "PerformChildTask(): error opening write file stream");
+			trace(TRACE_ERROR, "%s,%s: error opening write file stream",
+					__FILE__, __func__);
 			fclose(client.rx);
 			close(clientSocket);
 			memset(&client, 0, sizeof(client));
@@ -370,8 +379,8 @@
 		setvbuf(client.tx, (char *) NULL, _IOLBF, 0);
 		setvbuf(client.rx, (char *) NULL, _IOLBF, 0);
 
-		trace(TRACE_DEBUG,
-		      "PerformChildTask(): client info init complete, calling client handler");
+		trace(TRACE_DEBUG, "%s,%s: client info init complete, calling client handler",
+				__FILE__, __func__);
 
 		/* streams are ready, perform handling */
 		info->ClientHandler(&client);
@@ -379,17 +388,19 @@
 		set_proc_title("%s", "Idle");
 #endif
 
-		trace(TRACE_DEBUG,
-		      "PerformChildTask(): client handling complete, closing streams");
+		trace(TRACE_DEBUG, "%s,%s: client handling complete, closing streams",
+				__FILE__, __func__);
 		client_close();
-		trace(TRACE_INFO, "PerformChildTask(): connection closed");
+		trace(TRACE_INFO, "%s,%s: connection closed",
+				__FILE__, __func__);
 	}
 
 	if (!ChildStopRequested)
-		trace(TRACE_ERROR,
-		      "PerformChildTask(): maximum number of connections reached, stopping now");
+		trace(TRACE_ERROR, "%s,%s: maximum number of connections reached, stopping now",
+				__FILE__, __func__);
 	else
-		trace(TRACE_ERROR, "PerformChildTask(): stop requested");
+		trace(TRACE_ERROR, "%s,%s: stop requested",
+				__FILE__, __func__);
 
 	child_reg_disconnected();
 	disconnect_all();