[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Backport fixes for task #43: make worker threads start and ...



Update of /home/or/cvsroot/tor/src/or
In directory moria.mit.edu:/tmp/cvs-serv26706/src/or

Modified Files:
      Tag: tor-0_0_9-patches
	cpuworker.c dns.c 
Log Message:
Backport fixes for task #43: make worker threads start and run on win32.

Index: cpuworker.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/cpuworker.c,v
retrieving revision 1.61
retrieving revision 1.61.2.1
diff -u -d -r1.61 -r1.61.2.1
--- cpuworker.c	6 Dec 2004 23:19:55 -0000	1.61
+++ cpuworker.c	3 Jan 2005 19:27:31 -0000	1.61.2.1
@@ -210,11 +210,12 @@
   crypto_pk_env_t *onion_key = NULL, *last_onion_key = NULL;
 
   fd = fdarray[1]; /* this side is ours */
-#ifndef MS_WINDOWS
+#ifndef TOR_IS_MULTITHREADED
   tor_close_socket(fdarray[0]); /* this is the side of the socketpair the parent uses */
   connection_free_all(); /* so the child doesn't hold the parent's fd's open */
-#endif
   handle_signals(0); /* ignore interrupts from the keyboard, etc */
+#endif
+  tor_free(data);
 
   dup_onion_keys(&onion_key, &last_onion_key);
 
@@ -270,31 +271,38 @@
 /** Launch a new cpuworker.
  */
 static int spawn_cpuworker(void) {
-  int fd[2];
+  int *fdarray;
+  int fd;
   connection_t *conn;
 
-  if (tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
+  fdarray = tor_malloc(sizeof(int)*2);
+  if (tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray) < 0) {
     log(LOG_ERR, "Couldn't construct socketpair: %s",
         tor_socket_strerror(tor_socket_errno(-1)));
     tor_cleanup();
+    tor_free(fdarray);
     exit(1);
   }
 
-  spawn_func(cpuworker_main, (void*)fd);
+  fd = fdarray[0];
+  spawn_func(cpuworker_main, (void*)fdarray);
   log_fn(LOG_DEBUG,"just spawned a worker.");
-  tor_close_socket(fd[1]); /* we don't need the worker's side of the pipe */
+#ifndef TOR_IS_MULTITHREADED
+  tor_close_socket(fdarray[1]); /* we don't need the worker's side of the pipe */
+  tor_free(fdarray);
+#endif
 
   conn = connection_new(CONN_TYPE_CPUWORKER);
 
-  set_socket_nonblocking(fd[0]);
+  set_socket_nonblocking(fd);
 
   /* set up conn so it's got all the data we need to remember */
-  conn->s = fd[0];
+  conn->s = fd;
   conn->address = tor_strdup("localhost");
 
   if (connection_add(conn) < 0) { /* no space, forget it */
     log_fn(LOG_WARN,"connection_add failed. Giving up.");
-    connection_free(conn); /* this closes fd[0] */
+    connection_free(conn); /* this closes fd */
     return -1;
   }
 

Index: dns.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/dns.c,v
retrieving revision 1.126
retrieving revision 1.126.2.1
diff -u -d -r1.126 -r1.126.2.1
--- dns.c	13 Dec 2004 00:44:38 -0000	1.126
+++ dns.c	3 Jan 2005 19:27:31 -0000	1.126.2.1
@@ -618,6 +618,7 @@
 void dnsworkers_rotate(void)
 {
   connection_t *dnsconn;
+  log_fn(LOG_INFO, "Rotating DNS workers.");
   while ((dnsconn = connection_get_by_type_state(CONN_TYPE_DNSWORKER,
                                                  DNSWORKER_STATE_IDLE))) {
     connection_mark_for_close(dnsconn);
@@ -653,17 +654,21 @@
   int fd;
   int result;
 
+  /* log_fn(LOG_NOTICE,"After spawn: fdarray @%d has %d:%d", (int)fdarray, fdarray[0],fdarray[1]); */
+
   fd = fdarray[1]; /* this side is ours */
-#ifndef MS_WINDOWS
+#ifndef TOR_IS_MULTITHREADED
   tor_close_socket(fdarray[0]); /* this is the side of the socketpair the parent uses */
   connection_free_all(); /* so the child doesn't hold the parent's fd's open */
-#endif
   handle_signals(0); /* ignore interrupts from the keyboard, etc */
+#endif
+  tor_free(data);
 
   for (;;) {
 
     if (recv(fd, &address_len, 1, 0) != 1) {
       log_fn(LOG_INFO,"dnsworker exiting because tor process closed connection (either pruned idle dnsworker or died).");
+      log_fn(LOG_INFO,"Error on %d was %s", fd, tor_socket_strerror(tor_socket_errno(fd)));
       spawn_exit();
     }
 
@@ -704,31 +709,40 @@
 /** Launch a new DNS worker; return 0 on success, -1 on failure.
  */
 static int spawn_dnsworker(void) {
-  int fd[2];
+  int *fdarray;
+  int fd;
   connection_t *conn;
 
-  if (tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
+  fdarray = tor_malloc(sizeof(int)*2);
+  if (tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray) < 0) {
     log(LOG_ERR, "Couldn't construct socketpair: %s",
         tor_socket_strerror(tor_socket_errno(-1)));
     tor_cleanup();
+    tor_free(fdarray);
     exit(1);
   }
 
-  spawn_func(dnsworker_main, (void*)fd);
+  /* log_fn(LOG_NOTICE,"Before spawn: fdarray @%d has %d:%d", (int)fdarray, fdarray[0],fdarray[1]); */
+
+  fd = fdarray[0]; /* We copy this out here, since dnsworker_main may free fdarray */
+  spawn_func(dnsworker_main, (void*)fdarray);
   log_fn(LOG_DEBUG,"just spawned a worker.");
-  tor_close_socket(fd[1]); /* we don't need the worker's side of the pipe */
+#ifndef TOR_IS_MULTITHREADED
+  tor_close_socket(fdarray[1]); /* we don't need the worker's side of the pipe */
+  tor_free(fdarray);
+#endif
 
   conn = connection_new(CONN_TYPE_DNSWORKER);
 
-  set_socket_nonblocking(fd[0]);
+  set_socket_nonblocking(fd);
 
   /* set up conn so it's got all the data we need to remember */
-  conn->s = fd[0];
+  conn->s = fd;
   conn->address = tor_strdup("<unused>");
 
   if (connection_add(conn) < 0) { /* no space, forget it */
     log_fn(LOG_WARN,"connection_add failed. Giving up.");
-    connection_free(conn); /* this closes fd[0] */
+    connection_free(conn); /* this closes fd */
     return -1;
   }