Branch data Line data Source code
1 : : /* An external file is a file, which is dumped with help a plugin */
2 : :
3 : : #include <unistd.h>
4 : :
5 : : #include "fdset.h"
6 : : #include "files.h"
7 : : #include "plugin.h"
8 : :
9 : : #include "protobuf.h"
10 : : #include "protobuf/ext-file.pb-c.h"
11 : :
12 : 4 : static int dump_one_ext_file(int lfd, u32 id, const struct fd_parms *p)
13 : : {
14 : : int rfd, ret;
15 : :
16 : 4 : ExtFileEntry xfe = EXT_FILE_ENTRY__INIT;
17 : :
18 : 4 : ret = cr_plugin_dump_file(lfd, id);
19 [ + - ]: 4 : if (ret < 0)
20 : : return ret;
21 : :
22 : 4 : xfe.id = id;
23 : 4 : xfe.fown = (FownEntry *)&p->fown;
24 : :
25 : 4 : rfd = fdset_fd(glob_fdset, CR_FD_EXT_FILES);
26 : :
27 : 4 : return pb_write_one(rfd, &xfe, PB_EXT_FILE);
28 : : }
29 : :
30 : : const struct fdtype_ops ext_dump_ops = {
31 : : .type = FD_TYPES__EXT,
32 : : .dump = dump_one_ext_file,
33 : : };
34 : :
35 : : struct ext_file_info {
36 : : struct file_desc d;
37 : : ExtFileEntry *xfe;
38 : : };
39 : :
40 : 2 : static int open_fd(struct file_desc *d)
41 : : {
42 : : struct ext_file_info *xfi;
43 : : int fd;
44 : :
45 : : xfi = container_of(d, struct ext_file_info, d);
46 : :
47 : 2 : fd = cr_plugin_restore_file(xfi->xfe->id);
48 [ - + ]: 2 : if (fd < 0) {
49 : 0 : pr_err("Unable to restore %#x\n", xfi->xfe->id);
50 : 0 : return -1;
51 : : }
52 : :
53 [ + - ]: 2 : if (restore_fown(fd, xfi->xfe->fown))
54 : : return -1;
55 : :
56 : 2 : return fd;
57 : : }
58 : :
59 : : static struct file_desc_ops ext_desc_ops = {
60 : : .type = FD_TYPES__EXT,
61 : : .open = open_fd,
62 : : };
63 : :
64 : 2 : static int collect_one_ext(void *o, ProtobufCMessage *base)
65 : : {
66 : : struct ext_file_info *xfi = o;
67 : :
68 : 2 : xfi->xfe = pb_msg(base, ExtFileEntry);
69 : :
70 : 2 : pr_info("Collected external file with ID %#x\n", xfi->xfe->id);
71 : 2 : return file_desc_add(&xfi->d, xfi->xfe->id, &ext_desc_ops);
72 : : }
73 : :
74 : : struct collect_image_info ext_file_cinfo = {
75 : : .fd_type = CR_FD_EXT_FILES,
76 : : .pb_type = PB_EXT_FILE,
77 : : .priv_size = sizeof(struct ext_file_info),
78 : : .collect = collect_one_ext,
79 : : };
80 : :
81 : 4 : int dump_unsupp_fd(struct fd_parms *p, int lfd,
82 : : const int fdinfo, char *more, char *info)
83 : : {
84 : : int ret;
85 : :
86 : 4 : ret = do_dump_gen_file(p, lfd, &ext_dump_ops, fdinfo);
87 [ - + ]: 4 : if (ret == 0)
88 : : return 0;
89 [ # # ]: 0 : if (ret == -ENOTSUP)
90 : 0 : pr_err("Can't dump file %d of that type [%o] (%s %s)\n",
91 : : p->fd, p->stat.st_mode, more, info);
92 : : return -1;
93 : : }
|