static void slirp_close(int fd, void *data) { struct slirp_data *pri = data; int status,err; os_close_file(fd); os_close_file(pri->slave); pri->slave = -1; if(pri->pid<1) { printk("slirp_close: no child process to shut down\n"); return; } #if 0 if(kill(pri->pid, SIGHUP)<0) { printk("slirp_close: sending hangup to %d failed (%d)\n", pri->pid, errno); } #endif CATCH_EINTR(err = waitpid(pri->pid, &status, WNOHANG)); if(err < 0) { printk("slirp_close: waitpid returned %d\n", errno); return; } if(err == 0) { printk("slirp_close: process %d has not exited\n", pri->pid); return; } pri->pid = -1; }
unsigned long os_process_pc(int pid) { char proc_stat[STAT_PATH_LEN], buf[256]; unsigned long pc; int fd, err; sprintf(proc_stat, "/proc/%d/stat", pid); fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0); if(fd < 0){ printk("os_process_pc - couldn't open '%s', err = %d\n", proc_stat, -fd); return(ARBITRARY_ADDR); } err = os_read_file(fd, buf, sizeof(buf)); if(err < 0){ printk("os_process_pc - couldn't read '%s', err = %d\n", proc_stat, -err); os_close_file(fd); return(ARBITRARY_ADDR); } os_close_file(fd); pc = ARBITRARY_ADDR; if(sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d " "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d " "%*d %*d %*d %*d %*d %lu", &pc) != 1){ printk("os_process_pc - couldn't find pc in '%s'\n", buf); } return(pc); }
static int slirp_open(void *data) { struct slirp_data *pri = data; int fds[2], pid, err; err = os_pipe(fds, 1, 1); if(err) return(err); err = slirp_tramp(pri->argw.argv, fds[1]); if(err < 0){ printk("slirp_tramp failed - errno = %d\n", -err); goto out; } pid = err; pri->slave = fds[1]; pri->slip.pos = 0; pri->slip.esc = 0; pri->pid = err; return(fds[0]); out: os_close_file(fds[0]); os_close_file(fds[1]); return err; }
static void etap_pre_exec(void *arg) { struct etap_pre_exec_data *data = arg; dup2(data->control_remote, 1); os_close_file(data->data_me); os_close_file(data->control_me); }
static void daemon_remove(void *data) { struct daemon_data *pri = data; os_close_file(pri->fd); os_close_file(pri->control); kfree(pri->data_addr); kfree(pri->ctl_addr); kfree(pri->local_addr); }
static int slip_tramp(char **argv, int fd) { struct slip_pre_exec_data pe_data; char *output; int status, pid, fds[2], err, output_len; err = os_pipe(fds, 1, 0); if(err < 0){ printk("slip_tramp : pipe failed, err = %d\n", -err); goto out; } err = 0; pe_data.stdin = fd; pe_data.stdout = fds[1]; pe_data.close_me = fds[0]; err = run_helper(slip_pre_exec, &pe_data, argv, NULL); if(err < 0) goto out_close; pid = err; output_len = page_size(); output = um_kmalloc(output_len); if(output == NULL){ printk("slip_tramp : failed to allocate output buffer\n"); os_kill_process(pid, 1); err = -ENOMEM; goto out_free; } os_close_file(fds[1]); read_output(fds[0], output, output_len); printk("%s", output); CATCH_EINTR(err = waitpid(pid, &status, 0)); if(err < 0) err = errno; else if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0)){ printk("'%s' didn't exit with status 0\n", argv[0]); err = -EINVAL; } else err = 0; os_close_file(fds[0]); out_free: kfree(output); return err; out_close: os_close_file(fds[0]); os_close_file(fds[1]); out: return err; }
static void pre_exec(void *d) { struct dog_data *data = d; dup2(data->stdin, 0); dup2(data->stdout, 1); dup2(data->stdout, 2); os_close_file(data->stdin); os_close_file(data->stdout); os_close_file(data->close_me[0]); os_close_file(data->close_me[1]); }
static void etap_close(int fd, void *data) { struct ethertap_data *pri = data; iter_addresses(pri->dev, etap_close_addr, &pri->control_fd); os_close_file(fd); os_shutdown_socket(pri->data_fd, 1, 1); os_close_file(pri->data_fd); pri->data_fd = -1; os_close_file(pri->control_fd); pri->control_fd = -1; }
int os_process_parent(int pid) { char stat[STAT_PATH_LEN]; char data[256]; int parent, n, fd; if(pid == -1) return(-1); snprintf(stat, sizeof(stat), "/proc/%d/stat", pid); fd = os_open_file(stat, of_read(OPENFLAGS()), 0); if(fd < 0){ printk("Couldn't open '%s', err = %d\n", stat, -fd); return(FAILURE_PID); } n = os_read_file(fd, data, sizeof(data)); os_close_file(fd); if(n < 0){ printk("Couldn't read '%s', err = %d\n", stat, -n); return(FAILURE_PID); } parent = FAILURE_PID; n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent); if(n != 1) printk("Failed to scan '%s'\n", data); return(parent); }
static void change_pre_exec(void *arg) { struct change_pre_exec_data *data = arg; os_close_file(data->close_me); dup2(data->stdout, 1); }
void file_close(file_t* file) { u32 i; cache_entry_t* entry; /* Unmap cached pages. */ for (i = 0; i < CACHE_SIZE; i++) { entry = &file->cache.entries[i]; if (entry->ptr != NULL) { if (entry->ref > 0) { fprintf(stderr, "file_close: unmapping used page %u (%p)\n", entry->pageid, entry->ptr); } DPRINT("file_close: unmapping cached page %u (%p)\n", entry->pageid, entry->ptr); if (os_put_page(file->handle, entry->ptr) == 0) file->stats.unmapped++; } } #if !defined(NSTATS) file_dump_info(file); #endif /* !defined(NSTATS) */ /* Unmap the header page. */ os_put_page(file->handle, file->header); /* Close the file. */ os_close_file(file->handle); /* Free the file object. */ memset(file, 0, sizeof(file_t)); free(file); }
void destroy_context(struct mm_struct *mm) { struct mm_context *mmu = &mm->context; if (proc_mm) os_close_file(mmu->id.u.mm_fd); else { /* * If init_new_context wasn't called, this will be * zero, resulting in a kill(0), which will result in the * whole UML suddenly dying. Also, cover negative and * 1 cases, since they shouldn't happen either. */ if (mmu->id.u.pid < 2) { printk(KERN_ERR "corrupt mm_context - pid = %d\n", mmu->id.u.pid); return; } os_kill_ptraced_process(mmu->id.u.pid, 1); } if (skas_needs_stub) free_page(mmu->id.stack); free_ldt(mmu); }
static int file_removed(struct dentry *dentry, const char *file) { char *host_file; int extra, fd; extra = 0; if(file != NULL) extra += strlen(file) + 1; host_file = dentry_name(dentry, extra + strlen("/remove")); if(host_file == NULL){ printk("file_removed : allocation failed\n"); return(-ENOMEM); } if(file != NULL){ strcat(host_file, "/"); strcat(host_file, file); } strcat(host_file, "/remove"); fd = os_open_file(host_file, of_read(OPENFLAGS()), 0); kfree(host_file); if(fd >= 0){ os_close_file(fd); return(1); } return(0); }
unsigned long os_process_pc(int pid) { char proc_stat[STAT_PATH_LEN], buf[256]; unsigned long pc = ARBITRARY_ADDR; int fd, err; sprintf(proc_stat, "/proc/%d/stat", pid); fd = open(proc_stat, O_RDONLY, 0); if (fd < 0) { printk(UM_KERN_ERR "os_process_pc - couldn't open '%s', " "errno = %d\n", proc_stat, errno); goto out; } CATCH_EINTR(err = read(fd, buf, sizeof(buf))); if (err < 0) { printk(UM_KERN_ERR "os_process_pc - couldn't read '%s', " "err = %d\n", proc_stat, errno); goto out_close; } os_close_file(fd); pc = ARBITRARY_ADDR; if (sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d " "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d " "%*d %*d %*d %*d %*d %lu", &pc) != 1) printk(UM_KERN_ERR "os_process_pc - couldn't find pc in '%s'\n", buf); out_close: close(fd); out: return pc; }
static int tuntap_open_tramp(char *gate, int *fd_out, int me, int remote, char *buffer, int buffer_len, int *used_out) { struct tuntap_pre_exec_data data; char version_buf[sizeof("nnnnn\0")]; char *argv[] = { "uml_net", version_buf, "tuntap", "up", gate, NULL }; char buf[CMSG_SPACE(sizeof(*fd_out))]; struct msghdr msg; struct cmsghdr *cmsg; struct iovec iov; int pid, n, err; sprintf(version_buf, "%d", UML_NET_VERSION); data.stdout = remote; data.close_me = me; pid = run_helper(tuntap_pre_exec, &data, argv, NULL); if(pid < 0) return(-pid); os_close_file(remote); msg.msg_name = NULL; msg.msg_namelen = 0; if(buffer != NULL){ iov = ((struct iovec) { buffer, buffer_len });
static void tuntap_pre_exec(void *arg) { struct tuntap_pre_exec_data *data = arg; dup2(data->stdout, 1); os_close_file(data->close_me); }
static int etap_tramp(char *dev, char *gate, int control_me, int control_remote, int data_me, int data_remote) { struct etap_pre_exec_data pe_data; int pid, status, err, n; char version_buf[sizeof("nnnnn\0")]; char data_fd_buf[sizeof("nnnnnn\0")]; char gate_buf[sizeof("nnn.nnn.nnn.nnn\0")]; char *setup_args[] = { "uml_net", version_buf, "ethertap", dev, data_fd_buf, gate_buf, NULL }; char *nosetup_args[] = { "uml_net", version_buf, "ethertap", dev, data_fd_buf, NULL }; char **args, c; sprintf(data_fd_buf, "%d", data_remote); sprintf(version_buf, "%d", UML_NET_VERSION); if(gate != NULL){ strcpy(gate_buf, gate); args = setup_args; } else args = nosetup_args; err = 0; pe_data.control_remote = control_remote; pe_data.control_me = control_me; pe_data.data_me = data_me; pid = run_helper(etap_pre_exec, &pe_data, args, NULL); if(pid < 0) err = pid; os_close_file(data_remote); os_close_file(control_remote); n = os_read_file(control_me, &c, sizeof(c)); if(n != sizeof(c)){ printk("etap_tramp : read of status failed, err = %d\n", -n); return(-EINVAL); } if(c != 1){ printk("etap_tramp : uml_net failed\n"); err = -EINVAL; CATCH_EINTR(n = waitpid(pid, &status, 0)); if(n < 0) err = -errno; else if(!WIFEXITED(status) || (WEXITSTATUS(status) != 1)) printk("uml_net didn't exit with status 1\n"); } return(err); }
void release_thread_tt(struct task_struct *task) { int pid = task->thread.mode.tt.extern_pid; /* * We first have to kill the other process, before * closing its switch_pipe. Else it might wake up * and receive "EOF" before we could kill it. */ if(os_getpid() != pid) os_kill_process(pid, 0); os_close_file(task->thread.mode.tt.switch_pipe[0]); os_close_file(task->thread.mode.tt.switch_pipe[1]); /* use switch_pipe as flag: thread is released */ task->thread.mode.tt.switch_pipe[0] = -1; }
static void slip_pre_exec(void *arg) { struct slip_pre_exec_data *data = arg; if(data->stdin >= 0) dup2(data->stdin, 0); dup2(data->stdout, 1); if(data->close_me >= 0) os_close_file(data->close_me); }
static int hostaudio_release(struct inode *inode, struct file *file) { struct hostaudio_state *state = file->private_data; #ifdef DEBUG printk(KERN_DEBUG "hostaudio: release called\n"); #endif os_close_file(state->fd); kfree(state); return 0; }
static void slip_close(int fd, void *data) { struct slip_data *pri = data; char version_buf[sizeof("nnnnn\0")]; char *argv[] = { "uml_net", version_buf, "slip", "down", pri->name, NULL }; int err; if(pri->gate_addr != NULL) iter_addresses(pri->dev, close_addr, pri->name); sprintf(version_buf, "%d", UML_NET_VERSION); err = slip_tramp(argv, pri->slave); if(err != 0) printk("slip_tramp failed - errno = %d\n", -err); os_close_file(fd); os_close_file(pri->slave); pri->slave = -1; }
static int change_tramp(char **argv, char *output, int output_len) { int pid, fds[2], err; struct change_pre_exec_data pe_data; err = os_pipe(fds, 1, 0); if(err < 0){ printk("change_tramp - pipe failed, err = %d\n", -err); return(err); } pe_data.close_me = fds[0]; pe_data.stdout = fds[1]; pid = run_helper(change_pre_exec, &pe_data, argv, NULL); read_output(fds[0], output, output_len); os_close_file(fds[0]); os_close_file(fds[1]); if (pid > 0) CATCH_EINTR(err = waitpid(pid, NULL, 0)); return(pid); }
static int hostmixer_release(struct inode *inode, struct file *file) { struct hostmixer_state *state = file->private_data; #ifdef DEBUG printk("hostmixer: release called\n"); #endif os_close_file(state->fd); kfree(state); return(0); }
void os_reportExit() { char * name; os_mutexDestroy(&reportMutex); if (error_log) { name = os_reportGetInfoFileName(); os_close_file(name, error_log); os_free (name); error_log = NULL; } if (info_log) { name = os_reportGetErrorFileName(); os_close_file(name, info_log); os_free (name); info_log = NULL; } }
int start_io_thread(unsigned long sp, int *fd_out) { int pid, fds[2], err; err = os_pipe(fds, 1, 1); if(err < 0){ printk("start_io_thread - os_pipe failed, err = %d\n", -err); goto out; } kernel_fd = fds[0]; *fd_out = fds[1]; err = os_set_fd_block(*fd_out, 0); if (err) { printk("start_io_thread - failed to set nonblocking I/O.\n"); goto out_close; } pid = clone(io_thread, (void *) sp, CLONE_FILES | CLONE_VM, NULL); if(pid < 0){ err = -errno; printk("start_io_thread - clone failed : errno = %d\n", errno); goto out_close; } return(pid); out_close: os_close_file(fds[0]); os_close_file(fds[1]); kernel_fd = -1; *fd_out = -1; out: return err; }
static void mcast_close(int fd, void *data) { struct ip_mreq mreq; struct mcast_data *pri = data; struct sockaddr_in *sin = pri->mcast_addr; mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr; mreq.imr_interface.s_addr = 0; if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) { printk("mcast_open: IP_DROP_MEMBERSHIP failed, error = %d\n", errno); } os_close_file(fd); }
int new_mm(unsigned long stack) { int fd, err; fd = os_open_file("/proc/mm", of_cloexec(of_write(OPENFLAGS())), 0); if (fd < 0) return fd; if (skas_needs_stub) { err = map_stub_pages(fd, STUB_CODE, STUB_DATA, stack); if (err) { os_close_file(fd); return err; } } return fd; }
static int __init make_umid(int (*printer)(const char *fmt, ...)) { int fd, err; char tmp[strlen(uml_dir) + UMID_LEN + 1]; strlcpy(tmp, uml_dir, sizeof(tmp)); if(!umid_inited){ strcat(tmp, "XXXXXX"); fd = mkstemp(tmp); if(fd < 0){ (*printer)("make_umid - mkstemp(%s) failed: %s\n", tmp,strerror(errno)); return(1); } os_close_file(fd); /* There's a nice tiny little race between this unlink and * the mkdir below. It'd be nice if there were a mkstemp * for directories. */ unlink(tmp); set_umid(&tmp[strlen(uml_dir)], 1, printer); } sprintf(tmp, "%s%s", uml_dir, umid); err = mkdir(tmp, 0777); if(err < 0){ if(errno == EEXIST){ if(not_dead_yet(tmp)){ (*printer)("umid '%s' is in use\n", umid); return(-1); } err = mkdir(tmp, 0777); } } if(err < 0){ (*printer)("Failed to create %s - errno = %d\n", umid, errno); return(-1); } return(0); }
void destroy_context_skas(struct mm_struct *mm) { struct mmu_context_skas *mmu = &mm->context.skas; if(proc_mm) os_close_file(mmu->id.u.mm_fd); else os_kill_ptraced_process(mmu->id.u.pid, 1); if(!proc_mm || !ptrace_faultinfo){ free_page(mmu->id.stack); pte_lock_deinit(virt_to_page(mmu->last_page_table)); pte_free_kernel((pte_t *) mmu->last_page_table); dec_zone_page_state(virt_to_page(mmu->last_page_table), NR_PAGETABLE); #ifdef CONFIG_3_LEVEL_PGTABLES pmd_free((pmd_t *) mmu->last_pmd); #endif } }
static int load_initrd(char *filename, void *buf, int size) { int fd, n; fd = os_open_file(filename, of_read(OPENFLAGS()), 0); if (fd < 0) { printk(KERN_ERR "Opening '%s' failed - err = %d\n", filename, -fd); return -1; } n = os_read_file(fd, buf, size); if (n != size) { printk(KERN_ERR "Read of %d bytes from '%s' failed, " "err = %d\n", size, filename, -n); return -1; } os_close_file(fd); return 0; }