1229533Sray/*- 2229533Sray * Copyright (c) 2012 The FreeBSD Foundation 3229533Sray * All rights reserved. 4229533Sray * 5229533Sray * This software was developed by Pawel Jakub Dawidek under sponsorship from 6229533Sray * the FreeBSD Foundation. 7229533Sray * 8229533Sray * Redistribution and use in source and binary forms, with or without 9229533Sray * modification, are permitted provided that the following conditions 10229533Sray * are met: 11229533Sray * 1. Redistributions of source code must retain the above copyright 12229533Sray * notice, this list of conditions and the following disclaimer. 13229533Sray * 2. Redistributions in binary form must reproduce the above copyright 14229533Sray * notice, this list of conditions and the following disclaimer in the 15229533Sray * documentation and/or other materials provided with the distribution. 16229533Sray * 17229533Sray * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 18229533Sray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19229533Sray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20229533Sray * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 21229533Sray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22229533Sray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23229533Sray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24229533Sray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25229533Sray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26229533Sray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27229533Sray * SUCH DAMAGE. 28229533Sray * 29229533Sray * $P4: //depot/projects/trustedbsd/openbsm/bin/auditdistd/openat.h#1 $ 30229533Sray */ 31229533Sray 32229533Sray#ifndef _OPENAT_H_ 33229533Sray#define _OPENAT_H_ 34229533Sray 35229533Sray#include <fcntl.h> 36229533Sray#include <stdarg.h> 37229533Sray#include <unistd.h> 38229533Sray 39229533Sraystatic int 40229533Srayopenat(int fd, const char *path, int flags, ...) 41229533Sray{ 42229533Sray int cfd, ffd, error; 43229533Sray 44229533Sray cfd = open(".", O_RDONLY | O_DIRECTORY); 45229533Sray if (cfd == -1) 46229533Sray return (-1); 47229533Sray 48229533Sray if (fchdir(fd) == -1) { 49229533Sray error = errno; 50229533Sray (void)close(cfd); 51229533Sray errno = error; 52229533Sray return (-1); 53229533Sray } 54229533Sray 55229533Sray if ((flags & O_CREAT) != 0) { 56229533Sray va_list ap; 57229533Sray int mode; 58229533Sray 59229533Sray va_start(ap, flags); 60229533Sray mode = va_arg(ap, int); 61229533Sray va_end(ap); 62229533Sray 63229533Sray ffd = open(path, flags, mode); 64229533Sray } else { 65229533Sray ffd = open(path, flags); 66229533Sray } 67229533Sray 68229533Sray error = errno; 69229533Sray (void)fchdir(cfd); 70229533Sray (void)close(cfd); 71229533Sray errno = error; 72229533Sray return (ffd); 73229533Sray} 74 75#endif /* !_OPENAT_H_ */ 76