[PATCH] tests: add openat.test

Dmitry V. Levin ldv at altlinux.org
Wed Mar 23 01:18:42 UTC 2016


On Wed, Mar 23, 2016 at 12:12:01AM +0200, Katerina Koukiou wrote:
> +int
> +main(void)
> +{
> +	int dir_fd;
> +	int fd;
> +	char cwd[1024];
> +	static const char fname[] = "openat_test_file";
> +
> +	if (getcwd(cwd, sizeof(cwd)) == NULL)
> +		perror_msg_and_fail("getcwd");

sizeof(cwd) < PATH_MAX.

> +	dir_fd = open(cwd, O_RDONLY);

The same effect could be achieved by opening "." instead of cwd.
Remember that on recently added architectures open(3) is implemented using
openat(2).

> +	if (dir_fd < 0)
> +		perror_msg_and_fail("open");
> +
> +	if (open(fname, O_CREAT|O_RDONLY, 0400) == -1)
> +		perror_msg_and_fail("open");

Descriptor leak.
The same comment about open(3) implemented using openat(2).

> +	fd = openat(dir_fd,fname, O_RDONLY);

The same effect could be achieved by using AT_FDCWD instead of dir_fd.

Manual page on openat says that "openat() was added to Linux in kernel
2.6.16; library support was added to glibc in version 2.4.".

Possible workarounds: a configure check for openat together with a check
in the test, or change to syscall() with a check for __NR_openat.

> +	if (fd < 0)
> +		perror_msg_and_fail("openat");
> +	else
> +		printf("openat(%d, \"%s\", O_RDONLY) = %d\n", dir_fd, fname, fd);
> +
> +	if (unlink(fname) == -1)
> +		perror_msg_and_fail("unlink");
> +
> +	fd = openat(dir_fd,fname, O_RDONLY);

A space is needed after comma.

> +	if (fd < 0)
> +		printf("openat(%d, \"%s\", O_RDONLY) = %d ENOENT (%m)\n", dir_fd, fname,fd);

This line is too long.

> +	else
> +		perror_msg_and_fail("openat");
> +
> +	close(fd);

fd == -1 at this point.

> +	close(dir_fd);
> +
> +	puts("+++ exited with 0 +++");
> +	return 0;
> +}
> +

-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20160323/6d4e5638/attachment.bin>


More information about the Strace-devel mailing list