[lwatch-cvs] files/src daemon.c, NONE, 1.1 daemon.h, NONE, 1.1 log.c, NONE, 1.1 log.h, NONE, 1.1 Ma
Brought to you by:
arturcz
|
From: Artur R. C. <ar...@us...> - 2010-07-29 17:03:53
|
Update of /cvsroot/lwatch/files/src In directory sfp-cvsdas-2.v30.ch3.sourceforge.com:/tmp/cvs-serv20964 Modified Files: Makefile.am lwatch.c settings.c settings.h Added Files: daemon.c daemon.h log.c log.h Log Message: Finally, two features are almost ready: - syslog support - run as daemon Index: settings.c =================================================================== RCS file: /cvsroot/lwatch/files/src/settings.c,v retrieving revision 1.38 retrieving revision 1.39 diff -C2 -d -r1.38 -r1.39 *** settings.c 11 Feb 2010 18:34:17 -0000 1.38 --- settings.c 29 Jul 2010 17:03:45 -0000 1.39 *************** *** 106,109 **** --- 106,111 ---- " -s, --show-unparsed", "\tshow unparsed lines (like `last message repeated X times')", + " -d, --daemon", + "\trun as daemon (move to background and detach from control terminal", " -O, --omit-rc", "\tdo not read values from config file", *************** *** 126,129 **** --- 128,132 ---- {"output",1,0,'o'}, {"show-unparsed",0,0,'s'}, + {"daemon",0,0,'d'}, {"omit-rc",0,0,'O'}, {"help",0,0,'h'}, *************** *** 172,175 **** --- 175,184 ---- #endif break; + case 'd': + lw_conf.daemon_mode=1; + #ifdef DEBUG + fprintf(stderr,"Run in daemon mode\n"); + #endif + break; case 'O': omit_config=1; --- NEW FILE: daemon.h --- /* daemon.h -- LogWatcher This file is part of the LogWatcher tool. Copyright (C) 2002-2009 Artur Robert Czechowski The LogWatcher is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. The LogWatcher is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with the LogWatcher; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Artur R. Czechowski <ar...@he...> http://hell.pl/arturcz/ */ /* * $Id: daemon.h,v 1.1 2010/07/29 17:03:45 arturcz Exp $ * $Source: /cvsroot/lwatch/files/src/daemon.h,v $ */ void daemonize(); Index: settings.h =================================================================== RCS file: /cvsroot/lwatch/files/src/settings.h,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** settings.h 11 Feb 2010 18:34:17 -0000 1.17 --- settings.h 29 Jul 2010 17:03:45 -0000 1.18 *************** *** 40,43 **** --- 40,44 ---- char *out_file; int show_unparsed; + int daemon_mode; int use_syslog; int log_level; --- NEW FILE: daemon.c --- /* daemon.c - some functions useful for daemonizing the process */ /* * This file is part of the LogWatcher tool. * Copyright (C) 2009,2010 Artur R. Czechowski <ar...@he...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <errno.h> #include <fcntl.h> #include <sys/file.h> #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include "config.h" #include "log.h" #define PID_LENGTH 7 const char *pid_file=PIDFILE; static int pidfd=-1; /* Algorithm: if file does not exist create and open it else open it; remember the file descriptor try to lock the file if lock is successfull we are OK if lock failed we assume there is another copy */ int is_another_copy(void) { int res; #ifdef O_CLOEXEC pidfd=open(pid_file, O_RDWR|O_CREAT|O_CLOEXEC|O_SYNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); /* mode 644 */ #else pidfd=open(pid_file, O_RDWR|O_CREAT|O_SYNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); /* mode 644 */ #endif if(pidfd==-1) { send_log(LOG_ERR,"Cannot open PID file %s. Error [%d] %s\n", pid_file,errno,strerror(errno)); exit(1); } #ifndef O_CLOEXEC res=fcntl(pidfd,F_SETFD,FD_CLOEXEC); if(res==-1) { send_log(LOG_ERR,"Cannot set FD_CLOEXEC flag on PID file %s. " "Error [%d] %s\n", pid_file,errno,strerror(errno)); } #endif send_log(LOG_DEBUG,"Opened PID file, fd=%d\n",pidfd); res=flock(pidfd, LOCK_EX|LOCK_NB); send_log(LOG_DEBUG,"Trying to lock the pidfile with result %d\n",res); if(res==-1) { send_log(LOG_DEBUG,"Extended result is [%d] %s\n", errno,strerror(errno)); if(errno==EWOULDBLOCK) { send_log(LOG_DEBUG,"EWOULDBLOCK - other copy is running"); return 1; } send_log(LOG_ERR,"Cannot lock PID file %s. Error [%d] %s\n", pid_file,errno,strerror(errno)); exit(1); } send_log(LOG_DEBUG,"We can run!\n"); /* At the moment we have exclusive access to the file, that means no other copy is running */ return 0; } void write_pidfile(void) { char pid_string[PID_LENGTH]; int res; if(pidfd==-1) { send_log(LOG_ERR,"PID file is unavailable for some reason. Please see syslog for hints. If still in doubt please send us a bugreport.\n"); exit(1); } if(snprintf(pid_string,PID_LENGTH,"%i\n",getpid())==-1) { send_log(LOG_ERR,"PID_LENGTH should be increased, please, send us a bugreport.\n"); exit(1); } send_log(LOG_DEBUG,"Write PID %s(%d) to file fd=%d\n",pid_string,strlen(pid_string),pidfd); res=write(pidfd,(void*)pid_string,strlen(pid_string)); if(res==-1) { send_log(LOG_ERR,"Cannot write to PID file [%d] %s\n", errno,strerror(errno)); exit(1); } } void release_pidfile() { close(pidfd); unlink(PIDFILE); /* FIXME-ac error handling */ } void daemonize() { send_log(LOG_ERR,"%s %d: daemonize not implemented yet", __FILE__, __LINE__); exit(1); } Index: lwatch.c =================================================================== RCS file: /cvsroot/lwatch/files/src/lwatch.c,v retrieving revision 1.29 retrieving revision 1.30 diff -C2 -d -r1.29 -r1.30 *** lwatch.c 11 Feb 2010 18:34:17 -0000 1.29 --- lwatch.c 29 Jul 2010 17:03:45 -0000 1.30 *************** *** 42,45 **** --- 42,46 ---- #include "config.h" #include "control.h" + #include "daemon.h" #include "defaults.h" #include "settings.h" *************** *** 96,99 **** --- 97,104 ---- input.fd=fd; input.events=POLLIN|POLLHUP; + + if(lw_conf.daemon_mode) + daemonize(); + while(loop) { input.revents=0; --- NEW FILE: log.c --- /* log.c - syslog support, output errors */ /* * This file is part of the LogWatcher tool. * Copyright (C) 2003,2010 Artur R. Czechowski <ar...@he...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <stdio.h> #include <stdarg.h> #include <stdlib.h> #include "log.h" #include "settings.h" int logger_subsystem_initialized=1; void start_log(void) { openlog("rrdcollect",LOG_CONS|LOG_PID,LOG_DAEMON); logger_subsystem_initialized=1; } void stop_log(void) { closelog(); logger_subsystem_initialized=0; } void send_log(int prio, char *fmt, ...) { va_list ap; va_start(ap,fmt); if(!logger_subsystem_initialized) { vfprintf(stderr,fmt,ap); fprintf(stderr,"send_log: Fatal error. Logger subsystem uninitialized. Please send a bugreport.\n"); exit(1); } if(prio<=lw_conf.log_level) vsyslog(prio,fmt,ap); va_end(ap); } Index: Makefile.am =================================================================== RCS file: /cvsroot/lwatch/files/src/Makefile.am,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** Makefile.am 16 Feb 2010 14:10:45 -0000 1.11 --- Makefile.am 29 Jul 2010 17:03:45 -0000 1.12 *************** *** 1,5 **** bin_PROGRAMS = lwatch ! xh = acolors.h control.h defaults.h settings.h strpcre.h yparse.h ! lwatch_SOURCES = acolors.c control.c lwatch.c settings.c strpcre.c yparse.l $(xh) dist_sysconf_DATA = lwatch.conf CLEANFILES = stamp-h.in *~ --- 1,5 ---- bin_PROGRAMS = lwatch ! xh = acolors.h control.h daemon.h defaults.h log.h settings.h strpcre.h yparse.h ! lwatch_SOURCES = acolors.c control.c daemon.c log.c lwatch.c settings.c strpcre.c yparse.l $(xh) dist_sysconf_DATA = lwatch.conf CLEANFILES = stamp-h.in *~ --- NEW FILE: log.h --- /* log.h - syslog support, output errors */ /* * This file is part of the LogWatcher tool. * Copyright (C) 2003,2010 Artur R. Czechowski <ar...@he...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <syslog.h> extern int debuglevel; void start_log(void); void stop_log(void); void send_log(int prio, char *fmt, ...); |