1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
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
@@ -37,6 +37,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
@@ -466,7 +466,6 @@
/* Don't log to a separate log file in production mode */
- #ifdef DEV
if ( pInfo->Filename != NULL ) {
FILE *fd;
@@ -486,8 +485,6 @@
}
} /* end if pInfo->Filename */
- #endif /* DEV */
-
/* Always log to syslog, if we're using it */
@@ -632,6 +629,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
@@ -116,6 +116,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
@@ -71,6 +71,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
@@ -8,6 +8,8 @@
* https://opensource.org/licenses/cpl1.0.php
*/
+#define _GNU_SOURCE
+
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@@ -44,6 +46,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
@@ -290,6 +295,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
@@ -299,14 +321,47 @@
int main ( int argc, char *argv[], char *envp[]) {
int ret, i;
+ 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();
|