Browse code

delay the call to daemonize() (bb#839)

git-svn: trunk@3642

Tomasz Kojm authored on 2008/02/16 02:37:53
Showing 4 changed files
... ...
@@ -1,3 +1,7 @@
1
+Fri Feb 15 18:24:22 CET 2008 (tk)
2
+---------------------------------
3
+  * clamd: delay the call to daemonize() (bb#839)
4
+
1 5
 Fri Feb 15 13:15:42 CET 2008 (acab)
2 6
 -----------------------------------
3 7
   * configure: provide a way to specify the version of sendmail
... ...
@@ -279,58 +279,43 @@ int main(int argc, char **argv)
279 279
     if(cfgopt(copt, "LeaveTemporaryFiles")->enabled)
280 280
 	cl_settempdir(NULL, 1);
281 281
 
282
-    /* fork into background */
283
-    if(!cfgopt(copt, "Foreground")->enabled) {
284
-	if(daemonize() == -1) {
285
-	    logg("!daemonize() failed\n");
286
-	    logg_close();
287
-	    freecfg(copt);
288
-	    return 1;
289
-	}
290
-	if(!debug_mode)
291
-	    if(chdir("/") == -1)
292
-		logg("^Can't change current working directory to root\n");
293
-
294
-    } else
295
-        foreground = 1;
296
-
297
-    logg("clamd daemon "VERSION" (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE")\n");
282
+    logg("#clamd daemon "VERSION" (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE")\n");
298 283
 
299 284
     if(user)
300
-	logg("Running as user %s (UID %u, GID %u)\n", user->pw_name, user->pw_uid, user->pw_gid);
285
+	logg("#Running as user %s (UID %u, GID %u)\n", user->pw_name, user->pw_uid, user->pw_gid);
301 286
 
302 287
     if(logg_size)
303
-	logg("Log file size limited to %d bytes.\n", logg_size);
288
+	logg("#Log file size limited to %d bytes.\n", logg_size);
304 289
     else
305
-	logg("Log file size limit disabled.\n");
290
+	logg("#Log file size limit disabled.\n");
306 291
 
307 292
     /* load the database(s) */
308 293
     dbdir = cfgopt(copt, "DatabaseDirectory")->strarg;
309
-    logg("Reading databases from %s\n", dbdir);
294
+    logg("#Reading databases from %s\n", dbdir);
310 295
 
311 296
     if(cfgopt(copt, "DetectPUA")->enabled)
312 297
 	dboptions |= CL_DB_PUA;
313 298
     else
314
-	logg("Not loading PUA signatures.\n");
299
+	logg("#Not loading PUA signatures.\n");
315 300
 
316 301
     if(cfgopt(copt, "PhishingSignatures")->enabled)
317 302
 	dboptions |= CL_DB_PHISHING;
318 303
     else
319
-	logg("Not loading phishing signatures.\n");
304
+	logg("#Not loading phishing signatures.\n");
320 305
 
321 306
     if(cfgopt(copt,"PhishingScanURLs")->enabled)
322 307
 	dboptions |= CL_DB_PHISHING_URLS;
323 308
     else
324
-	logg("Disabling URL based phishing detection.\n");
309
+	logg("#Disabling URL based phishing detection.\n");
325 310
 
326 311
     if(cfgopt(copt,"DevACOnly")->enabled) {
327
-	logg("Only using the A-C matcher.\n");
312
+	logg("#Only using the A-C matcher.\n");
328 313
 	dboptions |= CL_DB_ACONLY;
329 314
     }
330 315
 
331 316
     if((cpt = cfgopt(copt, "DevACDepth"))->enabled) {
332 317
 	cli_ac_setdepth(AC_DEFAULT_MIN_DEPTH, cpt->numarg);
333
-	logg("Max A-C depth set to %u\n", cpt->numarg);
318
+	logg("#Max A-C depth set to %u\n", cpt->numarg);
334 319
     }
335 320
 
336 321
     if((ret = cl_load(dbdir, &engine, &sigs, dboptions))) {
... ...
@@ -347,7 +332,7 @@ int main(int argc, char **argv)
347 347
 	return 1;
348 348
     }
349 349
 
350
-    logg("Loaded %d signatures.\n", sigs);
350
+    logg("#Loaded %u signatures.\n", sigs);
351 351
     if((ret = cl_build(engine)) != 0) {
352 352
 	logg("!Database initialization error: %s\n", cl_strerror(ret));;
353 353
 	logg_close();
... ...
@@ -387,6 +372,22 @@ int main(int argc, char **argv)
387 387
 	nlsockets++;
388 388
     }
389 389
 
390
+    /* fork into background */
391
+    if(!cfgopt(copt, "Foreground")->enabled) {
392
+	if(daemonize() == -1) {
393
+	    logg("!daemonize() failed\n");
394
+	    logg_close();
395
+	    freecfg(copt);
396
+	    return 1;
397
+	}
398
+	if(!debug_mode)
399
+	    if(chdir("/") == -1)
400
+		logg("^Can't change current working directory to root\n");
401
+
402
+    } else
403
+        foreground = 1;
404
+
405
+
390 406
     ret = acceptloop_th(lsockets, nlsockets, engine, dboptions, copt);
391 407
 
392 408
 #ifdef C_WINDOWS
... ...
@@ -67,52 +67,52 @@ int localserver(const struct cfgstruct *copt)
67 67
 
68 68
     if((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
69 69
 	estr = strerror(errno);
70
-	logg("!Socket allocation error: %s\n", estr);
70
+	logg("!LOCAL: Socket allocation error: %s\n", estr);
71 71
 	return -1;
72 72
     }
73 73
 
74 74
     if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) == -1) {
75 75
 	if(errno == EADDRINUSE) {
76 76
 	    if(connect(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) >= 0) {
77
-		logg("!Socket file %s is in use by another process.\n", server.sun_path);
77
+		logg("!LOCAL: Socket file %s is in use by another process.\n", server.sun_path);
78 78
 		close(sockfd);
79 79
 		return -1;
80 80
 	    }
81 81
 	    if(cfgopt(copt, "FixStaleSocket")->enabled) {
82
-		logg("^Socket file %s exists. Unclean shutdown? Removing...\n", server.sun_path);
82
+		logg("#LOCAL: Removing stale socket file %s\n", server.sun_path);
83 83
 		if(unlink(server.sun_path) == -1) {
84 84
 		    estr = strerror(errno);
85
-		    logg("!Socket file %s could not be removed: %s\n", server.sun_path, estr);
85
+		    logg("!LOCAL: Socket file %s could not be removed: %s\n", server.sun_path, estr);
86 86
 		    close(sockfd);
87 87
 		    return -1;
88 88
 		}
89 89
 		if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) == -1) {
90 90
 		    estr = strerror(errno);
91
-		    logg("!Socket file %s could not be bound: %s (unlink tried)\n", server.sun_path, estr);
91
+		    logg("!LOCAL: Socket file %s could not be bound: %s (unlink tried)\n", server.sun_path, estr);
92 92
 		    close(sockfd);
93 93
 		    return -1;
94 94
 		}
95 95
 	    } else if(stat(server.sun_path, &foo) != -1) {
96
-		logg("!Socket file %s exists. Either remove it, or configure a different one.\n", server.sun_path);
96
+		logg("!LOCAL: Socket file %s exists. Either remove it, or configure a different one.\n", server.sun_path);
97 97
 		close(sockfd);
98 98
 		return -1;
99 99
 	    }
100 100
 	} else {
101 101
 	    estr = strerror(errno);
102
-	    logg("!Socket file %s could not be bound: %s\n", server.sun_path, estr);
102
+	    logg("!LOCAL: Socket file %s could not be bound: %s\n", server.sun_path, estr);
103 103
 	    close(sockfd);
104 104
 	    return -1;
105 105
 	}
