1/*
2 * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 *    derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifdef HAVE_CONFIG_H
29# include "config.h"
30#endif
31
32#include <alloca.h>
33#include <assert.h>
34#include <errno.h>
35#include <fcntl.h>
36#include <stdio.h>
37
38#ifdef MAX_HANDLE_SZ
39
40int
41main(void)
42{
43	struct file_handle *handle =
44		alloca(sizeof(struct file_handle) + MAX_HANDLE_SZ);
45	const int dirfd = AT_FDCWD;
46	const int flags = AT_SYMLINK_FOLLOW;
47	int mount_id;
48	unsigned int i;
49
50	handle->handle_bytes = 0;
51
52	if (name_to_handle_at(dirfd, ".", handle, &mount_id, flags | 1) != -1
53	    || EINVAL != errno) {
54		perror("name_to_handle_at");
55		return 77;
56	}
57	printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=0}, %p"
58	       ", AT_SYMLINK_FOLLOW|0x1) = -1 EINVAL (Invalid argument)\n",
59	       &mount_id);
60
61	if (name_to_handle_at(dirfd, ".", handle, &mount_id, flags) != -1
62	    || EOVERFLOW != errno) {
63		perror("name_to_handle_at");
64		return 77;
65	}
66	printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=0 => %u}"
67	       ", %p, AT_SYMLINK_FOLLOW) = -1 EOVERFLOW"
68	       " (Value too large for defined data type)\n",
69	       handle->handle_bytes, &mount_id);
70
71	assert(!name_to_handle_at(dirfd, ".", handle, &mount_id, flags));
72	printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=%u"
73	       ", handle_type=%d, f_handle=0x",
74	       handle->handle_bytes, handle->handle_type);
75	for (i = 0; i < handle->handle_bytes; ++i)
76		printf("%02x", handle->f_handle[i]);
77	printf("}, [%d], AT_SYMLINK_FOLLOW) = 0\n", mount_id);
78
79	assert(open_by_handle_at(-1, handle, O_RDONLY | O_DIRECTORY));
80	printf("open_by_handle_at(-1, {handle_bytes=%u, handle_type=%d"
81	       ", f_handle=0x", handle->handle_bytes, handle->handle_type);
82	for (i = 0; i < handle->handle_bytes; ++i)
83		printf("%02x", handle->f_handle[i]);
84	printf("}, O_RDONLY|O_DIRECTORY) = -1 %s\n",
85	       EPERM == errno ? "EPERM (Operation not permitted)" :
86	       EINVAL == errno ? "EINVAL (Invalid argument)" :
87				 "EBADF (Bad file descriptor)");
88
89	puts("+++ exited with 0 +++");
90	return 0;
91}
92
93#else
94
95int
96main(void)
97{
98	return 77;
99}
100
101#endif
102