From: Jens Axboe Date: Mon, 5 Sep 2005 17:50:29 +0000 (+0200) Subject: [PATCH] Misc error handling and style fixes X-Git-Tag: blktrace-0.99~205 X-Git-Url: https://git.kernel.dk/?a=commitdiff_plain;h=1f79c4a01e9bd3261529c83bc41e86e40c4321e5;p=blktrace.git [PATCH] Misc error handling and style fixes --- diff --git a/blkparse.c b/blkparse.c index 37f9ae3..4ae8857 100644 --- a/blkparse.c +++ b/blkparse.c @@ -90,7 +90,6 @@ static struct per_cpu_info per_cpu_info[MAX_CPUS]; static unsigned long long events; static int max_cpus; -static int nfiles; static char *dev, *output_name; @@ -325,7 +324,7 @@ static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci) break; default: fprintf(stderr, "Bad fs action %x\n", t->action); - return; + break; } } @@ -513,11 +512,44 @@ static void show_entries_rb(void) } while ((n = rb_next(n)) != NULL); } +static int read_data(int fd, void *buffer, int bytes, int block) +{ + int ret, bytes_left, fl; + void *p; + + fl = fcntl(fd, F_GETFL); + + if (!block) + fcntl(fd, F_SETFL, fl | O_NONBLOCK); + else + fcntl(fd, F_SETFL, fl & ~O_NONBLOCK); + + bytes_left = bytes; + p = buffer; + while (bytes_left > 0) { + ret = read(fd, p, bytes_left); + if (!ret) + return 1; + else if (ret < 0) { + if (errno != EAGAIN) + perror("read"); + return -1; + } else { + p += ret; + bytes_left -= ret; + } + } + + return 0; +} + static int do_file(void) { - int i, ret; + int i, ret, nfiles; - for (max_cpus = 0, i = 0; i < MAX_CPUS; i++, nfiles++, max_cpus++) { + nfiles = 0; + max_cpus = 0; + for (i = 0; i < MAX_CPUS; i++, nfiles++, max_cpus++) { struct per_cpu_info *pci = &per_cpu_info[i]; struct stat st; void *tb; @@ -541,10 +573,8 @@ static int do_file(void) break; } - if (read(pci->fd, tb, st.st_size) != st.st_size) { - fprintf(stderr, "error reading\n"); + if (read_data(pci->fd, tb, st.st_size, 1)) break; - } ret = sort_entries(tb, st.st_size, ~0U); if (ret == -1) @@ -564,37 +594,6 @@ static int do_file(void) return 0; } -static int read_data(int fd, void *buffer, int bytes, int block) -{ - int ret, bytes_left, fl; - void *p; - - fl = fcntl(fd, F_GETFL); - - if (!block) - fcntl(fd, F_SETFL, fl | O_NONBLOCK); - else - fcntl(fd, F_SETFL, fl & ~O_NONBLOCK); - - bytes_left = bytes; - p = buffer; - while (bytes_left > 0) { - ret = read(fd, p, bytes_left); - if (!ret) - return 1; - else if (ret < 0) { - if (errno != EAGAIN) - perror("read"); - return -1; - } else { - p += ret; - bytes_left -= ret; - } - } - - return 0; -} - static void resize_buffer(void **buffer, long *old_size) { long cur_size = *old_size; @@ -655,7 +654,7 @@ static int do_stdin(void) int fd; void *ptr; - fd = dup(0); + fd = dup(STDIN_FILENO); do { int events; @@ -673,7 +672,7 @@ static int do_stdin(void) return 0; } -void flush_output(void) +static void flush_output(void) { int i; @@ -688,12 +687,17 @@ void flush_output(void) } } -void handle_sigint(int sig) +static void handle_sigint(int sig) { done = 1; flush_output(); } +static void usage(char *prog) +{ + fprintf(stderr, "Usage: %s -i [-o ]\n", prog); +} + int main(int argc, char *argv[]) { int c, ret; @@ -707,13 +711,13 @@ int main(int argc, char *argv[]) output_name = strdup(optarg); break; default: - fprintf(stderr, "Usage: %s -i \n", argv[0]); + usage(argv[0]); return 1; } } if (!dev) { - fprintf(stderr, "Usage: %s -i \n", argv[0]); + usage(argv[0]); return 1; } diff --git a/blktrace.c b/blktrace.c index 43837f4..94ed28a 100644 --- a/blktrace.c +++ b/blktrace.c @@ -140,7 +140,7 @@ static int kill_running_trace; static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER; -int find_mask_map(char *string) +static int find_mask_map(char *string) { int i; @@ -155,7 +155,7 @@ static int start_trace(char *dev) { struct blk_user_trace_setup buts; - memset(&buts, sizeof(buts), 0); + memset(&buts, 0, sizeof(buts)); buts.buf_size = BUF_SIZE; buts.buf_nr = BUF_NR; buts.act_mask = act_mask; @@ -308,7 +308,7 @@ static int start_threads(void) ncpus = sysconf(_SC_NPROCESSORS_ONLN); if (ncpus < 0) { fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n"); - return 1; + return 0; } thread_information = malloc(ncpus * sizeof(struct thread_information)); @@ -318,7 +318,7 @@ static int start_threads(void) tip->events_processed = 0; if (!strcmp(output_name, "-")) { - tip->ofd = dup(1); + tip->ofd = dup(STDOUT_FILENO); tip->fd_lock = &stdout_mutex; } else { sprintf(op, "%s_out.%d", output_name, tip->cpu); @@ -327,7 +327,7 @@ static int start_threads(void) if (tip->ofd < 0) { perror(op); - return 1; + return 0; } if (pthread_create(&tip->thread, NULL, extract, tip)) { @@ -355,7 +355,7 @@ static void stop_threads(void) } } -void show_stats(void) +static void show_stats(void) { int i; struct thread_information *tip; @@ -373,7 +373,7 @@ void show_stats(void) printf("Total: %20ld events\n", events_processed); } -void handle_sigint(int sig) +static void handle_sigint(int sig) { done = 1; }