[or-cvs] r20083: {torvm} Enable static ARP in VM when passed in; clean up thread help (in torvm/trunk/build: kamikaze/patches win32/src/torvm-w32)

coderman at seul.org coderman at seul.org
Sat Jul 18 21:51:21 UTC 2009


Author: coderman
Date: 2009-07-18 17:51:21 -0400 (Sat, 18 Jul 2009)
New Revision: 20083

Modified:
   torvm/trunk/build/kamikaze/patches/001-kamikaze-tor-package.patch
   torvm/trunk/build/win32/src/torvm-w32/thr.c
   torvm/trunk/build/win32/src/torvm-w32/thr.h
   torvm/trunk/build/win32/src/torvm-w32/torvm.c
Log:
Enable static ARP in VM when passed in; clean up thread helper funcs.

Modified: torvm/trunk/build/kamikaze/patches/001-kamikaze-tor-package.patch
===================================================================
--- torvm/trunk/build/kamikaze/patches/001-kamikaze-tor-package.patch	2009-07-18 20:46:15 UTC (rev 20082)
+++ torvm/trunk/build/kamikaze/patches/001-kamikaze-tor-package.patch	2009-07-18 21:51:21 UTC (rev 20083)
@@ -491,8 +491,8 @@
 +TransListenAddress 0.0.0.0
 diff -Naur a/package/tor/files/torvminit b/package/tor/files/torvminit
 --- a/package/tor/files/torvminit	1970-01-01 00:00:00.000000000 +0000
-+++ b/package/tor/files/torvminit	2009-06-29 15:45:27.142518017 +0000
-@@ -0,0 +1,240 @@
++++ b/package/tor/files/torvminit	2009-07-18 09:37:22.267830591 +0000
+@@ -0,0 +1,256 @@
 +#!/bin/sh
 +# Copyright (C) 2008-2009  The Tor Project, Inc.
 +# See LICENSE file for rights and terms.
@@ -643,6 +643,22 @@
 +  echo 'tty1::respawn:/etc/init.d/tor status' >> /etc/inittab
 +fi
 +
++# set any static ARP entries before DHCP / interface up
++done=0
++cidx=1
++while [ $done -eq 0 ]; do
++  echo $ARGS | grep " ARPENT${cidx}" >/dev/null 2>&1
++  if [ $? -eq 0 ]; then
++    CENT=`echo $ARGS | sed "s/.* ARPENT${cidx}=//" | sed 's/ .*//' | sed 's/[^0-9a-fA-F:.-]//g'`
++    CMAC=`echo $CENT | sed 's/-.*//'`
++    CIP=`echo $CENT | sed 's/.*-//'`
++    vmr_setarp eth0 $CIP $CMAC
++  else
++    done=1
++  fi
++  cidx=`expr $cidx + 1`
++done
++
 +# if we're passed IP/routing info then do network pivot, otherwise just use dhcp
 +netup=0
 +echo $ARGS | grep ' IP=' >/dev/null 2>&1
@@ -735,8 +751,8 @@
 +fi
 diff -Naur a/package/tor/files/vmrouter.sh b/package/tor/files/vmrouter.sh
 --- a/package/tor/files/vmrouter.sh	1970-01-01 00:00:00.000000000 +0000
-+++ b/package/tor/files/vmrouter.sh	2009-07-06 11:38:36.499455187 +0000
-@@ -0,0 +1,138 @@
++++ b/package/tor/files/vmrouter.sh	2009-07-18 11:24:47.211822577 +0000
+@@ -0,0 +1,149 @@
 +#!/bin/bash
 +# Utility script for Tor VM routing
 +# Source or run directly.
@@ -805,10 +821,6 @@
 +  iptables -t filter -A OUTPUT -j LOG
 +}
 +
