[Onhm-cvs-commits] CVS: Software/common/src daemon.c,1.2,1.3 logger.c,1.3,1.4
Status: Planning
Brought to you by:
cheezel
|
From: Brian C. <bch...@us...> - 2005-12-06 12:19:06
|
Update of /cvsroot/onhm/Software/common/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3160/common/src Modified Files: daemon.c logger.c Log Message: Miscellaneous updates to the codebase. Index: daemon.c =================================================================== RCS file: /cvsroot/onhm/Software/common/src/daemon.c,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** daemon.c 20 May 2005 10:10:38 -0000 1.2 --- daemon.c 6 Dec 2005 12:18:53 -0000 1.3 *************** *** 13,16 **** --- 13,24 ---- /****************************************************************************** + * Variable Name: continueDaemon + * Type: Integer + * Purpose: To maintain the state of wether or not the daemon should + * continue to operate. + ******************************************************************************/ + int continueDaemon; + + /****************************************************************************** * Function Name: goDaemon() * Purpose: To place the current program into a daemon execution state. *************** *** 32,37 **** /* Set the uid and gid we are running as. */ ! if ( (getuid()==0) || (getuid()==0) ) { pwd = getpwnam(user); grp = getgrnam(group); --- 40,46 ---- /* Set the uid and gid we are running as. */ ! if ( (getuid() == 0) && (getuid() == 0) ) { + // Get the uid and gid details for the user and group which we are to run as pwd = getpwnam(user); grp = getgrnam(group); *************** *** 99,102 **** --- 108,113 ---- close(STDOUT_FILENO); close(STDERR_FILENO); + + continueDaemon = 1; /* Return from this function with success. */ Index: logger.c =================================================================== RCS file: /cvsroot/onhm/Software/common/src/logger.c,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** logger.c 20 Aug 2005 06:53:03 -0000 1.3 --- logger.c 6 Dec 2005 12:18:53 -0000 1.4 *************** *** 1,157 **** ! /****************************************************************************** ! * File name: common/src/logger.c ! * Purpose: To provide the support functions which will enable the program ! * making use of these routines to reliably log required information ! * either to a self controlled file, or via syslog. ! * Author: Brian A Cheeseman. ! * Date: 17 September 2004. ! * Copyright: (c) 2004, Brian A Cheeseman. ! * License: GNU General Public License Version 2. See LICENSE for details. ! ******************************************************************************/ ! ! #include "logger.h" ! ! char *onhmLevelStrings[] = ! { ! "EMERG", ! "ALERT", ! "CRIT ", ! "ERROR", ! "WARN ", ! "NOTCE", ! "INFO ", ! "DEBUG" ! }; ! ! int onhmLoggingMethod = -1; ! char *onhmLoggingFileName = NULL; ! ! int initialiseLogging(int loggingMethod, char *identifier, char *fileName) ! { ! // Local Variables. ! FILE *fdlog; // File Descriptor for writing to log file. ! ! // Set the value of the global variable for the logging method. ! onhmLoggingMethod = loggingMethod; ! ! // Pick the correct path for logging based on the requested method. ! switch (onhmLoggingMethod) ! { ! // Are we logging through syslog? ! case ONHM_LOG_SYSLOG: ! { ! // Do we have an identifier? if not, default to onhm. ! if (identifier == NULL || strlen(identifier) == 0) ! { ! identifier = (char *)malloc(5); ! strncpy(identifier, (char *)"onhm\000", 5); ! } // End of if (identifier == NULL || strlen(identifier) == 0) ! ! // Open the system logger. ! openlog(identifier, LOG_CONS || LOG_PID || LOG_ODELAY, LOG_DAEMON); ! ! break; ! } // End of case ONHM_LOG_SYSLOG ! ! // Are we logging to a file? ! case ONHM_LOG_FILE: ! { ! // Store the fileName value in our global storage. ! onhmLoggingFileName = strdup(fileName); ! ! // Open the file for appending so that it is created if it doesn't exist. ! fdlog = fopen(onhmLoggingFileName, "a"); ! ! // Close the log off. ! fclose(fdlog); ! break; ! } ! ! // Bugger, something is not right. ! default: ! { ! return -1; ! } ! ! } // End of switch (loggingMethod) ! ! // If we get to here, then logging is configured. ! return 0; ! } // End of bool initialiseLogging(int loggingMethod, char *identifier, char *fileName) ! ! void logMessage(int level, char *message, ...) ! { ! // Local Variables. ! va_list ap; // Variable parmeter list. ! FILE *fdlog; // File Descriptor for writing to log file. ! ! switch (onhmLoggingMethod) ! { ! case ONHM_LOG_SYSLOG: ! { ! va_start(ap, message); ! vsyslog(level, message, ap); ! va_end(ap); ! break; ! } ! ! case ONHM_LOG_FILE: ! { ! time_t now; ! struct tm *tmn; ! ! // Open the file for appending so that it is created if it doesn't exist. ! fdlog = fopen(onhmLoggingFileName, "a"); ! ! // Build string for writing to log file. ! now = time(NULL); ! tmn = malloc(sizeof(struct tm)); ! tmn = localtime_r(&now, tmn); ! fprintf(fdlog, "%02d/%02d/%02d %02d:%02d:%02d [%s] ", tmn->tm_mday, ! tmn->tm_mon, tmn->tm_year, tmn->tm_hour, tmn->tm_min, tmn->tm_sec, ! onhmLevelStrings[level]); ! ! va_start(ap, message); ! vfprintf(fdlog, message, ap); ! va_end(ap); ! fprintf(fdlog, "\n"); ! ! // Close the log off. ! fclose(fdlog); ! break; ! } ! ! default: ! { ! // Hmmm, what is going on here then, someone is using my code and not setting ! // up the environment correctly. ! break; ! } // End of default: ! } // End of switch (ONHM_LOGGING_METHOD) ! } // End of void LogMessage(int level, char *message, va_list ap) ! ! void shutdownLogging() ! { ! switch (onhmLoggingMethod) ! { ! case ONHM_LOG_SYSLOG: ! { ! // Close the connection to the system logger. ! closelog(); ! break; ! } ! ! case ONHM_LOG_FILE: ! { ! // There is nothing to do here. ! break; ! } ! ! default: ! { ! // Hmmm, what is going on here then, someone is using my code and not setting ! // up the environment correctly. ! break; ! } ! } // End of switch (onhmLoggingMethod) ! } // End of void shutdownLogging() --- 1,157 ---- ! /****************************************************************************** ! * File name: common/src/logger.c ! * Purpose: To provide the support functions which will enable the program ! * making use of these routines to reliably log required information ! * either to a self controlled file, or via syslog. ! * Author: Brian A Cheeseman. ! * Date: 17 September 2004. ! * Copyright: (c) 2004, Brian A Cheeseman. ! * License: GNU General Public License Version 2. See LICENSE for details. ! ******************************************************************************/ ! ! #include "logger.h" ! ! char *onhmLevelStrings[] = ! { ! "EMERG", ! "ALERT", ! "CRIT ", ! "ERROR", ! "WARN ", ! "NOTCE", ! "INFO ", ! "DEBUG" ! }; ! ! int onhmLoggingMethod = -1; ! char *onhmLoggingFileName = NULL; ! ! int initialiseLogging(int loggingMethod, char *identifier, char *fileName) ! { ! // Local Variables. ! FILE *fdlog; // File Descriptor for writing to log file. ! ! // Set the value of the global variable for the logging method. ! onhmLoggingMethod = loggingMethod; ! ! // Pick the correct path for logging based on the requested method. ! switch (onhmLoggingMethod) ! { ! // Are we logging through syslog? ! case ONHM_LOG_SYSLOG: ! { ! // Do we have an identifier? if not, default to onhm. ! if (identifier == NULL || strlen(identifier) == 0) ! { ! identifier = (char *)malloc(5); ! strncpy(identifier, (char *)"onhm\000", 5); ! } // End of if (identifier == NULL || strlen(identifier) == 0) ! ! // Open the system logger. ! openlog(identifier, LOG_CONS || LOG_PID || LOG_ODELAY, LOG_DAEMON); ! ! break; ! } // End of case ONHM_LOG_SYSLOG ! ! // Are we logging to a file? ! case ONHM_LOG_FILE: ! { ! // Store the fileName value in our global storage. ! onhmLoggingFileName = strdup(fileName); ! ! // Open the file for appending so that it is created if it doesn't exist. ! fdlog = fopen(onhmLoggingFileName, "a"); ! ! // Close the log off. ! fclose(fdlog); ! break; ! } ! ! // Bugger, something is not right. ! default: ! { ! return -1; ! } ! ! } // End of switch (loggingMethod) ! ! // If we get to here, then logging is configured. ! return 0; ! } // End of bool initialiseLogging(int loggingMethod, char *identifier, char *fileName) ! ! void logMessage(int level, char *message, ...) ! { ! // Local Variables. ! va_list ap; // Variable parmeter list. ! FILE *fdlog; // File Descriptor for writing to log file. ! ! switch (onhmLoggingMethod) ! { ! case ONHM_LOG_SYSLOG: ! { ! va_start(ap, message); ! vsyslog(level, message, ap); ! va_end(ap); ! break; ! } ! ! case ONHM_LOG_FILE: ! { ! time_t now; ! struct tm *tmn; ! ! // Open the file for appending so that it is created if it doesn't exist. ! fdlog = fopen(onhmLoggingFileName, "a"); ! ! // Build string for writing to log file. ! now = time(NULL); ! tmn = malloc(sizeof(struct tm)); ! tmn = localtime_r(&now, tmn); ! fprintf(fdlog, "%02d/%02d/%04d %02d:%02d:%02d [%s] ", tmn->tm_mday, ! tmn->tm_mon, tmn->tm_year + 1900, tmn->tm_hour, tmn->tm_min, tmn->tm_sec, ! onhmLevelStrings[level]); ! ! va_start(ap, message); ! vfprintf(fdlog, message, ap); ! va_end(ap); ! fprintf(fdlog, "\n"); ! ! // Close the log off. ! fclose(fdlog); ! break; ! } ! ! default: ! { ! // Hmmm, what is going on here then, someone is using my code and not setting ! // up the environment correctly. ! break; ! } // End of default: ! } // End of switch (ONHM_LOGGING_METHOD) ! } // End of void LogMessage(int level, char *message, va_list ap) ! ! void shutdownLogging() ! { ! switch (onhmLoggingMethod) ! { ! case ONHM_LOG_SYSLOG: ! { ! // Close the connection to the system logger. ! closelog(); ! break; ! } ! ! case ONHM_LOG_FILE: ! { ! // There is nothing to do here. ! break; ! } ! ! default: ! { ! // Hmmm, what is going on here then, someone is using my code and not setting ! // up the environment correctly. ! break; ! } ! } // End of switch (onhmLoggingMethod) ! } // End of void shutdownLogging() |