chiark / gitweb /
060 release
[elogind.git] / udevrulescompile.c
1 /*
2  * udevrulescompile.c - store already parsed config on disk
3  *
4  * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
5  * 
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  * 
10  *      This program is distributed in the hope that it will be useful, but
11  *      WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *      General Public License for more details.
14  * 
15  *      You should have received a copy of the GNU General Public License along
16  *      with this program; if not, write to the Free Software Foundation, Inc.,
17  *      675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
20
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <errno.h>
27
28 #include "udev_libc_wrapper.h"
29 #include "udev_sysfs.h"
30 #include "udev.h"
31 #include "udev_version.h"
32 #include "logging.h"
33 #include "udev_rules.h"
34 #include "udev_utils.h"
35 #include "list.h"
36
37 #ifdef USE_LOG
38 void log_message(int priority, const char *format, ...)
39 {
40         va_list args;
41
42         if (priority > udev_log_priority)
43                 return;
44
45         va_start(args, format);
46         vsyslog(priority, format, args);
47         va_end(args);
48 }
49 #endif
50
51 int main(int argc, char *argv[], char *envp[])
52 {
53         struct udev_rule *rule;
54         FILE *f;
55         char comp[PATH_SIZE];
56         char comp_tmp[PATH_SIZE];
57         int retval = 0;
58
59         logging_init("udevrulescompile");
60         udev_init_config();
61         dbg("version %s", UDEV_VERSION);
62
63         strlcpy(comp, udev_rules_filename, sizeof(comp));
64         strlcat(comp, ".compiled", sizeof(comp));
65         strlcpy(comp_tmp, comp, sizeof(comp_tmp));
66         strlcat(comp_tmp, ".tmp", sizeof(comp_tmp));
67
68         /* remove old version, otherwise we would read it
69          * instead of the real rules */
70         unlink(comp);
71         unlink(comp_tmp);
72
73         udev_rules_init();
74
75         f = fopen(comp_tmp, "w");
76         if (f == NULL) {
77                 err("unable to create db file '%s'", comp_tmp);
78                 unlink(comp_tmp);
79                 retval = 1;
80                 goto exit;
81         }
82         dbg("storing compiled rules in '%s'", comp_tmp);
83
84         udev_rules_iter_init();
85         while (1) {
86                 char *endptr;
87                 unsigned long id;
88
89                 rule = udev_rules_iter_next();
90                 if (rule == NULL)
91                         break;
92
93                 id = strtoul(rule->owner, &endptr, 10);
94                 if (endptr[0] != '\0') {
95                         uid_t uid;
96
97                         uid = lookup_user(rule->owner);
98                         dbg("replacing username='%s' by id=%i", rule->owner, uid);
99                         sprintf(rule->owner, "%li", uid);
100                 }
101
102                 id = strtoul(rule->group, &endptr, 10);
103                 if (endptr[0] != '\0') {
104                         gid_t gid;
105
106                         gid = lookup_group(rule->group);
107                         dbg("replacing groupname='%s' by id=%i", rule->group, gid);
108                         sprintf(rule->group, "%li", gid);
109                 }
110
111                 dbg("kernel='%s' name='%s'", rule->kernel, rule->name);
112                 fwrite(rule, sizeof(struct udev_rule), 1, f);
113         }
114
115         fclose(f);
116         dbg("activating compiled rules in '%s'", comp);
117         if (rename(comp_tmp, comp) != 0) {
118                 err("unable to write file");
119                 unlink(comp);
120                 unlink(comp_tmp);
121                 retval = 2;
122         }
123
124 exit:
125         logging_close();
126         return retval;
127 }