-+vmr_addmac() {
-+  iptables -t filter -m mac --mac-source "$1" -j RETURN
-+}
-+
 +vmr_fwdsetup() {
 +  # expects default route interface argument
 +  if [ -z "$1" ]; then
@@ -875,3 +887,18 @@
 +vmr_undirtcp() {
 +  iptables -t nat -D $cli_prenat_tbl -i "$1" -d "$2" -p tcp --dport "$3" -j REDIRECT --to "$4"
 +}
++
++vmr_setarp() {
++  # expects interface, ip, mac arguments
++  if [ -z "$1" ]; then
++    return $FAIL
++  fi
++  if [ -z "$2" ]; then
++    return $FAIL
++  fi
++  if [ -z "$3" ]; then
++    return $FAIL
++  fi
++  arp -i "$1" -s "$2" "$3"
++}
++

Modified: torvm/trunk/build/win32/src/torvm-w32/thr.c
===================================================================
--- torvm/trunk/build/win32/src/torvm-w32/thr.c	2009-07-18 20:46:15 UTC (rev 20082)
+++ torvm/trunk/build/win32/src/torvm-w32/thr.c	2009-07-18 21:51:21 UTC (rev 20083)
@@ -5,9 +5,10 @@
  * allocated by an external process with privs in the Tor VM process.
  * Inter-process threading and locking is explicitly not provided.
  */
-LPCRITICAL_SECTION  s_thridx_cs = NULL;
-DWORD s_thrcount = 0;
-struct s_thrinfo *  s_thrlist = NULL;
+static LPCRITICAL_SECTION  s_thridx_cs = NULL;
+static DWORD s_thrcount = 0;
+static DWORD s_currthrnum = 0;
+static struct s_thrinfo *  s_thrlist = NULL;
 
 BOOL  createcs (LPCRITICAL_SECTION cs)
 {
@@ -22,27 +23,24 @@
 
 BOOL  destroycs (LPCRITICAL_SECTION cs)
 {
-  if (!cs) {
+  if (!cs)
     return FALSE;
-  }
   DeleteCriticalSection(cs);
   return TRUE;
 }
 
 BOOL  entercs (LPCRITICAL_SECTION cs)
 {
-  if (!cs) {
+  if (!cs)
     return FALSE;
-  }
   EnterCriticalSection(cs);
   return TRUE;
 }
 
 BOOL  leavecs (LPCRITICAL_SECTION cs)
 {
-  if (!cs) {
+  if (!cs)
     return FALSE;
-  }
   LeaveCriticalSection(cs);
   return TRUE;
 }
@@ -113,26 +111,53 @@
 
 BOOL  createthr (PFnThreadMain  thrmain,
                  LPVOID         arg,
-                 LPDWORD        thrid,
                  BOOL           suspended)
 {
+  BOOL retval = FALSE;
   LPTHREAD_START_ROUTINE f = (LPTHREAD_START_ROUTINE) thrmain;
   DWORD tid;
-  DWORD cflags = 0;
+  DWORD cflags = CREATE_SUSPENDED;
   HANDLE newthr;
-  if (suspended) cflags |= CREATE_SUSPENDED;
+  struct s_thrinfo * thrinfo = NULL;
+
+  entercs(s_thridx_cs);
   newthr = CreateThread(NULL,
                         0,
                         f,
                         arg,
                         cflags,
                         &tid);
-  return TRUE;
+  if (!newthr)
+    goto finish;
+
+  thrinfo = malloc(sizeof(struct s_thrinfo));
+  thrinfo->next = s_thrlist;
+  thrinfo->hnd = newthr;
+  thrinfo->id = tid;
+  thrinfo->num = s_currthrnum++;
+  s_thrcount++;
+  s_thrlist = thrinfo;
+  retval = TRUE;
+
+ finish:
+  leavecs(s_thridx_cs);
+  if (retval && !suspended)
+    ResumeThread(newthr);
+
+  return(retval);
 }
 
 BOOL  destroythr (HANDLE thr)
 {
-  return TRUE;
+  BOOL retval = FALSE;
+  struct s_thrinfo * cinfo = NULL;
+  struct s_thrinfo * delthrinfo = NULL;
+
+  entercs(s_thridx_cs);
+    
+  leavecs(s_thridx_cs);
+
+  return(retval);
 }
 
 BOOL  pausethr (HANDLE thr)
@@ -180,7 +205,18 @@
 
 BOOL  setupthrctx (VOID)
 {
-  s_thridx_cs = 0;
+  s_thridx_cs = malloc(sizeof(CRITICAL_SECTION));
+  if (!s_thridx_cs)
+    return FALSE;
+  createcs(s_thridx_cs);
+  entercs(s_thridx_cs);
+  s_thrlist = malloc(sizeof(struct s_thrinfo));
+  s_thrlist->next = NULL;
+  s_thrlist->hnd = GetCurrentThread();
+  s_thrlist->id = GetCurrentThreadId();
+  s_thrlist->num = s_currthrnum++;
+  s_thrcount++;
+  leavecs(s_thridx_cs);
   return TRUE;
 }
 
@@ -189,12 +225,45 @@
   return;
 }
 
-BOOL  enumthrhnds (LPHANDLE *hndlist)
+int thrnum (VOID)
 {
+  int retval = -1;
+  struct s_thrinfo * thrinfo = NULL;
+  entercs(s_thridx_cs);
+  thrinfo = s_thrlist;
+  while (thrinfo && (retval < 0)) {
+    if (thrinfo->id == GetCurrentThreadId())
+      retval = thrinfo->num;
+    thrinfo = thrinfo->next;
+  }
+  leavecs(s_thridx_cs);
+  return (retval);
+}
+
+BOOL getthrnum (HANDLE thrhnd,
+                int*   num)
+{
+  *num = -1;
+  struct s_thrinfo * thrinfo = NULL;
+  entercs(s_thridx_cs);
+  thrinfo = s_thrlist;
+  while (thrinfo && (*num < 0)) {
+    if (thrinfo->hnd == thrhnd)
+      *num = thrinfo->num;
+    thrinfo = thrinfo->next;
+  }
+  leavecs(s_thridx_cs);
+  if (*num < 0)
+    return FALSE;
   return TRUE;
 }
 
-VOID  destroythrhnds (LPHANDLE hndlist)
+int numthreads (VOID)
 {
-  return;
+  int retval = 0;
+  entercs(s_thridx_cs);
+  retval = s_thrcount;
+  leavecs(s_thridx_cs);
+  return (retval);
 }
+

Modified: torvm/trunk/build/win32/src/torvm-w32/thr.h
===================================================================
--- torvm/trunk/build/win32/src/torvm-w32/thr.h	2009-07-18 20:46:15 UTC (rev 20082)
+++ torvm/trunk/build/win32/src/torvm-w32/thr.h	2009-07-18 21:51:21 UTC (rev 20083)
@@ -40,7 +40,6 @@
 typedef DWORD (__stdcall *PFnThreadMain)(LPVOID param);
 BOOL  createthr (PFnThreadMain  thrmain,
                  LPVOID         arg,
-                 LPDWORD        thrid,
                  BOOL           suspended);
 BOOL  destroythr (HANDLE thr);
 BOOL  pausethr (HANDLE thr);
@@ -79,18 +78,13 @@
 struct s_thrinfo {
   HANDLE  hnd;
   DWORD   id;
-  LONG    num;
+  int     num;
   struct s_thrinfo *next;
 };
 
-LONG  mythrnum (VOID);
-BOOL  getthrnum (HANDLE  thr,
-                 LONG *  num);
-LONG  numthreads (VOID);
+int thrnum (VOID);
+BOOL getthrnum (HANDLE thrhnd,
+                int*   num);
+int numthreads (VOID);
 
-/* Enumerate all known thread handles. Caller must destory hndlist after
- * successful invocation.  XXX: handle inheritance only part solved here.
- */
-BOOL  enumthrhnds (LPHANDLE *hndlist);
-VOID  destroythrhnds (LPHANDLE hndlist);
 #endif /* thr_h */

Modified: torvm/trunk/build/win32/src/torvm-w32/torvm.c
===================================================================
--- torvm/trunk/build/win32/src/torvm-w32/torvm.c	2009-07-18 20:46:15 UTC (rev 20082)
+++ torvm/trunk/build/win32/src/torvm-w32/torvm.c	2009-07-18 21:51:21 UTC (rev 20083)
@@ -84,15 +84,20 @@
   static char *     coff = NULL;
   const char *      newline = "\r\n";
   int               len;
+  int               thrno;
   DWORD             written;
   SYSTEMTIME        now;
   va_list           ap;
 
+  GetSystemTime (&now);
+  thrno = thrnum();
+
   /* XXX: This will block all other threads trying to log while waiting for
    * file I/O. To prevent badness when writes block a log writer thread
    * dedicated to disk I/O should be used and all other logging appends to
    * queue and unblocks.
    *   (For example, write to stalled SMB/USB/etc file system.)
+   * XXX: Fix potential race if we're moving log files around.
    */
   loginit();
   entercs(s_logcs);
@@ -107,12 +112,11 @@
     if (!msgbuf) 
       goto finished;
   }
