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.
This commit is contained in:
parent
3b6f60be7d
commit
31407abc25
|
@ -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') {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue