summaryrefslogtreecommitdiff
path: root/networking/udhcp/files.c
diff options
context:
space:
mode:
Diffstat (limited to 'networking/udhcp/files.c')
-rw-r--r--networking/udhcp/files.c54
1 files changed, 32 insertions, 22 deletions
diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c
index 317e861c0..2c2c5422c 100644
--- a/networking/udhcp/files.c
+++ b/networking/udhcp/files.c
@@ -272,27 +272,32 @@ int read_config(const char *file)
272 if (keywords[i].def[0]) 272 if (keywords[i].def[0])
273 keywords[i].handler(keywords[i].def, keywords[i].var); 273 keywords[i].handler(keywords[i].def, keywords[i].var);
274 274
275 if (!(in = fopen(file, "r"))) { 275 in = fopen(file, "r");
276 bb_error_msg("unable to open config file: %s", file); 276 if (!in) {
277 bb_error_msg("cannot open config file: %s", file);
277 return 0; 278 return 0;
278 } 279 }
279 280
280 while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) { 281 while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
281 char debug_orig[READ_CONFIG_BUF_SIZE]; 282 char debug_orig[READ_CONFIG_BUF_SIZE];
283 char *p;
282 284
283 lm++; 285 lm++;
284 if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0'; 286 p = strchr(buffer, '\n');
287 if (p) *p = '\0';
285 if (ENABLE_FEATURE_UDHCP_DEBUG) strcpy(debug_orig, buffer); 288 if (ENABLE_FEATURE_UDHCP_DEBUG) strcpy(debug_orig, buffer);
286 if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0'; 289 p = strchr(buffer, '#');
290 if (p) *p = '\0';
287 291
288 if (!(token = strtok(buffer, " \t"))) continue; 292 if (!(token = strtok(buffer, " \t"))) continue;
289 if (!(line = strtok(NULL, ""))) continue; 293 if (!(line = strtok(NULL, ""))) continue;
290 294
291 /* eat leading whitespace */ 295 /* eat leading whitespace */
292 line = line + strspn(line, " \t="); 296 line = skip_whitespace(line);
293 /* eat trailing whitespace */ 297 /* eat trailing whitespace */
294 for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--); 298 i = strlen(line) - 1;
295 line[i] = '\0'; 299 while (i >= 0 && isspace(line[i]))
300 line[i--] = '\0';
296 301
297 for (i = 0; keywords[i].keyword[0]; i++) 302 for (i = 0; keywords[i].keyword[0]; i++)
298 if (!strcasecmp(token, keywords[i].keyword)) 303 if (!strcasecmp(token, keywords[i].keyword))
@@ -311,14 +316,14 @@ int read_config(const char *file)
311 316
312void write_leases(void) 317void write_leases(void)
313{ 318{
314 FILE *fp; 319 int fp;
315 unsigned int i; 320 unsigned i;
316 char buf[255];
317 time_t curr = time(0); 321 time_t curr = time(0);
318 unsigned long tmp_time; 322 unsigned long tmp_time;
319 323
320 if (!(fp = fopen(server_config.lease_file, "w"))) { 324 fp = open(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
321 bb_error_msg("unable to open %s for writing", server_config.lease_file); 325 if (fp < 0) {
326 bb_error_msg("cannot open %s for writing", server_config.lease_file);
322 return; 327 return;
323 } 328 }
324 329
@@ -334,33 +339,38 @@ void write_leases(void)
334 else leases[i].expires -= curr; 339 else leases[i].expires -= curr;
335 } /* else stick with the time we got */ 340 } /* else stick with the time we got */
336 leases[i].expires = htonl(leases[i].expires); 341 leases[i].expires = htonl(leases[i].expires);
337 fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp); 342 // FIXME: error check??
343 full_write(fp, &leases[i], sizeof(leases[i]));
338 344
339 /* Then restore it when done. */ 345 /* then restore it when done */
340 leases[i].expires = tmp_time; 346 leases[i].expires = tmp_time;
341 } 347 }
342 } 348 }
343 fclose(fp); 349 close(fp);
344 350
345 if (server_config.notify_file) { 351 if (server_config.notify_file) {
346 sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file); 352 char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
347 system(buf); 353 system(cmd);
354 free(cmd);
348 } 355 }
349} 356}
350 357
351 358
352void read_leases(const char *file) 359void read_leases(const char *file)
353{ 360{
354 FILE *fp; 361 int fp;
355 unsigned int i = 0; 362 unsigned int i = 0;
356 struct dhcpOfferedAddr lease; 363 struct dhcpOfferedAddr lease;
357 364
358 if (!(fp = fopen(file, "r"))) { 365 fp = open(file, O_RDONLY);
359 bb_error_msg("unable to open %s for reading", file); 366 if (fp < 0) {
367 bb_error_msg("cannot open %s for reading", file);
360 return; 368 return;
361 } 369 }
362 370
363 while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) { 371 while (i < server_config.max_leases
372 && full_read(fp, &lease, sizeof(lease)) == sizeof(lease)
373 ) {
364 /* ADDME: is it a static lease */ 374 /* ADDME: is it a static lease */
365 if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) { 375 if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) {
366 lease.expires = ntohl(lease.expires); 376 lease.expires = ntohl(lease.expires);
@@ -373,5 +383,5 @@ void read_leases(const char *file)
373 } 383 }
374 } 384 }
375 DEBUG("Read %d leases", i); 385 DEBUG("Read %d leases", i);
376 fclose(fp); 386 close(fp);
377} 387}