3 __RCSID("$MirOS: contrib/code/jupp/selinux.c,v 1.11 2017/12/06 23:02:05 tg Exp $");
5 #if defined(HAVE_SELINUX_CONTEXT_H) && defined(HAVE_SELINUX_SELINUX_H) && \
6 defined(HAVE_SELINUX_FUN)
15 #include <selinux/selinux.h>
16 static int selinux_enabled = -1;
23 copy_security_context(const char *from_file, const char *to_file)
27 security_context_t from_context;
28 security_context_t to_context;
30 if (selinux_enabled == -1)
31 selinux_enabled = (is_selinux_enabled() > 0);
36 if (getfilecon(from_file, &from_context) < 0) {
38 * If the filesystem doesn't support extended
39 * attributes, the original had no special security
40 * context and the target cannot have one either.
42 if (errno == EOPNOTSUPP)
45 warn("Could not get security context for %s",
50 if (getfilecon(to_file, &to_context) < 0) {
51 warn("Could not get security context for %s",
53 freecon(from_context);
57 if (strcmp(from_context, to_context) != 0) {
58 if (setfilecon(to_file, from_context) < 0) {
60 "Could not set security context for %s",
67 freecon(from_context);