-/* Read an ioctl dump file, and call the ioc_func for each ioctl buffer
- * in the file. For example:
- *
- * parse_dump("lctl.dump", l_ioctl);
- *
- * Note: if using l_ioctl, then you also need to register_ioc_dev() for
- * each device used in the dump.
- */
-int
-parse_dump(char * dump_file, ioc_handler_t ioc_func)
-{
- int line =0;
- struct stat st;
- char *start, *buf, *end;
-#ifndef __CYGWIN__
- int fd;
-#else
- HANDLE fd, hmap;
- DWORD size;
-#endif
-
-#ifndef __CYGWIN__
- fd = open(dump_file, O_RDONLY);
- if (fd < 0) {
- fprintf(stderr, "couldn't open %s: %s\n", dump_file,
- strerror(errno));
- exit(1);
- }
-
- if (fstat(fd, &st)) {
- perror("stat fails");
- exit(1);
- }
-
- if (st.st_size < 1) {
- fprintf(stderr, "KML is empty\n");
- exit(1);
- }
-
- start = buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE , fd, 0);
- end = start + st.st_size;
- close(fd);
- if (start == MAP_FAILED) {
- fprintf(stderr, "can't create file mapping\n");
- exit(1);
- }
-#else
- fd = CreateFile(dump_file, GENERIC_READ, FILE_SHARE_READ, NULL,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- size = GetFileSize(fd, NULL);
- if (size < 1) {
- fprintf(stderr, "KML is empty\n");
- exit(1);
- }
-
- hmap = CreateFileMapping(fd, NULL, PAGE_READONLY, 0,0, NULL);
- start = buf = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
- end = buf + size;
- CloseHandle(fd);
- if (start == NULL) {
- fprintf(stderr, "can't create file mapping\n");
- exit(1);
- }
-#endif /* __CYGWIN__ */
-
- while (buf < end) {
- struct dump_hdr *dump_hdr = (struct dump_hdr *) buf;
- struct libcfs_ioctl_hdr * data;
- char tmp[8096];
- int rc;
-
- line++;
-
- data = (struct libcfs_ioctl_hdr *) (buf + sizeof(*dump_hdr));
- if (buf + data->ioc_len > end ) {
- fprintf(stderr, "dump file overflow, %p + %d > %p\n", buf,
- data->ioc_len, end);
- return -1;
- }
-#if 0
- printf ("dump_hdr: %lx data: %lx\n",
- (unsigned long)dump_hdr - (unsigned long)buf, (unsigned long)data - (unsigned long)buf);
-
- printf("%d: opcode %x len: %d ver: %x ", line, dump_hdr->opc,
- data->ioc_len, data->ioc_version);
-#endif
-
- memcpy(tmp, data, data->ioc_len);
-
- rc = ioc_func(dump_hdr->dev_id, dump_hdr->opc, tmp);
- if (rc) {
- printf("failed: %d\n", rc);
- exit(1);
- }
-
- buf += data->ioc_len + sizeof(*dump_hdr);
- }
-
-#ifndef __CYGWIN__
- munmap(start, end - start);
-#else
- UnmapViewOfFile(start);
- CloseHandle(hmap);
-#endif
-
- return 0;