Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # HG changeset patch
- # User alexey@ubuntu64.COLLEGE
- # Date 1354602095 -25200
- # Node ID 5b943f6164c5be8617e28b0b26aaf64de49621fa
- # Parent 076d182af50930013519233ea1a8ee067c6538f6
- daemonize && syslog
- diff --git a/Unix/log.c b/Unix/log.c
- --- a/Unix/log.c
- +++ b/Unix/log.c
- @@ -1,38 +1,92 @@
- #include "common.h"
- -#include "log.h"
- +#include "log.h"
- +#ifndef WIN32
- +#include <syslog.h>
- +#endif
- // global logging level
- int LogLevel = 0;
- // log file object
- -FILE *logFile = NULL;
- +FILE *logFile;
- +int useSyslog;
- void OpenLog(const char *fileName)
- -{
- - logFile = (strcmp(fileName, "-") == 0) ? stdout : fopen(fileName, "a");
- +{
- + useSyslog = 0;
- + logFile = NULL;
- + if ( strcmp(fileName, "-")==0 )
- + {
- + logFile = stdout;
- + }
- + else if ( strcmp(fileName, "syslog")==0 )
- + {
- + useSyslog = 1;
- +#ifndef WIN32
- + openlog ( "z-agent", LOG_PID, LOG_DAEMON );
- +#endif
- + }
- + else
- + {
- + logFile = fopen(fileName, "a");
- + }
- - if (logFile == NULL)
- - {
- - printf("Failed to open log file (%s): %s\n", fileName, strerror(errno));
- + if ( !logFile )
- + {
- + if ( !useSyslog )
- + printf("Failed to open log file (%s): %s\n", fileName, strerror(errno));
- +#ifdef WIN32
- + else
- + printf("Logging ty syslog on Windows not supported. Sorry\n");
- +#endif
- }
- }
- void CloseLog()
- {
- - if (logFile != NULL && logFile != stdout)
- - fclose(logFile);
- - logFile = NULL;
- + if (logFile != NULL && logFile != stdout && !useSyslog )
- + {
- + if (!useSyslog)
- + fclose(logFile);
- + }
- +
- + logFile = NULL;
- +#ifndef WIN32
- + if ( useSyslog )
- + closelog();
- +#endif
- }
- void Log(int level, const char *fmt, ...)
- -{
- +{
- +#ifndef WIN32
- + const int levels[] = { LOG_EMERG, LOG_WARNING, LOG_INFO, LOG_DEBUG };
- +#endif
- va_list ap;
- if (level > LogLevel) return;
- +
- +#ifndef WIN32
- + if ( useSyslog )
- + {
- + vsyslog ( levels[level], fmt, ap );
- + return;
- + }
- +#endif
- va_start(ap, fmt);
- - vfprintf((logFile != NULL) ? logFile : stdout, fmt, ap);
- - va_end(ap);
- + vfprintf((logFile != NULL) ? logFile : stdout, fmt, ap);
- + va_end(ap);
- - fflush((logFile != NULL) ? logFile : stdout);
- + fflush((logFile != NULL) ? logFile : stdout);
- +}
- +
- +void Error(const char *fmt, ...)
- +{
- + va_list ap;
- + va_start(ap, fmt);
- + vfprintf( stderr, fmt, ap);
- + va_end(ap);
- + fflush(stderr);
- + exit (1);
- }
- diff --git a/Unix/log.h b/Unix/log.h
- --- a/Unix/log.h
- +++ b/Unix/log.h
- @@ -6,11 +6,13 @@
- void OpenLog(const char *fileName);
- void CloseLog();
- -void Log(int level, const char *fmt, ...);
- +void Log(int level, const char *fmt, ...);
- +void Error(const char *fmt, ...);
- #define ERR(s, ...) Log(0, "[E] " s, ##__VA_ARGS__)
- #define WARN(s, ...) Log(1, "[W] " s, ##__VA_ARGS__)
- #define INFO(s, ...) Log(2, "[I] " s, ##__VA_ARGS__)
- -#define DBG(s, ...) Log(3, "[D] " s, ##__VA_ARGS__)
- +#define DBG(s, ...) Log(3, "[D] " s, ##__VA_ARGS__)
- +#define FATAL(s, ...) Error( "[FATAL] " s, ##__VA_ARGS__)
- #endif
- \ No newline at end of file
- diff --git a/Unix/main.c b/Unix/main.c
- --- a/Unix/main.c
- +++ b/Unix/main.c
- @@ -24,7 +24,7 @@
- void DumpBuffer(const char *data, int length)
- {
- char line[49];
- - int i, j;
- + int i;
- line[48] = '\0';
- @@ -119,7 +119,6 @@
- {
- struct hostent *srv;
- int dev = -1;
- - FILE* log = NULL;
- SOCKET sock = SOCKET_ERROR;
- struct sockaddr_in srvaddr;
- int keepalive = 1, hasCert, retCode;
- @@ -186,11 +185,11 @@
- {
- WARN("Failed to enable keep-alives\n");
- }
- -
- +
- srvaddr.sin_family = AF_INET;
- srvaddr.sin_port = htons((u_short) port);
- memcpy(&srvaddr.sin_addr, srv->h_addr_list[0], 4);
- -
- +
- INFO("Connecting to server...\n");
- if (0 != connect(sock, (const struct sockaddr *) &srvaddr, sizeof(srvaddr)))
- @@ -241,8 +240,8 @@
- RETURN(3);
- }
- }
- -
- -
- +
- +
- IsTerminating = 0;
- signal(SIGTERM, sig_handler);
- @@ -280,18 +279,22 @@
- struct arg_file *cert = arg_file0(NULL, "cert", NULL, "personal certificate");
- struct arg_file *key = arg_file0(NULL, "key", NULL, "personal certificate key");
- struct arg_file *cacert = arg_file0(NULL, "cacert", NULL, "CA certificate");
- - struct arg_file *log = arg_file0("lL", "log", NULL, "file to write log to (defaults to stdout)");
- + struct arg_file *log = arg_file0("lL", "log", NULL, "file to write log to (defaults to stdout or syslog)");
- struct arg_lit *debug = arg_lit0(NULL, "debug", "log debugging information");
- - struct arg_lit *help = arg_lit0("h", "help", "print this help and exit");
- +#ifndef WIN32
- + struct arg_file *daemon = arg_file0("bB", "pid", NULL, "daemonize and write the pid into given pidfile");
- +#endif
- + struct arg_lit *help = arg_lit0("h", "help", "print this help and exit");
- struct arg_end *end = arg_end(20);
- - void* argTable[] = {device, server, port, cert, key, cacert, log, debug, help, end};
- -
- -#ifdef WIN32
- +#ifdef WIN32
- + void* argTable[] = {device, server, port, cert, key, cacert, log, debug, help, end};
- WSADATA wsa;
- WORD wsaVersion = MAKEWORD(2, 2);
- int wsaError;
- - DWORD win32err;
- + DWORD win32err;
- +#else
- + void* argTable[] = {device, server, port, cert, key, cacert, log, debug, daemon, help, end};
- #endif
- int retCode, nErrors;
- @@ -316,7 +319,15 @@
- CLEAN_RETURN(0);
- }
- - LogLevel = (IS_ARG_SET(debug)) ? 3 : 2;
- + LogLevel = (IS_ARG_SET(debug)) ? 3 : 2;
- +
- +#ifndef WIN32
- + int IDaemon = 0;
- + int iPid = -1;
- + const char * sPidFN = SAFE_FILE (daemon, "");
- + if ( HAS_STR(sPidFN) )
- + IDaemon = 1;
- +#endif
- if (nErrors > 0)
- {
- @@ -325,7 +336,8 @@
- CLEAN_RETURN(2);
- }
- -
- +
- + const char * defLog = "-";
- #ifdef WIN32
- hStoppingEvent = CreateEvent(NULL, TRUE, FALSE, "Local\\Z-Agent-Stop");
- if (hStoppingEvent == NULL)
- @@ -361,17 +373,108 @@
- CloseHandle(hStoppingEvent);
- CLEAN_RETURN(3);
- }
- +#else
- + if (IDaemon)
- + {
- + // first, look if we can create and lock the PID
- + iPid = open ( sPidFN, O_CREAT | O_WRONLY, S_IREAD | S_IWRITE );
- + if ( iPid<0 )
- + FATAL ( "failed to create pid file '%s': %s\n", sPidFN, strerror(errno) );
- +
- + struct flock tLock;
- + tLock.l_type = F_WRLCK;
- + tLock.l_whence = SEEK_SET;
- + tLock.l_start = 0;
- + tLock.l_len = 0;
- + if ( fcntl ( iPid, F_SETLK, &tLock )==-1 )
- + FATAL ( "failed to lock pid file '%s': %s (daemon already running?)\n", sPidFN, strerror(errno) );
- +
- + // prepare to detach
- + int iDevNull = open ( "/dev/null", O_RDWR );
- + close ( STDIN_FILENO );
- + close ( STDOUT_FILENO );
- + dup2 ( iDevNull, STDIN_FILENO );
- + dup2 ( iDevNull, STDOUT_FILENO );
- +
- + // unlock the pid (will relock again in child)
- + tLock.l_type = F_UNLCK;
- + fcntl ( iPid, F_SETLK, &tLock );
- +
- + // Fork #1 - detach from controlling terminal
- + switch ( fork() )
- + {
- + case -1:
- + // error
- + FATAL ( "fork() failed (reason: %s)\n", strerror ( errno ) );
- + case 0:
- + // daemonized child - or new and free watchdog :)
- + break;
- +
- + default:
- + exit ( 0 );
- + }
- +
- + // became the session leader
- + if ( setsid()==-1 )
- + {
- + FATAL ( "fork() failed (reason: %s)\n", strerror ( errno ) );
- + }
- +
- + // Fork #2 - detach from session leadership (may be not necessary, however)
- + switch ( fork() )
- + {
- + case -1:
- + // error
- + FATAL ( "fork() failed (reason: %s)\n", strerror ( errno ) );
- + exit ( 1 );
- + case 0:
- + // daemonized child - or new and free watchdog :)
- + break;
- +
- + default:
- + // the parent
- + exit ( 0 );
- + }
- +
- + // relock the pid
- + tLock.l_type = F_WRLCK;
- + if ( fcntl ( iPid, F_SETLK, &tLock )==-1 )
- + FATAL ( "failed to re-lock pid file '%s': %s\n", sPidFN, strerror(errno) );
- +
- + // write our pid into pidfile
- + char sPid[16];
- + snprintf ( sPid, sizeof(sPid), "%d\n", (int)getpid() );
- + int iPidLen = strlen(sPid);
- +
- + lseek(iPid, 0, SEEK_SET);
- + if ( iPidLen!=write ( iPid, sPid, iPidLen ) )
- + FATAL ( "failed to write to pid file '%s' (errno=%d, msg=%s)\n", sPidFN, errno, strerror(errno) );
- +
- + if ( ftruncate ( iPid, iPidLen ) )
- + FATAL ( "failed to truncate pid file '%s' (errno=%d, msg=%s)\n", sPidFN, errno, strerror(errno) );
- +
- + close ( STDERR_FILENO );
- + dup2 ( iDevNull, STDERR_FILENO );
- + defLog = "syslog";
- +
- + }
- #endif
- - retCode = main_impl(device->filename[0],
- + retCode = main_impl(device->filename[0],
- server->sval[0], SAFE_INT(port, 9087),
- - SAFE_FILE(cert, ""), SAFE_FILE(key, ""), SAFE_FILE(cacert, ""),
- - SAFE_FILE(log, "-"));
- + SAFE_FILE(cert, ""), SAFE_FILE(key, ""), SAFE_FILE(cacert, ""),
- + SAFE_FILE(log, defLog));
- #ifdef WIN32
- WSACleanup();
- CloseHandle(hStoppingEvent);
- +#else
- + if ( iPid>=0 )
- + {
- + close ( iPid );
- + unlink ( sPidFN );
- + }
- #endif
- CLEAN_RETURN(retCode);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement