pstree: Avoid multiple calls to getpid()

Submitted by Radostin Stoyanov on Oct. 28, 2018, 8:42 p.m.

Details

Message ID 20181028204251.16413-1-rstoyanov1@gmail.com
State Accepted
Series "pstree: Avoid multiple calls to getpid()"
Headers show

Commit Message

Radostin Stoyanov Oct. 28, 2018, 8:42 p.m.
Signed-off-by: Radostin Stoyanov <rstoyanov1@gmail.com>
---
 criu/pstree.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/pstree.c b/criu/pstree.c
index 928fe2d1..980d19fd 100644
--- a/criu/pstree.c
+++ b/criu/pstree.c
@@ -333,10 +333,10 @@  err:
 	return ret;
 }
 
-static int prepare_pstree_for_shell_job(void)
+static int prepare_pstree_for_shell_job(pid_t current_pid)
 {
-	pid_t current_sid = getsid(getpid());
-	pid_t current_gid = getpgid(getpid());
+	pid_t current_sid = getsid(current_pid);
+	pid_t current_gid = getpgid(current_pid);
 
 	struct pstree_item *pi;
 
@@ -625,12 +625,12 @@  static int get_free_pid()
 	return -1;
 }
 
-static int prepare_pstree_ids(void)
+static int prepare_pstree_ids(pid_t current_pid)
 {
 	struct pstree_item *item, *child, *helper, *tmp;
 	LIST_HEAD(helpers);
 
-	pid_t current_pgid = getpgid(getpid());
+	pid_t current_pgid = getpgid(current_pid);
 
 	/*
 	 * Some task can be reparented to init. A helper task should be added
@@ -910,7 +910,7 @@  static int prepare_pstree_kobj_ids(void)
 int prepare_pstree(void)
 {
 	int ret;
-	pid_t pid_max = 0, kpid_max = 0;
+	pid_t pid_max = 0, kpid_max = 0, current_pid;
 	int fd;
 	char buf[21];
 
@@ -945,12 +945,14 @@  int prepare_pstree(void)
 		}
 	}
 
+	current_pid = getpid();
+
 	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();
+		ret = prepare_pstree_for_shell_job(current_pid);
 	if (!ret)
 		/*
 		 * Walk the collected tree and prepare for restoring
@@ -962,7 +964,7 @@  int prepare_pstree(void)
 		 * Session/Group leaders might be dead. Need to fix
 		 * pstree with properly injected helper tasks.
 		 */
-		ret = prepare_pstree_ids();
+		ret = prepare_pstree_ids(current_pid);
 
 	return ret;
 }

Comments

Dmitry Safonov Oct. 30, 2018, 3:54 p.m.
On 10/28/18 8:42 PM, Radostin Stoyanov wrote:
> Signed-off-by: Radostin Stoyanov <rstoyanov1@gmail.com>

Reviewed-by: Dmitry Safonov <0x7f454c46@gmail.com>

> ---
>   criu/pstree.c | 18 ++++++++++--------
>   1 file changed, 10 insertions(+), 8 deletions(-)
> 
> diff --git a/criu/pstree.c b/criu/pstree.c
> index 928fe2d1..980d19fd 100644
> --- a/criu/pstree.c
> +++ b/criu/pstree.c
> @@ -333,10 +333,10 @@ err:
>   	return ret;
>   }
>   
> -static int prepare_pstree_for_shell_job(void)
> +static int prepare_pstree_for_shell_job(pid_t current_pid)
>   {
> -	pid_t current_sid = getsid(getpid());
> -	pid_t current_gid = getpgid(getpid());
> +	pid_t current_sid = getsid(current_pid);
> +	pid_t current_gid = getpgid(current_pid);
>   
>   	struct pstree_item *pi;
>   
> @@ -625,12 +625,12 @@ static int get_free_pid()
>   	return -1;
>   }
>   
> -static int prepare_pstree_ids(void)
> +static int prepare_pstree_ids(pid_t current_pid)
>   {
>   	struct pstree_item *item, *child, *helper, *tmp;
>   	LIST_HEAD(helpers);
>   
> -	pid_t current_pgid = getpgid(getpid());
> +	pid_t current_pgid = getpgid(current_pid);
>   
>   	/*
>   	 * Some task can be reparented to init. A helper task should be added
> @@ -910,7 +910,7 @@ static int prepare_pstree_kobj_ids(void)
>   int prepare_pstree(void)
>   {
>   	int ret;
> -	pid_t pid_max = 0, kpid_max = 0;
> +	pid_t pid_max = 0, kpid_max = 0, current_pid;
>   	int fd;
>   	char buf[21];
>   
> @@ -945,12 +945,14 @@ int prepare_pstree(void)
>   		}
>   	}
>   
> +	current_pid = getpid();
> +
>   	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();
> +		ret = prepare_pstree_for_shell_job(current_pid);
>   	if (!ret)
>   		/*
>   		 * Walk the collected tree and prepare for restoring
> @@ -962,7 +964,7 @@ int prepare_pstree(void)
>   		 * Session/Group leaders might be dead. Need to fix
>   		 * pstree with properly injected helper tasks.
>   		 */
> -		ret = prepare_pstree_ids();
> +		ret = prepare_pstree_ids(current_pid);
>   
>   	return ret;
>   }
> 

