exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / getfilecon.c
blob76927820863344f21d19c0bef80598c6700c568f
1 /* wrap getfilecon, lgetfilecon, and fgetfilecon
2 Copyright (C) 2009-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* written by Jim Meyering */
19 #include <config.h>
21 #include <selinux/selinux.h>
23 #include <sys/types.h>
24 #include <errno.h>
25 #include <string.h>
27 /* FIXME: remove this once there is an errno-gnu module
28 that guarantees the definition of ENODATA. */
29 #ifndef ENODATA
30 # define ENODATA ENOTSUP
31 #endif
33 #undef getfilecon
34 #undef getfilecon_raw
35 #undef lgetfilecon
36 #undef lgetfilecon_raw
37 #undef fgetfilecon
38 #undef fgetfilecon_raw
39 int getfilecon (char const *file, char **con);
40 int getfilecon_raw (char const *file, char **con);
41 int lgetfilecon (char const *file, char **con);
42 int lgetfilecon_raw (char const *file, char **con);
43 int fgetfilecon (int fd, char **con);
44 int fgetfilecon_raw (int fd, char **con);
46 /* getfilecon, lgetfilecon, and fgetfilecon can all misbehave, be it
47 via an old version of libselinux where these would return 0 and set the
48 result context to NULL, or via a modern kernel+lib operating on a file
49 from a disk whose attributes were set by a kernel from around 2006.
50 In that latter case, the functions return a length of 10 for the
51 "unlabeled" context. Map both failures to a return value of -1, and
52 set errno to ENOTSUP in the first case, and ENODATA in the latter. */
54 static int
55 map_to_failure (int ret, char **con)
57 if (ret == 0)
59 errno = ENOTSUP;
60 return -1;
63 if (ret == 10 && strcmp (*con, "unlabeled") == 0)
65 freecon (*con);
66 *con = NULL;
67 errno = ENODATA;
68 return -1;
71 return ret;
74 int
75 rpl_getfilecon (char const *file, char **con)
77 int ret = getfilecon (file, con);
78 return map_to_failure (ret, con);
81 int
82 rpl_getfilecon_raw (char const *file, char **con)
84 int ret = getfilecon_raw (file, con);
85 return map_to_failure (ret, con);
88 int
89 rpl_lgetfilecon (char const *file, char **con)
91 int ret = lgetfilecon (file, con);
92 return map_to_failure (ret, con);
95 int
96 rpl_lgetfilecon_raw (char const *file, char **con)
98 int ret = lgetfilecon_raw (file, con);
99 return map_to_failure (ret, con);
103 rpl_fgetfilecon (int fd, char**con)
105 int ret = fgetfilecon (fd, con);
106 return map_to_failure (ret, con);
110 rpl_fgetfilecon_raw (int fd, char**con)
112 int ret = fgetfilecon_raw (fd, con);
113 return map_to_failure (ret, con);