int log_init(const char *output) { int new_logfd, sfd, dfd; dfd = get_service_fd(LOG_DIR_FD_OFF); if (dfd < 0) { pr_msg("Can't obtain logfd"); goto err; } if (logdir < 0) { int tmp; tmp = open(".", O_RDONLY); if (tmp == -1) { pr_perror("Can't open a current directory"); return -1; } if (reopen_fd_as(dfd, tmp) < 0) return -1; logdir = dfd; } sfd = get_service_fd(LOG_FD_OFF); if (sfd < 0) { pr_msg("Can't obtain logfd"); goto err; } if (output) { new_logfd = openat(logdir, output, O_CREAT | O_TRUNC | O_WRONLY | O_APPEND, 0600); if (new_logfd < 0) { pr_perror("Can't create log file %s", output); return -1; } if (sfd == current_logfd) close(sfd); if (reopen_fd_as(sfd, new_logfd) < 0) goto err; } else { new_logfd = dup2(DEFAULT_LOGFD, sfd); if (new_logfd < 0) { pr_perror("Dup %d -> %d failed", DEFAULT_LOGFD, sfd); goto err; } } current_logfd = sfd; return 0; err: pr_perror("Log engine failure, can't duplicate descriptor"); return -1; }
int set_proc_fd(int fd) { int sfd = get_service_fd(PROC_FD_OFF); sfd = dup2(fd, sfd); if (sfd < 0) { pr_perror("Can't set proc fd\n"); return -1; } proc_dir_fd = sfd; return 0; }
void close_image(struct cr_img *img) { if (lazy_image(img)) { /* * Remove the image file if it's there so that * subsequent restore doesn't read wrong or fake * data from it. */ unlinkat(get_service_fd(IMG_FD_OFF), img->path, 0); xfree(img->path); } else if (!empty_image(img)) bclose(&img->_x); xfree(img); }
int open_image_lazy(struct cr_img *img) { int dfd; char *path = img->path; img->path = NULL; dfd = get_service_fd(IMG_FD_OFF); if (do_open_image(img, dfd, img->type, img->oflags, path)) { xfree(path); return -1; } xfree(path); return 0; }
int check_mnt_id(void) { struct fdinfo_common fdinfo = { .mnt_id = -1 }; int ret; if (opts.check_ms_kernel) { pr_warn("Skipping mnt_id support check\n"); return 0; } ret = parse_fdinfo(get_service_fd(LOG_FD_OFF), FD_TYPES__UND, NULL, &fdinfo); if (ret < 0) return -1; if (fdinfo.mnt_id == -1) { pr_err("fdinfo doesn't contain the mnt_id field\n"); return -1; } return 0; } static int check_fdinfo_signalfd(void) { int fd, ret; sigset_t mask; sigemptyset(&mask); sigaddset(&mask, SIGUSR1); fd = signalfd(-1, &mask, 0); if (fd < 0) { pr_perror("Can't make signalfd"); return -1; } ret = parse_fdinfo(fd, FD_TYPES__SIGNALFD, check_one_sfd, NULL); close(fd); if (ret) { pr_err("Error parsing proc fdinfo\n"); return -1; } return 0; }
int open_image_dir(void) { int fd; image_dir_fd = get_service_fd(IMG_FD_OFF); if (image_dir_fd < 0) { pr_perror("Can't get image fd"); return -1; } fd = open(".", O_RDONLY); if (fd < 0) { pr_perror("Can't open cwd"); return -1; } pr_info("Image dir fd is %d\n", image_dir_fd); return reopen_fd_as(image_dir_fd, fd); }
struct cr_img *open_image_at(int dfd, int type, unsigned long flags, ...) { struct cr_img *img; unsigned long oflags; char path[PATH_MAX]; va_list args; bool lazy = false; if (dfd == -1) { dfd = get_service_fd(IMG_FD_OFF); lazy = (flags & O_CREAT); } img = xmalloc(sizeof(*img)); if (!img) return NULL; oflags = flags | imgset_template[type].oflags; va_start(args, flags); vsnprintf(path, PATH_MAX, imgset_template[type].fmt, args); va_end(args); if (lazy) { img->fd = LAZY_IMG_FD; img->type = type; img->oflags = oflags; img->path = xstrdup(path); return img; } else img->fd = EMPTY_IMG_FD; if (do_open_image(img, dfd, type, oflags, path)) { close_image(img); return NULL; } return img; }
int set_proc_mountpoint(char *path) { int sfd = get_service_fd(PROC_FD_OFF), fd; close_proc(); fd = open(path, O_DIRECTORY | O_RDONLY); if (fd == -1) { pr_err("Can't open %s\n", path); return -1; } sfd = dup2(fd, sfd); close(fd); if (sfd < 0) { pr_err("Can't set proc fd"); return -1; } proc_dir_fd = sfd; return 0; }
InventoryEntry *get_parent_inventory(void) { struct cr_img *img; InventoryEntry *ie; int dir; dir = openat(get_service_fd(IMG_FD_OFF), CR_PARENT_LINK, O_RDONLY); if (dir == -1) { pr_warn("Failed to open parent directory\n"); return NULL; } img = open_image_at(dir, CR_FD_INVENTORY, O_RSTR); if (!img) { pr_warn("Failed to open parent pre-dump inventory image\n"); close(dir); return NULL; } if (pb_read_one(img, &ie, PB_INVENTORY) < 0) { pr_warn("Failed to read parent pre-dump inventory entry\n"); close_image(img); close(dir); return NULL; } if (!ie->has_dump_uptime) { pr_warn("Parent pre-dump inventory has no uptime\n"); inventory_entry__free_unpacked(ie, NULL); ie = NULL; } close_image(img); close(dir); return ie; }
void kill_inventory(void) { unlinkat(get_service_fd(IMG_FD_OFF), fdset_template[CR_FD_INVENTORY].fmt, 0); }
int open_pages_image(unsigned long flags, int pm_fd) { return open_pages_image_at(get_service_fd(IMG_FD_OFF), flags, pm_fd); }
struct cr_img *open_pages_image(unsigned long flags, struct cr_img *pmi) { return open_pages_image_at(get_service_fd(IMG_FD_OFF), flags, pmi); }
int log_get_fd(void) { int fd = get_service_fd(LOG_FD_OFF); return fd < 0 ? DEFAULT_LOGFD : fd; }
bool is_service_fd(int fd, enum sfd_type type) { return fd == get_service_fd(type); }