From e3bc3148c9ebff93586539f25b581d5f2f931d6d Mon Sep 17 00:00:00 2001
From: robk-tahoe <robk-tahoe@allmydata.com>
Date: Thu, 16 Oct 2008 09:36:37 -0700
Subject: [PATCH] fuse/blackmatch: fix linkage problems with daemonize

the daemonize() function imported from twisted was causing problems when
run from a frozen (py2app) build.  I simply copied the daemonize function
into this file, and that fixes the problem.

also removed a couple of lines of debugging spam that slipped through.
---
 contrib/fuse/impl_c/blackmatch.py | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/contrib/fuse/impl_c/blackmatch.py b/contrib/fuse/impl_c/blackmatch.py
index 3f655824..cce9a779 100644
--- a/contrib/fuse/impl_c/blackmatch.py
+++ b/contrib/fuse/impl_c/blackmatch.py
@@ -1347,6 +1347,25 @@ class TRPC(object):
         log('shutdown() closing keepalive %s' % (self.keepalive,))
         self.keepalive.close()
 
+# (cut-n-pasted here due to an ImportError / some py2app linkage issues)
+#from twisted.scripts._twistd_unix import daemonize
+def daemonize():
+    # See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16
+    if os.fork():   # launch child and...
+        os._exit(0) # kill off parent
+    os.setsid()
+    if os.fork():   # launch child and...
+        os._exit(0) # kill off parent again.
+    os.umask(077)
+    null=os.open('/dev/null', os.O_RDWR)
+    for i in range(3):
+        try:
+            os.dup2(null, i)
+        except OSError, e:
+            if e.errno != errno.EBADF:
+                raise
+    os.close(null)
+
 def main(argv):
     log("main(%s)" % (argv,))
 
@@ -1447,7 +1466,6 @@ def main(argv):
         log('\n'+(24*'_')+'init (server)'+(24*'_')+'\n')
 
         log('daemonizing')
-        from twisted.scripts._twistd_unix import daemonize
         daemonize()
 
         cache_timeout = float(config['cache-timeout'])
@@ -1481,12 +1499,10 @@ def main(argv):
         wait_at_most = 8
         while not os.path.exists(socket_path):
             log('waiting for appearance of %r' % (socket_path,))
-            print 'waiting...'
             time.sleep(1)
             if time.time() - waiting_since > wait_at_most:
                 log('%r did not appear within %ss' % (socket_path, wait_at_most))
                 raise IOError(2, 'no socket %s' % (socket_path,))
-            print 'running...'
         #print 'launched server'
         trpc = TRPC(socket_path)
 
-- 
2.45.2