mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Use C code to redirect standard I/O streams for --detach and --daemonize
This commit is contained in:
parent
177998f6a3
commit
df49debcf8
@ -880,10 +880,10 @@ def detach_gui():
|
|||||||
if os.fork() != 0:
|
if os.fork() != 0:
|
||||||
raise SystemExit(0)
|
raise SystemExit(0)
|
||||||
os.setsid()
|
os.setsid()
|
||||||
si, so, se = os.open(os.devnull, os.O_RDONLY), os.open(os.devnull, os.O_WRONLY), os.open(os.devnull, os.O_WRONLY)
|
try:
|
||||||
os.dup2(si, sys.__stdin__.fileno())
|
plugins['speedup'][0].detach(os.devnull)
|
||||||
os.dup2(so, sys.__stdout__.fileno())
|
except AttributeError:
|
||||||
os.dup2(se, sys.__stderr__.fileno())
|
pass # people running from source without updated binaries
|
||||||
|
|
||||||
class Application(QApplication):
|
class Application(QApplication):
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ from threading import Thread
|
|||||||
|
|
||||||
from calibre.library.server import server_config as config
|
from calibre.library.server import server_config as config
|
||||||
from calibre.library.server.base import LibraryServer
|
from calibre.library.server.base import LibraryServer
|
||||||
from calibre.constants import iswindows
|
from calibre.constants import iswindows, plugins
|
||||||
import cherrypy
|
import cherrypy
|
||||||
|
|
||||||
def start_threaded_server(db, opts):
|
def start_threaded_server(db, opts):
|
||||||
@ -67,7 +67,7 @@ The OPDS interface is advertised via BonJour automatically.
|
|||||||
' work in all environments.'))
|
' work in all environments.'))
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
|
def daemonize():
|
||||||
try:
|
try:
|
||||||
pid = os.fork()
|
pid = os.fork()
|
||||||
if pid > 0:
|
if pid > 0:
|
||||||
@ -93,12 +93,15 @@ def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Redirect standard file descriptors.
|
# Redirect standard file descriptors.
|
||||||
si = file(stdin, 'r')
|
try:
|
||||||
so = file(stdout, 'a+')
|
plugins['speedup'][0].detach(os.devnull)
|
||||||
se = file(stderr, 'a+', 0)
|
except AttributeError: # people running from source without updated binaries
|
||||||
os.dup2(si.fileno(), sys.stdin.fileno())
|
si = os.open(os.devnull, os.O_RDONLY)
|
||||||
os.dup2(so.fileno(), sys.stdout.fileno())
|
so = os.open(os.devnull, os.O_WRONLY)
|
||||||
os.dup2(se.fileno(), sys.stderr.fileno())
|
se = os.open(os.devnull, os.O_WRONLY)
|
||||||
|
os.dup2(si, sys.stdin.fileno())
|
||||||
|
os.dup2(so, sys.stdout.fileno())
|
||||||
|
os.dup2(se, sys.stderr.fileno())
|
||||||
|
|
||||||
|
|
||||||
def main(args=sys.argv):
|
def main(args=sys.argv):
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#define min(x, y) ((x < y) ? x : y)
|
#define min(x, y) ((x < y) ? x : y)
|
||||||
#define max(x, y) ((x > y) ? x : y)
|
#define max(x, y) ((x > y) ? x : y)
|
||||||
@ -97,6 +98,16 @@ speedup_pdf_float(PyObject *self, PyObject *args) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyObject*
|
||||||
|
speedup_detach(PyObject *self, PyObject *args) {
|
||||||
|
char *devnull = NULL;
|
||||||
|
if (!PyArg_ParseTuple(args, "s", &devnull)) return NULL;
|
||||||
|
if (freopen(devnull, "r", stdin) == NULL) return PyErr_SetFromErrno(PyExc_EnvironmentError);
|
||||||
|
if (freopen(devnull, "w", stdout) == NULL) return PyErr_SetFromErrno(PyExc_EnvironmentError);
|
||||||
|
if (freopen(devnull, "w", stderr) == NULL) return PyErr_SetFromErrno(PyExc_EnvironmentError);
|
||||||
|
Py_RETURN_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
static PyMethodDef speedup_methods[] = {
|
static PyMethodDef speedup_methods[] = {
|
||||||
{"parse_date", speedup_parse_date, METH_VARARGS,
|
{"parse_date", speedup_parse_date, METH_VARARGS,
|
||||||
"parse_date()\n\nParse ISO dates faster."
|
"parse_date()\n\nParse ISO dates faster."
|
||||||
@ -106,6 +117,10 @@ static PyMethodDef speedup_methods[] = {
|
|||||||
"pdf_float()\n\nConvert float to a string representation suitable for PDF"
|
"pdf_float()\n\nConvert float to a string representation suitable for PDF"
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{"detach", speedup_detach, METH_VARARGS,
|
||||||
|
"detach()\n\nRedirect the standard I/O stream to the specified file (usually os.devnull)"
|
||||||
|
},
|
||||||
|
|
||||||
{NULL, NULL, 0, NULL}
|
{NULL, NULL, 0, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user