public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: Wei Gao <wegao@suse.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH v3 1/2] ptrace05: Refactor the test using new LTP API
Date: Fri, 28 Jun 2024 17:35:07 +0200	[thread overview]
Message-ID: <Zn7YK-5Ub5NxBtDA@yuki> (raw)
In-Reply-To: <20240603103553.8318-2-wegao@suse.com>

Hi!
> +	for (signum = 0; signum <= SIGRTMAX; signum++) {

I would put the code in this for loop into a function so that we save
some indentation as:

	for (signum = 0; signum <= SIGRTMAX; signum++) {
		if (signum >= __SIGRTMIN && signum < SIGRTMIN)
			continue;

		test_signal(sig);
	}
>  
> -		switch (child = fork()) {
> -		case -1:
> -			tst_brkm(TBROK | TERRNO, NULL, "fork() failed");
> -		case 0:
> +		child = SAFE_FORK();
>  
> -			if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != -1) {
> -				tst_resm(TINFO, "[child] Sending kill(.., %d)",
> -					 signum);
> -				if (kill(getpid(), signum) < 0) {
> -					tst_resm(TINFO | TERRNO,
> -						 "[child] kill(.., %d) failed.",
> -						 signum);
> -				}
> +		if (child == 0) {
> +			TEST(ptrace(PTRACE_TRACEME, 0, NULL, NULL));

TST_EXP_PASS()?

I guess it would look like:


	if (!child) {
		TST_EXP_PASS(ptrace(PTRACE_TRACEME, 0, NULL, NULL);

		if (!TST_PASS)
			exit(0);

		tst_res(...);

		SAFE_KILL(getpid(), signum);
	} else {
		...
	}

> +			if (TST_RET != -1) {
> +				tst_res(TINFO, "[child] Sending kill(.., %d)",
> +						signum);

Maybe TDEBUG instead of TINFO to avoid overly long output?

And we can use tst_strsig() to print the signal name as well.

> +				SAFE_KILL(getpid(), signum);
>  			} else {
> -
> -				/*
> -				 * This won't increment the TST_COUNT var.
> -				 * properly, but it'll show up as a failure
> -				 * nonetheless.
> -				 */
> -				tst_resm(TFAIL | TERRNO,
> -					 "Failed to ptrace(PTRACE_TRACEME, ...) "
> -					 "properly");
> -
> +				tst_brk(TFAIL | TERRNO,
> +						"Failed to ptrace(PTRACE_TRACEME, ...) "
> +						"properly");
>  			}
> +
>  			/* Shouldn't get here if signum == 0. */
>  			exit((signum == 0 ? 0 : 2));

We do not use the exit value for anything in the new code, so this can
be just exit(0).

> -			break;
> -
> -		default:
> +		}
>  
> -			waitpid(child, &status, 0);
> +		SAFE_WAITPID(child, &status, 0);
>  
> -			switch (signum) {
> -			case 0:
> -				if (WIFEXITED(status)
> -				    && WEXITSTATUS(status) == 0) {
> -					tst_resm(TPASS,
> -						 "kill(.., 0) exited "
> -						 "with 0, as expected.");
> -				} else {
> -					tst_resm(TFAIL,
> -						 "kill(.., 0) didn't exit "
> -						 "with 0.");
> -				}
> -				break;
> -			case SIGKILL:
> -				if (WIFSIGNALED(status)) {
> -					/* SIGKILL must be uncatchable. */
> -					if (WTERMSIG(status) == SIGKILL) {
> -						tst_resm(TPASS,
> -							 "Killed with SIGKILL, "
> -							 "as expected.");
> -					} else {
> -						tst_resm(TPASS,
> -							 "Didn't die with "
> -							 "SIGKILL (?!) ");
> -					}
> -				} else if (WIFEXITED(status)) {
> -					tst_resm(TFAIL,
> -						 "Exited unexpectedly instead "
> -						 "of dying with SIGKILL.");
> -				} else if (WIFSTOPPED(status)) {
> -					tst_resm(TFAIL,
> -						 "Stopped instead of dying "
> -						 "with SIGKILL.");
> -				}
> -				break;
> -				/* All other processes should be stopped. */
> -			default:
> -				if (WIFSTOPPED(status)) {
> -					tst_resm(TPASS, "Stopped as expected");
> +		switch (signum) {
> +		case 0:
> +			if (WIFEXITED(status)
> +					&& WEXITSTATUS(status) == 0) {
> +				tst_res(TPASS,
> +						"kill(.., 0) exited with 0, as expected.");
> +			} else {
> +				tst_brk(TFAIL | TERRNO,
> +						"kill(.., 0) didn't exit with 0.");

We do have tst_strstatus() so that we can print what was the reason
child did exit, please use it this TFAIL message.

> +			}
> +			break;
> +		case SIGKILL:
> +			if (WIFSIGNALED(status)) {
> +				/* SIGKILL must be uncatchable. */
> +				if (WTERMSIG(status) == SIGKILL) {
> +					tst_res(TPASS,
> +							"Killed with SIGKILL, as expected.");
>  				} else {
> -					tst_resm(TFAIL, "Didn't stop as "
> -						 "expected.");
> -					if (kill(child, 0)) {
> -						tst_resm(TINFO,
> -							 "Is still alive!?");
> -					} else if (WIFEXITED(status)) {
> -						tst_resm(TINFO,
> -							 "Exited normally");
> -					} else if (WIFSIGNALED(status)) {
> -						tst_resm(TINFO,
> -							 "Was signaled with "
> -							 "signum=%d",
> -							 WTERMSIG(status));
> -					}
> -
> +					tst_brk(TFAIL | TERRNO,
> +							"Didn't die with SIGKILL (?!) ");
>  				}
> -
> -				break;
> -
> +			} else if (WIFEXITED(status)) {
> +				tst_res(TFAIL | TERRNO,
> +						"Exited unexpectedly instead of dying with SIGKILL.");
> +			} else if (WIFSTOPPED(status)) {
> +				tst_res(TFAIL | TERRNO,
> +						"Stopped instead of dying with SIGKILL.");

And here as well, this whole part can be replaced by just:

		if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL)
			tst_res(TPASS, "Child killed by SIGKILL");
		else
			tst_res(TFAIL, "Child %s", tst_strstatus(status));

Also notice that we shouldn't print errno in this case, so no TERRNO
flag there.

> -
> +			break;
> +			/* All other processes should be stopped. */
> +		default:
> +			if (WIFSTOPPED(status))
> +				tst_res(TPASS, "Stopped as expected");
> +			else
> +				tst_res(TFAIL | TERRNO, "Didn't stop as expected.");

Here as well, make use of the tst_strstatus() please.

> +			break;
>  		}
> -		/* Make sure the child dies a quick and painless death ... */
> -		kill(child, 9);
>  
> +		if (signum != 0 && signum != SIGKILL)
> +			SAFE_PTRACE(PTRACE_CONT, child, NULL, NULL);
>  	}
> -
> -	tst_exit();
> -
>  }
> +
> +static struct tst_test test = {
> +	.test_all = run,
> +	.forks_child = 1,
> +};
> -- 
> 2.35.3
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  reply	other threads:[~2024-06-28 15:35 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-25 11:22 [LTP] [PATCH v1 0/2] ptrace: Refactor Wei Gao via ltp
2023-09-25 11:22 ` [LTP] [PATCH v1 1/2] ptrace05: Refactor the test using new LTP API Wei Gao via ltp
2023-11-28  8:57   ` Richard Palethorpe
2023-11-28  9:24   ` Petr Vorel
2023-09-25 11:22 ` [LTP] [PATCH v1 2/2] ptrace06: " Wei Gao via ltp
2023-11-28  9:31   ` Richard Palethorpe
2023-11-28  9:51   ` Petr Vorel
2023-12-01  1:06     ` Wei Gao via ltp
2023-12-01  0:59 ` [LTP] [PATCH v2 0/2] ptrace: Refactor Wei Gao via ltp
2023-12-01  0:59   ` [LTP] [PATCH v2 1/2] ptrace05: Refactor the test using new LTP API Wei Gao via ltp
2024-02-08 16:15     ` Andrea Cervesato via ltp
2023-12-01  0:59   ` [LTP] [PATCH v2 2/2] ptrace06: " Wei Gao via ltp
2024-02-08 16:25     ` Andrea Cervesato via ltp
2024-06-03 10:35   ` [LTP] [PATCH v3 0/2] ptrace: Refactor Wei Gao via ltp
2024-06-03 10:35     ` [LTP] [PATCH v3 1/2] ptrace05: Refactor the test using new LTP API Wei Gao via ltp
2024-06-28 15:35       ` Cyril Hrubis [this message]
2024-06-03 10:35     ` [LTP] [PATCH v3 2/2] ptrace06: " Wei Gao via ltp
2024-06-28 16:15       ` Cyril Hrubis
2024-12-17  6:16     ` [LTP] [PATCH v4 0/2] ptrace: Refactor Wei Gao via ltp
2024-12-17  6:16       ` [LTP] [PATCH v4 1/2] ptrace05: Refactor the test using new LTP API Wei Gao via ltp
2025-01-08 13:39         ` Petr Vorel
2024-12-17  6:16       ` [LTP] [PATCH v4 2/2] ptrace06: " Wei Gao via ltp
2025-01-09  8:55         ` Petr Vorel
2025-01-13  8:16       ` [LTP] [PATCH v5 0/2] ptrace: Refactor Wei Gao via ltp
2025-01-13  8:16         ` [LTP] [PATCH v5 1/2] ptrace05: Refactor the test using new LTP API Wei Gao via ltp
2025-01-13 16:02           ` Cyril Hrubis
2025-01-13 21:40             ` Petr Vorel
2025-01-14  9:25               ` Cyril Hrubis
2025-01-13  8:16         ` [LTP] [PATCH v5 2/2] ptrace06: " Wei Gao via ltp
2025-01-14 12:40         ` [LTP] [PATCH v6 0/2] ptrace: Refactor Wei Gao via ltp
2025-01-14 12:40           ` [LTP] [PATCH v6 1/2] ptrace05: Refactor the test using new LTP API Wei Gao via ltp
2025-01-14 13:05             ` Cyril Hrubis
2025-01-14 12:40           ` [LTP] [PATCH v6 2/2] ptrace06: " Wei Gao via ltp
2025-01-14 14:32           ` [LTP] [PATCH v7 0/2] ptrace: Refactor Wei Gao via ltp
2025-01-14 14:32             ` [LTP] [PATCH v7 1/2] ptrace05: Refactor the test using new LTP API Wei Gao via ltp
2025-01-16 16:44               ` Cyril Hrubis
2025-01-14 14:32             ` [LTP] [PATCH v7 2/2] ptrace06: " Wei Gao via ltp
2025-01-16 16:50               ` Cyril Hrubis
2025-01-17 10:40               ` Petr Vorel
2025-01-17 10:42                 ` Cyril Hrubis
2025-01-17 11:12                   ` Petr Vorel
2025-01-20  4:14             ` [LTP] [PATCH v8 0/2] ptrace: Refactor Wei Gao via ltp
2025-01-20  4:14               ` [LTP] [PATCH v8 1/2] ptrace06: Refactor the test using new LTP API Wei Gao via ltp
2025-01-20 13:35                 ` Petr Vorel
2025-01-20  4:14               ` [LTP] [PATCH v8 2/2] ptrace06_child.c: Remove unused ptrace06_child.c Wei Gao via ltp

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Zn7YK-5Ub5NxBtDA@yuki \
    --to=chrubis@suse.cz \
    --cc=ltp@lists.linux.it \
    --cc=wegao@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox