int lua_apr_file_open(lua_State *L) { apr_status_t status; lua_apr_file *file; apr_int32_t flags; const char *path; apr_fileperms_t perm; # if defined(WIN32) || defined(OS2) || defined(NETWARE) /* On Windows apr_os_file_t isn't an integer: it's a HANDLE. */ if (0) ; # else /* On UNIX like systems apr_os_file_t is an integer. */ if (lua_isnumber(L, 1)) { apr_os_file_t fd = (apr_os_file_t) lua_tonumber(L, 1); flags = parse_mode_str(luaL_optstring(L, 2, "r")); file = file_alloc(L, NULL, NULL); status = apr_os_file_put(&file->handle, &fd, flags, file->pool->ptr); } # endif else { path = luaL_checkstring(L, 1); perm = check_permissions(L, 3, 0); flags = parse_mode_str(luaL_optstring(L, 2, "r")); file = file_alloc(L, path, NULL); status = apr_file_open(&file->handle, path, flags, perm, file->pool->ptr); } if (status != APR_SUCCESS) return push_file_error(L, file, status); init_file_buffers(L, file, !(flags & APR_FOPEN_BINARY)); return 1; }
int main(int argc ,char *argv[]) { struct stat buf; pthread_t tid1,tid2; void *rect; if (argc < 3) usage(); stat(argv[1],&buf); if (!buf.st_size) { printf("empty file!aborted.\n"); exit(-1); } fi = file_alloc(argv[1],argv[2]); sys_err(fi); printf("starting copy %s to %s...\n",argv[1],argv[2]); pthread_create(&tid1,NULL,tcopy,(void *)0); pthread_create(&tid2,NULL,tcopy,(void *)1); pthread_join(tid1,&rect); pthread_join(tid2,&rect); printf("copy %s to %s,completed.\n",argv[1],argv[2]); free_mem(); return 0; }
int fatfs_open(const char *pathname, int flags) { int ret; int fildes; FRESULT result; fildes = file_alloc(); if (fildes < 0) { errno = ENFILE; ret = -1; } else { result = fatfs_open_file_or_dir(pathname, flags, fildes); if (result == FR_OK) { ret = fildes; } else { errno = fresult2errno(result); file_free(fildes); ret = -1; } } return ret; }
FILE * serdrv_tty_fopen(struct serdrv * drv) { struct tty_dev * tty; FILE * f_raw; FILE * f_tty; f_raw = file_alloc(drv, &serdrv_ops); tty = tty_attach(f_raw); f_tty = tty_fopen(tty); return f_tty; }
int main(int argc, char **argv) { char *src,*srcf = "foo.tar"; int srcn; char *f1,*f1t,*f1f = "temp.in.txt"; int f1n, f1nt; char *f2,*f2t,*f2f = "temp.out.txt"; int f2n, f2nt; FILE *fp; Tar *t; src = file_alloc(srcf,&srcn); t = tar_from_data(src,srcn); printf("testing tar\n"); #define TST(COND,MSG) \ if(!(COND)){ \ printf("error " MSG " cond " #COND "\n"); \ exit(1); \ } f1 = file_alloc(f1f,&f1n); f1t = tar_alloc_cur_file(t,&f1nt); TST(0==strcmp(f1f,t->hdr.fname),"not the same filename") TST(f1n == f1nt,"f1 lengths not equal"); TST(0==memcmp(f1,f1t,f1n),"f1 memory not the same"); free(f1); free(f1t); TST(tar_next(t),"couldn't move to second archive'd file"); f2 = file_alloc(f2f,&f2n); f2t = tar_cur_file(t,&f2nt); TST(0==strcmp(f2f,t->hdr.fname),"not the same filename") TST(f2n == f2nt,"f2 lengths not equal"); TST(0==memcmp(f2,f2t,f2n),"f2 memory not the same"); free(f2); TST(!tar_next(t),"able to move to third archive, should be EOF"); Tar_Destroy(t); printf("done\n"); }
int _epoll_create(int ignored) { int i, fd; struct epoll_file *ef = NULL; struct file *f = NULL; /* find epoll_file struct */ for(i = 0; i < EPOLL_MAXFDS; i++) { if(!files[i].file) { ef = &files[i]; break; } } if(!ef) { errno = ENOMEM; return -1; } /* allocate file struct */ f = file_alloc(); if(!f) { errno = ENOMEM; return -1; } /* allocate fd */ fd = fd_alloc(f); if(fd == -1) { file_close(f); errno = EMFILE; return -1; } /* got everything, fill out structures */ f->f_ops = &epoll_operations; f->f_cookie_ptr = (void *)ef; ef->file = f; for(i = 0; i < EPOLL_MAXFDS; i++) { ef->entries[i].fd = -1; } /* return fd */ return fd; }
static int get_pipe(lua_State *L, apr_file_t *handle, const char *key) { lua_apr_file *file; if (object_env_private(L, 1)) { lua_getfield(L, -1, key); /* get cached pipe object */ if (lua_type(L, -1) == LUA_TUSERDATA) return 1; /* return cached pipe object */ lua_pop(L, 1); /* pop nil result from lua_getfield() */ } if (handle != NULL) { file = file_alloc(L, NULL, NULL); file->handle = handle; init_file_buffers(L, file, 1); lua_pushvalue(L, -1); /* copy reference to userdata */ lua_setfield(L, -3, key); /* cache userdata in environment table */ return 1; /* return new pipe */ } return 0; }
/* * Attempt to load the file (file) as an ELF module. It will be stored at * (dest), and a pointer to a module structure describing the loaded object * will be saved in (result). */ int __elfN(obj_loadfile)(char *filename, u_int64_t dest, struct preloaded_file **result) { struct preloaded_file *fp, *kfp; struct elf_file ef; Elf_Ehdr *hdr; int err; ssize_t bytes_read; fp = NULL; bzero(&ef, sizeof(struct elf_file)); /* * Open the image, read and validate the ELF header */ if (filename == NULL) /* can't handle nameless */ return(EFTYPE); if ((ef.fd = open(filename, O_RDONLY)) == -1) return(errno); hdr = &ef.hdr; bytes_read = read(ef.fd, hdr, sizeof(*hdr)); if (bytes_read != sizeof(*hdr)) { err = EFTYPE; /* could be EIO, but may be small file */ goto oerr; } /* Is it ELF? */ if (!IS_ELF(*hdr)) { err = EFTYPE; goto oerr; } if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */ hdr->e_ident[EI_DATA] != ELF_TARG_DATA || hdr->e_ident[EI_VERSION] != EV_CURRENT || /* Version ? */ hdr->e_version != EV_CURRENT || hdr->e_machine != ELF_TARG_MACH || /* Machine ? */ hdr->e_type != ET_REL) { err = EFTYPE; goto oerr; } if (hdr->e_shnum * hdr->e_shentsize == 0 || hdr->e_shoff == 0 || hdr->e_shentsize != sizeof(Elf_Shdr)) { err = EFTYPE; goto oerr; } kfp = file_findfile(NULL, NULL); if (kfp == NULL) { printf("elf" __XSTRING(__ELF_WORD_SIZE) "_obj_loadfile: can't load module before kernel\n"); err = EPERM; goto oerr; } if (strcmp(__elfN(obj_kerneltype), kfp->f_type)) { printf("elf" __XSTRING(__ELF_WORD_SIZE) "_obj_loadfile: can't load module with kernel type '%s'\n", kfp->f_type); err = EPERM; goto oerr; } if (archsw.arch_loadaddr != NULL) dest = archsw.arch_loadaddr(LOAD_ELF, hdr, dest); else dest = roundup(dest, PAGE_SIZE); /* * Ok, we think we should handle this. */ fp = file_alloc(); if (fp == NULL) { printf("elf" __XSTRING(__ELF_WORD_SIZE) "_obj_loadfile: cannot allocate module info\n"); err = EPERM; goto out; } fp->f_name = strdup(filename); fp->f_type = strdup(__elfN(obj_moduletype)); printf("%s ", filename); fp->f_size = __elfN(obj_loadimage)(fp, &ef, dest); if (fp->f_size == 0 || fp->f_addr == 0) goto ioerr; /* save exec header as metadata */ file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*hdr), hdr); /* Load OK, return module pointer */ *result = (struct preloaded_file *)fp; err = 0; goto out; ioerr: err = EIO; oerr: file_discard(fp); out: close(ef.fd); if (ef.e_shdr != NULL) free(ef.e_shdr); return(err); }
int __elfN(loadfile_raw)(char *filename, u_int64_t dest, struct preloaded_file **result, int multiboot) { struct preloaded_file *fp, *kfp; struct elf_file ef; Elf_Ehdr *ehdr; int err; fp = NULL; bzero(&ef, sizeof(struct elf_file)); ef.fd = -1; err = __elfN(load_elf_header)(filename, &ef); if (err != 0) return (err); ehdr = ef.ehdr; /* * Check to see what sort of module we are. */ kfp = file_findfile(NULL, __elfN(kerneltype)); #ifdef __powerpc__ /* * Kernels can be ET_DYN, so just assume the first loaded object is the * kernel. This assumption will be checked later. */ if (kfp == NULL) ef.kernel = 1; #endif if (ef.kernel || ehdr->e_type == ET_EXEC) { /* Looks like a kernel */ if (kfp != NULL) { printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: kernel already loaded\n"); err = EPERM; goto oerr; } /* * Calculate destination address based on kernel entrypoint. * * For ARM, the destination address is independent of any values in the * elf header (an ARM kernel can be loaded at any 2MB boundary), so we * leave dest set to the value calculated by archsw.arch_loadaddr() and * passed in to this function. */ #ifndef __arm__ if (ehdr->e_type == ET_EXEC) dest = (ehdr->e_entry & ~PAGE_MASK); #endif if ((ehdr->e_entry & ~PAGE_MASK) == 0) { printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: not a kernel (maybe static binary?)\n"); err = EPERM; goto oerr; } ef.kernel = 1; } else if (ehdr->e_type == ET_DYN) { /* Looks like a kld module */ if (multiboot != 0) { printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module as multiboot\n"); err = EPERM; goto oerr; } if (kfp == NULL) { printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module before kernel\n"); err = EPERM; goto oerr; } if (strcmp(__elfN(kerneltype), kfp->f_type)) { printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module with kernel type '%s'\n", kfp->f_type); err = EPERM; goto oerr; } /* Looks OK, got ahead */ ef.kernel = 0; } else { err = EFTYPE; goto oerr; } if (archsw.arch_loadaddr != NULL) dest = archsw.arch_loadaddr(LOAD_ELF, ehdr, dest); else dest = roundup(dest, PAGE_SIZE); /* * Ok, we think we should handle this. */ fp = file_alloc(); if (fp == NULL) { printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: cannot allocate module info\n"); err = EPERM; goto out; } if (ef.kernel == 1 && multiboot == 0) setenv("kernelname", filename, 1); fp->f_name = strdup(filename); if (multiboot == 0) fp->f_type = strdup(ef.kernel ? __elfN(kerneltype) : __elfN(moduletype)); else fp->f_type = strdup("elf multiboot kernel"); #ifdef ELF_VERBOSE if (ef.kernel) printf("%s entry at 0x%jx\n", filename, (uintmax_t)ehdr->e_entry); #else printf("%s ", filename); #endif fp->f_size = __elfN(loadimage)(fp, &ef, dest); if (fp->f_size == 0 || fp->f_addr == 0) goto ioerr; /* save exec header as metadata */ file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr); /* Load OK, return module pointer */ *result = (struct preloaded_file *)fp; err = 0; goto out; ioerr: err = EIO; oerr: file_discard(fp); out: if (ef.firstpage) free(ef.firstpage); if (ef.fd != -1) close(ef.fd); return(err); }
static int multiboot_loadfile(char *filename, u_int64_t dest, struct preloaded_file **result) { uint32_t *magic; int i, error; caddr_t header_search; ssize_t search_size; int fd; struct multiboot_header *header; struct preloaded_file *fp; if (filename == NULL) return (EFTYPE); /* is kernel already loaded? */ fp = file_findfile(NULL, NULL); if (fp != NULL) { return (EFTYPE); } if ((fd = open(filename, O_RDONLY)) == -1) return (errno); /* * Read MULTIBOOT_SEARCH size in order to search for the * multiboot magic header. */ header_search = malloc(MULTIBOOT_SEARCH); if (header_search == NULL) { close(fd); return (ENOMEM); } search_size = read(fd, header_search, MULTIBOOT_SEARCH); magic = (uint32_t *)header_search; header = NULL; for (i = 0; i < (search_size / sizeof(uint32_t)); i++) { if (magic[i] == MULTIBOOT_HEADER_MAGIC) { header = (struct multiboot_header *)&magic[i]; break; } } if (header == NULL) { error = EFTYPE; goto out; } /* Valid multiboot header has been found, validate checksum */ if (header->magic + header->flags + header->checksum != 0) { printf( "Multiboot checksum failed, magic: 0x%x flags: 0x%x checksum: 0x%x\n", header->magic, header->flags, header->checksum); error = EFTYPE; goto out; } if ((header->flags & ~MULTIBOOT_SUPPORTED_FLAGS) != 0) { printf("Unsupported multiboot flags found: 0x%x\n", header->flags); error = EFTYPE; goto out; } /* AOUT KLUDGE means we just load entire flat file as blob */ if (header->flags & MULTIBOOT_AOUT_KLUDGE) { vm_offset_t laddr; int got; dest = header->load_addr; if (lseek(fd, 0, SEEK_SET) == -1) { printf("lseek failed\n"); error = EIO; goto out; } laddr = dest; for (;;) { got = archsw.arch_readin(fd, laddr, 4096); if (got == 0) break; if (got < 0) { printf("error reading: %s", strerror(errno)); error = EIO; goto out; } laddr += got; } fp = file_alloc(); if (fp == NULL) { error = ENOMEM; goto out; } fp->f_name = strdup(filename); fp->f_type = strdup("aout multiboot kernel"); fp->f_addr = header->entry_addr; fp->f_size = laddr - dest; if (fp->f_size == 0) { file_discard(fp); error = EIO; goto out; } fp->f_metadata = NULL; error = 0; } else { error = elf32_loadfile_raw(filename, dest, &fp, 1); if (error != 0) { printf("elf32_loadfile_raw failed: %d unable to " "load multiboot kernel\n", error); goto out; } } setenv("kernelname", fp->f_name, 1); bios_addsmapdata(fp); *result = fp; out: free(header_search); close(fd); return (error); }
/* * Attempt to load the file (file) as an ELF module. It will be stored at * (dest), and a pointer to a module structure describing the loaded object * will be saved in (result). */ int __elfN(loadfile)(char *filename, u_int64_t dest, struct preloaded_file **result) { struct preloaded_file *fp, *kfp; struct elf_file ef; Elf_Ehdr *ehdr; int err; ssize_t bytes_read; fp = NULL; bzero(&ef, sizeof(struct elf_file)); /* * Open the image, read and validate the ELF header */ if (filename == NULL) /* can't handle nameless */ return(EFTYPE); if ((ef.fd = open(filename, O_RDONLY)) == -1) return(errno); ef.firstpage = malloc(PAGE_SIZE); if (ef.firstpage == NULL) { close(ef.fd); return(ENOMEM); } bytes_read = read(ef.fd, ef.firstpage, PAGE_SIZE); ef.firstlen = (size_t)bytes_read; if (bytes_read < 0 || ef.firstlen <= sizeof(Elf_Ehdr)) { err = EFTYPE; /* could be EIO, but may be small file */ goto oerr; } ehdr = ef.ehdr = (Elf_Ehdr *)ef.firstpage; /* Is it ELF? */ if (!IS_ELF(*ehdr)) { err = EFTYPE; goto oerr; } if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */ ehdr->e_ident[EI_DATA] != ELF_TARG_DATA || ehdr->e_ident[EI_VERSION] != EV_CURRENT || /* Version ? */ ehdr->e_version != EV_CURRENT || ehdr->e_machine != ELF_TARG_MACH) { /* Machine ? */ err = EFTYPE; goto oerr; } /* * Check to see what sort of module we are. */ kfp = file_findfile(NULL, NULL); if (ehdr->e_type == ET_DYN) { /* Looks like a kld module */ if (kfp == NULL) { printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module before kernel\n"); err = EPERM; goto oerr; } if (strcmp(__elfN(kerneltype), kfp->f_type)) { printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module with kernel type '%s'\n", kfp->f_type); err = EPERM; goto oerr; } /* Looks OK, got ahead */ ef.kernel = 0; } else if (ehdr->e_type == ET_EXEC) { /* Looks like a kernel */ if (kfp != NULL) { printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: kernel already loaded\n"); err = EPERM; goto oerr; } /* * Calculate destination address based on kernel entrypoint */ dest = (ehdr->e_entry & ~PAGE_MASK); if (dest == 0) { printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: not a kernel (maybe static binary?)\n"); err = EPERM; goto oerr; } ef.kernel = 1; } else { err = EFTYPE; goto oerr; } if (archsw.arch_loadaddr != NULL) dest = archsw.arch_loadaddr(LOAD_ELF, ehdr, dest); else dest = roundup(dest, PAGE_SIZE); /* * Ok, we think we should handle this. */ fp = file_alloc(); if (fp == NULL) { printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: cannot allocate module info\n"); err = EPERM; goto out; } if (ef.kernel) setenv("kernelname", filename, 1); fp->f_name = strdup(filename); fp->f_type = strdup(ef.kernel ? __elfN(kerneltype) : __elfN(moduletype)); #ifdef ELF_VERBOSE if (ef.kernel) printf("%s entry at 0x%jx\n", filename, (uintmax_t)ehdr->e_entry); #else printf("%s ", filename); #endif fp->f_size = __elfN(loadimage)(fp, &ef, dest); if (fp->f_size == 0 || fp->f_addr == 0) goto ioerr; /* save exec header as metadata */ file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr); /* Load OK, return module pointer */ *result = (struct preloaded_file *)fp; err = 0; goto out; ioerr: err = EIO; oerr: file_discard(fp); out: if (ef.firstpage) free(ef.firstpage); close(ef.fd); return(err); }
struct file * serial_fopen(struct serial_dev * dev) { return file_alloc(dev, &serial_fileop); }