blktrace: don't stop tracer if not setup trace successfully
[blktrace.git] / btt / misc.c
index 4ec2c18aa5255a25845a7c5bdef6427d1b342203..2e27c81e099dffaa330704d6660bb14ab2126b61 100644 (file)
 #define INLINE_DECLARE
 #include "globals.h"
 
-int in_devices(struct blk_io_trace *t)
-{
-       int i;
-       unsigned int mjr, mnr;
-       char *p = devices;
-
-       if (p == NULL) return 1;        /* Allow anything */
-
-       for (;;) {
-               i = sscanf(p, "%u,%u;", &mjr, &mnr);
-               if ((mjr == MAJOR(t->device) && (mnr == MINOR(t->device))))
-                       return 1;
+struct file_info {
+       struct list_head head;
+       FILE *ofp;
+       char *oname;
+};
 
-               p = strchr(p, ';');
-               if (!p)
-                       break;
-               p++;
-       }
-
-       return 0;
-}
-
-void add_file(struct file_info **fipp, FILE *fp, char *oname)
-{
-       struct file_info *fip = malloc(sizeof(*fip));
+struct buf_info {
+       struct list_head head;
+       void *buf;
+};
 
-       fip->ofp = fp;
-       fip->oname = oname;
-       fip->next = *fipp;
-       *fipp = fip;
-}
+LIST_HEAD(files_to_clean);
+LIST_HEAD(all_bufs);
 
-void clean_files(struct file_info **fipp)
+static void clean_files(void)
 {
-       struct stat buf;
-       struct file_info *fip;
+       struct list_head *p, *q;
 
-       while ((fip = *fipp) != NULL) {
-               *fipp = fip->next;
+       list_for_each_safe(p, q, &files_to_clean) {
+               struct stat buf;
+               struct file_info *fip = list_entry(p, struct file_info, head);
 
                fclose(fip->ofp);
                if (!stat(fip->oname, &buf) && (buf.st_size == 0))
                        unlink(fip->oname);
 
+               list_del(&fip->head);
                free(fip->oname);
                free(fip);
        }
 }
 
-struct buf_info {
-       struct buf_info *next;
-       void *buf;
-} *all_bufs;
-void add_buf(void *buf)
+static void clean_bufs(void)
 {
-       struct buf_info *bip = malloc(sizeof(*bip));
+       struct list_head *p, *q;
 
-       bip->buf = buf;
-       bip->next = all_bufs;
-       all_bufs = bip;
-}
+       list_for_each_safe(p, q, &all_bufs) {
+               struct buf_info *bip = list_entry(p, struct buf_info, head);
 
-void clean_bufs(void)
-{
-       struct buf_info *bip;
-
-       while ((bip = all_bufs) != NULL) {
-               all_bufs = bip->next;
+               list_del(&bip->head);
                free(bip->buf);
                free(bip);
        }
 }
 
-char *make_dev_hdr(char *pad, size_t len, struct d_info *dip, int add_parens)
-{
-       if (dip->map == NULL) {
-               if (add_parens)
-                       snprintf(pad, len, "(%3d,%3d)",
-                                MAJOR(dip->device), MINOR(dip->device));
-               else
-                       snprintf(pad, len, "%d,%d",
-                                MAJOR(dip->device), MINOR(dip->device));
-       }
-       else
-               snprintf(pad, len, "%s", dip->map->device);
-
-       return pad;
-}
-
 /*
  * Due to the N(devs) parts of a lot of the output features provided
  * by btt, it will fail opens on large(ish) systems. Here we try to
@@ -149,9 +105,52 @@ static int handle_open_failure(void)
 {
        if (errno == ENFILE || errno == EMFILE)
                return increase_limit(RLIMIT_NOFILE, 16);
+
        return 0;
 }
 
+void add_file(FILE *fp, char *oname)
+{
+       struct file_info *fip = malloc(sizeof(*fip));
+
+       fip->ofp = fp;
+       fip->oname = oname;
+       list_add_tail(&fip->head, &files_to_clean);
+}
+
+void add_buf(void *buf)
+{
+       struct buf_info *bip = malloc(sizeof(*bip));
+
+       bip->buf = buf;
+       list_add_tail(&bip->head, &all_bufs);
+}
+
+void clean_allocs(void)
+{
+       clean_files();
+       clean_bufs();
+}
+
+char *make_dev_hdr(char *pad, size_t len, struct d_info *dip, int add_parens)
+{
+       if (dip->devmap)
+               snprintf(pad, len, "%s", dip->devmap);
+       else if (add_parens)
+               snprintf(pad, len, "(%3d,%3d)",
+                        MAJOR(dip->device), MINOR(dip->device));
+       else
+               snprintf(pad, len, "%d,%d",
+                        MAJOR(dip->device), MINOR(dip->device));
+
+       return pad;
+}
+
+char *mkhandle(struct d_info *dip, char *str, size_t len)
+{
+       return make_dev_hdr(str, len, dip, 0);
+}
+
 FILE *my_fopen(const char *path, const char *mode)
 {
        FILE *fp;