Commit 2875a195 authored by Andrew Vagin's avatar Andrew Vagin Committed by Pavel Emelyanov

pstree: try to find a free pid between busy pids (v2)

Currently our pid allocator returns max_pid++ and it can return a pid
which is bigger than kernel.max_pid.

(00.821430)   5506: Error (cr-restore.c:1540): Pid 300 do not match expected 32768

v2: handle error code from insert_item()
Signed-off-by: 's avatarAndrew Vagin <avagin@virtuozzo.com>
Signed-off-by: 's avatarPavel Emelyanov <xemul@virtuozzo.com>
parent 42205175
......@@ -318,8 +318,6 @@ err:
return ret;
}
static int max_pid = 0;
static int prepare_pstree_for_shell_job(void)
{
pid_t current_sid = getsid(getpid());
......@@ -366,9 +364,6 @@ static int prepare_pstree_for_shell_job(void)
pi->sid = current_sid;
}
max_pid = max((int)current_sid, max_pid);
max_pid = max((int)current_gid, max_pid);
if (lookup_create_item(current_sid) == NULL)
return -1;
if (lookup_create_item(current_gid) == NULL)
......@@ -503,11 +498,8 @@ static int read_pstree_image(void)
break;
pi->pid.virt = e->pid;
max_pid = max((int)e->pid, max_pid);
pi->pgid = e->pgid;
max_pid = max((int)e->pgid, max_pid);
pi->sid = e->sid;
max_pid = max((int)e->sid, max_pid);
pi->pid.state = TASK_ALIVE;
if (e->ppid == 0) {
......@@ -580,11 +572,34 @@ static int read_pstree_image(void)
if (ret < 0)
goto err;
}
err:
close_image(img);
return ret;
}
#define RESERVED_PIDS 300
static int get_free_pid()
{
static struct pstree_item *prev, *next;
if (prev == NULL)
prev = rb_entry(rb_first(&pid_root_rb), struct pstree_item, pid.node);
while (1) {
pid_t pid;
pid = prev->pid.virt + 1;
pid = pid < RESERVED_PIDS ? RESERVED_PIDS + 1 : pid;
next = rb_entry(rb_next(&prev->pid.node), struct pstree_item, pid.node);
if (&next->pid.node == NULL || next->pid.virt > pid)
return pid;
prev = next;
}
return -1;
}
static int prepare_pstree_ids(void)
{
struct pstree_item *item, *child, *helper, *tmp;
......@@ -612,7 +627,12 @@ static int prepare_pstree_ids(void)
leader = pstree_item_by_virt(item->sid);
BUG_ON(leader == NULL);
if (leader->pid.state != TASK_UNDEF) {
helper = lookup_create_item(++max_pid);
pid_t pid;
pid = get_free_pid();
if (pid < 0)
break;
helper = lookup_create_item(pid);
if (helper == NULL)
return -1;
......
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