* [PATCH] properly daemonize vncviewer
@ 2006-08-01 19:11 Charles Coffing
2006-08-02 8:32 ` Keir Fraser
0 siblings, 1 reply; 3+ messages in thread
From: Charles Coffing @ 2006-08-01 19:11 UTC (permalink / raw)
To: xen-devel
[-- Attachment #1: Type: text/plain, Size: 842 bytes --]
Hi,
Currently, vncviewer is spawned from xm, but it doesn't properly get
daemonized. The attached patch makes vncviewer run completely separate
from xm.
There are various reasons it should be daemonized, but the particular
problem we hit was that YaST called "xm create" and waited on output on
stdout/stderr; xm then spawned vncviewer (which never closed its
inherited stdout and stderr); xm then would exit, but YaST still had
open file descriptors, and therefore waited forever. It would be
possible to work around in YaST, but it seemed cleaner to daemonize
vncviewer.
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.
Thanks.
Signed-off-by: Charles Coffing <ccoffing@novell.com>
[-- Attachment #2: xen-daemonize-vncviewer.diff --]
[-- Type: application/octet-stream, Size: 1663 bytes --]
Index: xen-unstable/tools/python/xen/xm/create.py
===================================================================
--- xen-unstable.orig/tools/python/xen/xm/create.py
+++ xen-unstable/tools/python/xen/xm/create.py
@@ -857,11 +857,49 @@ def choose_vnc_display():
vncpid = None
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)
+ 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("vncviewer", vncargs)
+ os._exit(1)
+ else:
+ w.write(str(pid2 or 0))
+ w.close()
+ os._exit(0)
+
+ global vncpid
+ os.close(w)
+ r = os.fdopen(r)
+ vncpid = int(r.read())
+ r.close()
+ os.waitpid(pid, 0)
+ if vncpid == 0:
+ return 0
return VNC_BASE_PORT + display
def preprocess_vnc(vals):
[-- Attachment #3: Type: text/plain, Size: 138 bytes --]
_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xensource.com
http://lists.xensource.com/xen-devel
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] properly daemonize vncviewer
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
0 siblings, 1 reply; 3+ messages in thread
From: Keir Fraser @ 2006-08-02 8:32 UTC (permalink / raw)
To: Charles Coffing; +Cc: xen-devel
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)?
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.
-- Keir
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] properly daemonize vncviewer
2006-08-02 8:32 ` Keir Fraser
@ 2006-08-02 18:19 ` Charles Coffing
0 siblings, 0 replies; 3+ messages in thread
From: Charles Coffing @ 2006-08-02 18:19 UTC (permalink / raw)
To: Keir Fraser; +Cc: xen-devel
[-- 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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2006-08-02 18:19 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 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.