Commit 2de8de5d authored by Cyrill Gorcunov's avatar Cyrill Gorcunov

restore: Rename try_to_add into collect_ routines

As they are. Also add some more debug printing on
pipe users in debug purpose.
Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@gmail.com>
parent af1dfdda
...@@ -82,9 +82,10 @@ static void show_saved_pipes(void) ...@@ -82,9 +82,10 @@ static void show_saved_pipes(void)
pr_info("\tSaved pipes:\n"); pr_info("\tSaved pipes:\n");
for (i = 0; i < nr_pipes; i++) for (i = 0; i < nr_pipes; i++)
pr_info("\t\tid: %x -> pid: %d\n", pr_info("\t\tid: %x -> pid: %d -> users: %d\n",
pipes[i].id, pipes[i].id,
pipes[i].pid); pipes[i].pid,
pipes[i].users);
} }
static struct shmem_info *search_shmem(unsigned long addr, unsigned long id) static struct shmem_info *search_shmem(unsigned long addr, unsigned long id)
...@@ -155,7 +156,7 @@ static int shmem_wait_and_open(struct shmem_info *si) ...@@ -155,7 +156,7 @@ static int shmem_wait_and_open(struct shmem_info *si)
} }
} }
static int try_to_add_shmem(int pid, struct shmem_entry *e) static int collect_shmem(int pid, struct shmem_entry *e)
{ {
int i; int i;
...@@ -191,10 +192,17 @@ static int try_to_add_shmem(int pid, struct shmem_entry *e) ...@@ -191,10 +192,17 @@ static int try_to_add_shmem(int pid, struct shmem_entry *e)
return 0; return 0;
} }
static int try_to_add_pipe(int pid, struct pipe_entry *e, int p_fd) static int collect_pipe(int pid, struct pipe_entry *e, int p_fd)
{ {
int i; int i;
/*
* All pipes get collected into the one array,
* note the highest PID is the sign of which
* process pipe should be really created, all other
* processes (if they have pipes with pipeid matched)
* will be attached.
*/
for (i = 0; i < nr_pipes; i++) { for (i = 0; i < nr_pipes; i++) {
if (pipes[i].id != e->pipeid) if (pipes[i].id != e->pipeid)
continue; continue;
...@@ -208,7 +216,7 @@ static int try_to_add_pipe(int pid, struct pipe_entry *e, int p_fd) ...@@ -208,7 +216,7 @@ static int try_to_add_pipe(int pid, struct pipe_entry *e, int p_fd)
} }
if ((nr_pipes + 1) * sizeof(struct pipe_info) >= 4096) { if ((nr_pipes + 1) * sizeof(struct pipe_info) >= 4096) {
pr_info("OOM storing pipes\n"); pr_panic("OOM storing pipes\n");
return 1; return 1;
} }
...@@ -255,7 +263,7 @@ static int prepare_shmem_pid(int pid) ...@@ -255,7 +263,7 @@ static int prepare_shmem_pid(int pid)
return 1; return 1;
} }
if (try_to_add_shmem(pid, &e)) if (collect_shmem(pid, &e))
return 1; return 1;
} }
...@@ -296,7 +304,7 @@ static int prepare_pipes_pid(int pid) ...@@ -296,7 +304,7 @@ static int prepare_pipes_pid(int pid)
return 1; return 1;
} }
if (try_to_add_pipe(pid, &e, p_fd)) if (collect_pipe(pid, &e, p_fd))
return 1; return 1;
if (e.bytes) if (e.bytes)
...@@ -848,11 +856,11 @@ static int create_pipe(int pid, struct pipe_entry *e, struct pipe_info *pi, int ...@@ -848,11 +856,11 @@ static int create_pipe(int pid, struct pipe_entry *e, struct pipe_info *pi, int
pi->write_fd = pfd[1]; pi->write_fd = pfd[1];
pi->real_pid = getpid(); pi->real_pid = getpid();
pr_info("\t%d: Done, waiting for others on %d pid with r:%d w:%d\n", pr_info("\t%d: Done, waiting for others (users %d) on %d pid with r:%d w:%d\n",
pid, pi->real_pid, pi->read_fd, pi->write_fd); pid, pi->users - 1, pi->real_pid, pi->read_fd, pi->write_fd);
while (1) { while (1) {
if (pi->users == 1) /* only I left */ if (pi->users <= 1) /* only I left here, no need to wait */
break; break;
pr_info("\t%d: Waiting for %x pipe to attach (%d users left)\n", pr_info("\t%d: Waiting for %x pipe to attach (%d users left)\n",
...@@ -888,7 +896,8 @@ static int attach_pipe(int pid, struct pipe_entry *e, struct pipe_info *pi) ...@@ -888,7 +896,8 @@ static int attach_pipe(int pid, struct pipe_entry *e, struct pipe_info *pi)
char path[128]; char path[128];
int tmp, fd; int tmp, fd;
pr_info("\t%d: Wating for pipe %x to appear\n", pid, e->pipeid); pr_info("\t%d: Wating for pipe %x to appear\n",
pid, e->pipeid);
while (pi->real_pid == 0) while (pi->real_pid == 0)
usleep(1000); usleep(1000);
...@@ -904,7 +913,8 @@ static int attach_pipe(int pid, struct pipe_entry *e, struct pipe_info *pi) ...@@ -904,7 +913,8 @@ static int attach_pipe(int pid, struct pipe_entry *e, struct pipe_info *pi)
} }
sprintf(path, "/proc/%d/fd/%d", pi->real_pid, tmp); sprintf(path, "/proc/%d/fd/%d", pi->real_pid, tmp);
pr_info("\t%d: Attaching pipe %s\n", pid, path); pr_info("\t%d: Attaching pipe %s (%d users left)\n",
pid, path, pi->users - 1);
fd = open(path, e->flags); fd = open(path, e->flags);
if (fd < 0) { if (fd < 0) {
...@@ -913,10 +923,10 @@ static int attach_pipe(int pid, struct pipe_entry *e, struct pipe_info *pi) ...@@ -913,10 +923,10 @@ static int attach_pipe(int pid, struct pipe_entry *e, struct pipe_info *pi)
} }
pr_info("\t%d: Done, reopening for %d\n", pid, e->fd); pr_info("\t%d: Done, reopening for %d\n", pid, e->fd);
pi->users--;
tmp = reopen_fd_as(e->fd, fd); tmp = reopen_fd_as(e->fd, fd);
if (tmp < 0) if (tmp < 0)
return 1; return 1;
pi->users--;
return 0; return 0;
...@@ -940,11 +950,14 @@ static int open_pipe(int pid, struct pipe_entry *e, int *pipes_fd) ...@@ -940,11 +950,14 @@ static int open_pipe(int pid, struct pipe_entry *e, int *pipes_fd)
} }
pi = search_pipe(e->pipeid); pi = search_pipe(e->pipeid);
if (pi == NULL) { if (!pi) {
fprintf(stderr, "BUG: can't find my pipe %x\n", e->pipeid); fprintf(stderr, "BUG: can't find my pipe %x\n", e->pipeid);
return 1; return 1;
} }
/*
* Pipe get created once, other processes do attach to it.
*/
if (pi->pid == pid) if (pi->pid == pid)
return create_pipe(pid, e, pi, *pipes_fd); return create_pipe(pid, e, pi, *pipes_fd);
else else
......
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