chiark / gitweb /
allow rules to have labels and skip to next label
[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_rules rules;
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 instead of the real rules */
69         unlink(comp);
70         unlink(comp_tmp);
71
72         udev_rules_init(&rules, 1);
73
74         f = fopen(comp_tmp, "w");
75         if (f == NULL) {
76                 err("unable to create db file '%s'", comp_tmp);
77                 unlink(comp_tmp);
78                 retval = 1;
79                 goto exit;
80         }
81
82         dbg("storing compiled rules in '%s' size=%zi", comp_tmp, rules.bufsize);
83         fwrite(rules.buf, rules.bufsize, 1, f);
84         fclose(f);
85
86         dbg("activating compiled rules in '%s'", comp);
87         if (rename(comp_tmp, comp) != 0) {
88                 err("unable to write file");
89                 unlink(comp);
90                 unlink(comp_tmp);
91                 retval = 2;
92         }
93
94 exit:
95         logging_close();
96         return retval;
97 }