Skip to contentSkip to conversation

getsid02.c: Rewrite using new LTP API

PATCH In review Avinesh Kumar 2 participants 4 messages Aug 30, 2022 – Sep 2, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 getsid02.c: Rewrite using new LTP API

---
 testcases/kernel/syscalls/getsid/getsid02.c | 83 +++++----------------
 1 file changed, 17 insertions(+), 66 deletions(-)
testcases/kernel/syscalls/getsid/getsid02.c +17 -66
index b5ab339e2..c235af362 100644
@@ -1,81 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *
  *   Copyright (c) International Business Machines  Corp., 2001
  *   Copyright (c) 2012 Cyril Hrubis <[email protected]>
- *
- *   This program is free software;  you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- *   the GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *   Copyright (c) 2022 SUSE LLC Avinesh Kumar <[email protected]>
  */
 
-#define _GNU_SOURCE 1
-
-#include "test.h"
-
-#include <errno.h>
+/*\
+ * [Description]
+ *
+ * Verify that getsid(2) fails with ESRCH errno when there is no
+ * process found with process ID pid.
+ */
 
-char *TCID = "getsid02";
-int TST_TOTAL = 1;
+#include "tst_test.h"
 
 static pid_t unused_pid;
 
-static void cleanup(void);
-static void setup(void);
-
-int main(int ac, char **av)
+static void setup(void)
 {
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		TEST(getsid(unused_pid));
-
-		if (TEST_RETURN == 0) {
-			tst_resm(TFAIL, "call succeed when failure expected");
-			continue;
-		}
-
-		switch (TEST_ERRNO) {
-		case ESRCH:
-			tst_resm(TPASS, "expected failure - errno = %d - %s",
-				 TEST_ERRNO, strerror(TEST_ERRNO));
-			break;
-		default:
-			tst_resm(TFAIL, "call failed to produce "
-				 "expected error - errno = %d - %s",
-				 TEST_ERRNO, strerror(TEST_ERRNO));
-			break;
-		}
-	}
-
-	cleanup();
-	tst_exit();
+	unused_pid = tst_get_unused_pid();
 }
 
-void setup(void)
+static void run(void)
 {
-	unused_pid = tst_get_unused_pid(cleanup);
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
+	TST_EXP_FAIL(getsid(unused_pid), ESRCH);
 }
 
-void cleanup(void)
-{
-}
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = run
+};
Signature · 5 lines
2.37.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw
Quoted 83 lines · Avinesh Kumar
---
 testcases/kernel/syscalls/getsid/getsid02.c | 83 +++++----------------
 1 file changed, 17 insertions(+), 66 deletions(-)
testcases/kernel/syscalls/getsid/getsid02.c +1 -15
index b5ab339e2..c235af362 100644
@@ -1,81 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *
  *   Copyright (c) International Business Machines  Corp., 2001
  *   Copyright (c) 2012 Cyril Hrubis <[email protected]>
- *
- *   This program is free software;  you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- *   the GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA

02110-1301 USA
+ * Copyright (c) 2022 SUSE LLC Avinesh Kumar <[email protected]>
*/

testcases/kernel/syscalls/getsid/getsid02.c +8 -24
-#define _GNU_SOURCE 1
-
-#include "test.h"
-
-#include <errno.h>
+/*\
+ * [Description]
+ *
+ * Verify that getsid(2) fails with ESRCH errno when there is no
+ * process found with process ID pid.
+ */
 
-char *TCID = "getsid02";
-int TST_TOTAL = 1;
+#include "tst_test.h"
 
 static pid_t unused_pid;
 
