int cr_plugin_restore_ext_mount(int id, char *mountpoint, char *old_root, int *is_file) { int fd; char img[64], src[256], *src_file; pr_info("Restoring my mount %d?\n", id); sprintf(img, IMG_NAME, id); fd = openat(criu_get_image_dir(), img, O_RDONLY); if (fd < 0) { if (errno == ENOENT) return -ENOTSUP; pr_perror("Can't open my image"); return -1; } close(fd); src_file = getenv("EMP_ROOT_P"); if (!src_file) { pr_err("Can't get EMP_ROOT_P env\n"); return -1; } if (creat(mountpoint, 0600) < 0) { if (errno != EEXIST) { pr_perror("Can't make mountpoint"); return -1; } } if (is_file) *is_file = 1; sprintf(src, "/%s/%s", old_root, src_file); pr_info("Mount %s -> %s\n", src, mountpoint); if (mount(src, mountpoint, NULL, MS_BIND, NULL) < 0) { pr_perror("Can't bind mount"); return -1; } return 0; }
static int __sysctl_op(int dir, struct sysctl_req *req, int op) { int fd, ret = -1, nr = 1, flags; if (op == CTL_READ) flags = O_RDONLY; else flags = O_WRONLY; fd = openat(dir, req->name, flags); if (fd < 0) { if (errno == ENOENT && (req->flags & CTL_FLAGS_OPTIONAL)) return 0; pr_perror("Can't open sysctl %s", req->name); return -1; } switch (CTL_TYPE(req->type)) { case __CTL_U32A: nr = CTL_LEN(req->type); case CTL_U32: __SYSCTL_OP(ret, fd, req, u32, nr, op); break; case CTL_32: __SYSCTL_OP(ret, fd, req, s32, nr, op); break; case __CTL_U64A: nr = CTL_LEN(req->type); case CTL_U64: __SYSCTL_OP(ret, fd, req, u64, nr, op); break; case __CTL_STR: nr = CTL_LEN(req->type); __SYSCTL_OP(ret, fd, req, char, nr, op); break; } close_safe(&fd); return ret; }
_q_static q_vdb_ctx *q_vdb_open(/*const char *sroot, const char *svdb*/void) { q_vdb_ctx *ctx = xmalloc(sizeof(*ctx)); const char *sroot = NULL; const char *svdb = NULL; if (!sroot) sroot = portroot; ctx->portroot_fd = open(sroot, O_RDONLY|O_CLOEXEC|O_PATH); if (ctx->portroot_fd == -1) { warnp("could not open root: %s", sroot); goto f_error; } if (!svdb) svdb = portvdb; /* Skip the leading slash */ svdb++; if (*svdb == '\0') svdb = "."; /* Cannot use O_PATH as we want to use fdopendir() */ ctx->vdb_fd = openat(ctx->portroot_fd, svdb, O_RDONLY|O_CLOEXEC); if (ctx->vdb_fd == -1) { warnp("could not open vdb: %s (in root %s)", svdb, sroot); goto cp_error; } ctx->dir = fdopendir(ctx->vdb_fd); if (ctx->dir == NULL) goto cv_error; return ctx; cv_error: close(ctx->vdb_fd); cp_error: close(ctx->portroot_fd); f_error: free(ctx); return NULL; }
int ev_init(ev_callback input_cb, void *data) { DIR *dir; struct dirent *de; int fd; dir = opendir("/dev/input"); if(dir != 0) { while((de = readdir(dir))) { unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // fprintf(stderr,"/dev/input/%s\n", de->d_name); if(strncmp(de->d_name,"event",5)) continue; fd = openat(dirfd(dir), de->d_name, O_RDONLY); if(fd < 0) continue; /* read the evbits of the input device */ if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) < 0) { close(fd); continue; } /* TODO: add ability to specify event masks. For now, just assume * that only EV_KEY, EV_REL and EV_ABS event types are ever needed. */ if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_ABS, ev_bits)) { close(fd); continue; } ev_fds[ev_count].fd = fd; ev_fds[ev_count].events = POLLIN; ev_fdinfo[ev_count].cb = input_cb; ev_fdinfo[ev_count].data = data; ev_count++; ev_dev_count++; if(ev_dev_count == MAX_DEVICES) break; } } return 0; }
static int enumerate_binaries(const char *esp_path, const char *path, const char *prefix) { char *p; _cleanup_closedir_ DIR *d = NULL; struct dirent *de; int r = 0, c = 0; p = strjoina(esp_path, "/", path); d = opendir(p); if (!d) { if (errno == ENOENT) return 0; return log_error_errno(errno, "Failed to read \"%s\": %m", p); } FOREACH_DIRENT(de, d, break) { _cleanup_close_ int fd = -1; _cleanup_free_ char *v = NULL; if (!endswith_no_case(de->d_name, ".efi")) continue; if (prefix && !startswith_no_case(de->d_name, prefix)) continue; fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC); if (fd < 0) return log_error_errno(errno, "Failed to open \"%s/%s\" for reading: %m", p, de->d_name); r = get_file_version(fd, &v); if (r < 0) return r; if (r > 0) printf(" File: %s/%s/%s (%s)\n", special_glyph(TREE_RIGHT), path, de->d_name, v); else printf(" File: %s/%s/%s\n", special_glyph(TREE_RIGHT), path, de->d_name); c++; } return c; }
int simple_stable::init(int dfd, const char * name, const params & config, sys_journal * sysj) { int r = -1; params meta_config, data_config; const dtable_factory * meta = dtable_factory::lookup(config, "meta"); const ctable_factory * data = ctable_factory::lookup(config, "data"); if(md_dfd >= 0) deinit(); assert(column_map.empty()); if(!meta || !data) return -ENOENT; if(!config.get("meta_config", &meta_config, params())) return -EINVAL; if(!config.get("data_config", &data_config, params())) return -EINVAL; md_dfd = openat(dfd, name, O_RDONLY); if(md_dfd < 0) return md_dfd; dt_meta = meta->open(md_dfd, "st_meta", meta_config, sysj); if(!dt_meta) goto fail_meta; ct_data = data->open(md_dfd, "st_data", data_config, sysj); if(!ct_data) goto fail_data; /* check sanity? */ r = load_columns(); if(r < 0) goto fail_check; return 0; fail_check: delete ct_data; fail_data: dt_meta->destroy(); fail_meta: close(md_dfd); md_dfd = -1; return r; }
int simple_stable::create(int dfd, const char * name, const params & config, dtype::ctype key_type) { int md_dfd, r; params meta_config, data_config; const dtable_factory * meta = dtable_factory::lookup(config, "meta"); const ctable_factory * data = ctable_factory::lookup(config, "data"); if(!meta || !data) return -ENOENT; if(!config.get("meta_config", &meta_config, params())) return -EINVAL; if(!config.get("data_config", &data_config, params())) return -EINVAL; r = mkdirat(dfd, name, 0755); if(r < 0) return r; md_dfd = openat(dfd, name, O_RDONLY); if(md_dfd < 0) { r = md_dfd; goto fail_open; } /* the metadata is keyed by named properties (strings) */ r = meta->create(md_dfd, "st_meta", meta_config, dtype::STRING); if(r < 0) goto fail_meta; r = data->create(md_dfd, "st_data", data_config, key_type); if(r < 0) goto fail_data; close(md_dfd); return 0; fail_data: util::rm_r(md_dfd, "st_meta"); fail_meta: close(md_dfd); fail_open: unlinkat(dfd, name, AT_REMOVEDIR); return r; }
/* Return the filenames in directory NAME, relative to the chdir_fd. If the directory does not exist, report error if MUST_EXIST is true. Return NULL on errors. */ char * tar_savedir (const char *name, int must_exist) { char *ret = NULL; DIR *dir = NULL; int fd = openat (chdir_fd, name, open_read_flags | O_DIRECTORY); if (fd < 0) { if (!must_exist && errno == ENOENT) return NULL; open_error (name); } else if (! ((dir = fdopendir (fd)) && (ret = streamsavedir (dir)))) savedir_error (name); if (dir ? closedir (dir) != 0 : 0 <= fd && close (fd) != 0) savedir_error (name); return ret; }
int load_package_signature(struct pkg *pkg, int dirfd) { struct file_t file; _cleanup_free_ char *signame = joinstring(pkg->filename, ".sig", NULL); _cleanup_close_ int fd = openat(dirfd, signame, O_RDONLY); if (fd < 0) return -1; if (file_from_fd(&file, fd) < 0) return -1; base64_encode((unsigned char **)&pkg->base64sig, (const unsigned char *)file.mmap, file.st.st_size); if (file.st.st_mtime > pkg->mtime) file.st.st_mtime = pkg->mtime; file_close(&file); return 0; }
int mdir_open(mdir_t *mdir, const char *path) { int status = -1; START_PROFILING(mdir_open); if ((mdir->fdp = open(path, O_RDONLY | O_NOATIME, S_IRWXU)) < 0) { goto out; } if ((mdir->fdattrs = openat(mdir->fdp, MDIR_ATTRS_FNAME, O_RDWR | O_CREAT, S_IRWXU)) < 0) { int xerrno = errno; close(mdir->fdp); errno = xerrno; goto out; } status = 0; out: STOP_PROFILING(mdir_open); return status; }
static int callback_readdir (const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) { DIR *dp; struct dirent *de; int dfd; path = ENSURE_RELPATH (path); if (!*path) { dfd = fcntl (basefd, F_DUPFD_CLOEXEC, 3); lseek (dfd, 0, SEEK_SET); } else { dfd = openat (basefd, path, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOCTTY); if (dfd == -1) return -errno; } /* Transfers ownership of fd */ dp = fdopendir (dfd); if (dp == NULL) return -errno; while ((de = readdir (dp)) != NULL) { struct stat st; memset (&st, 0, sizeof (st)); st.st_ino = de->d_ino; st.st_mode = de->d_type << 12; if (filler (buf, de->d_name, &st, 0)) break; } (void) closedir (dp); return 0; }
static int __nonuserns_sysctl_op(struct sysctl_req *req, size_t nr_req, int op) { int dir, ret, exit_code = -1;; dir = open("/proc/sys", O_RDONLY, O_DIRECTORY); if (dir < 0) { pr_perror("Can't open sysctl dir"); return -1; } while (nr_req--) { int fd, flags; if (op == CTL_READ) flags = O_RDONLY; else flags = O_WRONLY; fd = openat(dir, req->name, flags); if (fd < 0) { if (errno == ENOENT && (req->flags & CTL_FLAGS_OPTIONAL)) { req++; continue; } pr_perror("Can't open sysctl %s", req->name); goto out; } ret = do_sysctl_op(fd, req, op); if (ret) goto out; close(fd); req++; } exit_code = 0; out: close(dir); return exit_code; }
static int solaris_attropen(const char *path, const char *attrpath, int oflag, mode_t mode) { EC_INIT; int filedes = -1, eafd = -1; if ((filedes = open(path, O_RDONLY | (oflag & O_NOFOLLOW), mode)) == -1) { switch (errno) { case ENOENT: case EEXIST: case OPEN_NOFOLLOW_ERRNO: EC_FAIL; default: LOG(log_debug, logtype_default, "open(\"%s\"): %s", fullpathname(path), strerror(errno)); errno = ENOATTR; EC_FAIL; } } if ((eafd = openat(filedes, attrpath, oflag | O_XATTR, mode)) == -1) { switch (errno) { case ENOENT: case EEXIST: EC_FAIL; default: LOG(log_debug, logtype_default, "openat(\"%s\"): %s", fullpathname(path), strerror(errno)); errno = ENOATTR; EC_FAIL; } } EC_CLEANUP: if (filedes != -1) close(filedes); if (ret != 0) { if (eafd != -1) close(eafd); eafd = -1; } return eafd; }
int open_image_dir(char *dir) { int fd, ret; fd = open(dir, O_RDONLY); if (fd < 0) { pr_perror("Can't open dir %s", dir); return -1; } ret = install_service_fd(IMG_FD_OFF, fd); close(fd); fd = ret; if (opts.img_parent) { int pfd; ret = symlinkat(opts.img_parent, fd, CR_PARENT_LINK); if (ret < 0) { pr_perror("Can't link parent snapshot"); goto err; } pfd = openat(fd, CR_PARENT_LINK, O_RDONLY); if (pfd < 0) { pr_perror("Can't open parent snapshot"); goto err; } ret = install_service_fd(PARENT_FD_OFF, pfd); close(pfd); } return ret; err: close_image_dir(); return -1; }
static int recv_reg(int socket, struct server_data* server_data, int dir_fd) { char* msg = NULL; int filefd; // struct stat statdata; int ret = -1; //VIR_DEBUG("receiving new regular file"); if (server_receive(socket, server_data, data_handler_stringify, &msg) != 0) { VIR_DEBUG("server_receive was not successfull (on filename)!"); goto err; } // VIR_DEBUG("about to receive reg '%s' at fd %d", msg, dir_fd); //open with permission 664 filefd = openat(dir_fd, msg, O_RDWR | O_CREAT | O_NOFOLLOW, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); if (filefd < 0) { VIR_DEBUG("errno: %s", strerror(errno)); goto err; } if (server_receive(socket, server_data, data_handler_filewriter, &filefd) != 0) { VIR_DEBUG("server_received was not successfull!"); goto err; } /* if (fstat(filefd, &statdata)) { VIR_DEBUG("cannot stat file"); goto err; } */ VIR_DEBUG("received file '%s'.", msg); ret = 0; err: close(filefd); free(msg); return ret; }
int util::rm_r(int dfd, const char * path) { DIR * dir; struct stat64 st; struct dirent * ent; int fd, copy, r = fstatat64(dfd, path, &st, AT_SYMLINK_NOFOLLOW); if(r < 0) return r; if(!S_ISDIR(st.st_mode)) return unlinkat(dfd, path, 0); fd = openat(dfd, path, O_RDONLY); if(fd < 0) return fd; copy = dup(fd); if(copy < 0) { close(fd); return copy; } dir = fdopendir(copy); if(!dir) { close(copy); close(fd); return -1; } while((ent = readdir(dir))) { if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) continue; r = rm_r(fd, ent->d_name); if(r < 0) break; } closedir(dir); close(fd); if(r < 0) return r; return unlinkat(dfd, path, AT_REMOVEDIR); }
int smack_revoke_subject(const char *subject) { int ret; int fd; int len; if (init_smackfs_mnt()) return -1; len = get_label(NULL, subject, NULL); if (len < 0) return -1; fd = openat(smackfs_mnt_dirfd, "revoke-subject", O_WRONLY); if (fd < 0) return -1; ret = write(fd, subject, len); close(fd); return (ret < 0) ? -1 : 0; }
static struct file *_file_new(struct dir *top_dir, const char *filename, int flags) { struct file *file = malloc(sizeof(struct file)); memset(file, 0, sizeof(struct file)); file->db = top_dir->db; file->top_dir = top_dir; char pathname[FILENAME_MAX]; snprintf(pathname, sizeof(pathname), "%s/%s", top_dir->pathname, filename); file->fd = openat(dirfd(top_dir->dir), filename, flags, 0600); FILETRACE(file, file->fd, "openat(\"%s\")", pathname); if (file->fd == -1) { free(file); return NULL; } file->pathname = strdup(pathname); return file; }
int dir_is_empty_at(int dir_fd, const char *path) { _cleanup_close_ int fd = -1; _cleanup_closedir_ DIR *d = NULL; struct dirent *de; if (path) fd = openat(dir_fd, path, O_RDONLY|O_DIRECTORY|O_CLOEXEC); else fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); if (fd < 0) return -errno; d = fdopendir(fd); if (!d) return -errno; fd = -1; FOREACH_DIRENT(de, d, return -errno) return 0; return 1; }
int main(void){ int fd; int dir_fd; dir_fd=open("./open",O_RDONLY); if(dir_fd==-1){ printf("dir error\n"); exit(1); } printf("%d\n",dir_fd); fd=openat(dir_fd,"log.txt",O_RDWR | O_CREAT | O_TRUNC); if(fd==-1){ printf("txt error\n"); close(dir_fd); exit(1); } close(fd); close(fd); printf("%d\n",fd); printf("success\n"); exit(0); }
/** * -empty test. */ bool eval_empty(const struct expr *expr, struct eval_state *state) { bool ret = false; struct BFTW *ftwbuf = state->ftwbuf; if (ftwbuf->typeflag == BFTW_DIR) { int dfd = openat(ftwbuf->at_fd, ftwbuf->at_path, O_DIRECTORY); if (dfd < 0) { eval_error(state); goto done; } DIR *dir = fdopendir(dfd); if (!dir) { eval_error(state); close(dfd); goto done; } ret = true; struct dirent *de; while ((de = readdir(dir)) != NULL) { if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) { ret = false; break; } } closedir(dir); } else { const struct stat *statbuf = fill_statbuf(state); if (statbuf) { ret = statbuf->st_size == 0; } } done: return ret; }
int solaris_fsetxattr(int fd, const char* key, const char *value, size_t size, int flags) { int attrfd = -1; int ret = 0; attrfd = openat (fd, key, flags|O_CREAT|O_WRONLY|O_XATTR, 0777); if (attrfd >= 0) { ftruncate (attrfd, 0); ret = write (attrfd, value, size); close (attrfd); } else { if (errno != ENOENT) gf_log ("libglusterfs", GF_LOG_ERROR, "Couldn't set extended attribute for %d (%d)", fd, errno); return -1; } return 0; }
/* utility function to read serialized metadata */ int readMetadata(BackupMetadata *meta, int dirfd, const char *name, int failIfNotFound) { int fd; FILE *fh; fd = openat(dirfd, name, O_RDONLY); if (fd < 0) { if (errno == ENOENT) { /* nonexistent */ memset(meta, 0, sizeof(BackupMetadata)); meta->type = MD_TYPE_NONEXIST; errno = ENOENT; /* preserve the ENOENT */ return failIfNotFound ? -1 : 0; } return -1; } fh = fdopen(fd, "r"); /* backup metadata: char type; int mode; int uid; int gid; long long size; long long mtime; long long ctime; */ if (fscanf(fh, "%c\n%d\n%d\n%d\n%lld\n%lld\n%lld\n", &meta->type, &meta->mode, &meta->uid, &meta->gid, &meta->size, &meta->mtime, &meta->ctime) != 7) { errno = EIO; fclose(fh); return -1; } fclose(fh); return 0; }
TEST(rewinddir, example) { // This should already load the first two entries in the directory. DIR *dirp = fdopendir(fd_tmp); // Create 1000 files in the meantime. for (int i = 0; i < 1000; ++i) { char filename[8]; snprintf(filename, sizeof(filename), "file%03d", i); ASSERT_EQ(0, close(openat(fd_tmp, filename, O_RDWR | O_CREAT))); } // We should only see the original first two files. size_t count = 0; while (readdir(dirp) != NULL) ++count; ASSERT_EQ(2, count); // Rewind and count the files in the directory again. rewinddir(dirp); count = 0; while (readdir(dirp) != NULL) ++count; ASSERT_EQ(1002, count); // Delete the test files again. for (int i = 0; i < 1000; ++i) { char filename[8]; snprintf(filename, sizeof(filename), "file%03d", i); ASSERT_EQ(0, unlinkat(fd_tmp, filename, 0)); } // We should have only two files now. rewinddir(dirp); count = 0; while (readdir(dirp) != NULL) ++count; ASSERT_EQ(2, count); }
int ev_init(void) { DIR *dir; struct dirent *de; int fd; has_mouse = 0; dir = opendir("/dev/input"); if(dir != 0) { while((de = readdir(dir))) { // fprintf(stderr,"/dev/input/%s\n", de->d_name); if(strncmp(de->d_name,"event",5)) continue; fd = openat(dirfd(dir), de->d_name, O_RDONLY); if(fd < 0) continue; ev_fds[ev_count].fd = fd; ev_fds[ev_count].events = POLLIN; evs[ev_count].fd = &ev_fds[ev_count]; /* Load virtualkeys if there are any */ vk_init(&evs[ev_count]); check_mouse(fd); ev_count++; if(ev_count == MAX_DEVICES) break; } closedir(dir); } struct stat st; if(stat("/dev/input", &st) >= 0) lastInputMTime = st.st_mtime; gettimeofday(&lastInputStat, NULL); return 0; }
int64_t calculate_dir_size(int dfd) { int64_t size = 0; struct stat s; DIR *d; struct dirent *de; d = fdopendir(dfd); if (d == NULL) { close(dfd); return 0; } while ((de = readdir(d))) { const char *name = de->d_name; if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { size += stat_size(&s); } if (de->d_type == DT_DIR) { int subfd; /* always skip "." and ".." */ if (name[0] == '.') { if (name[1] == 0) continue; if ((name[1] == '.') && (name[2] == 0)) continue; } subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY); if (subfd >= 0) { size += calculate_dir_size(subfd); } } } closedir(d); return size; }
int get_rundir() { char *rootdir, *dirname; int f_root, f_run; // TODO: Check for mkdirat, openat, mkfifoat, asprintf, and friends #if RUNTIME_DIR_USE_XDG == 1 if (!(rootdir = getenv("XDG_RUNTIME_DIR"))) { return -1; } #else rootdir = RUNTIME_DIR_TMPFS; #endif // Form directory name asprintf(&dirname, RUNTIME_DIR_BASENAME ".%d", getpid()); printf("Runtime dir: %s/%s\n", rootdir, dirname); // Now open everything if ((f_root = open(rootdir, O_DIRECTORY)) < 0) { fprintf(stderr, "%s: could not open: %s\n", rootdir, strerror(errno)); return -1; } if ((mkdirat(f_root, dirname, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) != 0) { fprintf(stderr, "%s/%s: could not make directory: %s\n", rootdir, dirname, strerror(errno)); return -1; } if ((f_run = openat(f_root, dirname, O_DIRECTORY)) < 0) { fprintf(stderr, "%s/%s: could not open: %s\n", rootdir, dirname, strerror(errno)); return -1; } return f_run; }
static struct pkg *load_from_file(int dirfd, const char *filename, const char *arch) { _cleanup_close_ int pkgfd = openat(dirfd, filename, O_RDONLY); if (pkgfd < 0) { err(EXIT_FAILURE, "failed to open %s", filename); } struct pkg *pkg = malloc(sizeof(pkg_t)); zero(pkg, sizeof(pkg_t)); if (load_package(pkg, pkgfd) < 0) goto error; if (arch && pkg->arch && !match_arch(pkg, arch)) goto error; pkg->filename = strdup(filename); return pkg; error: package_free(pkg); return NULL; }
int ev_init(void) { DIR *dir; struct dirent *de; int fd; dir = opendir("/dev/input"); testde=readdir(dir); if(dir != 0) { while((de = readdir(dir))) { if(strncmp(de->d_name,"event",5)) continue; fd = openat(dirfd(dir), de->d_name, O_RDONLY); if(fd < 0) continue; //LOGD("ev_fds[ev_count].fd = %d", fd); ev_fds[ev_count].fd = fd; ev_fds[ev_count].events = POLLIN; ev_count++; if(ev_count == MAX_DEVICES) break; } } return 0; }
static void create_and_populate_homedir(struct userconf *cnf, struct passwd *pwd, const char *skeldir, mode_t homemode, bool update) { int skelfd = -1; /* Create home parents directories */ mkdir_home_parents(conf.rootfd, pwd->pw_dir); if (skeldir != NULL && *skeldir != '\0') { if (*skeldir == '/') skeldir++; skelfd = openat(conf.rootfd, skeldir, O_DIRECTORY|O_CLOEXEC); } copymkdir(conf.rootfd, pwd->pw_dir, skelfd, homemode, pwd->pw_uid, pwd->pw_gid, 0); pw_log(cnf, update ? M_UPDATE : M_ADD, W_USER, "%s(%ju) home %s made", pwd->pw_name, (uintmax_t)pwd->pw_uid, pwd->pw_dir); }