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)
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->id = id;
d->ops = ops;
INIT_LIST_HEAD(&d->fd_info_head);
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)
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)
{
struct reg_file_info *rfi = NULL;
......@@ -144,7 +152,8 @@ int collect_reg_files(void)
rfi->path[len] = '\0';
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) {
......@@ -303,12 +312,10 @@ void transport_name_gen(struct sockaddr_un *addr, int *len,
static int should_open_transport(struct fdinfo_entry *fe, struct file_desc *fd)
{
if (fe->type == FDINFO_PIPE)
return pipe_should_open_transport(fe, fd);
if (fe->type == FDINFO_UNIXSK)
return unixsk_should_open_transport(fe, fd);
return 0;
if (fd->ops->want_transport)
return fd->ops->want_transport(fe, fd);
else
return 0;
}
static int open_transport_fd(int pid, struct fdinfo_entry *fe, struct file_desc *d)
......@@ -372,24 +379,7 @@ static int open_fd(int pid, struct fdinfo_entry *fe,
if ((fle->pid != pid) || (fe->addr != fle->fd))
return 0;
switch (fe->type) {
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;
}
tmp = d->ops->open(d);
if (tmp < 0)
return -1;
......
......@@ -39,14 +39,23 @@ struct fdinfo_list_entry {
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 {
int type;
u32 id;
struct list_head hash;
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 file_desc *find_file_desc_raw(int type, u32 id);
......@@ -65,8 +74,5 @@ extern int self_exe_fd;
struct file_desc;
extern int collect_pipes(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_ */
......@@ -13,15 +13,11 @@ extern int dump_socket(struct fd_parms *p, int lfd,
struct fdinfo_list_entry;
struct file_desc;
struct fdinfo_entry;
extern int unixsk_should_open_transport(struct fdinfo_entry *,
struct file_desc *);
extern int collect_sockets(void);
extern int collect_inet_sockets(void);
extern int collect_unix_sockets(void);
extern int resolve_unix_peers(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;
extern void show_unixsk(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)
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)
{
struct pipe_info *pi = NULL, *tmp;
......@@ -59,7 +68,8 @@ int collect_pipes(void)
pr_info("Collected pipe entry ID %x PIPE ID %x\n",
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)
if (pi->pe.pipe_id == tmp->pe.pipe_id)
......@@ -173,7 +183,7 @@ void mark_pipe_master()
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 pipe_info *pi;
......@@ -256,7 +266,7 @@ err:
return ret;
}
int open_pipe(struct file_desc *d)
static int open_pipe(struct file_desc *d)
{
unsigned long time = 1000;
struct pipe_info *pi, *pc, *p;
......
......@@ -894,6 +894,12 @@ struct inet_sk_info {
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)
{
struct inet_sk_info *ii = NULL;
......@@ -913,7 +919,8 @@ int collect_inet_sockets(void)
if (ret <= 0)
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)
......@@ -923,7 +930,7 @@ int collect_inet_sockets(void)
return 0;
}
int open_inet_sk(struct file_desc *d)
static int open_inet_sk(struct file_desc *d)
{
int sk;
struct sockaddr_in addr;
......@@ -1235,7 +1242,7 @@ done:
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 unix_sk_info *ui;
......@@ -1360,6 +1367,11 @@ int open_unix_sk(struct file_desc *d)
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 fd, ret;
......@@ -1416,7 +1428,8 @@ int collect_unix_sockets(void)
ui->peer = NULL;
ui->flags = 0;
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);
}
......
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