2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local config = require "core.configmanager";
11 local encodings = require "util.encodings";
12 local stringprep = encodings.stringprep;
13 local usermanager = require "core.usermanager";
14 local signal = require "util.signal";
15 local lfs = require "lfs";
17 local nodeprep, nameprep = stringprep.nodeprep, stringprep.nameprep;
19 local io, os = io, os;
20 local tostring, tonumber = tostring, tonumber;
22 local CFG_SOURCEDIR = _G.CFG_SOURCEDIR;
26 function adduser(params)
27 local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
29 return false, "invalid-username";
31 return false, "invalid-hostname";
34 local ok = usermanager.create_user(user, password, host);
36 return false, "unable-to-save-data";
41 function user_exists(params)
42 return usermanager.user_exists(params.user, params.host);
45 function passwd(params)
46 if not _M.user_exists(params) then
47 return false, "no-such-user";
50 return _M.adduser(params);
53 function deluser(params)
54 if not _M.user_exists(params) then
55 return false, "no-such-user";
57 params.password = nil;
59 return _M.adduser(params);
63 local pidfile = config.get("*", "core", "pidfile");
65 return false, "no-pidfile";
68 local file, err = io.open(pidfile, "r+");
70 return false, "pidfile-read-failed", err;
73 local locked, err = lfs.lock(file, "w");
76 return false, "pidfile-not-locked";
79 local pid = tonumber(file:read("*a"));
83 return false, "invalid-pid";
90 local ok, pid, err = _M.getpid();
92 if pid == "pidfile-read-failed" or pid == "pidfile-not-locked" then
93 -- Report as not running, since we can't open the pidfile
94 -- (it probably doesn't exist)
99 return true, signal.kill(pid, 0) == 0;
103 local ok, ret = _M.isrunning();
108 return false, "already-running";
110 if not CFG_SOURCEDIR then
111 os.execute("./prosody");
112 elseif CFG_SOURCEDIR:match("^/usr/local") then
113 os.execute("/usr/local/bin/prosody");
115 os.execute("prosody");
121 local ok, ret = _M.isrunning();
126 return false, "not-running";
129 local ok, pid = _M.getpid()
130 if not ok then return false, pid; end
132 signal.kill(pid, signal.SIGTERM);