[or-cvs] use the spawn_func / spawn_exit abstraction for dnsworkers

Roger Dingledine arma at seul.org
Tue Aug 12 07:35:19 UTC 2003


Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/home2/arma/work/onion/cvs/src/or

Modified Files:
	dns.c 
Log Message:
use the spawn_func / spawn_exit abstraction for dnsworkers


Index: dns.c
===================================================================
RCS file: /home/or/cvsroot/src/or/dns.c,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- dns.c	12 Aug 2003 03:08:41 -0000	1.17
+++ dns.c	12 Aug 2003 07:35:17 -0000	1.18
@@ -21,7 +21,7 @@
 
 static int dns_assign_to_worker(connection_t *exitconn);
 static void dns_found_answer(char *question, uint32_t answer);
-static void dnsworker_main(int fd);
+int dnsworker_main(void *data);
 static int dns_spawn_worker(void);
 static void spawn_enough_workers(void);
 
@@ -313,22 +313,27 @@
   return 0;
 }
 
-static void dnsworker_main(int fd) {
+int dnsworker_main(void *data) {
   char question[MAX_ADDRESSLEN];
   unsigned char question_len;
   struct hostent *rent;
+  int fd;
+  int *fdarray = data;
+
+  close(fdarray[0]); /* this is the side of the socketpair the parent uses */
+  fd = fdarray[1]; /* this side is ours */
 
   for(;;) {
 
     if(read(fd, &question_len, 1) != 1) {
       log(LOG_INFO,"dnsworker_main(): read length failed. Exiting.");
-      exit(0);
+      spawn_exit();
     }
     assert(question_len > 0);
 
     if(read(fd, question, question_len) != question_len) {
       log(LOG_INFO,"dnsworker_main(): read hostname failed. Exiting.");
-      exit(0);
+      spawn_exit();
     }
     question[question_len] = 0; /* null terminate it */
 
@@ -338,21 +343,21 @@
       /* XXX it's conceivable write could return 1 through 3. but that's never gonna happen, right? */
       if(write(fd, "\0\0\0\0", 4) != 4) {
         log(LOG_INFO,"dnsworker_main(): writing nulls failed. Exiting.");
-        exit(0);
+        spawn_exit();
       }
     } else {
       assert(rent->h_length == 4); /* break to remind us if we move away from ipv4 */
       if(write(fd, rent->h_addr, 4) != 4) {
         log(LOG_INFO,"dnsworker_main(): writing answer failed. Exiting.");
-        exit(0);
+        spawn_exit();
       }
       log(LOG_INFO,"dnsworker_main(): Answered question '%s'.",question);
     }
   }
+  return 0; /* windows wants this function to return an int */
 }
 
 static int dns_spawn_worker(void) {
-  pid_t pid;
   int fd[2];
   connection_t *conn;
 
@@ -361,20 +366,9 @@
     exit(1);
   }
 
-  pid = fork();
-  if(pid < 0) {
-    perror("fork");
-    exit(1);
-  }
-  if(pid == 0) { /* i'm the child */
-    close(fd[0]);
-    dnsworker_main(fd[1]);
-    assert(0); /* never gets here */
-  }
-
-  /* i'm the parent */
+  spawn_func(dnsworker_main, (void*)fd);
   log(LOG_DEBUG,"dns_spawn_worker(): just spawned a worker.");
-  close(fd[1]);
+  close(fd[1]); /* we don't need the worker's side of the pipe */
 
   conn = connection_new(CONN_TYPE_DNSWORKER);
   if(!conn) {



More information about the tor-commits mailing list