Thanks,
           Dima
Andrei Vagin Nov. 8, 2018, 5:43 p.m.
Applied, thanks!

I replaced current_pid to pid. I think it looks a bit better. Let me
know if you don't thing so.

On Sun, Oct 28, 2018 at 08:42:51PM +0000, Radostin Stoyanov wrote:
> Signed-off-by: Radostin Stoyanov <rstoyanov1@gmail.com>
> ---
>  criu/pstree.c | 18 ++++++++++--------
>  1 file changed, 10 insertions(+), 8 deletions(-)
> 
> diff --git a/criu/pstree.c b/criu/pstree.c
> index 928fe2d1..980d19fd 100644
> --- a/criu/pstree.c
> +++ b/criu/pstree.c
> @@ -333,10 +333,10 @@ err:
>  	return ret;
>  }
>  
> -static int prepare_pstree_for_shell_job(void)
> +static int prepare_pstree_for_shell_job(pid_t current_pid)
>  {
> -	pid_t current_sid = getsid(getpid());
> -	pid_t current_gid = getpgid(getpid());
> +	pid_t current_sid = getsid(current_pid);
> +	pid_t current_gid = getpgid(current_pid);
>  
>  	struct pstree_item *pi;
>  
> @@ -625,12 +625,12 @@ static int get_free_pid()
>  	return -1;
>  }
>  
> -static int prepare_pstree_ids(void)
> +static int prepare_pstree_ids(pid_t current_pid)
>  {
>  	struct pstree_item *item, *child, *helper, *tmp;
>  	LIST_HEAD(helpers);
>  
> -	pid_t current_pgid = getpgid(getpid());
> +	pid_t current_pgid = getpgid(current_pid);
>  
>  	/*
>  	 * Some task can be reparented to init. A helper task should be added
> @@ -910,7 +910,7 @@ static int prepare_pstree_kobj_ids(void)
>  int prepare_pstree(void)
>  {
>  	int ret;
> -	pid_t pid_max = 0, kpid_max = 0;
> +	pid_t pid_max = 0, kpid_max = 0, current_pid;
>  	int fd;
>  	char buf[21];
>  
> @@ -945,12 +945,14 @@ int prepare_pstree(void)
>  		}
>  	}
>  
> +	current_pid = getpid();
> +
>  	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();
> +		ret = prepare_pstree_for_shell_job(current_pid);
>  	if (!ret)
>  		/*
>  		 * Walk the collected tree and prepare for restoring
> @@ -962,7 +964,7 @@ int prepare_pstree(void)
>  		 * Session/Group leaders might be dead. Need to fix
>  		 * pstree with properly injected helper tasks.
>  		 */
> -		ret = prepare_pstree_ids();
> +		ret = prepare_pstree_ids(current_pid);
>  
>  	return ret;
>  }
> -- 
> 2.17.2
> 
> _______________________________________________
> CRIU mailing list
> CRIU@openvz.org
> https://lists.openvz.org/mailman/listinfo/criu