Example #1
0
void fetch_chunks_vma(pid_t pid, struct list *l, long *bin_offset) {
	struct proc_chunk *chunk   = NULL;
	char tmp_fn[30]          = "", //map filename
			 map_line[1024]      = ""; //each line of map
	FILE        *f = NULL; //map file
	int vma_no     = 0; //count of vma
	//open maps file
	//iterate through map lines
	snprintf(tmp_fn, 30, "/proc/%d/maps", pid);
	f = fopen(tmp_fn, "r");
	while (fgets(map_line, sizeof(map_line), f)) {
		//alloc chunk and set type
		chunk = malloc(sizeof(struct proc_chunk));
		chunk->type = PROC_CHUNK_VMA;
		get_one_vma(pid, map_line, &chunk->vma,	vma_no, bin_offset);
		/*printf("VMA %08lx-%08lx (size:%8ld) %c%c%c%c %08lx %02x:%02x %d\t%s\n",
				chunk->vma.start, chunk->vma.start+chunk->vma.length, chunk->vma.length,
				(chunk->vma.prot&PROT_READ)?'r':'-',
				(chunk->vma.prot&PROT_WRITE)?'w':'-',
				(chunk->vma.prot&PROT_EXEC)?'x':'-',
				(chunk->vma.prot&MAP_PRIVATE)?'p':'s',
				chunk->vma.pg_off,
				chunk->vma.dev >> 8,
				chunk->vma.dev & 0xff,
				chunk->vma.inode,
				chunk->vma.filename);*/
		vma_no++;
		if (chunk->vma.filename==NULL	|| strstr(chunk->vma.filename,"[vsyscall]")==NULL) {
			list_append(l, chunk);
		} else {
			free(chunk);
		}
	}
	fclose(f);
}
void fetch_chunks_vma(pid_t pid, int flags, struct list *l, long *bin_offset)
{
    struct cp_chunk *chunk = NULL;
    char tmp_fn[30], *ret;
    char map_line[1024], map_line_save[1024];
    struct list work_list; /* VMAs we need to come back to */
    struct item *i = NULL;
    FILE *f;
    int vma_no = 0;

    list_init(work_list);

    snprintf(tmp_fn, 30, "/proc/%d/maps", pid);
    f = fopen(tmp_fn, "r");

    while ((ret = fgets(map_line, sizeof(map_line), f)) || i) {
	if (!ret)
	    strncpy(map_line, i->p, sizeof(map_line));
	strncpy(map_line_save, map_line, sizeof(map_line_save));

	if (!chunk)
	    chunk = xmalloc(sizeof(struct cp_chunk));
	chunk->type = CP_CHUNK_VMA;
	/* FIXME: we may not be able to do all VMA's in the first pass, as we
	 * need a syscall_loc in order to do non-readable VMAs (to call
	 * mprotect). Put these undoable segments into a list to process again
	 */
	switch (get_one_vma(pid, map_line, &chunk->vma, flags & GET_LIBRARIES_TOO,
		    vma_no, bin_offset)) {
	    case 0:
		debug("     Error parsing map: %s", map_line_save);
		continue;
	    case -1:
		/* Add to todo list */
		if (!ret) {
		    debug("     No system calls in the image. Can't save process!\n");
		    abort();
		}
		debug("     Cannot process map yet. Saving for later.");
		list_append(&work_list, strdup(map_line_save));
		if (!i)
		    i = work_list.head;
		continue;
	}
	vma_no++;
	list_append(l, chunk);
	chunk = NULL;

	if (!ret)
	    i = i->next;
    }
    if (chunk)
	free(chunk);

    /* FIXME: free work_list and strings if we're ever going to be long
     * running. */

    fclose(f);
}