xref: /freebsd/contrib/openbsm/bin/auditdistd/fstatat.h (revision b626f5a73a48f44a31a200291b141e1da408a2ff)
1*aa772005SRobert Watson /*-
2*aa772005SRobert Watson  * Copyright (c) 2012 The FreeBSD Foundation
3*aa772005SRobert Watson  * All rights reserved.
4*aa772005SRobert Watson  *
5*aa772005SRobert Watson  * This software was developed by Pawel Jakub Dawidek under sponsorship from
6*aa772005SRobert Watson  * the FreeBSD Foundation.
7*aa772005SRobert Watson  *
8*aa772005SRobert Watson  * Redistribution and use in source and binary forms, with or without
9*aa772005SRobert Watson  * modification, are permitted provided that the following conditions
10*aa772005SRobert Watson  * are met:
11*aa772005SRobert Watson  * 1. Redistributions of source code must retain the above copyright
12*aa772005SRobert Watson  *    notice, this list of conditions and the following disclaimer.
13*aa772005SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
14*aa772005SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
15*aa772005SRobert Watson  *    documentation and/or other materials provided with the distribution.
16*aa772005SRobert Watson  *
17*aa772005SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18*aa772005SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*aa772005SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*aa772005SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21*aa772005SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*aa772005SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*aa772005SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*aa772005SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*aa772005SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*aa772005SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*aa772005SRobert Watson  * SUCH DAMAGE.
28*aa772005SRobert Watson  */
29*aa772005SRobert Watson 
30*aa772005SRobert Watson #ifndef	_FSTATAT_H_
31*aa772005SRobert Watson #define	_FSTATAT_H_
32*aa772005SRobert Watson 
33*aa772005SRobert Watson #include <sys/stat.h>
34*aa772005SRobert Watson 
35*aa772005SRobert Watson #include <unistd.h>
36*aa772005SRobert Watson 
37*aa772005SRobert Watson #define	AT_SYMLINK_NOFOLLOW	0x01
38*aa772005SRobert Watson 
39*aa772005SRobert Watson static int
fstatat(int fd,const char * path,struct stat * buf,int flag)40*aa772005SRobert Watson fstatat(int fd, const char *path, struct stat *buf, int flag)
41*aa772005SRobert Watson {
42*aa772005SRobert Watson 	int cfd, error, ret;
43*aa772005SRobert Watson 
44*aa772005SRobert Watson 	cfd = open(".", O_RDONLY | O_DIRECTORY);
45*aa772005SRobert Watson 	if (cfd == -1)
46*aa772005SRobert Watson 		return (-1);
47*aa772005SRobert Watson 
48*aa772005SRobert Watson 	if (fchdir(fd) == -1) {
49*aa772005SRobert Watson 		error = errno;
50*aa772005SRobert Watson 		(void)close(cfd);
51*aa772005SRobert Watson 		errno = error;
52*aa772005SRobert Watson 		return (-1);
53*aa772005SRobert Watson 	}
54*aa772005SRobert Watson 
55*aa772005SRobert Watson 	if (flag == AT_SYMLINK_NOFOLLOW)
56*aa772005SRobert Watson 		ret = lstat(path, buf);
57*aa772005SRobert Watson 	else
58*aa772005SRobert Watson 		ret = stat(path, buf);
59*aa772005SRobert Watson 
60*aa772005SRobert Watson 	error = errno;
61*aa772005SRobert Watson 	(void)fchdir(cfd);
62*aa772005SRobert Watson 	(void)close(cfd);
63*aa772005SRobert Watson 	errno = error;
64*aa772005SRobert Watson 	return (ret);
65*aa772005SRobert Watson }
66*aa772005SRobert Watson 
67*aa772005SRobert Watson #endif	/* !_FSTATAT_H_ */
68