[05/10] pstree: add curr_sid/pgid updating them on fork/setsid/setpgid

Submitted by Pavel Tikhomirov on July 4, 2017, 9:08 a.m.

Details

Message ID 20170704090809.8127-6-ptikhomirov@virtuozzo.com
State New
Series "rework pgid restore for pidnses"
Headers show

Commit Message

Pavel Tikhomirov July 4, 2017, 9:08 a.m.
Signed-off-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
---
 criu/cr-restore.c       | 33 +++++++++++++++++++++++++++++++++
 criu/include/rst_info.h |  3 +++
 2 files changed, 36 insertions(+)

Patch hide | download patch | download mbox

diff --git a/criu/cr-restore.c b/criu/cr-restore.c
index 3ce27fd..4083f2a 100644
--- a/criu/cr-restore.c
+++ b/criu/cr-restore.c
@@ -1386,6 +1386,34 @@  static inline int fork_with_pid(struct pstree_item *item)
 	pid_t pid = vpid(item);
 	struct ns_id *pid_ns;
 
+	if (!item->parent) {
+		pid_t real_sid, real_pgid;
+
+		real_sid = getsid(0);
+		if (real_sid == -1) {
+			pr_perror("%d: Fail to getsid\n", vpid(item));
+			return -1;
+		}
+
+		real_pgid = getpgid(0);
+		if (real_pgid == -1) {
+			pr_perror("%d: Fail to getpgid\n", vpid(item));
+			return -1;
+		}
+
+		rsti(item)->curr_sid = real_sid;
+		rsti(item)->curr_pgid = real_pgid;
+	} else {
+		rsti(item)->curr_sid = rsti(item->parent)->curr_sid;
+		rsti(item)->curr_pgid = rsti(item->parent)->curr_pgid;
+	}
+
+	/* Check we are born with the right sid */
+	if (item->born_sid != -1)
+		BUG_ON(item->born_sid != rsti(item)->curr_sid);
+	else if (!is_session_leader(item))
+		BUG_ON(vsid(item) != rsti(item)->curr_sid);
+
 	if (item->pid->state != TASK_HELPER) {
 		if (open_core(pid, &ca.core))
 			return -1;
@@ -1541,6 +1569,8 @@  static void restore_sid(void)
 			pr_perror("Can't restore sid (%d)", sid);
 			exit(1);
 		}
+		rsti(current)->curr_sid = vpid(current);
+		rsti(current)->curr_pgid = vpid(current);
 	} else {
 		sid = getsid(getpid());
 		if (sid != last_level_pid(current->sid)) {
@@ -1552,6 +1582,8 @@  static void restore_sid(void)
 			exit(1);
 		}
 	}
+
+	BUG_ON(rsti(current)->curr_sid != vsid(current));
 }
 
 static void restore_pgid(void)
@@ -1598,6 +1630,7 @@  static void restore_pgid(void)
 		pr_perror("Can't restore pgid (%d/%d->%d)", vpid(current), pgid, vpgid(current));
 		exit(1);
 	}
+	rsti(current)->curr_pgid = vpid(current);
 
 	if (my_pgid == last_level_pid(current->pid))
 		futex_set_and_wake(&rsti(current)->pgrp_set, 1);
diff --git a/criu/include/rst_info.h b/criu/include/rst_info.h
index f9840d1..c1f8fdf 100644
--- a/criu/include/rst_info.h
+++ b/criu/include/rst_info.h
@@ -65,6 +65,9 @@  struct rst_info {
 	bool			has_thp_enabled;
 
 	void			*breakpoint;
+
+	int			curr_sid;
+	int			curr_pgid;
 };
 
 extern struct task_entries *task_entries;

Comments

Kirill Tkhai July 4, 2017, 9:32 a.m.
On 04.07.2017 12:08, Pavel Tikhomirov wrote:
> Signed-off-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
> ---
>  criu/cr-restore.c       | 33 +++++++++++++++++++++++++++++++++
>  criu/include/rst_info.h |  3 +++
>  2 files changed, 36 insertions(+)
> 
> diff --git a/criu/cr-restore.c b/criu/cr-restore.c
> index 3ce27fd..4083f2a 100644
> --- a/criu/cr-restore.c
> +++ b/criu/cr-restore.c
> @@ -1386,6 +1386,34 @@ static inline int fork_with_pid(struct pstree_item *item)
>  	pid_t pid = vpid(item);
>  	struct ns_id *pid_ns;
>  
> +	if (!item->parent) {
> +		pid_t real_sid, real_pgid;
> +
> +		real_sid = getsid(0);
> +		if (real_sid == -1) {
> +			pr_perror("%d: Fail to getsid\n", vpid(item));
> +			return -1;
> +		}
> +
> +		real_pgid = getpgid(0);
> +		if (real_pgid == -1) {
> +			pr_perror("%d: Fail to getpgid\n", vpid(item));
> +			return -1;
> +		}
> +
> +		rsti(item)->curr_sid = real_sid;
> +		rsti(item)->curr_pgid = real_pgid;
> +	} else {
> +		rsti(item)->curr_sid = rsti(item->parent)->curr_sid;
> +		rsti(item)->curr_pgid = rsti(item->parent)->curr_pgid;
> +	}
> +
> +	/* Check we are born with the right sid */
> +	if (item->born_sid != -1)
> +		BUG_ON(item->born_sid != rsti(item)->curr_sid);
> +	else if (!is_session_leader(item))
> +		BUG_ON(vsid(item) != rsti(item)->curr_sid);
> +

fork_with_pid() is already fat.. I'd suggested to move this to separate function.

>  	if (item->pid->state != TASK_HELPER) {
>  		if (open_core(pid, &ca.core))
>  			return -1;
> @@ -1541,6 +1569,8 @@ static void restore_sid(void)
>  			pr_perror("Can't restore sid (%d)", sid);
>  			exit(1);
>  		}
> +		rsti(current)->curr_sid = vpid(current);
> +		rsti(current)->curr_pgid = vpid(current);
>  	} else {
>  		sid = getsid(getpid());
>  		if (sid != last_level_pid(current->sid)) {
> @@ -1552,6 +1582,8 @@ static void restore_sid(void)
>  			exit(1);
>  		}
>  	}
> +
> +	BUG_ON(rsti(current)->curr_sid != vsid(current));
>  }
>  
>  static void restore_pgid(void)
> @@ -1598,6 +1630,7 @@ static void restore_pgid(void)
>  		pr_perror("Can't restore pgid (%d/%d->%d)", vpid(current), pgid, vpgid(current));
>  		exit(1);
>  	}
> +	rsti(current)->curr_pgid = vpid(current);
>  
>  	if (my_pgid == last_level_pid(current->pid))
>  		futex_set_and_wake(&rsti(current)->pgrp_set, 1);
> diff --git a/criu/include/rst_info.h b/criu/include/rst_info.h
> index f9840d1..c1f8fdf 100644
> --- a/criu/include/rst_info.h
> +++ b/criu/include/rst_info.h
> @@ -65,6 +65,9 @@ struct rst_info {
>  	bool			has_thp_enabled;
>  
>  	void			*breakpoint;
> +
> +	int			curr_sid;
> +	int			curr_pgid;
>  };
>  
>  extern struct task_entries *task_entries;
>