xref: /qemu/hw/9pfs/9p-util.h (revision b003fc0d8aa5e7060dbf7e5862b8013c73857c7f)
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 {
25918112c0SGreg Kurz #ifdef O_PATH
26918112c0SGreg Kurz #define OPENAT_DIR_O_PATH O_PATH
27918112c0SGreg Kurz #else
28918112c0SGreg Kurz #define OPENAT_DIR_O_PATH 0
29918112c0SGreg Kurz #endif
30*b003fc0dSGreg Kurz     return openat(dirfd, name,
31*b003fc0dSGreg Kurz                   O_DIRECTORY | O_RDONLY | O_NOFOLLOW | OPENAT_DIR_O_PATH);
326482a961SGreg Kurz }
336482a961SGreg Kurz 
346482a961SGreg Kurz static inline int openat_file(int dirfd, const char *name, int flags,
356482a961SGreg Kurz                               mode_t mode)
366482a961SGreg Kurz {
376482a961SGreg Kurz     int fd, serrno, ret;
386482a961SGreg Kurz 
396482a961SGreg Kurz     fd = openat(dirfd, name, flags | O_NOFOLLOW | O_NOCTTY | O_NONBLOCK,
406482a961SGreg Kurz                 mode);
416482a961SGreg Kurz     if (fd == -1) {
426482a961SGreg Kurz         return -1;
436482a961SGreg Kurz     }
446482a961SGreg Kurz 
456482a961SGreg Kurz     serrno = errno;
466482a961SGreg Kurz     /* O_NONBLOCK was only needed to open the file. Let's drop it. */
476482a961SGreg Kurz     ret = fcntl(fd, F_SETFL, flags);
486482a961SGreg Kurz     assert(!ret);
496482a961SGreg Kurz     errno = serrno;
506482a961SGreg Kurz     return fd;
516482a961SGreg Kurz }
526482a961SGreg Kurz 
536482a961SGreg Kurz int relative_openat_nofollow(int dirfd, const char *path, int flags,
546482a961SGreg Kurz                              mode_t mode);
5556ad3e54SGreg Kurz ssize_t fgetxattrat_nofollow(int dirfd, const char *path, const char *name,
5656ad3e54SGreg Kurz                              void *value, size_t size);
573e36aba7SGreg Kurz int fsetxattrat_nofollow(int dirfd, const char *path, const char *name,
583e36aba7SGreg Kurz                          void *value, size_t size, int flags);
596482a961SGreg Kurz 
606482a961SGreg Kurz #endif
61