Commit b1629341 authored by Pavel Emelyanov's avatar Pavel Emelyanov

files: Generalize file open, get rid of explicit types switch

Introduce and use generic file_desc ops for this.
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 3c174a88
...@@ -45,10 +45,12 @@ int prepare_shared_fdinfo(void) ...@@ -45,10 +45,12 @@ int prepare_shared_fdinfo(void)
return 0; return 0;
} }
void file_desc_add(struct file_desc *d, int type, u32 id) void file_desc_add(struct file_desc *d, int type, u32 id,
struct file_desc_ops *ops)
{ {
d->type = type; d->type = type;
d->id = id; d->id = id;
d->ops = ops;
INIT_LIST_HEAD(&d->fd_info_head); INIT_LIST_HEAD(&d->fd_info_head);
list_add_tail(&d->hash, &file_descs[id % FDESC_HASH_SIZE]); list_add_tail(&d->hash, &file_descs[id % FDESC_HASH_SIZE]);
...@@ -109,6 +111,12 @@ static struct reg_file_info *find_reg_file(int id) ...@@ -109,6 +111,12 @@ static struct reg_file_info *find_reg_file(int id)
return container_of(fd, struct reg_file_info, d); return container_of(fd, struct reg_file_info, d);
} }
static int open_fe_fd(struct file_desc *d);
static struct file_desc_ops reg_desc_ops = {
.open = open_fe_fd,
};
int collect_reg_files(void) int collect_reg_files(void)
{ {
struct reg_file_info *rfi = NULL; struct reg_file_info *rfi = NULL;
...@@ -144,7 +152,8 @@ int collect_reg_files(void) ...@@ -144,7 +152,8 @@ int collect_reg_files(void)
rfi->path[len] = '\0'; rfi->path[len] = '\0';
pr_info("Collected [%s] ID %x\n", rfi->path, rfi->rfe.id); pr_info("Collected [%s] ID %x\n", rfi->path, rfi->rfe.id);
file_desc_add(&rfi->d, FDINFO_REG, rfi->rfe.id); file_desc_add(&rfi->d, FDINFO_REG, rfi->rfe.id,
&reg_desc_ops);
} }
if (rfi) { if (rfi) {
...@@ -303,11 +312,9 @@ void transport_name_gen(struct sockaddr_un *addr, int *len, ...@@ -303,11 +312,9 @@ void transport_name_gen(struct sockaddr_un *addr, int *len,
static int should_open_transport(struct fdinfo_entry *fe, struct file_desc *fd) static int should_open_transport(struct fdinfo_entry *fe, struct file_desc *fd)
{ {
if (fe->type == FDINFO_PIPE) if (fd->ops->want_transport)
return pipe_should_open_transport(fe, fd); return fd->ops->want_transport(fe, fd);
if (fe->type == FDINFO_UNIXSK) else
return unixsk_should_open_transport(fe, fd);
return 0; return 0;
} }
...@@ -372,24 +379,7 @@ static int open_fd(int pid, struct fdinfo_entry *fe, ...@@ -372,24 +379,7 @@ static int open_fd(int pid, struct fdinfo_entry *fe,
if ((fle->pid != pid) || (fe->addr != fle->fd)) if ((fle->pid != pid) || (fe->addr != fle->fd))
return 0; return 0;
switch (fe->type) { tmp = d->ops->open(d);
case FDINFO_REG:
tmp = open_fe_fd(d);
break;
case FDINFO_INETSK:
tmp = open_inet_sk(d);
break;
case FDINFO_PIPE:
tmp = open_pipe(d);
break;
case FDINFO_UNIXSK:
tmp = open_unix_sk(d);
break;
default:
tmp = -1;
break;
}
if (tmp < 0) if (tmp < 0)
return -1; return -1;
......
...@@ -39,14 +39,23 @@ struct fdinfo_list_entry { ...@@ -39,14 +39,23 @@ struct fdinfo_list_entry {
futex_t real_pid; futex_t real_pid;
}; };
struct file_desc;
struct file_desc_ops {
int (*open)(struct file_desc *);
int (*want_transport)(struct fdinfo_entry *, struct file_desc *);
};
struct file_desc { struct file_desc {
int type; int type;
u32 id; u32 id;
struct list_head hash; struct list_head hash;
struct list_head fd_info_head; struct list_head fd_info_head;
struct file_desc_ops *ops;
}; };
extern void file_desc_add(struct file_desc *d, int type, u32 id); extern void file_desc_add(struct file_desc *d, int type, u32 id,
struct file_desc_ops *ops);
extern struct fdinfo_list_entry *file_master(struct file_desc *d); extern struct fdinfo_list_entry *file_master(struct file_desc *d);
extern struct file_desc *find_file_desc_raw(int type, u32 id); extern struct file_desc *find_file_desc_raw(int type, u32 id);
...@@ -65,8 +74,5 @@ extern int self_exe_fd; ...@@ -65,8 +74,5 @@ extern int self_exe_fd;
struct file_desc; struct file_desc;
extern int collect_pipes(void); extern int collect_pipes(void);
extern void mark_pipe_master(void); extern void mark_pipe_master(void);
extern int open_pipe(struct file_desc *);
extern int pipe_should_open_transport(struct fdinfo_entry *fe,
struct file_desc *d);
#endif /* FILES_H_ */ #endif /* FILES_H_ */
...@@ -13,15 +13,11 @@ extern int dump_socket(struct fd_parms *p, int lfd, ...@@ -13,15 +13,11 @@ extern int dump_socket(struct fd_parms *p, int lfd,
struct fdinfo_list_entry; struct fdinfo_list_entry;
struct file_desc; struct file_desc;
struct fdinfo_entry; struct fdinfo_entry;
extern int unixsk_should_open_transport(struct fdinfo_entry *,
struct file_desc *);
extern int collect_sockets(void); extern int collect_sockets(void);
extern int collect_inet_sockets(void); extern int collect_inet_sockets(void);
extern int collect_unix_sockets(void); extern int collect_unix_sockets(void);
extern int resolve_unix_peers(void); extern int resolve_unix_peers(void);
extern int run_unix_connections(void); extern int run_unix_connections(void);
extern int open_inet_sk(struct file_desc *);
extern int open_unix_sk(struct file_desc *);
struct cr_options; struct cr_options;
extern void show_unixsk(int fd, struct cr_options *); extern void show_unixsk(int fd, struct cr_options *);
extern void show_inetsk(int fd, struct cr_options *); extern void show_inetsk(int fd, struct cr_options *);
......
...@@ -35,6 +35,15 @@ static struct pipe_info *find_pipe(int id) ...@@ -35,6 +35,15 @@ static struct pipe_info *find_pipe(int id)
return container_of(fd, struct pipe_info, d); return container_of(fd, struct pipe_info, d);
} }
static int open_pipe(struct file_desc *d);
static int pipe_should_open_transport(struct fdinfo_entry *fe,
struct file_desc *d);
static struct file_desc_ops pipe_desc_ops = {
.open = open_pipe,
.want_transport = pipe_should_open_transport,
};
int collect_pipes(void) int collect_pipes(void)
{ {
struct pipe_info *pi = NULL, *tmp; struct pipe_info *pi = NULL, *tmp;
...@@ -59,7 +68,8 @@ int collect_pipes(void) ...@@ -59,7 +68,8 @@ int collect_pipes(void)
pr_info("Collected pipe entry ID %x PIPE ID %x\n", pr_info("Collected pipe entry ID %x PIPE ID %x\n",
pi->pe.id, pi->pe.pipe_id); pi->pe.id, pi->pe.pipe_id);
file_desc_add(&pi->d, FDINFO_PIPE, pi->pe.id); file_desc_add(&pi->d, FDINFO_PIPE, pi->pe.id,
&pipe_desc_ops);
list_for_each_entry(tmp, &pipes, list) list_for_each_entry(tmp, &pipes, list)
if (pi->pe.pipe_id == tmp->pe.pipe_id) if (pi->pe.pipe_id == tmp->pe.pipe_id)
...@@ -173,7 +183,7 @@ void mark_pipe_master() ...@@ -173,7 +183,7 @@ void mark_pipe_master()
handle_pipes_data(); handle_pipes_data();
} }
int pipe_should_open_transport(struct fdinfo_entry *fe, static int pipe_should_open_transport(struct fdinfo_entry *fe,
struct file_desc *d) struct file_desc *d)
{ {
struct pipe_info *pi; struct pipe_info *pi;
...@@ -256,7 +266,7 @@ err: ...@@ -256,7 +266,7 @@ err:
return ret; return ret;
} }
int open_pipe(struct file_desc *d) static int open_pipe(struct file_desc *d)
{ {
unsigned long time = 1000; unsigned long time = 1000;
struct pipe_info *pi, *pc, *p; struct pipe_info *pi, *pc, *p;
......
...@@ -894,6 +894,12 @@ struct inet_sk_info { ...@@ -894,6 +894,12 @@ struct inet_sk_info {
struct file_desc d; struct file_desc d;
}; };
static int open_inet_sk(struct file_desc *d);
static struct file_desc_ops inet_desc_ops = {
.open = open_inet_sk,
};
int collect_inet_sockets(void) int collect_inet_sockets(void)
{ {
struct inet_sk_info *ii = NULL; struct inet_sk_info *ii = NULL;
...@@ -913,7 +919,8 @@ int collect_inet_sockets(void) ...@@ -913,7 +919,8 @@ int collect_inet_sockets(void)
if (ret <= 0) if (ret <= 0)
break; break;
file_desc_add(&ii->d, FDINFO_INETSK, ii->ie.id); file_desc_add(&ii->d, FDINFO_INETSK, ii->ie.id,
&inet_desc_ops);
} }
if (ii) if (ii)
...@@ -923,7 +930,7 @@ int collect_inet_sockets(void) ...@@ -923,7 +930,7 @@ int collect_inet_sockets(void)
return 0; return 0;
} }
int open_inet_sk(struct file_desc *d) static int open_inet_sk(struct file_desc *d)
{ {
int sk; int sk;
struct sockaddr_in addr; struct sockaddr_in addr;
...@@ -1235,7 +1242,7 @@ done: ...@@ -1235,7 +1242,7 @@ done:
return 0; return 0;
} }
int unixsk_should_open_transport(struct fdinfo_entry *fe, static int unixsk_should_open_transport(struct fdinfo_entry *fe,
struct file_desc *d) struct file_desc *d)
{ {
struct unix_sk_info *ui; struct unix_sk_info *ui;
...@@ -1360,6 +1367,11 @@ int open_unix_sk(struct file_desc *d) ...@@ -1360,6 +1367,11 @@ int open_unix_sk(struct file_desc *d)
return open_unixsk_standalone(ui); return open_unixsk_standalone(ui);
} }
static struct file_desc_ops unix_desc_ops = {
.open = open_unix_sk,
.want_transport = unixsk_should_open_transport,
};
int collect_unix_sockets(void) int collect_unix_sockets(void)
{ {
int fd, ret; int fd, ret;
...@@ -1416,7 +1428,8 @@ int collect_unix_sockets(void) ...@@ -1416,7 +1428,8 @@ int collect_unix_sockets(void)
ui->peer = NULL; ui->peer = NULL;
ui->flags = 0; ui->flags = 0;
pr_info(" `- Got %u peer %u\n", ui->ue.id, ui->ue.peer); pr_info(" `- Got %u peer %u\n", ui->ue.id, ui->ue.peer);
file_desc_add(&ui->d, FDINFO_UNIXSK, ui->ue.id); file_desc_add(&ui->d, FDINFO_UNIXSK, ui->ue.id,
&unix_desc_ops);
list_add_tail(&ui->list, &unix_sockets); list_add_tail(&ui->list, &unix_sockets);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment