From: Denis Vlasenko Date: Sat, 18 Nov 2006 22:03:26 +0000 (-0000) Subject: small fixes: using fd-based io instead of FILE*-based, X-Git-Tag: 1_4_0~388 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=61126ab30a90b74e45a79ccb97074ab71afa6054;p=oweals%2Fbusybox.git small fixes: using fd-based io instead of FILE*-based, missed O_TRUNC, etc --- diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c index a9f82c5cc..6d3877a7c 100644 --- a/debianutils/start_stop_daemon.c +++ b/debianutils/start_stop_daemon.c @@ -40,7 +40,8 @@ static inline void push(pid_t pid) static int pid_is_exec(pid_t pid, const char *name) { - char buf[32], *execbuf; + char buf[sizeof("/proc//exe") + sizeof(int)*3]; + char *execbuf; int equal; sprintf(buf, "/proc/%d/exe", pid); @@ -56,7 +57,7 @@ static int pid_is_exec(pid_t pid, const char *name) static int pid_is_user(int pid, int uid) { struct stat sb; - char buf[32]; + char buf[sizeof("/proc/") + sizeof(int)*3]; sprintf(buf, "/proc/%d", pid); if (stat(buf, &sb) != 0) @@ -66,7 +67,7 @@ static int pid_is_user(int pid, int uid) static int pid_is_cmd(pid_t pid, const char *name) { - char buf[32]; + char buf[sizeof("/proc//stat") + sizeof(int)*3]; FILE *f; int c; @@ -115,7 +116,6 @@ static void do_pidfile(void) fclose(f); } else if (errno != ENOENT) bb_perror_msg_and_die("open pidfile %s", pidfile); - } static void do_procinit(void) @@ -146,28 +146,28 @@ static void do_procinit(void) static int do_stop(void) { - RESERVE_CONFIG_BUFFER(what, 1024); + char *what; struct pid_list *p; int killed = 0; do_procinit(); if (cmdname) - strcpy(what, cmdname); + what = xstrdup(cmdname); else if (execname) - strcpy(what, execname); + what = xstrdup(execname); else if (pidfile) - sprintf(what, "process in pidfile `%.200s'", pidfile); + what = xasprintf("process in pidfile '%s'", pidfile); else if (userspec) - sprintf(what, "process(es) owned by `%s'", userspec); + what = xasprintf("process(es) owned by '%s'", userspec); else - bb_error_msg_and_die ("internal error, please report"); + bb_error_msg_and_die("internal error, please report"); if (!found) { if (!quiet) - printf("no %s found; none killed.\n", what); + printf("no %s found; none killed\n", what); if (ENABLE_FEATURE_CLEAN_UP) - RELEASE_CONFIG_BUFFER(what); + free(what); return -1; } for (p = found; p; p = p->next) { @@ -183,10 +183,10 @@ static int do_stop(void) for (p = found; p; p = p->next) if(p->pid < 0) printf(" %d", -p->pid); - printf(").\n"); + puts(")"); } if (ENABLE_FEATURE_CLEAN_UP) - RELEASE_CONFIG_BUFFER(what); + free(what); return killed; } 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;