All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Charles Coffing" <ccoffing@novell.com>
To: Keir Fraser <Keir.Fraser@cl.cam.ac.uk>
Cc: xen-devel@lists.xensource.com
Subject: Re: [PATCH] properly daemonize vncviewer
Date: Wed, 02 Aug 2006 12:19:03 -0600	[thread overview]
Message-ID: <44D09804.D169.003C.0@novell.com> (raw)
In-Reply-To: <0b0944f308ee948afbb6734232e2a7c2@cl.cam.ac.uk>

[-- Attachment #1: Type: text/plain, Size: 922 bytes --]

On Wed, Aug 2, 2006 at  2:32 AM, Keir Fraser <Keir.Fraser@cl.cam.ac.uk>
wrote: 
> On 1 Aug 2006, at 20:11, Charles Coffing wrote:
> 
>> We've been running with a variant of this patch (a variant because
we
>> use tightvnc, which requires different arguments) for many months,
and
>> it works well.
>>
>> Please consider applying to xen- unstable.
> 
> Is there no Python library function that can do this (I couldn't find

> one)?

No.  And as an added twist, I wanted to get the PID of the daemonized
program back (without the mess or risk of writing to a temporary file)
and I didn't see any such thing out there already.

> If not, can you at least put the new code in a function with a 
> spawn- like interface (i.e., takes two parameters --  program name
and 
> program argument list). It's okay to put the new function somewhere 
> nearby in create.py.

I've attached the updated patch with the suggested changes.



[-- Attachment #2: xen-daemonize-vncviewer.diff --]
[-- Type: application/octet-stream, Size: 2044 bytes --]

Index: xen-3.0-testing/tools/python/xen/xm/create.py
===================================================================
--- xen-3.0-testing.orig/tools/python/xen/xm/create.py
+++ xen-3.0-testing/tools/python/xen/xm/create.py
@@ -765,14 +765,58 @@ def choose_vnc_display():
 
 vncpid = None
 
+def daemonize(prog, args):
+    """Runs a program as a daemon with the list of arguments.  Returns the PID
+    of the daemonized program, or returns 0 on error.
+    """
+    r, w = os.pipe()
+    pid = os.fork()
+
+    if pid == 0:
+        os.close(r)
+        w = os.fdopen(w, 'w')
+        os.setsid()
+        try:
+            pid2 = os.fork()
+        except:
+            pid2 = None
+        if pid2 == 0:
+            os.chdir("/")
+            for fd in range(0, 256):
+                try:
+                    os.close(fd)
+                except:
+                    pass
+            os.open("/dev/null", os.O_RDWR)
+            os.dup2(0, 1)
+            os.dup2(0, 2)
+            os.execvp(prog, args)
+            os._exit(1)
+        else:
+            w.write(str(pid2 or 0))
+            w.close()
+            os._exit(0)
+
+    os.close(w)
+    r = os.fdopen(r)
+    daemon_pid = int(r.read())
+    r.close()
+    os.waitpid(pid, 0)
+    return daemon_pid
+
 def spawn_vnc(display):
+    """Spawns a vncviewer that listens on the specified display.  On success,
+    returns the port that the vncviewer is listening on and sets the global
+    vncpid.  On failure, returns 0.  Note that vncviewer is daemonized.
+    """
     vncargs = (["vncviewer", "-log", "*:stdout:0",
             "-listen", "%d" % (VNC_BASE_PORT + display) ])
-    global vncpid    
-    vncpid = os.spawnvp(os.P_NOWAIT, "vncviewer", vncargs)
-
+    global vncpid
+    vncpid = daemonize("vncviewer", vncargs)
+    if vncpid == 0:
+        return 0
     return VNC_BASE_PORT + display
-    
+
 def preprocess_vnc(vals):
     """If vnc was specified, spawn a vncviewer in listen mode
     and pass its address to the domain on the kernel command line.

[-- Attachment #3: Type: text/plain, Size: 138 bytes --]

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xensource.com
http://lists.xensource.com/xen-devel

      reply	other threads:[~2006-08-02 18:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-08-01 19:11 [PATCH] properly daemonize vncviewer Charles Coffing
2006-08-02  8:32 ` Keir Fraser
2006-08-02 18:19   ` Charles Coffing [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=44D09804.D169.003C.0@novell.com \
    --to=ccoffing@novell.com \
    --cc=Keir.Fraser@cl.cam.ac.uk \
    --cc=xen-devel@lists.xensource.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.