chiark / gitweb /
[PATCH] fix dbus build in the udev.spec file.
[elogind.git] / extras / selinux / udev_selinux.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <ctype.h>
8 #include <selinux/selinux.h>
9
10 #include "../../udev_lib.h"
11 #include "../../logging.h"
12
13 #ifdef LOG
14 unsigned char logname[LOGNAME_SIZE];
15 void log_message(int level, const char *format, ...)
16 {
17         va_list args;
18
19         if (!udev_log)
20                 return;
21
22         va_start(args, format);
23         vsyslog(level, format, args);
24         va_end(args);
25 }
26 #endif
27
28 void selinux_add_node(char *filename)
29 {
30         int retval;
31
32         if (is_selinux_enabled() > 0) {
33                 security_context_t scontext;
34                 retval = matchpathcon(filename, 0, &scontext);
35                 if (retval < 0) {
36                         dbg("matchpathcon(%s) failed\n", filename);
37                 } else {
38                         retval=setfilecon(filename,scontext);
39                         if (retval < 0)
40                                 dbg("setfiles %s failed with error '%s'",
41                                     filename, strerror(errno));
42                         free(scontext);
43                 }
44         }
45 }
46
47 int main(int argc, char *argv[], char *envp[])
48 {
49         char *action;
50         char *devpath;
51         char *devnode;
52         int retval = 0;
53
54         init_logging("udev_selinux");
55
56         action = get_action();
57         if (!action) {
58                 dbg("no action?");
59                 goto exit;
60         }
61         devnode = get_devnode();
62         if (!devnode) {
63                 dbg("no devnode?");
64                 goto exit;
65         }
66
67         if (strcmp(action, "add") == 0)
68                 selinux_add_node(devnode);
69
70 exit:
71         return retval;
72 }