The resulting code is not compilable, rest of the patches in this serie must
be applied.

Signed-off-by: Jan Safranek <[email protected]>
---

 cgrulesengd.c |  142 +++++++++++++++++++++++++++++++++++++++++++--------------
 cgrulesengd.h |   18 ++++---
 2 files changed, 116 insertions(+), 44 deletions(-)

diff --git a/cgrulesengd.c b/cgrulesengd.c
index 527184b..42d24e7 100644
--- a/cgrulesengd.c
+++ b/cgrulesengd.c
@@ -44,15 +44,22 @@
 #include <linux/netlink.h>
 #include <signal.h>
 #include <time.h>
+#include <syslog.h>
 
 #include <sys/stat.h>
 #include <unistd.h>
 #include <linux/connector.h>
 #include <linux/cn_proc.h>
 
-/* Log file */
+/* Log file, NULL if logging to file is disabled */
 FILE* logfile;
 
+/* Log facility, 0 if logging to syslog is disabled */
+int logfacility;
+
+/* Current log level */
+int loglevel;
+
 /**
  * Prints the usage information for this program and, optionally, an error
  * message.  This function uses vfprintf.
@@ -78,24 +85,38 @@ void usage(FILE* fd, const char* msg, ...)
 }
 
 /**
- * Prints a formatted message (like printf()) to a file stream, and flushes
- * the file stream's buffer so that the message is immediately readable.
- *     @param fd The file stream to write to
+ * Prints a formatted message (like printf()) to all log destinations.
+ * Flushes the file stream's buffer so that the message is immediately
+ * readable.
+ *     @param level The log level (LOG_EMERG ... LOG_DEBUG)
  *     @param format The format for the message (printf style)
  *     @param ... Any args to format (printf style)
  */
-void flog(FILE* fd, const char* format, ...)
+void flog(int level, const char* format, ...)
 {
        /* List of args to format */
        va_list ap;
 
-       /* Print the message to the given stream. */
-       va_start(ap, format);
-       vfprintf(fd, format, ap);
-       va_end(ap);
+       /* Check the log level */
+       if (level > loglevel)
+               return;
+
+       if (logfile) {
+               /* Print the message to the given stream. */
+               va_start(ap, format);
+               vfprintf(logfile, format, ap);
+               va_end(ap);
+               fprintf(logfile, "\n");
 
-       /* Flush the stream's buffer, so the data is readable immediately. */
-       fflush(fd);
+               /* Flush the stream's buffer, so the data is readable 
immediately. */
+               fflush(logfile);
+       }
+
+       if (logfacility) {
+               va_start(ap, format);
+               vsyslog(LOG_MAKEPRI(logfacility, level), format, ap);
+               va_end(ap);
+       }
 }
 
 /**
@@ -370,23 +391,84 @@ close_and_exit:
 }
 
 /**
+ * Start logging. Opens syslog and/or log file and sets log level.
+ *     @param logp Path of the log file, NULL if no log file was specified
+ *     @param logf Syslog facility, NULL if no facility was specified
+ *     @param logv Log verbosity, 2 is the default, 0 = no logging, 4 = 
everything
+ */
+static void cgre_start_log(const char* logp, int logf, int logv)
+{
+       /* Current system time */
+       time_t tm;
+
+       /* Log levels */
+       int loglevels[] = {
+               LOG_EMERG,              /* -qq */
+               LOG_ERR,                /* -q */
+               LOG_NOTICE,             /* default */
+               LOG_INFO,               /* -v */
+               LOG_DEBUG               /* -vv */
+       };
+
+       /* Set default logging destination if nothing was specified */
+       if (!logp && !logf) {
+               logf = LOG_DAEMON;
+       }
+
+       /* Open log file */
+       if (logp) {
+               if (strcmp("-", logp) == 0) {
+                       logfile = stdout;
+               } else {
+                       logfile = fopen(logp, "a");
+                       if (!logfile) {
+                               fprintf(stderr, "Failed to open log file %s, 
error: %s."
+                                       "  Continuing anyway.\n", logp,
+                                       strerror(errno));
+                               logfile = stdout;
+                       }
+               }
+       } else
+               logfile = NULL;
+
+       /* Open syslog */
+       if (logf) {
+               openlog("CGRE", LOG_CONS | LOG_PID, logf);
+               logfacility = logf;
+       } else
+               logfacility = 0;
+
+       /* Set the log level */
+       if (logv < 0)
+               logv = 0;
+       if (logv >= sizeof(loglevels)/sizeof(int))
+               logv = sizeof(loglevels)/sizeof(int)-1;
+
+       loglevel = loglevels[logv];
+
+       flog(logfile, "CGroup Rules Engine Daemon\n");
+       tm = time(0);
+       flog(logfile, "Current time: %s", ctime(&tm));
+       flog(stdout, "Opened log file: %s\n", logp);
+}
+
+
+/**
  * Turns this program into a daemon.  In doing so, we fork() and kill the
  * parent process.  Note too that stdout, stdin, and stderr are closed in
  * daemon mode, and a file descriptor for a log file is opened.
- *     @param logp Path of the log file
+ *     @param logp Path of the log file, NULL if no log file was specified
+ *     @param logf Syslog facility, NULL if no facility was specified
  *     @param daemon False to turn off daemon mode (no fork, leave FDs open)
- *     @param logs False to disable logging (no log FD, leave stdout open)
+ *     @param logv Log verbosity, 2 is the default, 0 = no logging, 5 = 
everything
  *     @return 0 on success, > 0 on error
  */
