2 * udevrulescompile.c - store already parsed config on disk
4 * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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.
28 #include "udev_libc_wrapper.h"
29 #include "udev_sysfs.h"
31 #include "udev_version.h"
33 #include "udev_rules.h"
34 #include "udev_utils.h"
38 void log_message(int priority, const char *format, ...)
42 if (priority > udev_log_priority)
45 va_start(args, format);
46 vsyslog(priority, format, args);
51 int main(int argc, char *argv[], char *envp[])
53 struct udev_rule *rule;
56 char comp_tmp[PATH_SIZE];
59 logging_init("udevrulescompile");
61 dbg("version %s", UDEV_VERSION);
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));
68 /* remove old version, otherwise we would read it
69 * instead of the real rules */
75 f = fopen(comp_tmp, "w");
77 err("unable to create db file '%s'", comp_tmp);
82 dbg("storing compiled rules in '%s'", comp_tmp);
84 udev_rules_iter_init();
89 rule = udev_rules_iter_next();
93 id = strtoul(rule->owner, &endptr, 10);
94 if (endptr[0] != '\0') {
97 uid = lookup_user(rule->owner);
98 dbg("replacing username='%s' by id=%i", rule->owner, uid);
99 sprintf(rule->owner, "%li", uid);
102 id = strtoul(rule->group, &endptr, 10);
103 if (endptr[0] != '\0') {
106 gid = lookup_group(rule->group);
107 dbg("replacing groupname='%s' by id=%i", rule->group, gid);
108 sprintf(rule->group, "%li", gid);
111 dbg("kernel='%s' name='%s'", rule->kernel, rule->name);
112 fwrite(rule, sizeof(struct udev_rule), 1, f);
116 dbg("activating compiled rules in '%s'", comp);
117 if (rename(comp_tmp, comp) != 0) {
118 err("unable to write file");