-  GetSystemTime (&now);
   coff = msgbuf;
   coff[msgmax -1] = 0;
   len = snprintf (coff,
                   msgmax -1,
-                  "[%4.4d/%-2.2d/%-2.2d %-2.2d:%-2.2d:%-2.2d.%-3.3d UTC] %s: ",
+                  "[%4.4d/%-2.2d/%-2.2d %-2.2d:%-2.2d:%-2.2d.%-3.3d UTC][Thr %d] %s: ",
                   now.wYear,
                   now.wMonth,
                   now.wDay,
@@ -120,6 +124,7 @@
                   now.wMinute,
                   now.wSecond,
                   now.wMilliseconds,
+                  thrno,
                   msgtype);
   if (len > 0) {
     coff += len;
@@ -279,8 +284,7 @@
 
 BOOL copyvidaliacfg (LPTSTR srcpath,
                      LPTSTR destpath,
-                     LPTSTR datadir,
-                     LPTSTR polipocfg)
+                     LPTSTR datadir)
 {
   HANDLE src, dest;
   DWORD buffsz = CMDMAX;
@@ -315,6 +319,8 @@
   snprintf(buff, buffsz -1, "RunTorAtStart=true\r\n\r\n");
   WriteFile(dest, buff, strlen(buff), &written, NULL);
 
+  /* XXX: Let Tor VM launch Polipo directly now. */
+#if 0
   if (escquote(polipocfg, &epath)) {
     snprintf(buff, buffsz -1,
              "[General]\r\nProxyExecutableArguments=-c, %s\r\n",
@@ -324,6 +330,7 @@
   }
   while (ReadFile(src, buff, buffsz, &len, NULL) && (len > 0)) 
     WriteFile(dest, buff, len, &written, NULL);
+#endif
 
   snprintf(buff, buffsz -1, "[Tor]\r\nChanged=true\r\nTorExecutable=\r\n");
   WriteFile(dest, buff, strlen(buff), &written, NULL);
@@ -1523,7 +1530,7 @@
   else {
     if (brif->isdhcp == FALSE) {
       snprintf (*cmdline, cmdlen -1,
-                "%s %s%s %s IP=%s MASK=%s GW=%s MAC=%s MTU=%d PRIVIP=%s CTLSOCK=%s:9051 CTLREADY=9052 HASHPW=%s %s%s",
+                "%s %s%s %s IP=%s MASK=%s GW=%s MAC=%s MTU=%d PRIVIP=%s CTLSOCK=%s:9051 CTLREADY=9052 HASHPW=%s %s%s%s%s",
                 usedebug ? dbgcmds : basecmds,
                 myhostname ? "USEHOSTNAME=" : "",
                 myhostname ? myhostname : "",
@@ -1536,8 +1543,10 @@
                 TOR_TAP_VMIP,
                 TOR_TAP_VMIP,
                 ctlpass,
-                brif->gwmacaddr ? "ARPENT=" : "",
-                brif->gwmacaddr ? brif->gwmacaddr : "");
+                brif->gwmacaddr ? "ARPENT1=" : "",
+                brif->gwmacaddr ? brif->gwmacaddr : "",
+                brif->gwmacaddr ? "-" : "",
+                brif->gwmacaddr ? brif->gateway : "");
     }
     else {
       /* fallback if we can't get HOSTNAME, use DHCP client name. */
@@ -1545,7 +1554,7 @@
         myhostname = brif->dhcpname;
 
       snprintf (*cmdline, cmdlen -1,
-                "%s %s%s %s IP=%s MASK=%s GW=%s MAC=%s MTU=%d PRIVIP=%s ISDHCP DHCPSVR=%s DHCPNAME=%s CTLSOCK=%s:9051 CTLREADY=9052 HASHPW=%s %s%s %s%s",
+                "%s %s%s %s IP=%s MASK=%s GW=%s MAC=%s MTU=%d PRIVIP=%s ISDHCP DHCPSVR=%s DHCPNAME=%s CTLSOCK=%s:9051 CTLREADY=9052 HASHPW=%s %s%s%s%s %s%s%s%s",
                 usedebug ? dbgcmds : basecmds,
                 myhostname ? "USEHOSTNAME=" : "",
                 myhostname ? myhostname : "",
@@ -1560,10 +1569,14 @@
                 brif->dhcpname,
                 TOR_TAP_VMIP,
                 ctlpass,
-                brif->gwmacaddr ? "ARPENT=" : "",
+                brif->gwmacaddr ? "ARPENT1=" : "",
                 brif->gwmacaddr ? brif->gwmacaddr : "",
-                brif->svrmacaddr ? "ARPENT=" : "",
-                brif->svrmacaddr ? brif->svrmacaddr : "");
+                brif->gwmacaddr ? "-" : "",
+                brif->gwmacaddr ? brif->gateway : "",
+                brif->svrmacaddr ? "ARPENT2=" : "",
+                brif->svrmacaddr ? brif->svrmacaddr : "",
+                brif->svrmacaddr ? "-" : "",
+                brif->svrmacaddr ? brif->dhcpsvr : "");
     }
   }
   return TRUE;
@@ -1672,7 +1685,7 @@
    * flyspray 945
    */
   ldebug ("Copying default vidalia config from %s to %s", vcfgtmp, vcfgdest);
-  copyvidaliacfg(vcfgtmp, vcfgdest, dir, pcfgdest);
+  copyvidaliacfg(vcfgtmp, vcfgdest, dir);
 
   /* same for polipo and its backup file; see flyspray 946.
    */
@@ -2136,6 +2149,8 @@
   DWORD taptimeout = 60; /* the tap device can't be configured until the VM connects it */
   int c, optidx = 0;
 
+  setupthrctx();
+
   while (1) {
     c = getopt_long(argc, argv, "avubshrcXZ", torvm_options, &optidx);
     if (c == -1)



More information about the tor-commits mailing list