opencryptoki (2.3.1+dfsg-3) 04-pkcsslotd-cmdline-args.patch

Summary

 usr/sbin/pkcsslotd/daemon.c    |    7 ++++
 usr/sbin/pkcsslotd/log.c       |   43 ++++++++++++++++++++++++++--
 usr/sbin/pkcsslotd/log.h       |    3 ++
 usr/sbin/pkcsslotd/pkcsslotd.h |    1 
 usr/sbin/pkcsslotd/slotmgr.c   |   61 ++++++++++++++++++++++++++++++++++++++---
 5 files changed, 109 insertions(+), 6 deletions(-)

    
download this patch

Patch contents

Author: David Smith <dds@google.com>
Description:
 Add command line arguments for logging, verbosity, and daemonizing. Allow
 logging to files, only useful for debugging but controllable at runtime, not
 compile time.

diff -Naurp opencryptoki.orig/usr/sbin/pkcsslotd/daemon.c opencryptoki/usr/sbin/pkcsslotd/daemon.c
--- opencryptoki.orig/usr/sbin/pkcsslotd/daemon.c	2008-10-11 17:30:02.000000000 +0000
+++ opencryptoki/usr/sbin/pkcsslotd/daemon.c	2008-10-18 17:00:44.000000000 +0000
@@ -310,6 +310,13 @@ BOOL IsDaemon ( void ) {
 }
 
 
