Software / code / prosody
Comparison
plugins/mod_posix.lua @ 2445:9806fac994f8
mod_posix: Lock pidfile when in use, shut down if we can't write or lock the pidfile
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sun, 10 Jan 2010 23:49:38 +0000 |
| parent | 2440:11e3d16a128f |
| child | 2455:0b3184f3c9e4 |
comparison
equal
deleted
inserted
replaced
| 2444:267d6482bac6 | 2445:9806fac994f8 |
|---|---|
| 16 if type(signal) == "string" then | 16 if type(signal) == "string" then |
| 17 module:log("warn", "Couldn't load signal library, won't respond to SIGTERM"); | 17 module:log("warn", "Couldn't load signal library, won't respond to SIGTERM"); |
| 18 end | 18 end |
| 19 | 19 |
| 20 local logger_set = require "util.logger".setwriter; | 20 local logger_set = require "util.logger".setwriter; |
| 21 | |
| 22 local lfs = require "lfs"; | |
| 21 | 23 |
| 22 local prosody = _G.prosody; | 24 local prosody = _G.prosody; |
| 23 | 25 |
| 24 module.host = "*"; -- we're a global module | 26 module.host = "*"; -- we're a global module |
| 25 | 27 |
| 60 prosody.shutdown("Refusing to run as root"); | 62 prosody.shutdown("Refusing to run as root"); |
| 61 end | 63 end |
| 62 end | 64 end |
| 63 end); | 65 end); |
| 64 | 66 |
| 65 local pidfile_written; | 67 local pidfile; |
| 68 local pidfile_handle; | |
| 66 | 69 |
| 67 local function remove_pidfile() | 70 local function remove_pidfile() |
| 68 if pidfile_written then | 71 if pidfile_handle then |
| 69 os.remove(pidfile_written); | 72 pidfile_handle:close(); |
| 70 pidfile_written = nil; | 73 os.remove(pidfile); |
| 74 pidfile, pidfile_handle = nil, nil; | |
| 71 end | 75 end |
| 72 end | 76 end |
| 73 | 77 |
| 74 local function write_pidfile() | 78 local function write_pidfile() |
| 75 if pidfile_written then | 79 if pidfile_handle then |
| 76 remove_pidfile(); | 80 remove_pidfile(); |
| 77 end | 81 end |
| 78 local pidfile = module:get_option("pidfile"); | 82 pidfile = module:get_option("pidfile"); |
| 79 if pidfile then | 83 if pidfile then |
| 80 local pf, err = io.open(pidfile, "w+"); | 84 pidfile_handle, err = io.open(pidfile, "a+"); |
| 81 if not pf then | 85 if not pidfile_handle then |
| 82 module:log("error", "Couldn't write pidfile; %s", err); | 86 module:log("error", "Couldn't write pidfile at %s; %s", pidfile, err); |
| 87 prosody.shutdown("Couldn't write pidfile"); | |
| 83 else | 88 else |
| 84 pf:write(tostring(pposix.getpid())); | 89 if not lfs.lock(pidfile_handle, "w") then -- Exclusive lock |
| 85 pf:close(); | 90 local other_pid = pidfile_handle:read("*a"); |
| 86 pidfile_written = pidfile; | 91 module:log("error", "Another Prosody instance seems to be running with PID %s, quitting", other_pid); |
| 92 pidfile_handle = nil; | |
| 93 prosody.shutdown("Prosody already running"); | |
| 94 else | |
| 95 pidfile_handle:write(tostring(pposix.getpid())); | |
| 96 pidfile_handle:flush(); | |
| 97 end | |
| 87 end | 98 end |
| 88 end | 99 end |
| 89 end | 100 end |
| 90 | 101 |
| 91 local syslog_opened | 102 local syslog_opened |