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 */
21 #include <selinux/selinux.h>
23 #include <sys/types.h>
27 /* FIXME: remove this once there is an errno-gnu module
28 that guarantees the definition of ENODATA. */
30 # define ENODATA ENOTSUP
36 #undef lgetfilecon_raw
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. */
55 map_to_failure (int ret
, char **con
)
63 if (ret
== 10 && strcmp (*con
, "unlabeled") == 0)
75 rpl_getfilecon (char const *file
, char **con
)
77 int ret
= getfilecon (file
, con
);
78 return map_to_failure (ret
, con
);
82 rpl_getfilecon_raw (char const *file
, char **con
)
84 int ret
= getfilecon_raw (file
, con
);
85 return map_to_failure (ret
, con
);
89 rpl_lgetfilecon (char const *file
, char **con
)
91 int ret
= lgetfilecon (file
, con
);
92 return map_to_failure (ret
, con
);
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
);