+BOOL SetDaemon ( BOOL Val ) {
+  BOOL OldVal = Daemon;
+
+  Daemon = Val;
+  return OldVal;
+}
+
 
 BOOL SaveStartupDirectory ( char *Arg0 ) {
 
diff -Naurp opencryptoki.orig/usr/sbin/pkcsslotd/log.c opencryptoki/usr/sbin/pkcsslotd/log.c
--- opencryptoki.orig/usr/sbin/pkcsslotd/log.c	2008-10-11 17:30:02.000000000 +0000
+++ opencryptoki/usr/sbin/pkcsslotd/log.c	2008-10-18 17:00:44.000000000 +0000
@@ -791,7 +791,6 @@ BOOL PKCS_Log ( pLogHandle phLog, char *
 
 
   /* Don't log to a separate log file in production mode */
-  #ifdef DEV
   if ( pInfo->Filename != NULL ) {
 
     FILE *fd;
@@ -811,8 +810,6 @@ BOOL PKCS_Log ( pLogHandle phLog, char *
     }
 
   } /* end if pInfo->Filename */
-  #endif /* DEV */
-
 
 
   /* Always log to syslog, if we're using it */
@@ -957,6 +954,46 @@ void InfoLog ( char *Format, ... ) {
 }
 
 
+/***********************************************************************
+ * SetLogFile -
+ *
+ *   Sets a static log file.  Must be called before InitLogging.
+ ***********************************************************************/
+void SetLogFile ( const char *LogFile ) {
+  char *New_LogFile;
+  int i;
+
+  if (NULL == LogFile)
+    return;
+
+  /* this is never freed */
+  New_LogFile = strdup(LogFile);
+  for ( i = 0; i < ( sizeof(SystemLogFacilities) / (sizeof(SystemLogFacilities[0])) ); i++ ) {
+    SystemLogFacilities[i].Filename = New_LogFile;
+  }
+}
+
+/***********************************************************************
+ * SetProgramName -
+ *
+ *   Sets the program name for logging.
+ ***********************************************************************/
+void SetProgramName ( const char *New_ProgramName ) {
+  if (NULL == New_ProgramName)
+    return;
+
+  /* this is never freed */
+  ProgramName = strdup(New_ProgramName);
+}
+
+/***********************************************************************
+ * SetProgramName -
+ *
+ *   Gets the program name.
+ ***********************************************************************/
+char *GetProgramName ( void ) {
+  return ProgramName;
+}
 
 /***********************************************************************
  * InitLogging -
diff -Naurp opencryptoki.orig/usr/sbin/pkcsslotd/log.h opencryptoki/usr/sbin/pkcsslotd/log.h
--- opencryptoki.orig/usr/sbin/pkcsslotd/log.h	2008-10-11 17:30:02.000000000 +0000
+++ opencryptoki/usr/sbin/pkcsslotd/log.h	2008-10-18 17:00:44.000000000 +0000
@@ -402,6 +402,9 @@ BOOL     PKCS_Log             ( LogHandl
 BOOL     NewLoggingFacility   ( char       *ID,          pLoggingFacility pStuff );
 BOOL     CloseLoggingFacility ( LogHandle   hLog );
 BOOL     GetCurrentTimeString ( char       *Buffer );
+void     SetLogFile           ( const char *LogFile );
+void     SetProgramName       ( const char *New_ProgramName );
+char    *GetProgramName       ( void );
 
 
 u_int32  SetDebugLevel        ( u_int32 Val );
diff -Naurp opencryptoki.orig/usr/sbin/pkcsslotd/pkcsslotd.h opencryptoki/usr/sbin/pkcsslotd/pkcsslotd.h
--- opencryptoki.orig/usr/sbin/pkcsslotd/pkcsslotd.h	2008-10-11 17:30:02.000000000 +0000
+++ opencryptoki/usr/sbin/pkcsslotd/pkcsslotd.h	2008-10-18 17:00:44.000000000 +0000
@@ -495,6 +495,7 @@ nl_catd catd;
 
 /* daemon.c */
 BOOL                   IsDaemon ( void );
+BOOL                   SetDaemon ( BOOL Val );
 BOOL                   GetStartDirectory ( char *Buffer, u_int32 BufSize );
 BOOL                   SaveStartupDirectory ( char *Arg0 );
 
diff -Naurp opencryptoki.orig/usr/sbin/pkcsslotd/slotmgr.c opencryptoki/usr/sbin/pkcsslotd/slotmgr.c
--- opencryptoki.orig/usr/sbin/pkcsslotd/slotmgr.c	2008-10-11 17:30:02.000000000 +0000
+++ opencryptoki/usr/sbin/pkcsslotd/slotmgr.c	2008-10-18 17:04:59.000000000 +0000
@@ -301,12 +301,16 @@
 #include <semaphore.h>
 
 
+/* for getopt via unitstd */
+extern char		   *optarg;
+
 /* 
    We make main() able to modify Daemon so that we can 
    daemonize or not based on a command-line argument
  */
 extern BOOL               Daemon;
 extern BOOL               IveDaemonized;
+extern char		   *ProgramName;
 
 #if !(THREADED)
 extern void *GCMain ( void *Ptr);
@@ -316,6 +320,23 @@ void DumpSharedMemory ( void );
 
 
 /*****************************************
+ *  usage() -
+ *      Print command line options.
+ *
+ *****************************************/
+#define ARGS_STRING "hvfl:"
+void usage ( void ) {
+   printf(
+      "usage:\t%s [-v] [-f] [-l LOG_FILE] [-h]\n\n"                        \
+      "\t-v         increase verbosity, can be specified multiple times\n" \
+      "\t-f         run in the foreground\n"                               \
+      "\t-l FILE    send logs to FILE as well as syslog\n"                 \
+      "\t-h         display this help message\n"                           \
+      , GetProgramName());
+}
+
+
+/*****************************************
  *  main() -
  *      You know what main does.
  *      Comment block for ease of spotting
@@ -324,6 +345,8 @@ void DumpSharedMemory ( void );
  *****************************************/
 
 int main ( int argc, char *argv[], char *envp[]) {
+   int option;
+
    setlocale(LC_ALL, "");
    catd = catopen(MF_SLOTD,0);
 
@@ -331,10 +354,42 @@ int main ( int argc, char *argv[], char 
    /* Read in command-line arguments */
    /**********************************/
 
-   /* FIXME: Argument for daemonizing or not */
-   /* FIXME: Argument for debug level */
-   /* FIXME: Arguments affecting the log files, whether to use syslog, etc. (Read conf file?) */
+   /* Set default options */
+   SetProgramName(basename(argv[0]));
+#ifdef DEFAULT_DEBUG_LEVEL
+   SetDebugLevel(DEFAULT_DEBUG_LEVEL);
+#else
+   SetDebugLevel(0);
+#endif
+#ifdef BECOME_DAEMON
+   SetDaemon(BECOME_DAEMON);
+#else
+   SetDaemon(1);
+#endif
+#ifdef LOG_FILE
+   SetLogFile(LOG_FILE);
+#else
+   SetLogFile(NULL);
+#endif
 
+   /* Parse arguments */
+   while (-1 != (option = getopt(argc, argv, ARGS_STRING))) {
+      switch (option) {
+         case 'h':
+            usage();
+            exit(0);
+            break;
+         case 'v':
+            SetDebugLevel(GetDebugLevel() + 100);
+            break;
+         case 'f':
+            SetDaemon(0);
+            break;
+         case 'l':
+            SetLogFile(optarg);
+            break;
+      }
+   }
 
    /* Report our debug level */
    if ( GetDebugLevel() > DEBUG_NONE) {