pstree: Sanitize pstree preparation code

We have 3 calls for that, two called directly from cr-restore,
one -- from one the former. Clean this mess and write short
comments on each stage.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
This commit is contained in:
Pavel Emelyanov 2012-12-10 15:50:47 +03:00
parent 2a925722ca
commit be79f38d6c
3 changed files with 23 additions and 8 deletions

View file

@ -1250,9 +1250,6 @@ int cr_restore_tasks(pid_t pid, struct cr_options *opts)
if (prepare_pstree() < 0)
return -1;
if (prepare_pstree_ids() < 0)
return -1;
if (crtools_prepare_shared() < 0)
return -1;

View file

@ -55,7 +55,6 @@ extern struct pstree_item *pstree_item_next(struct pstree_item *item);
extern bool restore_before_setsid(struct pstree_item *child);
extern int prepare_pstree(void);
extern int prepare_pstree_ids(void);
extern int dump_pstree(struct pstree_item *root_item);

View file

@ -175,7 +175,7 @@ static int prepare_pstree_for_shell_job(struct pstree_item *root)
return 0;
}
int prepare_pstree(void)
static int read_pstree_image(void)
{
int ret = 0, i, ps_fd;
struct pstree_item *pi, *parent = NULL;
@ -261,14 +261,12 @@ int prepare_pstree(void)
pstree_entry__free_unpacked(e, NULL);
}
ret = prepare_pstree_for_shell_job(root_item);
err:
close(ps_fd);
return ret;
}
int prepare_pstree_ids(void)
static int prepare_pstree_ids(void)
{
struct pstree_item *item, *child, *helper, *tmp;
LIST_HEAD(helpers);
@ -427,6 +425,27 @@ int prepare_pstree_ids(void)
return 0;
}
int prepare_pstree(void)
{
int ret;
ret = read_pstree_image();
if (!ret)
/*
* Shell job may inherit sid/pgid from the current
* shell, not from image. Set things up for this.
*/
ret = prepare_pstree_for_shell_job(root_item);
if (!ret)
/*
* Session/Group leaders might be dead. Need to fix
* pstree with properly injected helper tasks.
*/
ret = prepare_pstree_ids();
return ret;
}
bool restore_before_setsid(struct pstree_item *child)
{
int csid = child->born_sid == -1 ? child->sid : child->born_sid;