From 964427e0ea7f92f694a9ea097fe466d71ba3a83e Mon Sep 17 00:00:00 2001 From: David van Moolenbroek Date: Sat, 19 Sep 2015 22:31:13 +0000 Subject: [PATCH] btrace(8): move to proper location - move from minix/commands to minix/usr.sbin; - install into /usr/sbin instead of /usr/bin; - move manual page into source directory; - resolve compilation warning; - convert to KNF. Change-Id: I08c16998bd499a468799a6587f6fe45f42590461 --- distrib/sets/lists/minix/mi | 3 +- minix/commands/Makefile | 2 +- minix/commands/btrace/btrace.c | 219 ------------------ minix/man/man8/Makefile | 2 +- minix/usr.sbin/Makefile | 1 + minix/{commands => usr.sbin}/btrace/Makefile | 2 +- minix/{man/man8 => usr.sbin/btrace}/btrace.8 | 0 minix/usr.sbin/btrace/btrace.c | 227 +++++++++++++++++++ 8 files changed, 233 insertions(+), 223 deletions(-) delete mode 100644 minix/commands/btrace/btrace.c rename minix/{commands => usr.sbin}/btrace/Makefile (72%) rename minix/{man/man8 => usr.sbin/btrace}/btrace.8 (100%) create mode 100644 minix/usr.sbin/btrace/btrace.c diff --git a/distrib/sets/lists/minix/mi b/distrib/sets/lists/minix/mi index 8ab16714b..66f9b556a 100644 --- a/distrib/sets/lists/minix/mi +++ b/distrib/sets/lists/minix/mi @@ -249,7 +249,7 @@ ./usr/bin/bdes minix-sys ./usr/bin/bsdtar minix-sys ./usr/bin/bsfilt minix-sys -./usr/bin/btrace minix-sys +./usr/bin/btrace minix-sys obsolete ./usr/bin/bunzip2 minix-sys ./usr/bin/bzcat minix-sys ./usr/bin/bzip2 minix-sys @@ -5308,6 +5308,7 @@ ./usr/preserve minix-sys ./usr/run minix-sys ./usr/sbin minix-sys +./usr/sbin/btrace minix-sys ./usr/sbin/chown minix-sys ./usr/sbin/chroot minix-sys ./usr/sbin/group minix-sys diff --git a/minix/commands/Makefile b/minix/commands/Makefile index 48716f071..b8093d8d8 100644 --- a/minix/commands/Makefile +++ b/minix/commands/Makefile @@ -2,7 +2,7 @@ .include -SUBDIR= add_route arp at backup btrace \ +SUBDIR= add_route arp at backup \ cawf cdprobe \ ci cleantmp co \ compress crc cron crontab \ diff --git a/minix/commands/btrace/btrace.c b/minix/commands/btrace/btrace.c deleted file mode 100644 index 6e9451adb..000000000 --- a/minix/commands/btrace/btrace.c +++ /dev/null @@ -1,219 +0,0 @@ -/* Block trace command line tool */ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -static btrace_entry buf[BTBUF_SIZE]; - -static void usage(char *name) -{ - printf("usage:\n" - "%s start \n" - "%s stop \n" - "%s reset \n" - "%s dump \n", - name, name, name, name); - - exit(EXIT_FAILURE); -} - -static void btrace_start(char *device, int nr_entries) -{ - int r, ctl, devfd; - size_t size; - - if ((devfd = open(device, O_RDONLY)) < 0) { - perror("device open"); - exit(EXIT_FAILURE); - } - - size = nr_entries; - if ((r = ioctl(devfd, BIOCTRACEBUF, &size)) < 0) { - perror("ioctl(BIOCTRACEBUF)"); - exit(EXIT_FAILURE); - } - - ctl = BTCTL_START; - if ((r = ioctl(devfd, BIOCTRACECTL, &ctl)) < 0) { - perror("ioctl(BIOCTRACECTL)"); - - size = 0; - (void) ioctl(devfd, BIOCTRACEBUF, &size); - - exit(EXIT_FAILURE); - } - - close(devfd); -} - -static void btrace_stop(char *device, char *file) -{ - int r, ctl, devfd, outfd; - size_t size; - - if ((devfd = open(device, O_RDONLY)) < 0) { - perror("device open"); - exit(EXIT_FAILURE); - } - - if ((outfd = open(file, O_CREAT|O_TRUNC|O_WRONLY, 0600)) < 0) { - perror("file open"); - exit(EXIT_FAILURE); - } - - ctl = BTCTL_STOP; - if ((r = ioctl(devfd, BIOCTRACECTL, &ctl)) < 0) { - perror("ioctl(BIOCTRACECTL)"); - exit(EXIT_FAILURE); - } - - for (;;) { - if ((r = ioctl(devfd, BIOCTRACEGET, buf)) < 0) { - perror("ioctl(BIOCTRACEGET)"); - break; - } - - if (r == 0) break; - - size = r * sizeof(buf[0]); - if ((r = write(outfd, (char *) buf, size)) != size) { - if (r < 0) perror("write"); - else fputs("short write\n", stderr); - } - } - - close(outfd); - - size = 0; - if ((r = ioctl(devfd, BIOCTRACEBUF, &size)) < 0) { - perror("ioctl(BIOCTRACEBUF)"); - exit(EXIT_FAILURE); - } - - close(devfd); -} - -static void btrace_reset(char *device) -{ - size_t size; - int r, ctl, devfd; - - if ((devfd = open(device, O_RDONLY)) < 0) { - perror("device open"); - exit(EXIT_FAILURE); - } - - ctl = BTCTL_STOP; - (void) ioctl(devfd, BIOCTRACECTL, &ctl); - - size = 0; - if ((r = ioctl(devfd, BIOCTRACEBUF, &size)) < 0) { - perror("ioctl(BIOCTRACEBUF)"); - exit(EXIT_FAILURE); - } - - close(devfd); -} - -static void dump_entry(btrace_entry *entry) -{ - switch (entry->request) { - case BTREQ_OPEN: printf("OPEN"); break; - case BTREQ_CLOSE: printf("CLOSE"); break; - case BTREQ_READ: printf("READ"); break; - case BTREQ_WRITE: printf("WRITE"); break; - case BTREQ_GATHER: printf("GATHER"); break; - case BTREQ_SCATTER: printf("SCATTER"); break; - case BTREQ_IOCTL: printf("IOCTL"); break; - } - - printf(" request\n"); - - switch (entry->request) { - case BTREQ_OPEN: - printf("- access:\t%x\n", entry->size); - break; - case BTREQ_READ: - case BTREQ_WRITE: - case BTREQ_GATHER: - case BTREQ_SCATTER: - printf("- position:\t%08lx%08lx\n", - ex64hi(entry->position), ex64lo(entry->position)); - printf("- size:\t\t%u\n", entry->size); - printf("- flags:\t%x\n", entry->flags); - break; - case BTREQ_IOCTL: - printf("- request:\t%08x\n", entry->size); - break; - } - - printf("- start:\t%u us\n", entry->start_time); - printf("- finish:\t%u us\n", entry->finish_time); - if (entry->result == BTRES_INPROGRESS) - printf("- result:\t(in progress)\n"); - else - printf("- result:\t%d\n", entry->result); - printf("\n"); -} - -static void btrace_dump(char *file) -{ - int i, r, infd; - - if ((infd = open(file, O_RDONLY)) < 0) { - perror("open"); - exit(EXIT_FAILURE); - } - - for (;;) { - if ((r = read(infd, (char *) buf, sizeof(buf))) <= 0) - break; - - r /= sizeof(buf[0]); - - for (i = 0; i < r; i++) - dump_entry(&buf[i]); - } - - if (r < 0) perror("read"); - - close(infd); -} - -int main(int argc, char **argv) -{ - int num; - char *name = argv[0]; - - if (argc < 3) usage(name); - - if (!strcmp(argv[1], "start")) { - if (argc < 4) usage(name); - - num = atoi(argv[3]); - - if (num <= 0) usage(name); - - btrace_start(argv[2], num); - } - else if (!strcmp(argv[1], "stop")) { - if (argc < 4) usage(name); - - btrace_stop(argv[2], argv[3]); - } - else if (!strcmp(argv[1], "reset")) { - btrace_reset(argv[2]); - } - else if (!strcmp(argv[1], "dump")) { - btrace_dump(argv[2]); - } - else usage(name); - - return EXIT_SUCCESS; -} diff --git a/minix/man/man8/Makefile b/minix/man/man8/Makefile index 38d8d5cbd..4cdb186d2 100644 --- a/minix/man/man8/Makefile +++ b/minix/man/man8/Makefile @@ -1,4 +1,4 @@ -MAN= add_route.8 backup.8 boot.8 btrace.8 \ +MAN= add_route.8 backup.8 boot.8 \ cdprobe.8 cleantmp.8 config.8 cron.8 \ dhcpd.8 diskctl.8 fbdctl.8 fdisk.8 \ getty.8 halt.8 hgfs.8 httpd.8 ifconfig.8 inet.8 init.8 \ diff --git a/minix/usr.sbin/Makefile b/minix/usr.sbin/Makefile index 858722d6d..006b48b20 100644 --- a/minix/usr.sbin/Makefile +++ b/minix/usr.sbin/Makefile @@ -1,5 +1,6 @@ .include +SUBDIR+= btrace SUBDIR+= mkfs.mfs SUBDIR+= mkproto diff --git a/minix/commands/btrace/Makefile b/minix/usr.sbin/btrace/Makefile similarity index 72% rename from minix/commands/btrace/Makefile rename to minix/usr.sbin/btrace/Makefile index 7daeefeab..4484adcc8 100644 --- a/minix/commands/btrace/Makefile +++ b/minix/usr.sbin/btrace/Makefile @@ -1,4 +1,4 @@ PROG= btrace -MAN= +MAN= btrace.8 .include diff --git a/minix/man/man8/btrace.8 b/minix/usr.sbin/btrace/btrace.8 similarity index 100% rename from minix/man/man8/btrace.8 rename to minix/usr.sbin/btrace/btrace.8 diff --git a/minix/usr.sbin/btrace/btrace.c b/minix/usr.sbin/btrace/btrace.c new file mode 100644 index 000000000..6b0514220 --- /dev/null +++ b/minix/usr.sbin/btrace/btrace.c @@ -0,0 +1,227 @@ +/* Block trace command line tool - by D.C. van Moolenbroek */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static btrace_entry buf[BTBUF_SIZE]; + +static void __dead +usage(void) +{ + fprintf(stderr, "usage:\n" + "%s start \n" + "%s stop \n" + "%s reset \n" + "%s dump \n", + getprogname(), getprogname(), getprogname(), getprogname()); + + exit(EXIT_FAILURE); +} + +static void +btrace_start(char * device, int nr_entries) +{ + int r, ctl, devfd; + size_t size; + + if ((devfd = open(device, O_RDONLY)) < 0) { + perror("device open"); + exit(EXIT_FAILURE); + } + + size = nr_entries; + if ((r = ioctl(devfd, BIOCTRACEBUF, &size)) < 0) { + perror("ioctl(BIOCTRACEBUF)"); + exit(EXIT_FAILURE); + } + + ctl = BTCTL_START; + if ((r = ioctl(devfd, BIOCTRACECTL, &ctl)) < 0) { + perror("ioctl(BIOCTRACECTL)"); + + size = 0; + (void)ioctl(devfd, BIOCTRACEBUF, &size); + + exit(EXIT_FAILURE); + } + + close(devfd); +} + +static void +btrace_stop(char * device, char * file) +{ + int r, ctl, devfd, outfd; + size_t size; + + if ((devfd = open(device, O_RDONLY)) < 0) { + perror("device open"); + exit(EXIT_FAILURE); + } + + if ((outfd = open(file, O_CREAT | O_TRUNC | O_WRONLY, 0600)) < 0) { + perror("file open"); + exit(EXIT_FAILURE); + } + + ctl = BTCTL_STOP; + if ((r = ioctl(devfd, BIOCTRACECTL, &ctl)) < 0) { + perror("ioctl(BIOCTRACECTL)"); + exit(EXIT_FAILURE); + } + + for (;;) { + if ((r = ioctl(devfd, BIOCTRACEGET, buf)) < 0) { + perror("ioctl(BIOCTRACEGET)"); + break; + } + + if (r == 0) break; + + size = r * sizeof(buf[0]); + if ((r = write(outfd, (char *)buf, size)) != size) { + if (r < 0) perror("write"); + else fputs("short write\n", stderr); + } + } + + close(outfd); + + size = 0; + if ((r = ioctl(devfd, BIOCTRACEBUF, &size)) < 0) { + perror("ioctl(BIOCTRACEBUF)"); + exit(EXIT_FAILURE); + } + + close(devfd); +} + +static void +btrace_reset(char * device) +{ + size_t size; + int r, ctl, devfd; + + if ((devfd = open(device, O_RDONLY)) < 0) { + perror("device open"); + exit(EXIT_FAILURE); + } + + ctl = BTCTL_STOP; + (void)ioctl(devfd, BIOCTRACECTL, &ctl); + + size = 0; + if ((r = ioctl(devfd, BIOCTRACEBUF, &size)) < 0) { + perror("ioctl(BIOCTRACEBUF)"); + exit(EXIT_FAILURE); + } + + close(devfd); +} + +static void +dump_entry(btrace_entry * entry) +{ + switch (entry->request) { + case BTREQ_OPEN: printf("OPEN"); break; + case BTREQ_CLOSE: printf("CLOSE"); break; + case BTREQ_READ: printf("READ"); break; + case BTREQ_WRITE: printf("WRITE"); break; + case BTREQ_GATHER: printf("GATHER"); break; + case BTREQ_SCATTER: printf("SCATTER"); break; + case BTREQ_IOCTL: printf("IOCTL"); break; + } + + printf(" request\n"); + + switch (entry->request) { + case BTREQ_OPEN: + printf("- access:\t%x\n", entry->size); + break; + case BTREQ_READ: + case BTREQ_WRITE: + case BTREQ_GATHER: + case BTREQ_SCATTER: + printf("- position:\t%08lx%08lx\n", + ex64hi(entry->position), ex64lo(entry->position)); + printf("- size:\t\t%u\n", entry->size); + printf("- flags:\t%x\n", entry->flags); + break; + case BTREQ_IOCTL: + printf("- request:\t%08x\n", entry->size); + break; + } + + printf("- start:\t%u us\n", entry->start_time); + printf("- finish:\t%u us\n", entry->finish_time); + if (entry->result == BTRES_INPROGRESS) + printf("- result:\t(in progress)\n"); + else + printf("- result:\t%d\n", entry->result); + printf("\n"); +} + +static void +btrace_dump(char * file) +{ + int i, r, infd; + + if ((infd = open(file, O_RDONLY)) < 0) { + perror("open"); + exit(EXIT_FAILURE); + } + + for (;;) { + if ((r = read(infd, (char *)buf, sizeof(buf))) <= 0) + break; + + r /= sizeof(buf[0]); + + for (i = 0; i < r; i++) + dump_entry(&buf[i]); + } + + if (r < 0) perror("read"); + + close(infd); +} + +int main(int argc, char ** argv) +{ + int num; + + setprogname(argv[0]); + + if (argc < 3) usage(); + + if (!strcmp(argv[1], "start")) { + if (argc < 4) usage(); + + num = atoi(argv[3]); + + if (num <= 0) usage(); + + btrace_start(argv[2], num); + + } else if (!strcmp(argv[1], "stop")) { + if (argc < 4) usage(); + + btrace_stop(argv[2], argv[3]); + + } else if (!strcmp(argv[1], "reset")) { + btrace_reset(argv[2]); + + } else if (!strcmp(argv[1], "dump")) { + btrace_dump(argv[2]); + + } else + usage(); + + return EXIT_SUCCESS; +}