public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v2 2/2] syscalls/fcntl: add new test for open file description locks
Date: Thu, 7 Apr 2016 14:38:16 +0200	[thread overview]
Message-ID: <20160407123816.GB16682@rei> (raw)
In-Reply-To: <1460016651-24181-2-git-send-email-alexey.kodanev@oracle.com>

Hi!
> +#include <sys/types.h>
> +#include <sys/stat.h>
> +#include <unistd.h>
> +#include <fcntl.h>
> +#include <pthread.h>
> +
> +#include "lapi/fcntl.h"
> +#include "tst_test.h"
> +
> +static int thread_cnt;
> +static const int max_thread_cnt = 32;
> +static const char fname[] = "tst_ofd_locks";
> +const int writes_num = 100;
> +const int write_size = 4096;
> +
> +static void setup(void)
> +{
> +	thread_cnt = tst_ncpus_conf() * 3;
> +	if (thread_cnt > max_thread_cnt)
> +		thread_cnt = max_thread_cnt;
> +}
> +
> +static void spawn_threads(pthread_t *id, void *(*thread_fn)(void *))
> +{
> +	intptr_t i;
> +
> +	tst_res(TINFO, "spawning '%d' threads", thread_cnt);
> +	for (i = 0; i < thread_cnt; ++i)
> +		SAFE_PTHREAD_CREATE(id + i, NULL, thread_fn, (void *)i);
> +}
> +
> +static void wait_threads(pthread_t *id)
> +{
> +	int i;
> +
> +	tst_res(TINFO, "waiting for '%d' threads", thread_cnt);
> +	for (i = 0; i < thread_cnt; ++i)
> +		SAFE_PTHREAD_JOIN(id[i], NULL);
> +}
> +
> +void *thread_fn_01(void *arg)
> +{
> +	int i;
> +	char buf[write_size];
> +	int fd = SAFE_OPEN(fname, O_RDWR);
> +
> +	memset(buf, (intptr_t)arg, write_size);
> +
> +	struct flock lck = {
> +		.l_whence = SEEK_SET,
> +		.l_start  = 0,
> +		.l_len    = 1,
> +	};
> +
> +	for (i = 0; i < writes_num; ++i) {
> +		lck.l_type = F_WRLCK;
> +		if (fcntl(fd, F_OFD_SETLKW, &lck) == -1)
> +			tst_brk(TBROK | TERRNO, "fcntl() failed");
> +
> +		SAFE_LSEEK(fd, 0, SEEK_END);
> +		SAFE_WRITE(1, fd, buf, write_size);
> +
> +		lck.l_type = F_UNLCK;
> +		if (fcntl(fd, F_OFD_SETLKW, &lck) == -1)
> +			tst_brk(TBROK | TERRNO, "fcntl() failed");
> +
> +		pthread_yield();
> +	}
> +
> +	SAFE_CLOSE(fd);
> +
> +	return NULL;
> +}
> +
> +static void test01(void)
> +{
> +	intptr_t i;
> +	int k;
> +	pthread_t id[thread_cnt];
> +	int res[thread_cnt];
> +	char buf[write_size];
> +
> +	tst_res(TINFO, "write to a file inside threads with OFD locks");
> +
> +	int fd = SAFE_OPEN(fname, O_CREAT | O_TRUNC | O_RDONLY, 0600);

I'm not sure that O_TRUNC | O_RDONLY would work. The manual says that
the result is unspecified.

We should either change the O_RDONLY for O_RDWR or simply unlink() the
file before we open it.

Otherwise (with the unsigned char buffer) it looks fine.

-- 
Cyril Hrubis
chrubis@suse.cz

  reply	other threads:[~2016-04-07 12:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-07  8:10 [LTP] [PATCH v2 1/2] lib: add safe_pthread_create() & safe_pthread_join() Alexey Kodanev
2016-04-07  8:10 ` [LTP] [PATCH v2 2/2] syscalls/fcntl: add new test for open file description locks Alexey Kodanev
2016-04-07 12:38   ` Cyril Hrubis [this message]
2016-04-12 10:01   ` Alexey Kodanev
2016-04-07 11:35 ` [LTP] [PATCH v2 1/2] lib: add safe_pthread_create() & safe_pthread_join() Cyril Hrubis
2016-04-07 16:42   ` Alexey Kodanev
2016-04-11 14:46     ` Cyril Hrubis
2016-04-12  9:21       ` Alexey Kodanev

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=20160407123816.GB16682@rei \
    --to=chrubis@suse.cz \
    --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