16482a961SGreg Kurz /* 26482a961SGreg Kurz * 9p utilities 36482a961SGreg Kurz * 46482a961SGreg Kurz * Copyright IBM, Corp. 2017 56482a961SGreg Kurz * 66482a961SGreg Kurz * Authors: 76482a961SGreg Kurz * Greg Kurz <groug@kaod.org> 86482a961SGreg Kurz * 96482a961SGreg Kurz * This work is licensed under the terms of the GNU GPL, version 2 or later. 106482a961SGreg Kurz * See the COPYING file in the top-level directory. 116482a961SGreg Kurz */ 126482a961SGreg Kurz 136482a961SGreg Kurz #ifndef QEMU_9P_UTIL_H 146482a961SGreg Kurz #define QEMU_9P_UTIL_H 156482a961SGreg Kurz 166482a961SGreg Kurz static inline void close_preserve_errno(int fd) 176482a961SGreg Kurz { 186482a961SGreg Kurz int serrno = errno; 196482a961SGreg Kurz close(fd); 206482a961SGreg Kurz errno = serrno; 216482a961SGreg Kurz } 226482a961SGreg Kurz 236482a961SGreg Kurz static inline int openat_dir(int dirfd, const char *name) 246482a961SGreg Kurz { 25*918112c0SGreg Kurz #ifdef O_PATH 26*918112c0SGreg Kurz #define OPENAT_DIR_O_PATH O_PATH 27*918112c0SGreg Kurz #else 28*918112c0SGreg Kurz #define OPENAT_DIR_O_PATH 0 29*918112c0SGreg Kurz #endif 30*918112c0SGreg Kurz return openat(dirfd, name, O_DIRECTORY | O_RDONLY | OPENAT_DIR_O_PATH); 316482a961SGreg Kurz } 326482a961SGreg Kurz 336482a961SGreg Kurz static inline int openat_file(int dirfd, const char *name, int flags, 346482a961SGreg Kurz mode_t mode) 356482a961SGreg Kurz { 366482a961SGreg Kurz int fd, serrno, ret; 376482a961SGreg Kurz 386482a961SGreg Kurz fd = openat(dirfd, name, flags | O_NOFOLLOW | O_NOCTTY | O_NONBLOCK, 396482a961SGreg Kurz mode); 406482a961SGreg Kurz if (fd == -1) { 416482a961SGreg Kurz return -1; 426482a961SGreg Kurz } 436482a961SGreg Kurz 446482a961SGreg Kurz serrno = errno; 456482a961SGreg Kurz /* O_NONBLOCK was only needed to open the file. Let's drop it. */ 466482a961SGreg Kurz ret = fcntl(fd, F_SETFL, flags); 476482a961SGreg Kurz assert(!ret); 486482a961SGreg Kurz errno = serrno; 496482a961SGreg Kurz return fd; 506482a961SGreg Kurz } 516482a961SGreg Kurz 526482a961SGreg Kurz int relative_openat_nofollow(int dirfd, const char *path, int flags, 536482a961SGreg Kurz mode_t mode); 5456ad3e54SGreg Kurz ssize_t fgetxattrat_nofollow(int dirfd, const char *path, const char *name, 5556ad3e54SGreg Kurz void *value, size_t size); 563e36aba7SGreg Kurz int fsetxattrat_nofollow(int dirfd, const char *path, const char *name, 573e36aba7SGreg Kurz void *value, size_t size, int flags); 586482a961SGreg Kurz 596482a961SGreg Kurz #endif 60