1
+ /* vim: set et sw=4 ts=4 sts=4 : */
1
2
/********************************************************************\
2
3
* This program is free software; you can redistribute it and/or *
3
4
* modify it under the terms of the GNU General Public License as *
18
19
* *
19
20
\********************************************************************/
20
21
21
- /* $Id$ */
22
22
/** @file commandline.c
23
23
@brief Command line argument handling
24
24
@author Copyright (C) 2004 Philippe April <[email protected] >
33
33
#include "debug.h"
34
34
#include "safe.h"
35
35
#include "conf.h"
36
+ #include "commandline.h"
36
37
37
38
#include "../config.h"
38
39
39
40
/*
40
41
* Holds an argv that could be passed to exec*() if we restart ourselves
41
42
*/
42
- /* Declare variable */
43
- extern char * * restartargv ;
44
- /* Define variable */
45
43
char * * restartargv = NULL ;
46
44
47
- static void usage (void );
48
-
49
- void parse_commandline (int argc , char * * argv );
50
-
51
- /*
45
+ /**
52
46
* A flag to denote whether we were restarted via a parent wifidog, or started normally
53
47
* 0 means normally, otherwise it will be populated by the PID of the parent
54
48
*/
55
- extern pid_t restart_orig_pid ;
56
49
pid_t restart_orig_pid = 0 ;
57
50
51
+ static void usage (void );
52
+
58
53
/** @internal
59
54
* @brief Print usage
60
55
*
@@ -73,115 +68,125 @@ usage(void)
73
68
fprintf (stdout , " -w <path> Wdctl socket path\n" );
74
69
fprintf (stdout , " -h Print usage\n" );
75
70
fprintf (stdout , " -v Print version information\n" );
76
- fprintf (stdout , " -x pid Used internally by WiFiDog when re-starting itself *DO NOT ISSUE THIS SWITCH MANUAlLY*\n" );
71
+ fprintf (stdout ,
72
+ " -x pid Used internally by WiFiDog when re-starting itself *DO NOT ISSUE THIS SWITCH MANUAlLY*\n" );
77
73
fprintf (stdout , " -i <path> Internal socket path used when re-starting self\n" );
78
74
fprintf (stdout , "\n" );
79
75
}
80
76
81
77
/** Uses getopt() to parse the command line and set configuration values
82
78
* also populates restartargv
83
79
*/
84
- void parse_commandline (int argc , char * * argv ) {
85
- int c ;
86
- int skiponrestart ;
87
- int i ;
80
+ void
81
+ parse_commandline (int argc , char * * argv )
82
+ {
83
+ int c ;
84
+ int skiponrestart ;
85
+ int i ;
88
86
89
87
s_config * config = config_get_config ();
90
88
91
- //MAGIC 3: Our own -x, the pid, and NULL :
92
- restartargv = safe_malloc ((size_t ) (argc + 3 ) * sizeof (char * ));
93
- i = 0 ;
94
- restartargv [i ++ ] = safe_strdup (argv [0 ]);
95
-
96
- while (-1 != (c = getopt (argc , argv , "c:hfd:sw:vx:i:" ))) {
97
-
98
- skiponrestart = 0 ;
99
-
100
- switch (c ) {
101
-
102
- case 'h' :
103
- usage ();
104
- exit (1 );
105
- break ;
106
-
107
- case 'c' :
108
- if (optarg ) {
109
- strncpy (config -> configfile , optarg , sizeof (config -> configfile ));
110
- }
111
- break ;
112
-
113
- case 'w' :
114
- if (optarg ) {
115
- free (config -> wdctl_sock );
116
- config -> wdctl_sock = safe_strdup (optarg );
117
- }
118
- break ;
119
-
120
- case 'f' :
121
- skiponrestart = 1 ;
122
- config -> daemon = 0 ;
123
- break ;
124
-
125
- case 'd' :
126
- if (optarg ) {
127
- config -> debuglevel = atoi (optarg );
128
- }
129
- break ;
130
-
131
- case 's' :
132
- config -> log_syslog = 1 ;
133
- break ;
134
-
135
- case 'v' :
136
- fprintf (stdout , "This is WiFiDog version " VERSION "\n" );
137
- exit (1 );
138
- break ;
139
-
140
- case 'x' :
141
- skiponrestart = 1 ;
142
- if (optarg ) {
143
- restart_orig_pid = atoi (optarg );
144
- }
145
- else {
146
- fprintf (stdout , "The expected PID to the -x switch was not supplied!" );
147
- exit (1 );
148
- }
149
- break ;
150
-
151
- case 'i' :
152
- if (optarg ) {
153
- free (config -> internal_sock );
154
- config -> internal_sock = safe_strdup (optarg );
155
- }
156
- break ;
157
-
158
- default :
159
- usage ();
160
- exit (1 );
161
- break ;
162
-
163
- }
164
-
165
- if (!skiponrestart ) {
166
- /* Add it to restartargv */
167
- safe_asprintf (& (restartargv [i ++ ]), "-%c" , c );
168
- if (optarg ) {
169
- restartargv [i ++ ] = safe_strdup (optarg );
170
- }
171
- }
172
-
173
- }
174
-
175
- /* Finally, we should add the -x, pid and NULL to restartargv
176
- * HOWEVER we cannot do it here, since this is called before we fork to background
177
- * so we'll leave this job to gateway.c after forking is completed
178
- * so that the correct PID is assigned
179
- *
180
- * We add 3 nulls, and the first 2 will be overridden later
181
- */
182
- restartargv [i ++ ] = NULL ;
183
- restartargv [i ++ ] = NULL ;
184
- restartargv [i ++ ] = NULL ;
185
-
89
+ //MAGIC 3: Our own -x, the pid, and NULL :
90
+ restartargv = safe_malloc ((size_t ) (argc + 3 ) * sizeof (char * ));
91
+ i = 0 ;
92
+ restartargv [i ++ ] = safe_strdup (argv [0 ]);
93
+
94
+ while (-1 != (c = getopt (argc , argv , "c:hfd:sw:vx:i:a:" ))) {
95
+
96
+ skiponrestart = 0 ;
97
+
98
+ switch (c ) {
99
+
100
+ case 'h' :
101
+ usage ();
102
+ exit (1 );
103
+ break ;
104
+
105
+ case 'c' :
106
+ if (optarg ) {
107
+ free (config -> configfile );
108
+ config -> configfile = safe_strdup (optarg );
109
+ }
110
+ break ;
111
+
112
+ case 'w' :
113
+ if (optarg ) {
114
+ free (config -> wdctl_sock );
115
+ config -> wdctl_sock = safe_strdup (optarg );
116
+ }
117
+ break ;
118
+
119
+ case 'f' :
120
+ skiponrestart = 1 ;
121
+ config -> daemon = 0 ;
122
+ break ;
123
+
124
+ case 'd' :
125
+ if (optarg ) {
126
+ config -> debuglevel = atoi (optarg );
127
+ }
128
+ break ;
129
+
130
+ case 's' :
131
+ config -> log_syslog = 1 ;
132
+ break ;
133
+
134
+ case 'v' :
135
+ fprintf (stdout , "This is WiFiDog version " VERSION "\n" );
136
+ exit (1 );
137
+ break ;
138
+
139
+ case 'x' :
140
+ skiponrestart = 1 ;
141
+ if (optarg ) {
142
+ restart_orig_pid = atoi (optarg );
143
+ } else {
144
+ fprintf (stdout , "The expected PID to the -x switch was not supplied!" );
145
+ exit (1 );
146
+ }
147
+ break ;
148
+
149
+ case 'i' :
150
+ if (optarg ) {
151
+ free (config -> internal_sock );
152
+ config -> internal_sock = safe_strdup (optarg );
153
+ }
154
+ break ;
155
+
156
+ case 'a' :
157
+ if (optarg ) {
158
+ free (config -> arp_table_path );
159
+ config -> arp_table_path = safe_strdup (optarg );
160
+ } else {
161
+ fprintf (stdout , "You must supply the path to the ARP table with -a!" );
162
+ exit (1 );
163
+ }
164
+ break ;
165
+ default :
166
+ usage ();
167
+ exit (1 );
168
+ break ;
169
+
170
+ }
171
+
172
+ if (!skiponrestart ) {
173
+ /* Add it to restartargv */
174
+ safe_asprintf (& (restartargv [i ++ ]), "-%c" , c );
175
+ if (optarg ) {
176
+ restartargv [i ++ ] = safe_strdup (optarg );
177
+ }
178
+ }
179
+
180
+ }
181
+
182
+ /* Finally, we should add the -x, pid and NULL to restartargv
183
+ * HOWEVER we cannot do it here, since this is called before we fork to background
184
+ * so we'll leave this job to gateway.c after forking is completed
185
+ * so that the correct PID is assigned
186
+ *
187
+ * We add 3 nulls, and the first 2 will be overridden later
188
+ */
189
+ restartargv [i ++ ] = NULL ;
190
+ restartargv [i ++ ] = NULL ;
191
+ restartargv [i ++ ] = NULL ;
186
192
}
187
-
0 commit comments