blob: f52b6200b13e4d796aeb7421c1f242a9a1d06273 [file] [log] [blame]
Dmitry V. Levin4b3a1702015-11-22 21:29:32 +00001/*
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
Dmitry V. Levin0c8853c2016-01-02 13:28:43 +000028#include "tests.h"
Dmitry V. Levin4b3a1702015-11-22 21:29:32 +000029#include <alloca.h>
30#include <assert.h>
31#include <errno.h>
32#include <fcntl.h>
33#include <stdio.h>
34
35#ifdef MAX_HANDLE_SZ
36
37int
38main(void)
39{
40 struct file_handle *handle =
41 alloca(sizeof(struct file_handle) + MAX_HANDLE_SZ);
42 const int dirfd = AT_FDCWD;
43 const int flags = AT_SYMLINK_FOLLOW;
44 int mount_id;
45 unsigned int i;
46
47 handle->handle_bytes = 0;
48
49 if (name_to_handle_at(dirfd, ".", handle, &mount_id, flags | 1) != -1
50 || EINVAL != errno) {
51 perror("name_to_handle_at");
52 return 77;
53 }
54 printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=0}, %p"
55 ", AT_SYMLINK_FOLLOW|0x1) = -1 EINVAL (Invalid argument)\n",
56 &mount_id);
57
58 if (name_to_handle_at(dirfd, ".", handle, &mount_id, flags) != -1
59 || EOVERFLOW != errno) {
60 perror("name_to_handle_at");
61 return 77;
62 }
63 printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=0 => %u}"
64 ", %p, AT_SYMLINK_FOLLOW) = -1 EOVERFLOW"
65 " (Value too large for defined data type)\n",
66 handle->handle_bytes, &mount_id);
67
68 assert(!name_to_handle_at(dirfd, ".", handle, &mount_id, flags));
69 printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=%u"
70 ", handle_type=%d, f_handle=0x",
71 handle->handle_bytes, handle->handle_type);
72 for (i = 0; i < handle->handle_bytes; ++i)
73 printf("%02x", handle->f_handle[i]);
74 printf("}, [%d], AT_SYMLINK_FOLLOW) = 0\n", mount_id);
75
76 assert(open_by_handle_at(-1, handle, O_RDONLY | O_DIRECTORY));
Dmitry V. Levin4c65ff02015-11-26 16:48:23 +000077 printf("open_by_handle_at(-1, {handle_bytes=%u, handle_type=%d"
78 ", f_handle=0x", handle->handle_bytes, handle->handle_type);
Dmitry V. Levin4b3a1702015-11-22 21:29:32 +000079 for (i = 0; i < handle->handle_bytes; ++i)
80 printf("%02x", handle->f_handle[i]);
81 printf("}, O_RDONLY|O_DIRECTORY) = -1 %s\n",
82 EPERM == errno ? "EPERM (Operation not permitted)" :
83 EINVAL == errno ? "EINVAL (Invalid argument)" :
84 "EBADF (Bad file descriptor)");
85
86 puts("+++ exited with 0 +++");
87 return 0;
88}
89
90#else
91
92int
93main(void)
94{
95 return 77;
96}
97
98#endif