int main(int argc, char** argv) { uint8_t* addr = mmap(NULL, 8192, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); if (addr == MAP_FAILED) exit(1); int err = mprotect(addr+4096, 4096, PROT_NONE); if (err != 0) exit(2); uint32_t* mem = malloc(sizeof(uint32_t) * 4096); addr[0] = 0x81; addr[1] = 0x80; addr[2] = 0x80; addr[3] = 0x00; addr[4] = 0x81; addr[5] = 0x80; addr[6] = 0x80; addr[7] = 0x00; addr[8] = 0x81; addr[9] = 0x80; addr[10] = 0x80; addr[11] = 0x00; for (int i = 12; i < 4096; i++) { addr[i] = 1; } init(); read_ints(0, addr, mem, 4087); assert(4087 == mem[4086]); for (int i = 0; i < 4096; i++) { addr[i] = 1; } read_ints(0, addr, mem, 4096); assert(4096 == mem[4095]); return 0; }
int main(int len, char *args[]) { if (len <= 1) { printf("No input file\n"); exit(1); } int *all = (int *) malloc(sizeof(int) * LEN); int num = read_ints(all, args[1]); int *all1 = copy_ints(num,all); int *all2 = copy_ints(num,all); long l1 = quick_sort(all,0,num-1,_partition_first); long l2 = quick_sort(all1,0,num-1,_partition_mid); long l3 = quick_sort(all2,0,num-1,_partition_end); printf("%ld,%ld,%ld\n",l1,l2,l3); // int *help = (int *) malloc(sizeof(int) * LEN); // long res = merge_sort_cal_inversions(all, help, 0, num-1); // printf("result : %ld\n", res); free(all); free(all1); free(all2); }
/* * This function will panic if errors occur when attempting to open an image * file. Now if only I could use exceptions to handle the errors in an elegant * fashion... */ int vmware3_image_t::open(const char * pathname) { COW_Header header; int file; int flags = O_RDWR; #ifdef O_BINARY flags |= O_BINARY; #endif // Set so close doesn't segfault, in case something goes wrong images = NULL; /* Open the virtual disk */ file = ::open(pathname, flags); if(file < 0) return -1; /* Read the header */ if(read_header(file, header) < 0) BX_PANIC(("unable to read vmware3 COW Disk header from file '%s'", pathname)); /* Make sure it's a valid header */ if(!is_valid_header(header)) BX_PANIC(("invalid vmware3 COW Disk image")); ::close(file); tlb_size = header.tlb_size_sectors * 512; slb_count = (1 << FL_SHIFT) / tlb_size; // we must have at least one chain unsigned count = header.number_of_chains; if (count < 1) count = 1; images = new COW_Image [count]; off_t offset = 0; for (unsigned i = 0; i < count; ++i) { char * filename = generate_cow_name(pathname, i); current = &images[i]; current->fd = ::open(filename, flags); if(current->fd < 0) BX_PANIC(("unable to open vmware3 COW Disk file '%s'", filename)); if(read_header(current->fd, current->header) < 0) BX_PANIC(("unable to read header or invalid header in vmware3 COW Disk file '%s'", filename)); if(!is_valid_header(current->header)) BX_PANIC(("invalid vmware3 COW Disk file '%s'", filename)); current->flb = new unsigned [current->header.flb_count]; if(current->flb == 0) BX_PANIC(("cannot allocate %d bytes for flb in vmware3 COW Disk '%s'", current->header.flb_count * 4, filename)); current->slb = new unsigned * [current->header.flb_count]; if(current->slb == 0) BX_PANIC(("cannot allocate %d bytes for slb in vmware3 COW Disk '%s'", current->header.flb_count * 4, filename)); unsigned j; for(j = 0; j < current->header.flb_count; ++j) { current->slb[j] = new unsigned [slb_count]; if(current->slb[j] == 0) BX_PANIC(("cannot allocate %d bytes for slb[] in vmware3 COW Disk '%s'", slb_count * 4, filename)); } current->tlb = new Bit8u [tlb_size]; if(current->tlb == 0) BX_PANIC(("cannot allocate %d bytes for tlb in vmware3 COW Disk '%s'", tlb_size, filename)); if(::lseek(current->fd, current->header.flb_offset_sectors * 512, SEEK_SET) < 0) BX_PANIC(("unable to seek vmware3 COW Disk file '%s'", filename)); if(read_ints(current->fd, current->flb, current->header.flb_count) < 0) BX_PANIC(("unable to read flb from vmware3 COW Disk file '%s'", filename)); for(j = 0; j < current->header.flb_count; ++j) if(current->flb[j] != 0) { if(::lseek(current->fd, current->flb[j] * 512, SEEK_SET) < 0) BX_PANIC(("unable to seek vmware3 COW Disk file '%s'", filename)); if(read_ints(current->fd, current->slb[j], slb_count) < 0) BX_PANIC(("unable to read slb from vmware3 COW Disk file '%s'", filename)); } current->min_offset = offset; offset += current->header.total_sectors * 512; current->max_offset = offset; current->offset = INVALID_OFFSET; current->synced = true; delete[] filename; } current = &images[0]; requested_offset = 0; if (header.total_sectors_in_disk!=0) { cylinders = header.cylinders_in_disk; heads = header.heads_in_disk; spt = header.sectors_in_disk; hd_size = header.total_sectors_in_disk * 512; } else { cylinders = header.cylinders; heads = header.heads; spt = header.sectors; hd_size = header.total_sectors * 512; } return 1; }
static ReadStatus read_data (DBusBabysitter *sitter, int fd) { int what; int got; DBusError error = DBUS_ERROR_INIT; ReadStatus r; r = read_ints (fd, &what, 1, &got, &error); switch (r) { case READ_STATUS_ERROR: _dbus_warn ("Failed to read data from fd %d: %s", fd, error.message); dbus_error_free (&error); return r; case READ_STATUS_EOF: return r; case READ_STATUS_OK: break; } if (got == 1) { switch (what) { case CHILD_EXITED: case CHILD_FORK_FAILED: case CHILD_EXEC_FAILED: { int arg; r = read_ints (fd, &arg, 1, &got, &error); switch (r) { case READ_STATUS_ERROR: _dbus_warn ("Failed to read arg from fd %d: %s", fd, error.message); dbus_error_free (&error); return r; case READ_STATUS_EOF: return r; case READ_STATUS_OK: break; } if (got == 1) { if (what == CHILD_EXITED) { /* Do not reset sitter->errnum to 0 here. We get here if * the babysitter reports that the grandchild process has * exited, and there are two ways that can happen: * * 1. grandchild successfully exec()s the desired process, * but then the desired process exits or is terminated * by a signal. The babysitter observes this and reports * CHILD_EXITED. * * 2. grandchild fails to exec() the desired process, * attempts to report the exec() failure (which * we will receive as CHILD_EXEC_FAILED), and then * exits itself (which will prompt the babysitter to * send CHILD_EXITED). We want the CHILD_EXEC_FAILED * to take precedence (and have its errno logged), * which _dbus_babysitter_set_child_exit_error() does. */ sitter->have_child_status = TRUE; sitter->status = arg; _dbus_verbose ("recorded child status exited = %d signaled = %d exitstatus = %d termsig = %d\n", WIFEXITED (sitter->status), WIFSIGNALED (sitter->status), WEXITSTATUS (sitter->status), WTERMSIG (sitter->status)); } else if (what == CHILD_FORK_FAILED) { sitter->have_fork_errnum = TRUE; sitter->errnum = arg; _dbus_verbose ("recorded fork errnum %d\n", sitter->errnum); } else if (what == CHILD_EXEC_FAILED) { sitter->have_exec_errnum = TRUE; sitter->errnum = arg; _dbus_verbose ("recorded exec errnum %d\n", sitter->errnum); } } } break; case CHILD_PID: { pid_t pid = -1; r = read_pid (fd, &pid, &error); switch (r) { case READ_STATUS_ERROR: _dbus_warn ("Failed to read PID from fd %d: %s", fd, error.message); dbus_error_free (&error); return r; case READ_STATUS_EOF: return r; case READ_STATUS_OK: break; } sitter->grandchild_pid = pid; _dbus_verbose ("recorded grandchild pid %d\n", sitter->grandchild_pid); } break; default: _dbus_warn ("Unknown message received from babysitter process"); break; } } return r; }
static ReadStatus read_data (DBusBabysitter *sitter, int fd) { int what; int got; DBusError error = DBUS_ERROR_INIT; ReadStatus r; r = read_ints (fd, &what, 1, &got, &error); switch (r) { case READ_STATUS_ERROR: _dbus_warn ("Failed to read data from fd %d: %s\n", fd, error.message); dbus_error_free (&error); return r; case READ_STATUS_EOF: return r; case READ_STATUS_OK: break; } if (got == 1) { switch (what) { case CHILD_EXITED: case CHILD_FORK_FAILED: case CHILD_EXEC_FAILED: { int arg; r = read_ints (fd, &arg, 1, &got, &error); switch (r) { case READ_STATUS_ERROR: _dbus_warn ("Failed to read arg from fd %d: %s\n", fd, error.message); dbus_error_free (&error); return r; case READ_STATUS_EOF: return r; case READ_STATUS_OK: break; } if (got == 1) { if (what == CHILD_EXITED) { sitter->have_child_status = TRUE; sitter->status = arg; sitter->errnum = 0; _dbus_verbose ("recorded child status exited = %d signaled = %d exitstatus = %d termsig = %d\n", WIFEXITED (sitter->status), WIFSIGNALED (sitter->status), WEXITSTATUS (sitter->status), WTERMSIG (sitter->status)); } else if (what == CHILD_FORK_FAILED) { sitter->have_fork_errnum = TRUE; sitter->errnum = arg; _dbus_verbose ("recorded fork errnum %d\n", sitter->errnum); } else if (what == CHILD_EXEC_FAILED) { sitter->have_exec_errnum = TRUE; sitter->errnum = arg; _dbus_verbose ("recorded exec errnum %d\n", sitter->errnum); } } } break; case CHILD_PID: { pid_t pid = -1; r = read_pid (fd, &pid, &error); switch (r) { case READ_STATUS_ERROR: _dbus_warn ("Failed to read PID from fd %d: %s\n", fd, error.message); dbus_error_free (&error); return r; case READ_STATUS_EOF: return r; case READ_STATUS_OK: break; } sitter->grandchild_pid = pid; _dbus_verbose ("recorded grandchild pid %d\n", sitter->grandchild_pid); } break; default: _dbus_warn ("Unknown message received from babysitter process\n"); break; } } return r; }
static int fork_child_async(const char* cmd, int* child_pid) { pid_t pid, grandchild_pid; int child_pid_report_pipe[2] = {-1, -1}; int child_err_report_pipe[2] = {-1, -1}; int null_dev; /* by default is assumed how command wasn't found */ int ret = RUN_NOT_FOUND; errno = 0; if(pipe(child_pid_report_pipe) != 0) { E_WARNING(E_STRLOC ": pipe() failed with '%s'\n", strerror(errno)); return RUN_PIPE_FAILED; } if(pipe(child_err_report_pipe) != 0) { E_WARNING(E_STRLOC ": pipe() failed with '%s'\n", strerror(errno)); return RUN_PIPE_FAILED; } pid = fork(); if(pid < 0) { E_WARNING(E_STRLOC ": fork() failed with '%s'\n", strerror(errno)); return RUN_FORK_FAILED; } else if(pid == 0) { signal(SIGPIPE, SIG_DFL); close_and_invalidate(&child_pid_report_pipe[0]); close_and_invalidate(&child_err_report_pipe[0]); /* TODO stdin, stdout, stderr */ grandchild_pid = fork(); if(grandchild_pid < 0) { /* report -1 as returned value */ write_int(child_pid_report_pipe[1], grandchild_pid); write_int(child_err_report_pipe[1], RUN_FORK_FAILED); _exit(1); } else if(grandchild_pid == 0) { /* * second child code is here; from now on every error * is reported via pipe */ null_dev = open("/dev/null", O_RDWR); if(null_dev == -1) { write_int(child_pid_report_pipe[1], grandchild_pid); write_int(child_err_report_pipe[1], errno); _exit(1); } /* close on exec */ fcntl(child_err_report_pipe[1], F_SETFD, FD_CLOEXEC); /* just send stdin, stdout, stderr to null dev */ close(0); dup(null_dev); close(1); dup(null_dev); close(2); dup(null_dev); exec_cmd(cmd, child_err_report_pipe[1]); } else { /* child (a grandchild parent) is here; report it's pid before normal exit */ write_int(child_pid_report_pipe[1], grandchild_pid); close_and_invalidate(&child_err_report_pipe[1]); _exit(0); } } else { /* parent */ int status, buf[2], n_ints = 0; close_and_invalidate(&child_pid_report_pipe[1]); close_and_invalidate(&child_err_report_pipe[1]); /* reap intermediate child */ while(waitpid(pid, &status, 0) < 0) { if(errno == EINTR) continue; break; } if(!read_ints(child_err_report_pipe[0], buf, 2, &n_ints)) goto fail; /* child signaled some error; inspect it and bail out */ if(n_ints >= 2) { ret = convert_from_errno(buf[1], buf[0]); goto fail; } /* get pid from grandchild pid, this is the pid of actual forked program */ n_ints = 0; if(!read_ints(child_pid_report_pipe[0], buf, 1, &n_ints)) { ret = RUN_PIPE_FAILED; goto fail; } if(n_ints < 1) { ret = RUN_PIPE_FAILED; goto fail; } if(child_pid) *child_pid = buf[0]; close_and_invalidate(&child_err_report_pipe[0]); close_and_invalidate(&child_pid_report_pipe[0]); /* everything went fine */ return 0; } fail: /* we got some error in first child; reap it so it does not become a zombie */ if(pid > 0) { while(waitpid(pid, NULL, 0) < 0) { if(errno != EINTR) break; } } close_and_invalidate(&child_pid_report_pipe[0]); close_and_invalidate(&child_pid_report_pipe[1]); close_and_invalidate(&child_err_report_pipe[0]); close_and_invalidate(&child_err_report_pipe[1]); return ret; }