2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
17 #include <selinux/selinux.h>
20 #include "libudev-private.h"
22 static int selinux_enabled;
23 security_context_t selinux_prev_scontext;
25 void udev_selinux_init(struct udev *udev)
27 /* record the present security context */
28 selinux_enabled = (is_selinux_enabled() > 0);
29 info(udev, "selinux=%i\n", selinux_enabled);
32 matchpathcon_init_prefix(NULL, udev_get_dev_path(udev));
33 if (getfscreatecon(&selinux_prev_scontext) < 0) {
34 err(udev, "getfscreatecon failed\n");
35 selinux_prev_scontext = NULL;
39 void udev_selinux_exit(struct udev *udev)
43 freecon(selinux_prev_scontext);
44 selinux_prev_scontext = NULL;
47 void udev_selinux_lsetfilecon(struct udev *udev, const char *file, unsigned int mode)
49 security_context_t scontext = NULL;
53 if (matchpathcon(file, mode, &scontext) < 0) {
54 err(udev, "matchpathcon(%s) failed\n", file);
57 if (lsetfilecon(file, scontext) < 0)
58 err(udev, "setfilecon %s failed: %m\n", file);
62 void udev_selinux_setfscreatecon(struct udev *udev, const char *file, unsigned int mode)
64 security_context_t scontext = NULL;
69 if (matchpathcon(file, mode, &scontext) < 0) {
70 err(udev, "matchpathcon(%s) failed\n", file);
73 if (setfscreatecon(scontext) < 0)
74 err(udev, "setfscreatecon %s failed: %m\n", file);
78 void udev_selinux_resetfscreatecon(struct udev *udev)
82 if (setfscreatecon(selinux_prev_scontext) < 0)
83 err(udev, "setfscreatecon failed: %m\n");
86 void udev_selinux_setfscreateconat(struct udev *udev, int dfd, const char *file, unsigned int mode)
88 char filename[UTIL_PATH_SIZE];
93 /* resolve relative filename */
95 char procfd[UTIL_PATH_SIZE];
96 char target[UTIL_PATH_SIZE];
99 snprintf(procfd, sizeof(procfd), "/proc/%u/fd/%u", getpid(), dfd);
100 len = readlink(procfd, target, sizeof(target));
101 if (len <= 0 || len == sizeof(target))
105 util_strscpyl(filename, sizeof(filename), target, "/", file, NULL);
108 udev_selinux_setfscreatecon(udev, file, mode);