2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
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 2 of the License, or
9 * (at your option) any later version.
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.
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/>.
29 void selinux_init(struct udev *udev) {}
30 void selinux_exit(struct udev *udev) {}
31 void udev_selinux_lsetfilecon(struct udev *udev, const char *file, unsigned int mode) {}
32 void udev_selinux_setfscreatecon(struct udev *udev, const char *file, unsigned int mode) {}
33 void udev_selinux_resetfscreatecon(struct udev *udev) {}
35 #include <selinux/selinux.h>
37 static int selinux_enabled;
38 security_context_t selinux_prev_scontext;
40 void selinux_init(struct udev *udev)
42 /* record the present security context */
43 selinux_enabled = (is_selinux_enabled() > 0);
44 info(udev, "selinux=%i\n", selinux_enabled);
47 matchpathcon_init_prefix(NULL, udev_get_dev_path(udev));
48 if (getfscreatecon(&selinux_prev_scontext) < 0) {
49 err(udev, "getfscreatecon failed\n");
50 selinux_prev_scontext = NULL;
54 void selinux_exit(struct udev *udev)
58 freecon(selinux_prev_scontext);
59 selinux_prev_scontext = NULL;
62 void udev_selinux_lsetfilecon(struct udev *udev, const char *file, unsigned int mode)
64 security_context_t scontext = NULL;
68 if (matchpathcon(file, mode, &scontext) < 0) {
69 err(udev, "matchpathcon(%s) failed\n", file);
72 if (lsetfilecon(file, scontext) < 0)
73 err(udev, "setfilecon %s failed: %m\n", file);
77 void udev_selinux_setfscreatecon(struct udev *udev, const char *file, unsigned int mode)
79 security_context_t scontext = NULL;
83 if (matchpathcon(file, mode, &scontext) < 0) {
84 err(udev, "matchpathcon(%s) failed\n", file);
87 if (setfscreatecon(scontext) < 0)
88 err(udev, "setfscreatecon %s failed: %m\n", file);
92 void udev_selinux_resetfscreatecon(struct udev *udev)
96 if (setfscreatecon(selinux_prev_scontext) < 0)
97 err(udev, "setfscreatecon failed: %m\n");