Main Page | Data Structures | Directories | File List | Data Fields

src/commandline.c

00001 /********************************************************************\
00002  * This program is free software; you can redistribute it and/or    *
00003  * modify it under the terms of the GNU General Public License as   *
00004  * published by the Free Software Foundation; either version 2 of   *
00005  * the License, or (at your option) any later version.              *
00006  *                                                                  *
00007  * This program is distributed in the hope that it will be useful,  *
00008  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00010  * GNU General Public License for more details.                     *
00011  *                                                                  *
00012  * You should have received a copy of the GNU General Public License*
00013  * along with this program; if not, contact:                        *
00014  *                                                                  *
00015  * Free Software Foundation           Voice:  +1-617-542-5942       *
00016  * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
00017  * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
00018  *                                                                  *
00019 \********************************************************************/
00020 
00021 /* $Id: commandline.c 935 2006-01-31 22:22:04 -0500 (Tue, 31 Jan 2006) benoitg $ */
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  * Holds an argv that could be passed to exec*() if we restart ourselves
00041  */
00042 char ** restartargv = NULL;
00043 
00044 static void usage(void);
00045 
00046 /*
00047  * A flag to denote whether we were restarted via a parent wifidog, or started normally
00048  * 0 means normally, otherwise it will be populated by the PID of the parent
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         //MAGIC 3: Our own -x, the pid, and NULL :
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                         /* Add it to restartargv */
00160                         safe_asprintf(&(restartargv[i++]), "-%c", c);
00161                         if (optarg) {
00162                                 restartargv[i++] = safe_strdup(optarg);
00163                         }
00164                 }
00165 
00166         }
00167 
00168         /* Finally, we should add  the -x, pid and NULL to restartargv
00169          * HOWEVER we cannot do it here, since this is called before we fork to background
00170          * so we'll leave this job to gateway.c after forking is completed
00171          * so that the correct PID is assigned
00172          *
00173          * We add 3 nulls, and the first 2 will be overridden later
00174          */
00175         restartargv[i++] = NULL;
00176         restartargv[i++] = NULL;
00177         restartargv[i++] = NULL;
00178 
00179 }
00180 

Generated on Tue Jan 31 23:13:15 2006 for WifiDog by  doxygen 1.4.4