added --pidfile

This commit is contained in:
Axel Kittenberger 2010-11-17 11:14:36 +00:00
parent dbbcf9d0bb
commit 8ba756ade0
2 changed files with 92 additions and 49 deletions

106
lsyncd.c
View File

@ -83,25 +83,30 @@ static const uint32_t standard_event_mask =
*/ */
static struct settings { static struct settings {
/** /**
* If not null lsyncd logs in this file. * If not NULL Lsyncd logs into this file.
*/ */
char * log_file; char * log_file;
/** /**
* If true lsyncd sends log messages to syslog * If true Lsyncd sends log messages to syslog
*/ */
bool log_syslog; bool log_syslog;
/** /**
* -1 logs everything, 0 normal mode, * -1 logs everything, 0 normal mode,
* LOG_ERROR errors only * LOG_ERROR logs errors only.
*/ */
int log_level; int log_level;
/** /**
* True if lsyncd shall not daemonize. * True if Lsyncd shall not daemonize.
*/ */
bool nodaemon; bool nodaemon;
/**
* If not NULL Lsyncd writes its pid into this file.
*/
char * pidfile;
} settings = { } settings = {
.log_file = NULL, .log_file = NULL,
@ -419,6 +424,48 @@ s_strdup(const char *src)
return s; return s;
} }
/*****************************************************************************
* Pipes management
****************************************************************************/
/**
* A child process gets text piped longer than on
* write() can manage.
*/
struct pipemsg {
/* pipe file descriptor */
int fd;
/* message to send */
char *text;
/* length of text */
int tlen;
/* position in message */
int pos;
};
/**
* All pipes currently active.
*/
static struct pipemsg *pipes = NULL;
/**
* amount of pipes allocated.
*/
size_t pipes_size = 0;
/**
* number of pipes used.
*/
size_t pipes_len = 0;
/*****************************************************************************
* helper routines.
****************************************************************************/
/** /**
* Sets the close-on-exit flag for an fd * Sets the close-on-exit flag for an fd
*/ */
@ -457,39 +504,19 @@ non_block_fd(int fd)
} }
} }
/** /**
* A child process gets text piped longer than on * Writes a pid file.
* write() can manage.
*/ */
struct pipemsg { void
/* pipe file descriptor */ write_pidfile(lua_State *L, const char *pidfile) {
int fd; FILE* f = fopen(pidfile, "w");
if (!f) {
/* message to send */ printlogf(L, "Error", "Cannot write pidfile; '%s'", pidfile);
char *text; exit(-1); // ERRNO
}
/* length of text */ fprintf(f, "%i\n", getpid());
int tlen; fclose(f);
}
/* position in message */
int pos;
};
/**
* All pipes currently active.
*/
static struct pipemsg *pipes = NULL;
/**
* amount of pipes allocated.
*/
size_t pipes_size = 0;
/**
* number of pipes used.
*/
size_t pipes_len = 0;
/***************************************************************************** /*****************************************************************************
@ -957,6 +984,9 @@ l_configure(lua_State *L)
* from this on log to configurated log end instead of * from this on log to configurated log end instead of
* stdout/stderr */ * stdout/stderr */
running = true; running = true;
if (settings.pidfile) {
write_pidfile(L, settings.pidfile);
}
if (!settings.nodaemon && !is_daemon) { if (!settings.nodaemon && !is_daemon) {
if (!settings.log_file) { if (!settings.log_file) {
settings.log_syslog = true; settings.log_syslog = true;
@ -975,6 +1005,12 @@ l_configure(lua_State *L)
free(settings.log_file); free(settings.log_file);
} }
settings.log_file = s_strdup(file); settings.log_file = s_strdup(file);
} else if (!strcmp(command, "pidfile")) {
const char * file = luaL_checkstring(L, 2);
if (settings.pidfile) {
free(settings.pidfile);
}
settings.pidfile = s_strdup(file);
} else { } else {
printlogf(L, "Error", printlogf(L, "Error",
"Internal error, unknown parameter in l_configure(%s)", "Internal error, unknown parameter in l_configure(%s)",

View File

@ -5,8 +5,8 @@
-- --
-- Authors: Axel Kittenberger <axkibe@gmail.com> -- Authors: Axel Kittenberger <axkibe@gmail.com>
-- --
-- This is the "runner" part of lsyncd. It containts all its high-level logic. -- This is the "runner" part of Lsyncd. It containts all its high-level logic.
-- It works closely together with the lsyncd core in lsyncd.c. This means it -- It works closely together with the Lsyncd core in lsyncd.c. This means it
-- cannot be runned directly from the standard lua interpreter. -- cannot be runned directly from the standard lua interpreter.
--============================================================================ --============================================================================
@ -1958,7 +1958,8 @@ OPTIONS:
-log [Category] Turns on logging for a debug category -log [Category] Turns on logging for a debug category
-logfile FILE Writes log to FILE (DEFAULT: uses syslog) -logfile FILE Writes log to FILE (DEFAULT: uses syslog)
-nodaemon Does not detach and logs to stdout/stderr -nodaemon Does not detach and logs to stdout/stderr
-runner FILE Loads lsyncds lua part from FILE -pidfile FILE Writes Lsyncds PID into FILE
-runner FILE Loads Lsyncds lua part from FILE
-version Prints versions and exits -version Prints versions and exits
LICENSE: LICENSE:
@ -1997,6 +1998,10 @@ function runner.configure(args)
{0, function() {0, function()
clSettings.nodaemon=true clSettings.nodaemon=true
end}, end},
pidfile =
{1, function(file)
clSettings.pidfile=file
end},
rsync = rsync =
{2, function(src, trg) {2, function(src, trg)
clSettings.syncs = clSettings.syncs or {} clSettings.syncs = clSettings.syncs or {}
@ -2013,7 +2018,7 @@ function runner.configure(args)
os.exit(0) os.exit(0)
end} end}
} }
-- filled with all args that were non --options -- nonopts is filled with all args that were no part dash options
local nonopts = {} local nonopts = {}
local i = 1 local i = 1
while i <= #args do while i <= #args do
@ -2081,7 +2086,7 @@ function runner.initialize()
-- From this point on, no globals may be created anymore -- From this point on, no globals may be created anymore
lockGlobals() lockGlobals()
-- Copies simple settings to "key=true" settings. -- copies simple settings with numeric keys to "key=true" settings.
for k, v in pairs(settings) do for k, v in pairs(settings) do
if settings[v] then if settings[v] then
log("Error", "Double setting '"..v.."'") log("Error", "Double setting '"..v.."'")
@ -2090,7 +2095,7 @@ function runner.initialize()
settings[v]=true settings[v]=true
end end
-- All command line settings overwrite settings -- all command line settings overwrite config file settings
for k, v in pairs(clSettings) do for k, v in pairs(clSettings) do
if k ~= "syncs" then if k ~= "syncs" then
settings[k]=v settings[k]=v
@ -2114,13 +2119,16 @@ function runner.initialize()
if settings.logfile then if settings.logfile then
lsyncd.configure("logfile", settings.logfile) lsyncd.configure("logfile", settings.logfile)
end end
if settings.pidfile then
lsyncd.configure("pidfile", settings.pidfile)
end
----- -----
-- transfers some defaults to settings -- transfers some defaults to settings
if settings.statusIntervall == nil then if settings.statusIntervall == nil then
settings.statusIntervall = default.statusIntervall settings.statusIntervall = default.statusIntervall
end end
-- makes sure the user gave lsyncd anything to do -- makes sure the user gave Lsyncd anything to do
if Syncs.size() == 0 then if Syncs.size() == 0 then
log("Error", "Nothing to watch!") log("Error", "Nothing to watch!")
log("Error", "Use sync(SOURCE, TARGET, BEHAVIOR) in your config file."); log("Error", "Use sync(SOURCE, TARGET, BEHAVIOR) in your config file.");
@ -2242,7 +2250,7 @@ function runner.term()
end end
--============================================================================ --============================================================================
-- lsyncd user interface -- Lsyncd user interface
--============================================================================ --============================================================================
----- -----
@ -2324,10 +2332,9 @@ end
--============================================================================ --============================================================================
-- lsyncd default settings -- Lsyncd default settings
--============================================================================ --============================================================================
----- -----
-- Exitcodes to retry on network failures of rsync. -- Exitcodes to retry on network failures of rsync.
-- --
@ -2359,7 +2366,7 @@ local rsync_ssh = {
} }
----- -----
-- lsyncd classic - sync with rsync -- Lsyncd classic - sync with rsync
-- --
local default_rsync = { local default_rsync = {
----- -----
@ -2415,7 +2422,7 @@ local default_rsync = {
----- -----
-- lsyncd classic - sync with rsync -- Lsyncd 2 improved rsync - sync with rsync but move over ssh.
-- --
local default_rsyncssh = { local default_rsyncssh = {
----- -----
@ -2652,7 +2659,7 @@ default = {
end, end,
----- -----
-- called on (re)initalizing of lsyncd. -- called on (re)initalizing of Lsyncd.
-- --
init = function(inlet) init = function(inlet)
local config = inlet.getConfig() local config = inlet.getConfig()
@ -2672,7 +2679,7 @@ default = {
end, end,
----- -----
-- The maximum number of processes lsyncd will spawn simultanously for -- The maximum number of processes Lsyncd will spawn simultanously for
-- one sync. -- one sync.
-- --
maxProcesses = 1, maxProcesses = 1,