1b9d6729f4a62ed732e1ff5ae8b6c5bb02576969
[debian/tar] / gnu / getfilecon.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* wrap getfilecon, lgetfilecon, and fgetfilecon
4    Copyright (C) 2009-2013 Free Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.  */
18
19 /* written by Jim Meyering */
20
21 #include <config.h>
22
23 #include <selinux/selinux.h>
24
25 #include <sys/types.h>
26 #include <errno.h>
27 #include <string.h>
28
29 /* FIXME: remove this once there is an errno-gnu module
30    that guarantees the definition of ENODATA.  */
31 #ifndef ENODATA
32 # define ENODATA ENOTSUP
33 #endif
34
35 #undef getfilecon
36 #undef lgetfilecon
37 #undef fgetfilecon
38 int getfilecon (char const *file, security_context_t *con);
39 int lgetfilecon (char const *file, security_context_t *con);
40 int fgetfilecon (int fd, security_context_t *con);
41
42 /* getfilecon, lgetfilecon, and fgetfilecon can all misbehave, be it
43    via an old version of libselinux where these would return 0 and set the
44    result context to NULL, or via a modern kernel+lib operating on a file
45    from a disk whose attributes were set by a kernel from around 2006.
46    In that latter case, the functions return a length of 10 for the
47    "unlabeled" context.  Map both failures to a return value of -1, and
48    set errno to ENOTSUP in the first case, and ENODATA in the latter.  */
49
50 static int
51 map_to_failure (int ret, security_context_t *con)
52 {
53   if (ret == 0)
54     {
55       errno = ENOTSUP;
56       return -1;
57     }
58
59   if (ret == 10 && strcmp (*con, "unlabeled") == 0)
60     {
61       freecon (*con);
62       errno = ENODATA;
63       return -1;
64     }
65
66   return ret;
67 }
68
69 int
70 rpl_getfilecon (char const *file, security_context_t *con)
71 {
72   int ret = getfilecon (file, con);
73   return map_to_failure (ret, con);
74 }
75
76 int
77 rpl_lgetfilecon (char const *file, security_context_t *con)
78 {
79   int ret = lgetfilecon (file, con);
80   return map_to_failure (ret, con);
81 }
82
83 int
84 rpl_fgetfilecon (int fd, security_context_t *con)
85 {
86   int ret = fgetfilecon (fd, con);
87   return map_to_failure (ret, con);
88 }