From: Seungyoun Ju Date: Wed, 8 Mar 2017 08:09:12 +0000 (+0900) Subject: Apply rotated file writing to hcidump X-Git-Tag: accepted/tizen/common/20170327.142440~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F30%2F118230%2F1;p=platform%2Fupstream%2Fbluez.git Apply rotated file writing to hcidump [Model] COMMON [BinType] AP [Customer] OPEN [Issue#] N/A [Request] Internal [Occurrence Version] N/A [Problem] Change was missing. Without this option, hci logging file could be increased infinitely. [Cause & Measure] Apply change [Checking Method] hcidump -c 2 -s 10 -w hci_log.cfa [Team] Basic connection [Developer] Seungyoun Ju [Solution company] Samsung [Change Type] Specification change Change-Id: I24bade6d0893c399ae10d7efa9ca3bb42a3a3b70 --- diff --git a/tools/hcidump.c b/tools/hcidump.c index ffb7499..b460daa 100644 --- a/tools/hcidump.c +++ b/tools/hcidump.c @@ -59,10 +59,27 @@ enum { /* Default options */ static int snap_len = SNAP_LEN; static int mode = PARSE; +#ifndef TIZEN_FEATURE_BLUEZ_MODIFY static char *dump_file = NULL; +#endif static char *pppdump_file = NULL; static char *audio_file = NULL; +#ifdef TIZEN_FEATURE_BLUEZ_MODIFY +#define DUMP_MAX_SIZE 10000000 /* 10MB */ +#define DUMP_MAX_COUNT 2 +#define NAME_MAX 255 + +struct dump_file { + char *filename; + int postfix_width; + unsigned int max_size; + int max_count; +}; + +struct dump_file df = {NULL, 1, DUMP_MAX_SIZE, DUMP_MAX_COUNT}; +#endif + struct hcidump_hdr { uint16_t len; uint8_t in; @@ -101,6 +118,10 @@ struct pktlog_hdr { } __attribute__ ((packed)); #define PKTLOG_HDR_SIZE (sizeof(struct pktlog_hdr)) +#ifdef TIZEN_FEATURE_BLUEZ_MODIFY +static int open_new_dumpfile(unsigned long flags); +#endif + static inline int read_n(int fd, char *buf, int len) { int t = 0, w; @@ -148,6 +169,9 @@ static int process_frames(int dev, int sock, int fd, unsigned long flags) char *buf; char ctrl[100]; int len, hdr_size = HCIDUMP_HDR_SIZE; +#ifdef TIZEN_FEATURE_BLUEZ_MODIFY + int written = 0; +#endif if (sock < 0) return -1; @@ -261,10 +285,28 @@ static int process_frames(int dev, int sock, int fd, unsigned long flags) dh->ts_usec = htobl(frm.ts.tv_usec); } +#ifdef TIZEN_FEATURE_BLUEZ_MODIFY + if (mode == WRITE && df.max_size != 0 && + written + frm.data_len + hdr_size > df.max_size) { + close(fd); + fd = open_new_dumpfile(flags); + if (fd < 0) + return -1; + written = 0; + } + + len = write_n(fd, buf, frm.data_len + hdr_size); + if (len < 0) { + perror("Write error"); + return -1; + } + written += len; +#else if (write_n(fd, buf, frm.data_len + hdr_size) < 0) { perror("Write error"); return -1; } +#endif break; default: @@ -518,6 +560,38 @@ static int open_file(char *file, int mode, unsigned long flags) return fd; } +#ifdef TIZEN_FEATURE_BLUEZ_MODIFY +static int open_new_dumpfile(unsigned long flags) +{ + char filename[NAME_MAX + 1]; + char new_filename[NAME_MAX + 1]; + int i; + + if (df.max_count <= 1) + return open_file(df.filename, WRITE, flags); + + for (i = df.max_count - 2; i >= 0; i--) { + if (i == 0) { + snprintf(filename, sizeof(filename), "%s", df.filename); + } else { + snprintf(filename, sizeof(filename), "%s.%0*d", + df.filename, df.postfix_width, i); + } + + if (access(filename, F_OK) < 0) + continue; + + snprintf(new_filename, sizeof(new_filename), "%s.%0*d", + df.filename, df.postfix_width, i + 1); + + if (rename(filename, new_filename) < 0) + perror("rename failed"); + } + + return open_file(df.filename, WRITE, flags); +} +#endif + static int open_socket(int dev, unsigned long flags) { struct sockaddr_hci addr; @@ -637,6 +711,10 @@ static void usage(void) " -D, --pppdump=file Extract PPP traffic\n" " -A, --audio=file Extract SCO audio data\n" " -Y, --novendor No vendor commands or events\n" +#ifdef TIZEN_FEATURE_BLUEZ_MODIFY + " -s --dump-size=size Maximum save-dump's file size. The unit is million bytes. Use this with -w. (default: 1,000,000 bytes)\n" + " -c --dump-count=count Specified count's dump files will be generated at most. Use this with -w. (default: 4)\n" +#endif " -h, --help Give this help list\n" " -v, --version Give version information\n" " --usage Give a short usage message\n" @@ -663,6 +741,10 @@ static struct option main_options[] = { { "pppdump", 1, 0, 'D' }, { "audio", 1, 0, 'A' }, { "novendor", 0, 0, 'Y' }, +#ifdef TIZEN_FEATURE_BLUEZ_MODIFY + { "dump-size", 1, 0, 's' }, + { "dump-count", 1, 0, 'c' }, +#endif { "help", 0, 0, 'h' }, { "version", 0, 0, 'v' }, { 0 } @@ -678,9 +760,15 @@ int main(int argc, char *argv[]) int opt, pppdump_fd = -1, audio_fd = -1; uint16_t obex_port; +#ifdef TIZEN_FEATURE_BLUEZ_MODIFY + while ((opt = getopt_long(argc, argv, + "i:l:p:m:w:r:taxXRC:H:O:P:S:D:A:Ys:c:hv", + main_options, NULL)) != -1) { +#else while ((opt = getopt_long(argc, argv, "i:l:p:m:w:r:taxXRC:H:O:P:S:D:A:Yhv", main_options, NULL)) != -1) { +#endif switch(opt) { case 'i': if (strcasecmp(optarg, "none") && strcasecmp(optarg, "system")) @@ -703,12 +791,20 @@ int main(int argc, char *argv[]) case 'w': mode = WRITE; +#ifdef TIZEN_FEATURE_BLUEZ_MODIFY + df.filename = strdup(optarg); +#else dump_file = strdup(optarg); +#endif break; case 'r': mode = READ; +#ifdef TIZEN_FEATURE_BLUEZ_MODIFY + df.filename = strdup(optarg); +#else dump_file = strdup(optarg); +#endif break; case 't': @@ -767,6 +863,21 @@ int main(int argc, char *argv[]) flags |= DUMP_NOVENDOR; break; +#ifdef TIZEN_FEATURE_BLUEZ_MODIFY + case 's': + df.max_size = atoi(optarg) * 1000000; + break; + + case 'c': + { + int i; + df.max_count = atoi(optarg); + for (i = df.max_count / 10; i; i /= 10) + df.postfix_width++; + break; + } +#endif + case 'v': printf("%s\n", VERSION); exit(0); @@ -809,13 +920,24 @@ int main(int argc, char *argv[]) flags |= DUMP_VERBOSE; init_parser(flags, filter, defpsm, defcompid, pppdump_fd, audio_fd); +#ifdef TIZEN_FEATURE_BLUEZ_MODIFY + read_dump(open_file(df.filename, mode, flags)); +#else read_dump(open_file(dump_file, mode, flags)); +#endif break; case WRITE: flags |= DUMP_BTSNOOP; +#ifdef TIZEN_FEATURE_BLUEZ_MODIFY + printf("Maximum size of one file : %u, Rotated file count : %d", + df.max_size, df.max_count); + process_frames(device, open_socket(device, flags), + open_file(df.filename, mode, flags), flags); +#else process_frames(device, open_socket(device, flags), open_file(dump_file, mode, flags), flags); +#endif break; }