Branch data Line data Source code
1 : : #include <unistd.h>
2 : : #include <stdio.h>
3 : : #include <stdlib.h>
4 : : #include <errno.h>
5 : : #include <fcntl.h>
6 : : #include <stdlib.h>
7 : : #include <string.h>
8 : : #include <limits.h>
9 : : #include <sys/stat.h>
10 : : #include <sys/statfs.h>
11 : : #include <sys/types.h>
12 : : #include <sys/ioctl.h>
13 : : #include <sys/eventfd.h>
14 : :
15 : : #include "compiler.h"
16 : : #include "asm/types.h"
17 : : #include "fdset.h"
18 : : #include "eventfd.h"
19 : : #include "proc_parse.h"
20 : : #include "image.h"
21 : : #include "util.h"
22 : : #include "log.h"
23 : :
24 : : #include "protobuf.h"
25 : : #include "protobuf/eventfd.pb-c.h"
26 : :
27 : : #undef LOG_PREFIX
28 : : #define LOG_PREFIX "eventfd: "
29 : :
30 : : struct eventfd_file_info {
31 : : EventfdFileEntry *efe;
32 : : struct file_desc d;
33 : : };
34 : :
35 : : /* Checks if file descriptor @lfd is eventfd */
36 : 20 : int is_eventfd_link(char *link)
37 : : {
38 : 20 : return is_anon_link_type(link, "[eventfd]");
39 : : }
40 : :
41 : : static void pr_info_eventfd(char *action, EventfdFileEntry *efe)
42 : : {
43 : 6 : pr_info("%s: id %#08x flags %#04x counter %#016"PRIx64"\n",
44 : : action, efe->id, efe->flags, efe->counter);
45 : : }
46 : :
47 : : struct eventfd_dump_arg {
48 : : u32 id;
49 : : const struct fd_parms *p;
50 : : bool dumped;
51 : : };
52 : :
53 : 4 : static int dump_eventfd_entry(union fdinfo_entries *e, void *arg)
54 : : {
55 : : struct eventfd_dump_arg *da = arg;
56 : :
57 [ - + ]: 4 : if (da->dumped) {
58 : 0 : pr_err("Several counters in a file?\n");
59 : 0 : return -1;
60 : : }
61 : :
62 : 4 : da->dumped = true;
63 : 4 : e->efd.id = da->id;
64 : 4 : e->efd.flags = da->p->flags;
65 : 4 : e->efd.fown = (FownEntry *)&da->p->fown;
66 : :
67 : 4 : pr_info_eventfd("Dumping ", &e->efd);
68 : 4 : return pb_write_one(fdset_fd(glob_fdset, CR_FD_EVENTFD_FILE),
69 : 4 : &e->efd, PB_EVENTFD_FILE);
70 : : }
71 : :
72 : 4 : static int dump_one_eventfd(int lfd, u32 id, const struct fd_parms *p)
73 : : {
74 : 4 : struct eventfd_dump_arg da = { .id = id, .p = p, };
75 : 4 : return parse_fdinfo(lfd, FD_TYPES__EVENTFD, dump_eventfd_entry, &da);
76 : : }
77 : :
78 : : const struct fdtype_ops eventfd_dump_ops = {
79 : : .type = FD_TYPES__EVENTFD,
80 : : .dump = dump_one_eventfd,
81 : : };
82 : :
83 : 2 : static int eventfd_open(struct file_desc *d)
84 : : {
85 : : struct eventfd_file_info *info;
86 : : int tmp;
87 : :
88 : : info = container_of(d, struct eventfd_file_info, d);
89 : :
90 : 2 : tmp = eventfd(info->efe->counter, 0);
91 [ - + ]: 2 : if (tmp < 0) {
92 : 0 : pr_perror("Can't create eventfd %#08x",
93 : : info->efe->id);
94 : 0 : return -1;
95 : : }
96 : :
97 [ - + ]: 2 : if (rst_file_params(tmp, info->efe->fown, info->efe->flags)) {
98 : 0 : pr_perror("Can't restore params on eventfd %#08x",
99 : : info->efe->id);
100 : : goto err_close;
101 : : }
102 : :
103 : : return tmp;
104 : :
105 : : err_close:
106 : 0 : close(tmp);
107 : 0 : return -1;
108 : : }
109 : :
110 : : static struct file_desc_ops eventfd_desc_ops = {
111 : : .type = FD_TYPES__EVENTFD,
112 : : .open = eventfd_open,
113 : : };
114 : :
115 : 2 : static int collect_one_efd(void *obj, ProtobufCMessage *msg)
116 : : {
117 : : struct eventfd_file_info *info = obj;
118 : :
119 : 2 : info->efe = pb_msg(msg, EventfdFileEntry);
120 : 2 : pr_info_eventfd("Collected ", info->efe);
121 : 2 : return file_desc_add(&info->d, info->efe->id, &eventfd_desc_ops);
122 : : }
123 : :
124 : : struct collect_image_info eventfd_cinfo = {
125 : : .fd_type = CR_FD_EVENTFD_FILE,
126 : : .pb_type = PB_EVENTFD_FILE,
127 : : .priv_size = sizeof(struct eventfd_file_info),
128 : : .collect = collect_one_efd,
129 : : };
|