xref: /qemu/hw/9pfs/9p-util.h (revision 56ad3e54dad6cdcee8668d170df161d89581846f)
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 {
256482a961SGreg Kurz     return openat(dirfd, name, O_DIRECTORY | O_RDONLY | O_PATH);
266482a961SGreg Kurz }
276482a961SGreg Kurz 
286482a961SGreg Kurz static inline int openat_file(int dirfd, const char *name, int flags,
296482a961SGreg Kurz                               mode_t mode)
306482a961SGreg Kurz {
316482a961SGreg Kurz     int fd, serrno, ret;
326482a961SGreg Kurz 
336482a961SGreg Kurz     fd = openat(dirfd, name, flags | O_NOFOLLOW | O_NOCTTY | O_NONBLOCK,
346482a961SGreg Kurz                 mode);
356482a961SGreg Kurz     if (fd == -1) {
366482a961SGreg Kurz         return -1;
376482a961SGreg Kurz     }
386482a961SGreg Kurz 
396482a961SGreg Kurz     serrno = errno;
406482a961SGreg Kurz     /* O_NONBLOCK was only needed to open the file. Let's drop it. */
416482a961SGreg Kurz     ret = fcntl(fd, F_SETFL, flags);
426482a961SGreg Kurz     assert(!ret);
436482a961SGreg Kurz     errno = serrno;
446482a961SGreg Kurz     return fd;
456482a961SGreg Kurz }
466482a961SGreg Kurz 
476482a961SGreg Kurz int relative_openat_nofollow(int dirfd, const char *path, int flags,
486482a961SGreg Kurz                              mode_t mode);
49*56ad3e54SGreg Kurz ssize_t fgetxattrat_nofollow(int dirfd, const char *path, const char *name,
50*56ad3e54SGreg Kurz                              void *value, size_t size);
516482a961SGreg Kurz 
526482a961SGreg Kurz #endif
53