Comparison

plugins/mod_posix.lua @ 2795:d6fcd13c07e7

mod_posix: Adjust file open mode depending on whether file exists (take that fopen designers!!!)
author Matthew Wild <mwild1@gmail.com>
date Wed, 13 Jan 2010 00:03:22 +0000
parent 2793:08892e3f24bd
child 2923:b7049746bd29
comparison
equal deleted inserted replaced
2794:5f14cd94a563 2795:d6fcd13c07e7
18 end 18 end
19 19
20 local logger_set = require "util.logger".setwriter; 20 local logger_set = require "util.logger".setwriter;
21 21
22 local lfs = require "lfs"; 22 local lfs = require "lfs";
23 local stat = lfs.attributes;
23 24
24 local prosody = _G.prosody; 25 local prosody = _G.prosody;
25 26
26 module.host = "*"; -- we're a global module 27 module.host = "*"; -- we're a global module
27 28
76 if pidfile_handle then 77 if pidfile_handle then
77 remove_pidfile(); 78 remove_pidfile();
78 end 79 end
79 pidfile = module:get_option("pidfile"); 80 pidfile = module:get_option("pidfile");
80 if pidfile then 81 if pidfile then
81 pidfile_handle, err = io.open(pidfile, "a+"); 82 local mode = stat(pidfile) and "r+" or "w+";
83 pidfile_handle, err = io.open(pidfile, mode);
82 if not pidfile_handle then 84 if not pidfile_handle then
83 module:log("error", "Couldn't write pidfile at %s; %s", pidfile, err); 85 module:log("error", "Couldn't write pidfile at %s; %s", pidfile, err);
84 prosody.shutdown("Couldn't write pidfile"); 86 prosody.shutdown("Couldn't write pidfile");
85 else 87 else
86 if not lfs.lock(pidfile_handle, "w") then -- Exclusive lock 88 if not lfs.lock(pidfile_handle, "w") then -- Exclusive lock