-int cgre_start_daemon(const char* logp, const unsigned char daemon,
-                       const unsigned char logs)
+int cgre_start_daemon(const char* logp, const int logf,
+                       const unsigned char daemon, const int logv)
 {
        /* PID returned from the fork() */
        pid_t pid;
 
-       /* Current system time */
-       time_t tm;
-
        /* Fork and die. */
        if (daemon) {
                pid = fork();
@@ -409,23 +491,7 @@ int cgre_start_daemon(const char* logp, const unsigned 
char daemon,
                pid = getpid();
        }
 
-       if (logs) {
-               logfile = fopen(logp, "a");
-               if (!logfile) {
-                       flog(stderr, "Failed to open log file %s, error: %s."
-                                       "  Continuing anyway.\n", logp,
-                                       strerror(errno));
-                       logfile = stdout;
-               } else {
-                       flog(logfile, "CGroup Rules Engine Daemon\n");
-                       tm = time(0);
-                       flog(logfile, "Current time: %s", ctime(&tm));
-                       flog(stdout, "Opened log file: %s\n", logp);
-               }
-       } else {
-               logfile = stdout;
-               flog(stdout, "Proceeding with stdout as log output.\n");
-       }
+       cgre_start_log(logp, logf, logv);
 
        if (!daemon) {
                /* We can skip the rest, since we're not becoming a daemon. */
@@ -496,10 +562,14 @@ void cgre_catch_term(int signum)
        flog(logfile, "========================================");
        flog(logfile, "========================================\n\n");
 
-       /* Close the log file, if we opened one. */
+       /* Close the log file, if we opened one */
        if (logfile && logfile != stdout)
                fclose(logfile);
 
+       /* Close syslog */
+       if (logfacility)
+               closelog();
+
        exit(EXIT_SUCCESS);
 }
 
diff --git a/cgrulesengd.h b/cgrulesengd.h
index bdffd31..c66969b 100644
--- a/cgrulesengd.h
+++ b/cgrulesengd.h
@@ -65,13 +65,14 @@ __BEGIN_DECLS
 void cgre_usage(FILE *fd, const char *msg, ...);
 
 /**
- * Prints a formatted message (like printf()) to a file stream, and flushes
- * the file stream's buffer so that the message is immediately readable.
- *     @param fd The file stream to write to
+ * Prints a formatted message (like printf()) to all log destinations.
+ * Flushes the file stream's buffer so that the message is immediately
+ * readable.
+ *     @param level The log level (LOG_EMERG ... LOG_DEBUG)
  *     @param format The format for the message (printf style)
  *     @param ... Any args to format (printf style)
  */
-void flog(FILE* fd, const char* msg, ...);
+void flog(int level, const char* msg, ...);
 
 /**
  * Process an event from the kernel, and determine the correct UID/GID/PID to
@@ -96,13 +97,14 @@ int cgre_handle_message(struct cn_msg *cn_hdr);
  * Turns this program into a daemon.  In doing so, we fork() and kill the
  * parent process.  Note too that stdout, stdin, and stderr are closed in
  * daemon mode, and a file descriptor for a log file is opened.
- *     @param logp Path of the log file
+ *     @param logp Path of the log file, NULL if no log file was specified
+ *     @param logf Syslog facility, NULL if no facility was specified
  *     @param daemon False to turn off daemon mode (no fork, leave FDs open)
- *     @param logs False to disable logging (no log FD, leave stdout open)
+ *     @param logv Log verbosity, 2 is the default, 0 = no logging, 5 = 
everything
  *     @return 0 on success, > 0 on error
  */
-int cgre_start_daemon(const char *logp, const unsigned char daemon,
-                       const unsigned char logs);
+int cgre_start_daemon(const char *logp, const int logf,
+                       const unsigned char daemon, const int logv);
 
 /**
  * Catch the SIGUSR2 signal and reload the rules configuration.  This function


------------------------------------------------------------------------------
Create and Deploy Rich Internet Apps outside the browser with Adobe(R)AIR(TM)
software. With Adobe AIR, Ajax developers can use existing skills and code to
build responsive, highly engaging applications that combine the power of local
resources and data with the reach of the web. Download the Adobe AIR SDK and
Ajax docs to start building applications today-http://p.sf.net/sfu/adobe-com
_______________________________________________
Libcg-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to