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 {
/**
* If not null lsyncd logs in this file.
* If not NULL Lsyncd logs into this file.
*/
char * log_file;
/**
* If true lsyncd sends log messages to syslog
* If true Lsyncd sends log messages to syslog
*/
bool log_syslog;
/**
* -1 logs everything, 0 normal mode,
* LOG_ERROR errors only
* LOG_ERROR logs errors only.
*/
int log_level;
/**
* True if lsyncd shall not daemonize.
* True if Lsyncd shall not daemonize.
*/
bool nodaemon;
/**
* If not NULL Lsyncd writes its pid into this file.
*/
char * pidfile;
} settings = {
.log_file = NULL,
@ -419,6 +424,48 @@ s_strdup(const char *src)
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
*/
@ -457,39 +504,19 @@ non_block_fd(int fd)
}
}
/**
* A child process gets text piped longer than on
* write() can manage.
* Writes a pid file.
*/
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;
void
write_pidfile(lua_State *L, const char *pidfile) {
FILE* f = fopen(pidfile, "w");
if (!f) {
printlogf(L, "Error", "Cannot write pidfile; '%s'", pidfile);
exit(-1); // ERRNO
}
fprintf(f, "%i\n", getpid());
fclose(f);
}
/*****************************************************************************
@ -957,6 +984,9 @@ l_configure(lua_State *L)
* from this on log to configurated log end instead of
* stdout/stderr */
running = true;
if (settings.pidfile) {
write_pidfile(L, settings.pidfile);
}
if (!settings.nodaemon && !is_daemon) {
if (!settings.log_file) {
settings.log_syslog = true;
@ -975,6 +1005,12 @@ l_configure(lua_State *L)
free(settings.log_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 {
printlogf(L, "Error",
"Internal error, unknown parameter in l_configure(%s)",

View File

@ -5,8 +5,8 @@
--
-- Authors: Axel Kittenberger <axkibe@gmail.com>
--
-- 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
-- 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
-- cannot be runned directly from the standard lua interpreter.
--============================================================================
@ -1958,7 +1958,8 @@ OPTIONS:
-log [Category] Turns on logging for a debug category
-logfile FILE Writes log to FILE (DEFAULT: uses syslog)
-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
LICENSE:
@ -1997,6 +1998,10 @@ function runner.configure(args)
{0, function()
clSettings.nodaemon=true
end},
pidfile =
{1, function(file)
clSettings.pidfile=file
end},
rsync =
{2, function(src, trg)
clSettings.syncs = clSettings.syncs or {}
@ -2013,7 +2018,7 @@ function runner.configure(args)
os.exit(0)
end}
}
-- filled with all args that were non --options
-- nonopts is filled with all args that were no part dash options
local nonopts = {}
local i = 1
while i <= #args do
@ -2081,7 +2086,7 @@ function runner.initialize()
-- From this point on, no globals may be created anymore
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
if settings[v] then
log("Error", "Double setting '"..v.."'")
@ -2090,7 +2095,7 @@ function runner.initialize()
settings[v]=true
end
-- All command line settings overwrite settings
-- all command line settings overwrite config file settings
for k, v in pairs(clSettings) do
if k ~= "syncs" then
settings[k]=v
@ -2114,13 +2119,16 @@ function runner.initialize()
if settings.logfile then
lsyncd.configure("logfile", settings.logfile)
end
if settings.pidfile then
lsyncd.configure("pidfile", settings.pidfile)
end
-----
-- transfers some defaults to settings
if settings.statusIntervall == nil then
settings.statusIntervall = default.statusIntervall
end
-- makes sure the user gave lsyncd anything to do
-- makes sure the user gave Lsyncd anything to do
if Syncs.size() == 0 then
log("Error", "Nothing to watch!")
log("Error", "Use sync(SOURCE, TARGET, BEHAVIOR) in your config file.");
@ -2242,7 +2250,7 @@ function runner.term()
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.
--
@ -2359,7 +2366,7 @@ local rsync_ssh = {
}
-----
-- lsyncd classic - sync with rsync
-- Lsyncd classic - sync with 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 = {
-----
@ -2652,7 +2659,7 @@ default = {
end,
-----
-- called on (re)initalizing of lsyncd.
-- called on (re)initalizing of Lsyncd.
--
init = function(inlet)
local config = inlet.getConfig()
@ -2672,7 +2679,7 @@ default = {
end,
-----
-- The maximum number of processes lsyncd will spawn simultanously for
-- The maximum number of processes Lsyncd will spawn simultanously for
-- one sync.
--
maxProcesses = 1,