587
|
1
|
|
2 local pposix = assert(require "util.pposix");
|
|
3
|
|
4 local config_get = require "core.configmanager".get;
|
|
5 local logger_set = require "util.logger".setwriter;
|
|
6
|
|
7 module.host = "*"; -- we're a global module
|
|
8
|
|
9 if not config_get("*", "core", "no_daemonize") then
|
|
10 local function daemonize_server()
|
|
11 local logwriter;
|
|
12
|
|
13 local logfilename = config_get("*", "core", "log");
|
|
14 if logfilename then
|
|
15 local logfile = io.open(logfilename, "a+");
|
|
16 if logfile then
|
|
17 local write, format, flush = logfile.write, string.format, logfile.flush;
|
|
18 logwriter = function (name, level, message, ...)
|
|
19 if ... then
|
|
20 write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
|
|
21 else
|
|
22 write(logfile, name, "\t" , level, "\t", message, "\n");
|
|
23 end
|
|
24 flush(logfile);
|
|
25 end;
|
|
26 end
|
|
27 else
|
|
28 log("debug", "No logging specified, will continue with default");
|
|
29 end
|
|
30
|
|
31 local ok, ret = pposix.daemonize();
|
|
32 if not ok then
|
|
33 log("error", "Failed to daemonize: %s", ret);
|
|
34 elseif ret and ret > 0 then
|
|
35 log("info", "Daemonized to pid %d", ret);
|
|
36 os.exit(0);
|
|
37 else
|
|
38 log("info", "Successfully daemonized");
|
|
39
|
|
40 if logwriter then
|
|
41 local ok, ret = logger_set(logwriter);
|
|
42 if not ok then
|
|
43 log("error", "Couldn't set new log output: %s", ret);
|
|
44 end
|
|
45 end
|
|
46
|
|
47 end
|
|
48 end
|
|
49 module:add_event_hook("server-starting", daemonize_server);
|
|
50 end
|