[sysadmin-bin: 39/168] Fix communication to subprocess



commit 5959cf9901eb06d7ecfd09135e45d2ac63b7a34a
Author: Owen W. Taylor <otaylor fishsoup net>
Date:   Thu Mar 5 08:31:14 2009 -0500

    Fix communication to subprocess
    
    gnome-post-receive-email: Redirecting stdin to (fork'ed but not
    exec'ed) subprocess doesn't work correctly because we are changing
    sys.stdin behind Python's back. Just read from the pipe explicitly.
    
    (Missed commit that gets the last revision to actually work.)

 gnome-post-receive-email |   51 ++++++++++++++++++++++++++++------------------
 1 files changed, 31 insertions(+), 20 deletions(-)
---
diff --git a/gnome-post-receive-email b/gnome-post-receive-email
index a96fc30..d3c2539 100755
--- a/gnome-post-receive-email
+++ b/gnome-post-receive-email
@@ -227,13 +227,13 @@ EMAIL_DELAY = 5
 EMAIL_BOUNDARY="---@@@--- gnome-post-receive-email ---@@@---\n"
 
 # Run in subprocess
-def do_send_emails():
+def do_send_emails(email_in):
     email_files = []
     current_file = None
     last_line = None
 
-    # Read emails from stdin and write each to a file
-    for line in sys.stdin:
+    # Read emails from the input pipe and write each to a file
+    for line in email_in:
         if current_file is None:
             current_file, filename = tempfile.mkstemp(suffix=".mail", prefix="gnome-post-receive-email-")
             email_files.append(filename)
@@ -283,25 +283,36 @@ def start_email():
         if pid == 0:
             # The child
 
-            # Redirect stdin from our pipe
             os.close(email_pipe[1])
+            email_in = os.fdopen(email_pipe[0])
+
+            # Redirect stdin/stdout/stderr to/from /dev/null
+            devnullin = os.open("/dev/null", os.O_RDONLY)
             os.close(0)
-            os.dup2(email_pipe[0], 0)
-            os.close(email_pipe[0])
+            os.dup2(devnullin, 0)
 
-            # Redirect stdout/stderr to /dev/null
-            devnull = os.open("/dev/null", os.O_WRONLY)
+            devnullout = os.open("/dev/null", os.O_WRONLY)
             os.close(1)
-            os.dup2(devnull, 1)
+            os.dup2(devnullout, 1)
             os.close(2)
-            os.dup2(devnull, 2)
-            os.close(devnull)
+            os.dup2(devnullout, 2)
+            os.close(devnullout)
 
             # Fork again to daemonize
             if os.fork() > 0:
                 sys.exit(0)
 
-            do_send_emails()
+            try:
+                do_send_emails(email_in)
+            except Exception:
+                import syslog
+                import traceback
+
+                syslog.openlog(os.path.basename(sys.argv[0]))
+                syslog.syslog(syslog.LOG_ERR, "Unexpected exception sending mail")
+                for line in traceback.format_exc().strip().split("\n"):
+                    syslog.syslog(syslog.LOG_ERR, line)
+
             sys.exit(0)
 
         email_file = os.fdopen(email_pipe[1], "w")
@@ -438,10 +449,10 @@ X-Git-Newrev: %(newrev)s
             extra = ""
         subject = "[" + projectshort + extra + "] " + self.get_subject()
 
-        mail_out = start_email()
+        email_out = start_email()
 
-        self.generate_header(mail_out, subject, include_revs=True, oldrev=self.oldrev, newrev=self.newrev)
-        self.generate_body(mail_out)
+        self.generate_header(email_out, subject, include_revs=True, oldrev=self.oldrev, newrev=self.newrev)
+        self.generate_body(email_out)
 
         end_email()
 
@@ -576,7 +587,7 @@ class BranchChange(RefChange):
             if not commit.id in self.detailed_commits:
                 continue
 
-            mail_out = start_email()
+            email_out = start_email()
 
             if self.short_refname == 'master':
                 branch = ""
@@ -603,15 +614,15 @@ class BranchChange(RefChange):
             # for the total branch update. Without a cover email, we are conceptually
             # breaking up the update into individual updates for each commit
             if self.needs_cover_email:
-                self.generate_header(mail_out, subject, include_revs=False)
+                self.generate_header(email_out, subject, include_revs=False)
             else:
                 parent = git.rev_parse(commit.id + "^")
-                self.generate_header(mail_out, subject,
+                self.generate_header(email_out, subject,
                                      include_revs=True,
                                      oldrev=parent, newrev=commit.id)
 
-            mail_out.flush()
-            git.show(commit.id, p=True, stat=True, _outfile=mail_out)
+            email_out.flush()
+            git.show(commit.id, p=True, stat=True, _outfile=email_out)
             end_email()
 
 class BranchCreation(BranchChange):



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