~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/openat2/helpers.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-or-later
  2 /*
  3  * Author: Aleksa Sarai <cyphar@cyphar.com>
  4  * Copyright (C) 2018-2019 SUSE LLC.
  5  */
  6 
  7 #define _GNU_SOURCE
  8 #include <errno.h>
  9 #include <fcntl.h>
 10 #include <stdbool.h>
 11 #include <string.h>
 12 #include <syscall.h>
 13 #include <limits.h>
 14 
 15 #include "helpers.h"
 16 
 17 bool needs_openat2(const struct open_how *how)
 18 {
 19         return how->resolve != 0;
 20 }
 21 
 22 int raw_openat2(int dfd, const char *path, void *how, size_t size)
 23 {
 24         int ret = syscall(__NR_openat2, dfd, path, how, size);
 25         return ret >= 0 ? ret : -errno;
 26 }
 27 
 28 int sys_openat2(int dfd, const char *path, struct open_how *how)
 29 {
 30         return raw_openat2(dfd, path, how, sizeof(*how));
 31 }
 32 
 33 int sys_openat(int dfd, const char *path, struct open_how *how)
 34 {
 35         int ret = openat(dfd, path, how->flags, how->mode);
 36         return ret >= 0 ? ret : -errno;
 37 }
 38 
 39 int sys_renameat2(int olddirfd, const char *oldpath,
 40                   int newdirfd, const char *newpath, unsigned int flags)
 41 {
 42         int ret = syscall(__NR_renameat2, olddirfd, oldpath,
 43                                           newdirfd, newpath, flags);
 44         return ret >= 0 ? ret : -errno;
 45 }
 46 
 47 int touchat(int dfd, const char *path)
 48 {
 49         int fd = openat(dfd, path, O_CREAT, 0700);
 50         if (fd >= 0)
 51                 close(fd);
 52         return fd;
 53 }
 54 
 55 char *fdreadlink(int fd)
 56 {
 57         char *target, *tmp;
 58 
 59         E_asprintf(&tmp, "/proc/self/fd/%d", fd);
 60 
 61         target = malloc(PATH_MAX);
 62         if (!target)
 63                 ksft_exit_fail_msg("fdreadlink: malloc failed\n");
 64         memset(target, 0, PATH_MAX);
 65 
 66         E_readlink(tmp, target, PATH_MAX);
 67         free(tmp);
 68         return target;
 69 }
 70 
 71 bool fdequal(int fd, int dfd, const char *path)
 72 {
 73         char *fdpath, *dfdpath, *other;
 74         bool cmp;
 75 
 76         fdpath = fdreadlink(fd);
 77         dfdpath = fdreadlink(dfd);
 78 
 79         if (!path)
 80                 E_asprintf(&other, "%s", dfdpath);
 81         else if (*path == '/')
 82                 E_asprintf(&other, "%s", path);
 83         else
 84                 E_asprintf(&other, "%s/%s", dfdpath, path);
 85 
 86         cmp = !strcmp(fdpath, other);
 87 
 88         free(fdpath);
 89         free(dfdpath);
 90         free(other);
 91         return cmp;
 92 }
 93 
 94 bool openat2_supported = false;
 95 
 96 void __attribute__((constructor)) init(void)
 97 {
 98         struct open_how how = {};
 99         int fd;
100 
101         BUILD_BUG_ON(sizeof(struct open_how) != OPEN_HOW_SIZE_VER0);
102 
103         /* Check openat2(2) support. */
104         fd = sys_openat2(AT_FDCWD, ".", &how);
105         openat2_supported = (fd >= 0);
106 
107         if (fd >= 0)
108                 close(fd);
109 }
110 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php