Dmitry V. Levin | 4b3a170 | 2015-11-22 21:29:32 +0000 | [diff] [blame] | 1 | /* |
Dmitry V. Levin | c83746a | 2016-01-06 16:01:46 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org> |
Dmitry V. Levin | 4b3a170 | 2015-11-22 21:29:32 +0000 | [diff] [blame] | 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. Levin | 0c8853c | 2016-01-02 13:28:43 +0000 | [diff] [blame] | 28 | #include "tests.h" |
Dmitry V. Levin | 4b3a170 | 2015-11-22 21:29:32 +0000 | [diff] [blame] | 29 | #include <fcntl.h> |
Dmitry V. Levin | 4b3a170 | 2015-11-22 21:29:32 +0000 | [diff] [blame] | 30 | |
| 31 | #ifdef MAX_HANDLE_SZ |
| 32 | |
Dmitry V. Levin | c83746a | 2016-01-06 16:01:46 +0000 | [diff] [blame] | 33 | # include <alloca.h> |
| 34 | # include <assert.h> |
| 35 | # include <errno.h> |
| 36 | # include <stdio.h> |
| 37 | |
Dmitry V. Levin | 4b3a170 | 2015-11-22 21:29:32 +0000 | [diff] [blame] | 38 | int |
| 39 | main(void) |
| 40 | { |
| 41 | struct file_handle *handle = |
| 42 | alloca(sizeof(struct file_handle) + MAX_HANDLE_SZ); |
| 43 | const int dirfd = AT_FDCWD; |
| 44 | const int flags = AT_SYMLINK_FOLLOW; |
| 45 | int mount_id; |
| 46 | unsigned int i; |
| 47 | |
| 48 | handle->handle_bytes = 0; |
| 49 | |
Dmitry V. Levin | c83746a | 2016-01-06 16:01:46 +0000 | [diff] [blame] | 50 | assert(name_to_handle_at(dirfd, ".", handle, &mount_id, flags | 1) == -1); |
| 51 | if (EINVAL != errno) |
| 52 | perror_msg_and_skip("name_to_handle_at"); |
Dmitry V. Levin | 4b3a170 | 2015-11-22 21:29:32 +0000 | [diff] [blame] | 53 | printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=0}, %p" |
Dmitry V. Levin | c83746a | 2016-01-06 16:01:46 +0000 | [diff] [blame] | 54 | ", AT_SYMLINK_FOLLOW|0x1) = -1 EINVAL (%m)\n", &mount_id); |
Dmitry V. Levin | 4b3a170 | 2015-11-22 21:29:32 +0000 | [diff] [blame] | 55 | |
Dmitry V. Levin | c83746a | 2016-01-06 16:01:46 +0000 | [diff] [blame] | 56 | assert(name_to_handle_at(dirfd, ".", handle, &mount_id, flags) == -1); |
| 57 | if (EOVERFLOW != errno) |
| 58 | perror_msg_and_skip("name_to_handle_at"); |
Dmitry V. Levin | 4b3a170 | 2015-11-22 21:29:32 +0000 | [diff] [blame] | 59 | printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=0 => %u}" |
Dmitry V. Levin | c83746a | 2016-01-06 16:01:46 +0000 | [diff] [blame] | 60 | ", %p, AT_SYMLINK_FOLLOW) = -1 EOVERFLOW (%m)\n", |
Dmitry V. Levin | 4b3a170 | 2015-11-22 21:29:32 +0000 | [diff] [blame] | 61 | handle->handle_bytes, &mount_id); |
| 62 | |
Dmitry V. Levin | c83746a | 2016-01-06 16:01:46 +0000 | [diff] [blame] | 63 | assert(name_to_handle_at(dirfd, ".", handle, &mount_id, flags) == 0); |
Dmitry V. Levin | 4b3a170 | 2015-11-22 21:29:32 +0000 | [diff] [blame] | 64 | printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=%u" |
| 65 | ", handle_type=%d, f_handle=0x", |
| 66 | handle->handle_bytes, handle->handle_type); |
| 67 | for (i = 0; i < handle->handle_bytes; ++i) |
| 68 | printf("%02x", handle->f_handle[i]); |
| 69 | printf("}, [%d], AT_SYMLINK_FOLLOW) = 0\n", mount_id); |
| 70 | |
Dmitry V. Levin | 4c65ff0 | 2015-11-26 16:48:23 +0000 | [diff] [blame] | 71 | printf("open_by_handle_at(-1, {handle_bytes=%u, handle_type=%d" |
| 72 | ", f_handle=0x", handle->handle_bytes, handle->handle_type); |
Dmitry V. Levin | 4b3a170 | 2015-11-22 21:29:32 +0000 | [diff] [blame] | 73 | for (i = 0; i < handle->handle_bytes; ++i) |
| 74 | printf("%02x", handle->f_handle[i]); |
Dmitry V. Levin | c83746a | 2016-01-06 16:01:46 +0000 | [diff] [blame] | 75 | printf("}, O_RDONLY|O_DIRECTORY) = -1 "); |
| 76 | assert(open_by_handle_at(-1, handle, O_RDONLY | O_DIRECTORY) == -1); |
| 77 | const char *errno_text; |
| 78 | switch (errno) { |
| 79 | case EPERM: |
| 80 | errno_text = "EPERM"; |
| 81 | break; |
| 82 | case EINVAL: |
| 83 | errno_text = "EINVAL"; |
| 84 | break; |
| 85 | default: |
| 86 | errno_text = "EBADF"; |
| 87 | } |
| 88 | printf("%s (%m)\n", errno_text); |
Dmitry V. Levin | 4b3a170 | 2015-11-22 21:29:32 +0000 | [diff] [blame] | 89 | |
| 90 | puts("+++ exited with 0 +++"); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | #else |
| 95 | |
Dmitry V. Levin | c83746a | 2016-01-06 16:01:46 +0000 | [diff] [blame] | 96 | SKIP_MAIN_UNDEFINED("MAX_HANDLE_SZ") |
Dmitry V. Levin | 4b3a170 | 2015-11-22 21:29:32 +0000 | [diff] [blame] | 97 | |
| 98 | #endif |