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 | |
parent | 5a3395bc01cd4b11309595a6ecdaf32f8279f378 (diff) |
small fixes: using fd-based io instead of FILE*-based,
missed O_TRUNC, etc
Diffstat (limited to 'networking')
-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) */ static inline void sanitize_fds(void) { - int fd = open(bb_dev_null, O_RDWR, 0); - if (fd < 0) - return; + int fd = xopen(bb_dev_null, O_RDWR); while (fd < 3) fd = dup(fd); 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 @@ int dumpleases_main(int argc, char *argv[]) { - FILE *fp; + int fp; int i, c, mode = REMAINING; - long expires; + unsigned long expires; const char *file = LEASES_FILE; struct dhcpOfferedAddr lease; struct in_addr addr; @@ -43,11 +43,11 @@ int dumpleases_main(int argc, char *argv[]) } } - fp = xfopen(file, "r"); + fp = xopen(file, O_RDONLY); printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at"); /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */ - while (fread(&lease, sizeof(lease), 1, fp)) { + while (full_read(fp, &lease, sizeof(lease)) == sizeof(lease)) { printf(":%02x"+1, lease.chaddr[0]); for (i = 1; i < 6; i++) { printf(":%02x", lease.chaddr[i]); @@ -59,23 +59,16 @@ int dumpleases_main(int argc, char *argv[]) if (!expires) printf("expired\n"); else { - if (expires > 60*60*24) { - printf("%ld days, ", expires / (60*60*24)); - expires %= 60*60*24; - } - if (expires > 60*60) { - printf("%ld hours, ", expires / (60*60)); - expires %= 60*60; - } - if (expires > 60) { - printf("%ld minutes, ", expires / 60); - expires %= 60; - } - printf("%ld seconds\n", expires); + unsigned d, h, m; + d = expires / (24*60*60); expires %= (24*60*60); + h = expires / (60*60); expires %= (60*60); + m = expires / 60; expires %= 60; + if (d) printf("%u days ", d); + printf("%02u:%02u:%02u\n", h, m, (unsigned)expires); } - } else printf("%s", ctime(&expires)); + } else fputs(ctime(&expires), stdout); } - fclose(fp); + /* close(fp); */ return 0; } 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) if (keywords[i].def[0]) keywords[i].handler(keywords[i].def, keywords[i].var); - if (!(in = fopen(file, "r"))) { - bb_error_msg("unable to open config file: %s", file); + in = fopen(file, "r"); + if (!in) { + bb_error_msg("cannot open config file: %s", file); return 0; } while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) { char debug_orig[READ_CONFIG_BUF_SIZE]; + char *p; lm++; - if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0'; + p = strchr(buffer, '\n'); + if (p) *p = '\0'; if (ENABLE_FEATURE_UDHCP_DEBUG) strcpy(debug_orig, buffer); - if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0'; + p = strchr(buffer, '#'); + if (p) *p = '\0'; if (!(token = strtok(buffer, " \t"))) continue; if (!(line = strtok(NULL, ""))) continue; /* eat leading whitespace */ - line = line + strspn(line, " \t="); + line = skip_whitespace(line); /* eat trailing whitespace */ - for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--); - line[i] = '\0'; + i = strlen(line) - 1; + while (i >= 0 && isspace(line[i])) + line[i--] = '\0'; for (i = 0; keywords[i].keyword[0]; i++) if (!strcasecmp(token, keywords[i].keyword)) @@ -311,14 +316,14 @@ int read_config(const char *file) void write_leases(void) { - FILE *fp; - unsigned int i; - char buf[255]; + int fp; + unsigned i; time_t curr = time(0); unsigned long tmp_time; - if (!(fp = fopen(server_config.lease_file, "w"))) { - bb_error_msg("unable to open %s for writing", server_config.lease_file); + fp = open(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC, 0666); + if (fp < 0) { + bb_error_msg("cannot open %s for writing", server_config.lease_file); return; } @@ -334,33 +339,38 @@ void write_leases(void) else leases[i].expires -= curr; } /* else stick with the time we got */ leases[i].expires = htonl(leases[i].expires); - fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp); + // FIXME: error check?? + full_write(fp, &leases[i], sizeof(leases[i])); - /* Then restore it when done. */ + /* then restore it when done */ leases[i].expires = tmp_time; } } - fclose(fp); + close(fp); if (server_config.notify_file) { - sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file); - system(buf); + char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file); + system(cmd); + free(cmd); } } void read_leases(const char *file) { - FILE *fp; + int fp; unsigned int i = 0; struct dhcpOfferedAddr lease; - if (!(fp = fopen(file, "r"))) { - bb_error_msg("unable to open %s for reading", file); + fp = open(file, O_RDONLY); + if (fp < 0) { + bb_error_msg("cannot open %s for reading", file); return; } - while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) { + while (i < server_config.max_leases + && full_read(fp, &lease, sizeof(lease)) == sizeof(lease) + ) { /* ADDME: is it a static lease */ if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) { lease.expires = ntohl(lease.expires); @@ -373,5 +383,5 @@ void read_leases(const char *file) } } DEBUG("Read %d leases", i); - fclose(fp); + close(fp); } 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 @@ #include "common.h" -static char *saved_pidfile; +static const char *saved_pidfile; static void pidfile_delete(void) { @@ -36,14 +36,14 @@ int pidfile_acquire(const char *pidfile) int pid_fd; if (!pidfile) return -1; - pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644); + pid_fd = open(pidfile, O_CREAT|O_WRONLY|O_TRUNC, 0644); if (pid_fd < 0) { - bb_perror_msg("unable to open pidfile %s", pidfile); + bb_perror_msg("cannot open pidfile %s", pidfile); } else { lockf(pid_fd, F_LOCK, 0); if (!saved_pidfile) atexit(pidfile_delete); - saved_pidfile = (char *) pidfile; + saved_pidfile = pidfile; } return pid_fd; |