[CRIU] [PATCH] Avoid unnecessary getpid() calls

Radostin Stoyanov rstoyanov1 at gmail.com
Thu Jan 17 23:33:04 MSK 2019


getsid(0) and getpgid(0) will return session ID and PGID of
the calling process.

Signed-off-by: Radostin Stoyanov <rstoyanov1 at gmail.com>
---
 criu/cr-restore.c            |  4 ++--
 test/zdtm/static/session00.c |  4 ++--
 test/zdtm/static/session01.c | 12 ++++++------
 test/zdtm/static/session03.c |  2 +-
 4 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/criu/cr-restore.c b/criu/cr-restore.c
index 7bbe79aae..f3addab3f 100644
--- a/criu/cr-restore.c
+++ b/criu/cr-restore.c
@@ -1489,7 +1489,7 @@ static void restore_sid(void)
 			exit(1);
 		}
 	} else {
-		sid = getsid(getpid());
+		sid = getsid(0);
 		if (sid != current->sid) {
 			/* Skip the root task if it's not init */
 			if (current == root_item && vpid(root_item) != INIT_PID)
@@ -1594,7 +1594,7 @@ static int create_children_and_session(void)
 		if (!restore_before_setsid(child))
 			continue;
 
-		BUG_ON(child->born_sid != -1 && getsid(getpid()) != child->born_sid);
+		BUG_ON(child->born_sid != -1 && getsid(0) != child->born_sid);
 
 		ret = fork_with_pid(child);
 		if (ret < 0)
diff --git a/test/zdtm/static/session00.c b/test/zdtm/static/session00.c
index 5889a92b9..4a239e159 100644
--- a/test/zdtm/static/session00.c
+++ b/test/zdtm/static/session00.c
@@ -90,7 +90,7 @@ static int fork_child(int i)
 
 			close(p[1]);
 			wait(NULL);
-			if (getsid(getpid()) != sid) {
+			if (getsid(0) != sid) {
 				fail("The process %d (%x) has SID=%d (expected %d)",
 					pid, testcases[i].flags, sid, testcases[i].sid);
 				exit(1);
@@ -117,7 +117,7 @@ static int fork_child(int i)
 
 			close(p[1]);
 			wait(NULL);
-			if (getsid(getpid()) != sid) {
+			if (getsid(0) != sid) {
 				fail("The process %d (%x) has SID=%d (expected %d)",
 					pid, testcases[i].flags, sid, testcases[i].sid);
 				exit(1);
diff --git a/test/zdtm/static/session01.c b/test/zdtm/static/session01.c
index 20c0d51cb..48cfb26de 100644
--- a/test/zdtm/static/session01.c
+++ b/test/zdtm/static/session01.c
@@ -102,14 +102,14 @@ static int child(const int c)
 		goto err;
 
 	if (t->pid == t->sid) {
-		if (getpid() != getsid(getpid()))
+		if (getpid() != getsid(0))
 			if (setsid() < 0)
 				goto err;
 		if (fork_children(t, 1))
 			goto err;
 	}
 	if (t->pid == t->pgid) {
-		if (getpid() != getpgid(getpid()))
+		if (getpid() != getpgid(0))
 			if (setpgid(getpid(), getpid()) < 0) {
 				pr_perror("setpgid() failed");
 				goto err;
@@ -129,7 +129,7 @@ static int child(const int c)
 			break;
 		if (t->pgid != testcases[i].pid)
 			continue;
-		if (getpgid(getpid()) != testcases[i].master.pid)
+		if (getpgid(0) != testcases[i].master.pid)
 			if (setpgid(getpid(), testcases[i].master.pid) < 0) {
 				pr_perror("setpgid() failed (%d) (%d)", c, i);
 				goto err;
@@ -161,7 +161,7 @@ static int child(const int c)
 
 	/* Save the master copy */
 	t->master.ppid	= getppid();
-	t->master.sid	= getsid(getpid());
+	t->master.sid	= getsid(0);
 
 	futex_set_and_wake(&t->futex, c);
 
@@ -169,8 +169,8 @@ static int child(const int c)
 
 	check("pid", t->master.pid,	getpid());
 	check("ppid", t->master.ppid,	getppid());
-	check("sid", t->master.sid,	getsid(getpid()));
-	check("pgid", t->master.pgid,	getpgid(getpid()));
+	check("sid", t->master.sid,	getsid(0));
+	check("pgid", t->master.pgid,	getpgid(0));
 
 	futex_set_and_wake(&t->futex, c);
 
diff --git a/test/zdtm/static/session03.c b/test/zdtm/static/session03.c
index 04fb83461..2b3c46c32 100644
--- a/test/zdtm/static/session03.c
+++ b/test/zdtm/static/session03.c
@@ -177,7 +177,7 @@ static void handle_command()
 		}
 		break;
 	case TEST_SETSID:
-		if (getsid(getpid()) == getpid())
+		if (getsid(0) == getpid())
 			break;
 		test_msg("%3d: setsid()\n", current);
 		if(setsid() == -1) {
-- 
2.20.1



More information about the CRIU mailing list