-static void cleanup(void);
-static void setup(void);
-
-int main(int ac, char **av)
+static void setup(void)
 {
-       int lc;
-
-       tst_parse_opts(ac, av, NULL, NULL);
-
-       setup();
-
-       for (lc = 0; TEST_LOOPING(lc); lc++) {
-               tst_count = 0;
-
-               TEST(getsid(unused_pid));
-
-               if (TEST_RETURN == 0) {
-                       tst_resm(TFAIL, "call succeed when failure

expected");

testcases/kernel/syscalls/getsid/getsid02.c +0 -6
-                       continue;
-               }
-
-               switch (TEST_ERRNO) {
-               case ESRCH:
-                       tst_resm(TPASS, "expected failure - errno = %d -

%s",

testcases/kernel/syscalls/getsid/getsid02.c +0 -3
-                                TEST_ERRNO, strerror(TEST_ERRNO));
-                       break;
-               default:

testcases/kernel/syscalls/getsid/getsid02.c line 21

Quoted 10 lines
testcases/kernel/syscalls/getsid/getsid02.c +1 -9
-                       tst_resm(TFAIL, "call failed to produce "
-                                "expected error - errno = %d - %s",
-                                TEST_ERRNO, strerror(TEST_ERRNO));
-                       break;
-               }
-       }
-
-       cleanup();
-       tst_exit();
+       unused_pid = tst_get_unused_pid();

We can simply move this to the main function to avoid unused PID reusing again if people perform test with '-i N'.

How about this below:

#include "tst_test.h"

static void run(void)

{
    pid_t unused_pid;
    unused_pid = tst_get_unused_pid();

    TST_EXP_FAIL(getsid(unused_pid), ESRCH);
}

static struct tst_test test = {
.test_all = run
};

Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Hi Li,

Quoted 111 lines · IST Li Wang
Quoted 83 lines · Avinesh Kumar
---
 testcases/kernel/syscalls/getsid/getsid02.c | 83 +++++----------------
 1 file changed, 17 insertions(+), 66 deletions(-)
testcases/kernel/syscalls/getsid/getsid02.c +1 -15
index b5ab339e2..c235af362 100644
@@ -1,81 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *
  *   Copyright (c) International Business Machines  Corp., 2001
  *   Copyright (c) 2012 Cyril Hrubis <[email protected]>
- *
- *   This program is free software;  you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- *   the GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA

02110-1301 USA
+ * Copyright (c) 2022 SUSE LLC Avinesh Kumar <[email protected]>
*/

testcases/kernel/syscalls/getsid/getsid02.c +8 -24
-#define _GNU_SOURCE 1
-
-#include "test.h"
-
-#include <errno.h>
+/*\
+ * [Description]
+ *
+ * Verify that getsid(2) fails with ESRCH errno when there is no
+ * process found with process ID pid.
+ */
 
-char *TCID = "getsid02";
-int TST_TOTAL = 1;
+#include "tst_test.h"
 
 static pid_t unused_pid;
 
-static void cleanup(void);
-static void setup(void);
-
-int main(int ac, char **av)
+static void setup(void)
 {
-       int lc;
-
-       tst_parse_opts(ac, av, NULL, NULL);
-
-       setup();
-
-       for (lc = 0; TEST_LOOPING(lc); lc++) {
-               tst_count = 0;
-
-               TEST(getsid(unused_pid));
-
-               if (TEST_RETURN == 0) {
-                       tst_resm(TFAIL, "call succeed when failure

expected");

testcases/kernel/syscalls/getsid/getsid02.c +0 -6
-                       continue;
-               }
-
-               switch (TEST_ERRNO) {
-               case ESRCH:
-                       tst_resm(TPASS, "expected failure - errno = %d -

%s",

testcases/kernel/syscalls/getsid/getsid02.c +0 -3
-                                TEST_ERRNO, strerror(TEST_ERRNO));
-                       break;
-               default:

testcases/kernel/syscalls/getsid/getsid02.c line 21

Quoted 10 lines
testcases/kernel/syscalls/getsid/getsid02.c +1 -9
-                       tst_resm(TFAIL, "call failed to produce "
-                                "expected error - errno = %d - %s",
-                                TEST_ERRNO, strerror(TEST_ERRNO));
-                       break;
-               }
-       }
-
-       cleanup();
-       tst_exit();
+       unused_pid = tst_get_unused_pid();

We can simply move this to the main function to avoid unused PID reusing again if people perform test with '-i N'.

How about this below:

#include "tst_test.h"

static void run(void)

{
    pid_t unused_pid;
    unused_pid = tst_get_unused_pid();

    TST_EXP_FAIL(getsid(unused_pid), ESRCH);
}
Quoted 5 lines

static struct tst_test test = {
.test_all = run
};

Yes, I agree to your suggestion. But while testing '-i N' with this code also I am getting the same unused_pid which is equal to /proc/sys/kernel/pid_max in my env.

Regards,
Avinesh

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw
Quoted 118 lines · Avinesh Kumar

Hi Li,

Quoted 7 lines · IST Li Wang
Quoted 5 lines · Avinesh Kumar
---
 testcases/kernel/syscalls/getsid/getsid02.c | 83 +++++----------------
 1 file changed, 17 insertions(+), 66 deletions(-)
Quoted 13 lines

testcases/kernel/syscalls/getsid/getsid02.c line 6

Quoted 13 lines
testcases/kernel/syscalls/getsid/getsid02.c +1 -3
index b5ab339e2..c235af362 100644
@@ -1,81 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *
  *   Copyright (c) International Business Machines  Corp., 2001
  *   Copyright (c) 2012 Cyril Hrubis <[email protected]>
- *
- *   This program is free software;  you can redistribute it and/or

modify

Quoted 1 line

testcases/kernel/syscalls/getsid/getsid02.c line 7

Quoted 1 line
testcases/kernel/syscalls/getsid/getsid02.c +0 -1
- *   it under the terms of the GNU General Public License as

published by

Quoted 5 lines
Quoted 5 lines
testcases/kernel/syscalls/getsid/getsid02.c +0 -5
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
Quoted 8 lines
Quoted 8 lines
testcases/kernel/syscalls/getsid/getsid02.c +0 -6
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- *   the GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA

02110-1301 USA
+ * Copyright (c) 2022 SUSE LLC Avinesh Kumar <

[email protected]>

Quoted 38 lines
Quoted 38 lines

*/

testcases/kernel/syscalls/getsid/getsid02.c +8 -23
-#define _GNU_SOURCE 1
-
-#include "test.h"
-
-#include <errno.h>
+/*\
+ * [Description]
+ *
+ * Verify that getsid(2) fails with ESRCH errno when there is no
+ * process found with process ID pid.
+ */
 
-char *TCID = "getsid02";
-int TST_TOTAL = 1;
+#include "tst_test.h"
 
 static pid_t unused_pid;
 
-static void cleanup(void);
-static void setup(void);
-
-int main(int ac, char **av)
+static void setup(void)
 {
-       int lc;
-
-       tst_parse_opts(ac, av, NULL, NULL);
-
-       setup();
-
-       for (lc = 0; TEST_LOOPING(lc); lc++) {
-               tst_count = 0;
-
-               TEST(getsid(unused_pid));
-
-               if (TEST_RETURN == 0) {
Quoted 8 lines

testcases/kernel/syscalls/getsid/getsid02.c

Quoted 8 lines

- tst_resm(TFAIL, "call succeed when failure
expected");

testcases/kernel/syscalls/getsid/getsid02.c +0 -6
-                       continue;
-               }
-
-               switch (TEST_ERRNO) {
-               case ESRCH:
-                       tst_resm(TPASS, "expected failure - errno = %d

-

Quoted 31 lines
Quoted 5 lines

%s",

testcases/kernel/syscalls/getsid/getsid02.c +0 -3
-                                TEST_ERRNO, strerror(TEST_ERRNO));
-                       break;
-               default:

testcases/kernel/syscalls/getsid/getsid02.c line 21

Quoted 10 lines
testcases/kernel/syscalls/getsid/getsid02.c +1 -9
-                       tst_resm(TFAIL, "call failed to produce "
-                                "expected error - errno = %d - %s",
-                                TEST_ERRNO, strerror(TEST_ERRNO));
-                       break;
-               }
-       }
-
-       cleanup();
-       tst_exit();
+       unused_pid = tst_get_unused_pid();

We can simply move this to the main function to avoid unused PID reusing again if people perform test with '-i N'.

How about this below:

#include "tst_test.h"

static void run(void)

{
    pid_t unused_pid;
    unused_pid = tst_get_unused_pid();

    TST_EXP_FAIL(getsid(unused_pid), ESRCH);
}
Quoted 9 lines
Quoted 5 lines

static struct tst_test test = {
.test_all = run
};

Yes, I agree to your suggestion. But while testing '-i N' with this code also I am getting the same unused_pid which is equal to /proc/sys/kernel/pid_max in my env.

Ah, right. I additionally checked the "Linux Programming Interface Handbook",
it says the maximum PID number that can be used is 'pid_max - 1'. So I realized
that why the tst_unused_pid() just read the value of '/proc/sys/kernel/pid_max'.

Anyway, I modified the patch as I commented above and pushed it. (that will be more easiler with removing setup() function)

Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp