public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Jan Stancek <jstancek@redhat.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH 4/5] syscalls/flock04: Rewrite to new library
Date: Thu, 2 Aug 2018 08:17:00 -0400 (EDT)	[thread overview]
Message-ID: <1739695693.37582576.1533212220162.JavaMail.zimbra@redhat.com> (raw)
In-Reply-To: <1533182024-18841-4-git-send-email-huangjh.jy@cn.fujitsu.com>


----- Original Message -----
> 1) Avoid locking closed fd when running flock04 in loops
> 2) Merge flock05 into flock04
> 
> Signed-off-by: Jinhui huang <huangjh.jy@cn.fujitsu.com>

+1 for merge, these are very similar

flock04.c:45: PASS: flock() succeeded in acquiring shared lockas expecetd
flock04.c:55: PASS: flock() failed to acquire shared lock as expecetd
flock04.c:55: PASS: flock() failed to acquire exclusive lock as expecetd
flock04.c:55: PASS: flock() failed to acquire exclusive lock as expecetd

This output is bit confusing. 2nd line is test where child is trying to
get exclusive lock". It would help to see what kind of lock parent holds
and what lock is child trying to acquire.

"lockas" is missing space
"expecetd" is typo

> +static void child(int opt1, int opt2, char *lock)
> +	int retval, fd1;
> +
> +	fd1 = SAFE_OPEN("testfile", O_RDWR);
> +	retval = flock(fd1, opt1);
> +	if (LOCK_SH & opt1 && opt2 == LOCK_SH) {
> +		if (retval == -1) {
> +			tst_res(TFAIL, "flock() failed to acquire %s",
> +				lock);

TERRNO would be nice to know here

> +			exit(1);

tst_res will propagate TFAIL to parent via shared memory, so
these exit codes don't matter much, if you use only one exit(0),
after SAFE_CLOSE below, it would work the same.

> +		} else {
> +			tst_res(TPASS, "flock() succeeded in acquiring %s"
> +				"as expecetd", lock);
> +			exit(0);
> +		}
> +	} else {
> +		if (retval == 0) {
> +			tst_res(TFAIL, "flock() succeeded in acquiring %s",
> +				lock);
> +			exit(1);
> +		} else {
> +			tst_res(TPASS, "flock() failed to acquire %s "
> +				"as expecetd", lock);
> +			exit(0);
> +		}
>  	}

If this is unreachable a comment would be nice.

> +	SAFE_CLOSE(fd1);
>  }

For example, I'd suggest following:

static void child(int opt1, int should_pass, char *lock)
{
        int retval, fd1;

        fd1 = SAFE_OPEN("testfile", O_RDWR);
        retval = flock(fd1, opt1);
        if (should_pass)
                tst_res(retval == -1 ? TFAIL : TPASS,
                        " child acquiring %s got %d", lock, retval);
        else
                tst_res(retval == -1 ? TPASS : TFAIL,
                        " child acquiring %s got %d", lock, retval);

        SAFE_CLOSE(fd1);
        exit(0);
}

static void verify_flock(unsigned n)
{
        int fd2;
        pid_t pid;
        struct tcase *tc = &tcases[n];

        fd2 = SAFE_OPEN("testfile", O_RDWR);
        TEST(flock(fd2, tc->operation));
        if (TST_RET != 0) {
                tst_res(TFAIL, "flock() failed to acquire %s", tc->filelock);
                return;
        }
        tst_res(TPASS, "Parent has %s", tc->filelock);

        pid = SAFE_FORK();
        if (pid == 0)
                child(LOCK_SH | LOCK_NB, tc->operation & LOCK_SH, "shared lock");
        else
                tst_reap_children();

        pid = SAFE_FORK();
        if (pid == 0)
                child(LOCK_EX | LOCK_NB, 0, "exclusive lock");
        else
                tst_reap_children();

        SAFE_CLOSE(fd2);
}

which produces:

tst_test.c:1018: INFO: Timeout per run is 0h 05m 00s
flock04.c:61: PASS: Parent has shared lock
flock04.c:40: PASS:  child acquiring shared lock got 0
flock04.c:43: PASS:  child acquiring exclusive lock got -1
flock04.c:61: PASS: Parent has exclusive lock
flock04.c:43: PASS:  child acquiring shared lock got -1
flock04.c:43: PASS:  child acquiring exclusive lock got -1

What do you think?

Regards,
Jan

  reply	other threads:[~2018-08-02 12:17 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-02  3:53 [LTP] [PATCH 1/5] syscalls/flock01: Rewrite to new library Jinhui huang
2018-08-02  3:53 ` [LTP] [PATCH 2/5] syscalls/flock02: " Jinhui huang
2018-08-02 11:12   ` Jan Stancek
2018-08-02  3:53 ` [LTP] [PATCH 3/5] syscalls/flock03: " Jinhui huang
2018-08-02 11:20   ` Jan Stancek
2018-08-02  3:53 ` [LTP] [PATCH 4/5] syscalls/flock04: " Jinhui huang
2018-08-02 12:17   ` Jan Stancek [this message]
2018-08-06  5:07     ` [LTP] [PATCH v2 " Jinhui huang
2018-08-02  3:53 ` [LTP] [PATCH 5/5] syscalls/flock06: " Jinhui huang
2018-08-02 12:29   ` Jan Stancek
2018-08-06  5:08     ` [LTP] [PATCH v2 " Jinhui huang
2018-08-06  7:07       ` Jan Stancek
2018-08-02 11:03 ` [LTP] [PATCH 1/5] syscalls/flock01: " Jan Stancek

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=1739695693.37582576.1533212220162.JavaMail.zimbra@redhat.com \
    --to=jstancek@redhat.com \
    --cc=ltp@lists.linux.it \
    /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