106 106
     }
107 107
 
108
-    logg("Unix socket file %s\n", server.sun_path);
108
+    logg("#LOCAL: Unix socket file %s\n", server.sun_path);
109 109
 
110 110
     backlog = cfgopt(copt, "MaxConnectionQueueLength")->numarg;
111
-    logg("Setting connection queue length to %d\n", backlog);
111
+    logg("#LOCAL: Setting connection queue length to %d\n", backlog);
112 112
 
113 113
     if(listen(sockfd, backlog) == -1) {
114 114
 	estr = strerror(errno);
115
-	logg("!listen() error: %s\n", estr);
115
+	logg("!LOCAL: listen() error: %s\n", estr);
116 116
 	close(sockfd);
117 117
 	return -1;
118 118
     }
... ...
@@ -66,7 +66,7 @@ int tcpserver(const struct cfgstruct *copt)
66 66
 
67 67
     if((taddr = cfgopt(copt, "TCPAddr"))->enabled) {
68 68
 	if(r_gethostbyname(taddr->strarg, &he, buf, sizeof(buf)) == -1) {
69
-	    logg("!r_gethostbyname(%s) error: %s\n", taddr->strarg, strerror(errno));
69
+	    logg("!TCP: r_gethostbyname(%s) error: %s\n", taddr->strarg, strerror(errno));
70 70
 	    return -1;
71 71
 	}
72 72
 	server.sin_addr = *(struct in_addr *) he.h_addr_list[0];
... ...
@@ -76,32 +76,32 @@ int tcpserver(const struct cfgstruct *copt)
76 76
 
77 77
     if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
78 78
 	estr = strerror(errno);
79
-	logg("!socket() error: %s\n", estr);
79
+	logg("!TCP: socket() error: %s\n", estr);
80 80
 	return -1;
81 81
     }
82 82
 
83 83
     if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *) &true, sizeof(true)) == -1) {
84
-	logg("!setsocktopt(SO_REUSEADDR) error: %s\n", strerror(errno));
84
+	logg("!TCP: setsocktopt(SO_REUSEADDR) error: %s\n", strerror(errno));
85 85
     }
86 86
 
87 87
     if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) == -1) {
88 88
 	estr = strerror(errno);
89
-	logg("!bind() error: %s\n", estr);
89
+	logg("!TCP: bind() error: %s\n", estr);
90 90
 	close(sockfd);
91 91
 	return -1;
92 92
     } else {
93 93
 	if(taddr->enabled)
94
-	    logg("Bound to address %s on tcp port %d\n", taddr->strarg, cfgopt(copt, "TCPSocket")->numarg);
94
+	    logg("#TCP: Bound to address %s on port %u\n", taddr->strarg, cfgopt(copt, "TCPSocket")->numarg);
95 95
 	else
96
-	    logg("Bound to tcp port %d\n", cfgopt(copt, "TCPSocket")->numarg);
96
+	    logg("#TCP: Bound to port %u\n", cfgopt(copt, "TCPSocket")->numarg);
97 97
     }
98 98
 
99 99
     backlog = cfgopt(copt, "MaxConnectionQueueLength")->numarg;
100
-    logg("Setting connection queue length to %d\n", backlog);
100
+    logg("#TCP: Setting connection queue length to %d\n", backlog);
101 101
 
102 102
     if(listen(sockfd, backlog) == -1) {
103 103
 	estr = strerror(errno);
104
-	logg("!listen() error: %s\n", estr);
104
+	logg("!TCP: listen() error: %s\n", estr);
105 105
 	close(sockfd);
106 106
 	return -1;
107 107
     }