int munmap_f( int argc, char **argv) { ssize_t length; unsigned int offset; if (munmap(mapping->addr, mapping->length) < 0) { perror("munmap"); return 0; } free(mapping->name); /* Shuffle the mapping table entries down over the removed entry */ offset = mapping - &maptable[0]; length = mapcount * sizeof(mmap_region_t); length -= (offset + 1) * sizeof(mmap_region_t); if (length) memmove(mapping, mapping + 1, length); /* Resize the memory allocated for the table, possibly freeing */ if (--mapcount) { maptable = (mmap_region_t *)realloc(maptable, /* shrinking */ mapcount * sizeof(mmap_region_t)); if (offset == mapcount) offset--; mapping = maptable + offset; } else { free(maptable); mapping = maptable = NULL; } maplist_f(); return 0; }
static int print_f( int argc, char **argv) { filelist_f(); maplist_f(); return 0; }
static int mapset_f( int argc, char **argv) { int i; ASSERT(argc == 2); i = atoi(argv[1]); if (i < 0 || i >= mapcount) { printf("value %d is out of range (0-%d)\n", i, mapcount); } else { mapping = &maptable[i]; maplist_f(); } return 0; }
static int mmap_f( int argc, char **argv) { off64_t offset; ssize_t length; void *address; char *filename; size_t blocksize, sectsize; int c, prot = 0; if (argc == 1) { if (mapping) return maplist_f(); fprintf(stderr, file ? _("no mapped regions, try 'help mmap'\n") : _("no files are open, try 'help open'\n")); return 0; } else if (argc == 2) { if (mapping) return mapset_f(argc, argv); fprintf(stderr, file ? _("no mapped regions, try 'help mmap'\n") : _("no files are open, try 'help open'\n")); return 0; } else if (!file) { fprintf(stderr, _("no files are open, try 'help open'\n")); return 0; } while ((c = getopt(argc, argv, "rwx")) != EOF) { switch (c) { case 'r': prot |= PROT_READ; break; case 'w': prot |= PROT_WRITE; break; case 'x': prot |= PROT_EXEC; break; default: return command_usage(&mmap_cmd); } } if (!prot) prot = PROT_READ | PROT_WRITE | PROT_EXEC; if (optind != argc - 2) return command_usage(&mmap_cmd); init_cvtnum(&blocksize, §size); offset = cvtnum(blocksize, sectsize, argv[optind]); if (offset < 0) { printf(_("non-numeric offset argument -- %s\n"), argv[optind]); return 0; } optind++; length = cvtnum(blocksize, sectsize, argv[optind]); if (length < 0) { printf(_("non-numeric length argument -- %s\n"), argv[optind]); return 0; } filename = strdup(file->name); if (!filename) { perror("strdup"); return 0; } address = mmap(NULL, length, prot, MAP_SHARED, file->fd, offset); if (address == MAP_FAILED) { perror("mmap"); free(filename); return 0; } /* Extend the control array of mmap'd regions */ maptable = (mmap_region_t *)realloc(maptable, /* growing */ ++mapcount * sizeof(mmap_region_t)); if (!maptable) { perror("realloc"); mapcount = 0; munmap(address, length); free(filename); return 0; } /* Finally, make this the new active mapping */ mapping = &maptable[mapcount - 1]; mapping->addr = address; mapping->length = length; mapping->offset = offset; mapping->name = filename; mapping->prot = prot; return 0; }