From 31407abc2597ef8e1b6935bea67986445f9636a6 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Sat, 17 Jul 2010 12:05:58 -0500 Subject: [PATCH] Issue #11: daemonize after opening listen port. The listen port should be opened before daemonizing otherwise if opening the port fails, the user will get no feedback. The only complication was that the listen socket needs to not be closed as part of daemonizing. Thanks to http://github.com/rickr for finding it. --- utils/websocket.c | 18 ++++++++++++------ utils/websocket.py | 14 ++++++++++---- utils/wsproxy.c | 2 ++ utils/wsproxy.py | 2 +- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/utils/websocket.c b/utils/websocket.c index 78736f2..ca6bba6 100644 --- a/utils/websocket.c +++ b/utils/websocket.c @@ -1,5 +1,7 @@ /* * WebSocket lib with support for "wss://" encryption. + * Copyright 2010 Joel Martin + * Licensed under LGPL version 3 (see docs/LICENSE.LGPL-3) * * You can make a cert/key with openssl using: * openssl req -new -x509 -days 365 -nodes -out self.pem -keyout self.pem @@ -460,7 +462,7 @@ void signal_handler(sig) { } } -void daemonize() { +void daemonize(int keepfd) { int pid, i; umask(0); @@ -483,7 +485,11 @@ void daemonize() { /* Close open files */ for (i=getdtablesize(); i>=0; --i) { - close(i); + if (i != keepfd) { + close(i); + } else { + printf("keeping fd %d\n", keepfd); + } } i=open("/dev/null", O_RDWR); // Redirect stdin dup(i); // Redirect stdout @@ -507,10 +513,6 @@ void start_server() { if (! (cbuf_tmp = malloc(bufsize)) ) { fatal("malloc()"); } - if (settings.daemon) { - daemonize(); - } - lsock = socket(AF_INET, SOCK_STREAM, 0); if (lsock < 0) { error("ERROR creating listener socket"); } bzero((char *) &serv_addr, sizeof(serv_addr)); @@ -532,6 +534,10 @@ void start_server() { } listen(lsock,100); + if (settings.daemon) { + daemonize(lsock); + } + while (1) { clilen = sizeof(cli_addr); if (settings.listen_host && settings.listen_host[0] != '\0') { diff --git a/utils/websocket.py b/utils/websocket.py index 7662480..5403146 100755 --- a/utils/websocket.py +++ b/utils/websocket.py @@ -2,6 +2,8 @@ ''' Python WebSocket library with support for "wss://" encryption. +Copyright 2010 Joel Martin +Licensed under LGPL version 3 (see docs/LICENSE.LGPL-3) You can make a cert/key with openssl using: openssl req -new -x509 -days 365 -nodes -out self.pem -keyout self.pem @@ -154,7 +156,7 @@ def do_handshake(sock): retsock.send(response) return retsock -def daemonize(): +def daemonize(keepfd=None): os.umask(0) os.chdir('/') os.setgid(os.getgid()) # relinquish elevations @@ -175,7 +177,10 @@ def daemonize(): if maxfd == resource.RLIM_INFINITY: maxfd = 256 for fd in reversed(range(maxfd)): try: - os.close(fd) + if fd != keepfd: + os.close(fd) + else: + print "Keeping fd: %d" % fd except OSError, exc: if exc.errno != errno.EBADF: raise @@ -187,12 +192,13 @@ def daemonize(): def start_server(): - if settings['daemon']: daemonize() - lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) lsock.bind((settings['listen_host'], settings['listen_port'])) lsock.listen(100) + + if settings['daemon']: daemonize(keepfd=lsock.fileno()) + while True: try: csock = startsock = None diff --git a/utils/wsproxy.c b/utils/wsproxy.c index 57e9501..a91e1a4 100644 --- a/utils/wsproxy.c +++ b/utils/wsproxy.c @@ -1,5 +1,7 @@ /* * A WebSocket to TCP socket proxy with support for "wss://" encryption. + * Copyright 2010 Joel Martin + * Licensed under LGPL version 3 (see docs/LICENSE.LGPL-3) * * You can make a cert/key with openssl using: * openssl req -new -x509 -days 365 -nodes -out self.pem -keyout self.pem diff --git a/utils/wsproxy.py b/utils/wsproxy.py index ba60735..389e175 100755 --- a/utils/wsproxy.py +++ b/utils/wsproxy.py @@ -3,7 +3,7 @@ ''' A WebSocket to TCP socket proxy with support for "wss://" encryption. Copyright 2010 Joel Martin -Licensed under LGPL version 3 (see LICENSE.LGPL-3) +Licensed under LGPL version 3 (see docs/LICENSE.LGPL-3) You can make a cert/key with openssl using: openssl req -new -x509 -days 365 -nodes -out self.pem -keyout self.pem