diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2006-11-18 22:03:26 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2006-11-18 22:03:26 +0000 |
commit | 61126ab30a90b74e45a79ccb97074ab71afa6054 (patch) | |
tree | cbc4d021bafef2561cbb37ea0d0c955bfd985401 /networking/udhcp | |
parent | 5a3395bc01cd4b11309595a6ecdaf32f8279f378 (diff) | |
download | busybox-w32-61126ab30a90b74e45a79ccb97074ab71afa6054.tar.gz busybox-w32-61126ab30a90b74e45a79ccb97074ab71afa6054.tar.bz2 busybox-w32-61126ab30a90b74e45a79ccb97074ab71afa6054.zip |
small fixes: using fd-based io instead of FILE*-based,
missed O_TRUNC, etc
Diffstat (limited to 'networking/udhcp')
-rw-r--r-- | networking/udhcp/common.c | 4 | ||||
-rw-r--r-- | networking/udhcp/dumpleases.c | 31 | ||||
-rw-r--r-- | networking/udhcp/files.c | 54 | ||||
-rw-r--r-- | networking/udhcp/pidfile.c | 8 |
4 files changed, 49 insertions, 48 deletions
diff --git a/networking/udhcp/common.c b/networking/udhcp/common.c index 4c18e5d51..3e916f422 100644 --- a/networking/udhcp/common.c +++ b/networking/udhcp/common.c | |||
@@ -29,9 +29,7 @@ long uptime(void) | |||
29 | */ | 29 | */ |
30 | static inline void sanitize_fds(void) | 30 | static inline void sanitize_fds(void) |
31 | { | 31 | { |
32 | int fd = open(bb_dev_null, O_RDWR, 0); | 32 | int fd = xopen(bb_dev_null, O_RDWR); |
33 | if (fd < 0) | ||
34 | return; | ||
35 | while (fd < 3) | 33 | while (fd < 3) |
36 | fd = dup(fd); | 34 | fd = dup(fd); |
37 | close(fd); | 35 | close(fd); |
diff --git a/networking/udhcp/dumpleases.c b/networking/udhcp/dumpleases.c index 4422d3099..a0e81bb13 100644 --- a/networking/udhcp/dumpleases.c +++ b/networking/udhcp/dumpleases.c | |||
@@ -13,9 +13,9 @@ | |||
13 | 13 | ||
14 | int dumpleases_main(int argc, char *argv[]) | 14 | int dumpleases_main(int argc, char *argv[]) |
15 | { | 15 | { |
16 | FILE *fp; | 16 | int fp; |
17 | int i, c, mode = REMAINING; | 17 | int i, c, mode = REMAINING; |
18 | long expires; | 18 | unsigned long expires; |
19 | const char *file = LEASES_FILE; | 19 | const char *file = LEASES_FILE; |
20 | struct dhcpOfferedAddr lease; | 20 | struct dhcpOfferedAddr lease; |
21 | struct in_addr addr; | 21 | struct in_addr addr; |
@@ -43,11 +43,11 @@ int dumpleases_main(int argc, char *argv[]) | |||
43 | } | 43 | } |
44 | } | 44 | } |
45 | 45 | ||
46 | fp = xfopen(file, "r"); | 46 | fp = xopen(file, O_RDONLY); |
47 | 47 | ||
48 | printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at"); | 48 | printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at"); |
49 | /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */ | 49 | /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */ |
50 | while (fread(&lease, sizeof(lease), 1, fp)) { | 50 | while (full_read(fp, &lease, sizeof(lease)) == sizeof(lease)) { |
51 | printf(":%02x"+1, lease.chaddr[0]); | 51 | printf(":%02x"+1, lease.chaddr[0]); |
52 | for (i = 1; i < 6; i++) { | 52 | for (i = 1; i < 6; i++) { |
53 | printf(":%02x", lease.chaddr[i]); | 53 | printf(":%02x", lease.chaddr[i]); |
@@ -59,23 +59,16 @@ int dumpleases_main(int argc, char *argv[]) | |||
59 | if (!expires) | 59 | if (!expires) |
60 | printf("expired\n"); | 60 | printf("expired\n"); |
61 | else { | 61 | else { |
62 | if (expires > 60*60*24) { | 62 | unsigned d, h, m; |
63 | printf("%ld days, ", expires / (60*60*24)); | 63 | d = expires / (24*60*60); expires %= (24*60*60); |
64 | expires %= 60*60*24; | 64 | h = expires / (60*60); expires %= (60*60); |
65 | } | 65 | m = expires / 60; expires %= 60; |
66 | if (expires > 60*60) { | 66 | if (d) printf("%u days ", d); |
67 | printf("%ld hours, ", expires / (60*60)); | 67 | printf("%02u:%02u:%02u\n", h, m, (unsigned)expires); |
68 | expires %= 60*60; | ||
69 | } | ||
70 | if (expires > 60) { | ||
71 | printf("%ld minutes, ", expires / 60); | ||
72 | expires %= 60; | ||
73 | } | ||
74 | printf("%ld seconds\n", expires); | ||
75 | } | 68 | } |
76 | } else printf("%s", ctime(&expires)); | 69 | } else fputs(ctime(&expires), stdout); |
77 | } | 70 | } |
78 | fclose(fp); | 71 | /* close(fp); */ |
79 | 72 | ||
80 | return 0; | 73 | return 0; |
81 | } | 74 | } |
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 | ||
312 | void write_leases(void) | 317 | void 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 | ||
352 | void read_leases(const char *file) | 359 | void 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 | } |
diff --git a/networking/udhcp/pidfile.c b/networking/udhcp/pidfile.c index 8d00490af..bcb2608c5 100644 --- a/networking/udhcp/pidfile.c +++ b/networking/udhcp/pidfile.c | |||
@@ -23,7 +23,7 @@ | |||
23 | #include "common.h" | 23 | #include "common.h" |
24 | 24 | ||
25 | 25 | ||
26 | static char *saved_pidfile; | 26 | static const char *saved_pidfile; |
27 | 27 | ||
28 | static void pidfile_delete(void) | 28 | static void pidfile_delete(void) |
29 | { | 29 | { |
@@ -36,14 +36,14 @@ int pidfile_acquire(const char *pidfile) | |||
36 | int pid_fd; | 36 | int pid_fd; |
37 | if (!pidfile) return -1; | 37 | if (!pidfile) return -1; |
38 | 38 | ||
39 | pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644); | 39 | pid_fd = open(pidfile, O_CREAT|O_WRONLY|O_TRUNC, 0644); |
40 | if (pid_fd < 0) { | 40 | if (pid_fd < 0) { |
41 | bb_perror_msg("unable to open pidfile %s", pidfile); | 41 | bb_perror_msg("cannot open pidfile %s", pidfile); |
42 | } else { | 42 | } else { |
43 | lockf(pid_fd, F_LOCK, 0); | 43 | lockf(pid_fd, F_LOCK, 0); |
44 | if (!saved_pidfile) | 44 | if (!saved_pidfile) |
45 | atexit(pidfile_delete); | 45 | atexit(pidfile_delete); |
46 | saved_pidfile = (char *) pidfile; | 46 | saved_pidfile = pidfile; |
47 | } | 47 | } |
48 | 48 | ||
49 | return pid_fd; | 49 | return pid_fd; |