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.

--- a/usr/sbin/pkcsslotd/daemon.c
+++ b/usr/sbin/pkcsslotd/daemon.c
@@ -318,6 +318,13 @@
 }
 
 
+BOOL SetDaemon ( BOOL Val ) {
+  BOOL OldVal = Daemon;
+
+  Daemon = Val;
+  return OldVal;
+}
+
 
 BOOL SaveStartupDirectory ( char *Arg0 ) {
 
--- a/usr/sbin/pkcsslotd/log.c
+++ b/usr/sbin/pkcsslotd/log.c
@@ -747,7 +747,6 @@
 
 
   /* Don't log to a separate log file in production mode */
-  #ifdef DEV
   if ( pInfo->Filename != NULL ) {
 
     FILE *fd;
@@ -767,8 +766,6 @@
     }
 
   } /* end if pInfo->Filename */
-  #endif /* DEV */
-
 
 
   /* Always log to syslog, if we're using it */
@@ -913,6 +910,24 @@
 }
 
 
+/***********************************************************************
+ * 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;
+  }
+}
 
 /***********************************************************************
  * InitLogging -
--- a/usr/sbin/pkcsslotd/log.h
+++ b/usr/sbin/pkcsslotd/log.h
@@ -402,6 +402,7 @@
 BOOL     NewLoggingFacility   ( char       *ID,          pLoggingFacility pStuff );
 BOOL     CloseLoggingFacility ( LogHandle   hLog );
 BOOL     GetCurrentTimeString ( char       *Buffer );
+void     SetLogFile           ( const char *LogFile );
 
 
 u_int32  SetDebugLevel        ( u_int32 Val );
--- a/usr/sbin/pkcsslotd/pkcsslotd.h
+++ b/usr/sbin/pkcsslotd/pkcsslotd.h
@@ -351,6 +351,7 @@
  ***********************/
 
 BOOL IsDaemon(void);
+BOOL SetDaemon (BOOL Val);
 BOOL GetStartDirectory(char *Buffer, u_int32 BufSize);
 BOOL SaveStartupDirectory(char *Arg0);
 BOOL StopGCThread(void *Ptr);
--- a/usr/sbin/pkcsslotd/slotmgr.c
+++ b/usr/sbin/pkcsslotd/slotmgr.c
@@ -288,6 +288,7 @@
 
 /* (C) COPYRIGHT International Business Machines Corp. 2001          */
 
+#define _GNU_SOURCE
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -316,6 +317,9 @@
 	int mode;
 };
 
+/* 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
@@ -417,6 +421,23 @@
 }
 
 /*****************************************
+ *  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"                           \
+      , program_invocation_short_name);
+}
+
+
+/*****************************************
  *  main() -
  *      You know what main does.
  *      Comment block for ease of spotting
@@ -426,14 +447,47 @@
 
 int main ( int argc, char *argv[], char *envp[]) {
 	int ret;
+	int option;
 
 	/**********************************/
 	/* 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 */
+#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;
+	    }
+	}
 
 	/* Do some basic sanity checks */
 	run_sanity_checks();
