00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <unistd.h>
00030 #include <string.h>
00031 #include <syslog.h>
00032
00033 #include "debug.h"
00034 #include "safe.h"
00035 #include "conf.h"
00036
00037 #include "../config.h"
00038
00039
00040
00041
00042 char ** restartargv = NULL;
00043
00044 static void usage(void);
00045
00046
00047
00048
00049
00050 pid_t restart_orig_pid = 0;
00051
00057 static void
00058 usage(void)
00059 {
00060 printf("Usage: wifidog [options]\n");
00061 printf("\n");
00062 printf(" -c [filename] Use this config file\n");
00063 printf(" -f Run in foreground\n");
00064 printf(" -d <level> Debug level\n");
00065 printf(" -s Log to syslog\n");
00066 printf(" -w <path> Wdctl socket path\n");
00067 printf(" -h Print usage\n");
00068 printf(" -v Print version information\n");
00069 printf(" -x pid Used internally by WiFiDog when re-starting itself *DO NOT ISSUE THIS SWITCH MANUAlLY*\n");
00070 printf(" -i <path> Internal socket path used when re-starting self\n");
00071 printf("\n");
00072 }
00073
00077 void parse_commandline(int argc, char **argv) {
00078 int c;
00079 int skiponrestart;
00080 int i;
00081
00082 s_config *config = config_get_config();
00083
00084
00085 restartargv = safe_malloc((argc + 3) * sizeof(char*));
00086 i=0;
00087 restartargv[i++] = safe_strdup(argv[0]);
00088
00089 while (-1 != (c = getopt(argc, argv, "c:hfd:sw:vx:i:"))) {
00090
00091 skiponrestart = 0;
00092
00093 switch(c) {
00094
00095 case 'h':
00096 usage();
00097 exit(1);
00098 break;
00099
00100 case 'c':
00101 if (optarg) {
00102 strncpy(config->configfile, optarg, sizeof(config->configfile));
00103 }
00104 break;
00105
00106 case 'w':
00107 if (optarg) {
00108 free(config->wdctl_sock);
00109 config->wdctl_sock = safe_strdup(optarg);
00110 }
00111 break;
00112
00113 case 'f':
00114 skiponrestart = 1;
00115 config->daemon = 0;
00116 break;
00117
00118 case 'd':
00119 if (optarg) {
00120 config->debuglevel = atoi(optarg);
00121 }
00122 break;
00123
00124 case 's':
00125 config->log_syslog = 1;
00126 break;
00127
00128 case 'v':
00129 printf("This is WiFiDog version " VERSION "\n");
00130 exit(1);
00131 break;
00132
00133 case 'x':
00134 skiponrestart = 1;
00135 if (optarg) {
00136 restart_orig_pid = atoi(optarg);
00137 }
00138 else {
00139 printf("The expected PID to the -x switch was not supplied!");
00140 exit(1);
00141 }
00142 break;
00143
00144 case 'i':
00145 if (optarg) {
00146 free(config->internal_sock);
00147 config->internal_sock = safe_strdup(optarg);
00148 }
00149 break;
00150
00151 default:
00152 usage();
00153 exit(1);
00154 break;
00155
00156 }
00157
00158 if (!skiponrestart) {
00159
00160 safe_asprintf(&(restartargv[i++]), "-%c", c);
00161 if (optarg) {
00162 restartargv[i++] = safe_strdup(optarg);
00163 }
00164 }
00165
00166 }
00167
00168
00169
00170
00171
00172
00173
00174
00175 restartargv[i++] = NULL;
00176 restartargv[i++] = NULL;
00177 restartargv[i++] = NULL;
00178
00179 }
00180