|
40 | 40 | * Holds an argv that could be passed to exec*() if we restart ourselves
|
41 | 41 | */
|
42 | 42 | /* Declare variable */
|
43 |
| -extern char ** restartargv; |
| 43 | +extern char **restartargv; |
44 | 44 | /* Define variable */
|
45 |
| -char ** restartargv = NULL; |
| 45 | +char **restartargv = NULL; |
46 | 46 |
|
47 | 47 | static void usage(void);
|
48 | 48 |
|
@@ -73,115 +73,117 @@ usage(void)
|
73 | 73 | fprintf(stdout, " -w <path> Wdctl socket path\n");
|
74 | 74 | fprintf(stdout, " -h Print usage\n");
|
75 | 75 | 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"); |
| 76 | + fprintf(stdout, |
| 77 | + " -x pid Used internally by WiFiDog when re-starting itself *DO NOT ISSUE THIS SWITCH MANUAlLY*\n"); |
77 | 78 | fprintf(stdout, " -i <path> Internal socket path used when re-starting self\n");
|
78 | 79 | fprintf(stdout, "\n");
|
79 | 80 | }
|
80 | 81 |
|
81 | 82 | /** Uses getopt() to parse the command line and set configuration values
|
82 | 83 | * also populates restartargv
|
83 | 84 | */
|
84 |
| -void parse_commandline(int argc, char **argv) { |
85 |
| - int c; |
86 |
| - int skiponrestart; |
87 |
| - int i; |
| 85 | +void |
| 86 | +parse_commandline(int argc, char **argv) |
| 87 | +{ |
| 88 | + int c; |
| 89 | + int skiponrestart; |
| 90 | + int i; |
88 | 91 |
|
89 | 92 | s_config *config = config_get_config();
|
90 | 93 |
|
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]); |
| 94 | + //MAGIC 3: Our own -x, the pid, and NULL : |
| 95 | + restartargv = safe_malloc((size_t) (argc + 3) * sizeof(char *)); |
| 96 | + i = 0; |
| 97 | + restartargv[i++] = safe_strdup(argv[0]); |
95 | 98 |
|
96 | 99 | while (-1 != (c = getopt(argc, argv, "c:hfd:sw:vx:i:"))) {
|
97 | 100 |
|
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; |
| 101 | + skiponrestart = 0; |
| 102 | + |
| 103 | + switch (c) { |
| 104 | + |
| 105 | + case 'h': |
| 106 | + usage(); |
| 107 | + exit(1); |
| 108 | + break; |
| 109 | + |
| 110 | + case 'c': |
| 111 | + if (optarg) { |
| 112 | + free(config->configfile); |
| 113 | + config->configfile = safe_strdup(optarg); |
| 114 | + } |
| 115 | + break; |
| 116 | + |
| 117 | + case 'w': |
| 118 | + if (optarg) { |
| 119 | + free(config->wdctl_sock); |
| 120 | + config->wdctl_sock = safe_strdup(optarg); |
| 121 | + } |
| 122 | + break; |
| 123 | + |
| 124 | + case 'f': |
| 125 | + skiponrestart = 1; |
| 126 | + config->daemon = 0; |
| 127 | + break; |
| 128 | + |
| 129 | + case 'd': |
| 130 | + if (optarg) { |
| 131 | + config->debuglevel = atoi(optarg); |
| 132 | + } |
| 133 | + break; |
| 134 | + |
| 135 | + case 's': |
| 136 | + config->log_syslog = 1; |
| 137 | + break; |
| 138 | + |
| 139 | + case 'v': |
| 140 | + fprintf(stdout, "This is WiFiDog version " VERSION "\n"); |
| 141 | + exit(1); |
| 142 | + break; |
| 143 | + |
| 144 | + case 'x': |
| 145 | + skiponrestart = 1; |
| 146 | + if (optarg) { |
| 147 | + restart_orig_pid = atoi(optarg); |
| 148 | + } else { |
| 149 | + fprintf(stdout, "The expected PID to the -x switch was not supplied!"); |
| 150 | + exit(1); |
| 151 | + } |
| 152 | + break; |
| 153 | + |
| 154 | + case 'i': |
| 155 | + if (optarg) { |
| 156 | + free(config->internal_sock); |
| 157 | + config->internal_sock = safe_strdup(optarg); |
| 158 | + } |
| 159 | + break; |
| 160 | + |
| 161 | + default: |
| 162 | + usage(); |
| 163 | + exit(1); |
| 164 | + break; |
| 165 | + |
| 166 | + } |
| 167 | + |
| 168 | + if (!skiponrestart) { |
| 169 | + /* Add it to restartargv */ |
| 170 | + safe_asprintf(&(restartargv[i++]), "-%c", c); |
| 171 | + if (optarg) { |
| 172 | + restartargv[i++] = safe_strdup(optarg); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + } |
| 177 | + |
| 178 | + /* Finally, we should add the -x, pid and NULL to restartargv |
| 179 | + * HOWEVER we cannot do it here, since this is called before we fork to background |
| 180 | + * so we'll leave this job to gateway.c after forking is completed |
| 181 | + * so that the correct PID is assigned |
| 182 | + * |
| 183 | + * We add 3 nulls, and the first 2 will be overridden later |
| 184 | + */ |
| 185 | + restartargv[i++] = NULL; |
| 186 | + restartargv[i++] = NULL; |
| 187 | + restartargv[i++] = NULL; |
185 | 188 |
|
186 | 189 | }
|
187 |
| - |
|
0 commit comments