/* * Crunch through the file, parsing the contents and creating a tag index. */ static int processFile(EventTagMap* map) { EventTag* tagArray = NULL; /* get a tag count */ map->numTags = countMapLines(map); if (map->numTags < 0) return -1; //printf("+++ found %d tags\n", map->numTags); /* allocate storage for the tag index array */ map->tagArray = calloc(1, sizeof(EventTag) * map->numTags); if (map->tagArray == NULL) return -1; /* parse the file, null-terminating tag strings */ if (parseMapLines(map) != 0) { fprintf(stderr, "%s: file parse failed\n", OUT_TAG); return -1; } /* sort the tags and check for duplicates */ if (sortTags(map) != 0) return -1; return 0; }
// Open the map file and allocate a structure to manage it. // // We create a private mapping because we want to terminate the log tag // strings with '\0'. LIBLOG_ABI_PUBLIC EventTagMap* android_openEventTagMap(const char* fileName) { EventTagMap* newTagMap; off_t end[NUM_MAPS]; int save_errno, fd[NUM_MAPS]; size_t which; memset(fd, -1, sizeof(fd)); memset(end, 0, sizeof(end)); for (which = 0; which < NUM_MAPS; ++which) { const char* tagfile = fileName ? fileName : eventTagFiles[which]; fd[which] = open(tagfile, O_RDONLY | O_CLOEXEC); if (fd[which] < 0) { if (!which) { save_errno = errno; fprintf(stderr, OUT_TAG ": unable to open map '%s': %s\n", tagfile, strerror(save_errno)); goto fail_errno; } continue; } end[which] = lseek(fd[which], 0L, SEEK_END); save_errno = errno; (void)lseek(fd[which], 0L, SEEK_SET); if (!which && (end[0] < 0)) { fprintf(stderr, OUT_TAG ": unable to seek map '%s' %s\n", tagfile, strerror(save_errno)); goto fail_close; } if (fileName) break; // Only allow one as specified } newTagMap = new EventTagMap; if (newTagMap == NULL) { save_errno = errno; goto fail_close; } for (which = 0; which < NUM_MAPS; ++which) { if (fd[which] >= 0) { newTagMap->mapAddr[which] = mmap(NULL, end[which], which ? PROT_READ : PROT_READ | PROT_WRITE, which ? MAP_SHARED : MAP_PRIVATE, fd[which], 0); save_errno = errno; close(fd[which]); /* fd DONE */ fd[which] = -1; if ((newTagMap->mapAddr[which] != MAP_FAILED) && (newTagMap->mapAddr[which] != NULL)) { newTagMap->mapLen[which] = end[which]; } else if (!which) { const char* tagfile = fileName ? fileName : eventTagFiles[which]; fprintf(stderr, OUT_TAG ": mmap(%s) failed: %s\n", tagfile, strerror(save_errno)); goto fail_unmap; } } } for (which = 0; which < NUM_MAPS; ++which) { if (parseMapLines(newTagMap, which) != 0) { delete newTagMap; return NULL; } /* See 'fd DONE' comments above and below, no need to clean up here */ } return newTagMap; fail_unmap: save_errno = EINVAL; delete newTagMap; fail_close: for (which = 0; which < NUM_MAPS; ++which) close(fd[which]); /* fd DONE */ fail_errno: errno = save_errno; return NULL; }