int main(int argc, char *argv[]) { if (argc < 3) { usage(argv); exit(1); } struct mmap_info binary; strncpy(binary.name, argv[1], BUFSIZ); if (mmap_file_read(&binary) < 0) { fprintf(stderr, "Unable to open binary file: %s\n", binary.name); exit(1); } struct mmap_info hide; strncpy(hide.name, argv[2], BUFSIZ); if (mmap_file_read(&hide) < 0) { fprintf(stderr, "Unable to open hide file: %s\n", hide.name); exit(1); } struct mmap_info binary_out; snprintf(binary_out.name, BUFSIZ, "%s.new", binary.name); binary_out.data_size = binary.data_size + adjust_size(hide.data_size, vm_page_size); binary_out.data = malloc(sizeof(u_char) * binary_out.data_size); bzero(binary_out.data, binary_out.data_size); if (stuff(&binary, &hide, &binary_out) < 0) { fprintf(stderr, "Unable to stuff binary\n"); exit(1); } munmap_file(&binary); munmap_file(&hide); if ((binary_out.fd = open(binary_out.name, O_WRONLY | O_CREAT | O_TRUNC, DEFFILEMODE)) < 0) { perror("open"); fprintf(stderr, "Unable to open %s\n", binary_out.name); return 1; } if (write(binary_out.fd, binary_out.data, binary_out.data_size) < 0) { perror("write"); fprintf(stderr, "Unable to write %s\n", binary_out.name); return 1; } close(binary_out.fd); free(binary_out.data); printf("PASS\n"); return 0; }
int main(int ac, char **av) { char *path; char *file; struct stat buf; if ((ac = ft_options(&av, NM_OPTIONS, 0))) { while (*av) { path = *av; file = 0; if (ac == 1) path = "a.out"; if (ac > 2) ft_printf("\n%s:\n", path); if (mmap_file(path, &buf, &file) == SUCCESS) { ft_nm(file, path); munmap_file(file, &buf); } av++; } } return (FT_EXIT); }
int main(int argc, char *argv[]) { if (argc < 3) { usage(argv); exit(1); } struct mmap_info binary; strncpy(binary.name, argv[1], BUFSIZ); if (mmap_file_read(&binary) < 0) { fprintf(stderr, "Unable to open binary file: %s\n", binary.name); exit(1); } sig(&binary, argv[2]); munmap_file(&binary); return 0; }
/* * Update the ELF file residing at @p path, replacing all occurrences * of @p search with @p replace in that file's ".debug_info" or * ".debug_str" section. * The replacement string must be the same length or shorter than * the search string. * Returns 0 on success (whether or not ".debug_info" section was * found or updated). * Returns 1 on serious error that should cause distcc to fail. */ static int update_debug_info(const char *path, const char *search, const char *replace) { struct stat st; int fd; void *base; base = mmap_file(path, &fd, &st); if (base == NULL) { return 0; } update_section(path, base, st.st_size, ".debug_info", search, replace); update_section(path, base, st.st_size, ".debug_str", search, replace); return munmap_file(base, path, fd, &st); }
void push_changes(lua_State *l, const char *base_path, const char *full_path) { struct dirent **dir_list = NULL; struct dirent *dir = NULL; int results; int i; int rv; char *path; if (strncmp(base_path, full_path, strlen(base_path)) != 0) die("wtf? %s != %s len %li", base_path, full_path, strlen(base_path)); gettimeofday(&now, NULL); path = strdup(full_path + strlen(base_path) + 1); /* Skip trailing slash in full_path */ log_debug("relative path is %s", path); scandir_baton_t baton; baton.ig = root_ignores; baton.base_path = base_path; baton.level = 0; results = ds_scandir(full_path, &dir_list, &changed_filter, &baton); if (results == -1) { log_debug("Error scanning directory %s: %s", full_path, strerror(errno)); return; } else if (results == 0) { log_debug("No results found in directory %s", full_path); return; } char *file_path; char *file_path_rel; for (i = 0; i < results; i++) { dir = dir_list[i]; ds_asprintf(&file_path, "%s%s", full_path, dir->d_name); ds_asprintf(&file_path_rel, "%s%s", path, dir->d_name); if (is_ignored(file_path)) { /* we triggered this event */ unignore_change(file_path); goto cleanup; } if (is_directory(full_path, dir)) { /* TODO: figure out if we need to recurse */ goto cleanup; } buf_t *buf = get_buf(file_path_rel); if (buf == NULL) { log_err("buf not found for path %s", file_path_rel); goto cleanup; } const char *f2 = file_path; mmapped_file_t *mf; struct stat file_stats; off_t f2_size; rv = lstat(f2, &file_stats); if (rv) { die("Error lstat()ing file %s.", f2); } f2_size = file_stats.st_size; if (f2_size == 0) { log_debug("%s is empty"); goto cleanup; } mf = mmap_file(f2, f2_size, 0, 0); if (is_binary(mf->buf, mf->len)) { log_debug("%s is binary. skipping", file_path); goto diff_cleanup; } char *new_text = strndup(mf->buf, mf->len); char *patch_text = make_patch(l, buf->buf, new_text); free(new_text); if (strlen(patch_text) == 0) { log_debug("no change. not sending patch"); goto diff_cleanup; } char *md5_after = md5(mf->buf, mf->len); send_json( "{s:s s:i s:s s:s s:s s:s}", "name", "patch", "id", buf->id, "patch", patch_text, "path", buf->path, "md5_before", buf->md5, "md5_after", md5_after ); free(md5_after); free(patch_text); buf->buf = realloc(buf->buf, mf->len + 1); buf->len = mf->len; memcpy(buf->buf, mf->buf, buf->len); diff_cleanup:; munmap_file(mf); free(mf); cleanup:; free(file_path); free(file_path_rel); free(dir); } free(dir_list); free(path); }