chiark / gitweb /
Merge rsync://rsync.kernel.org/pub/scm/linux/hotplug/udev
[elogind.git] / udev_utils.c
1 /*
2  * udev_utils.c - generic stuff used by udev
3  *
4  * Copyright (C) 2004-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
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <dirent.h>
30 #include <syslog.h>
31 #include <sys/utsname.h>
32
33 #include "udev_libc_wrapper.h"
34 #include "udev.h"
35 #include "logging.h"
36 #include "udev_utils.h"
37 #include "list.h"
38
39 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
40 int strcmp_pattern(const char *p, const char *s)
41 {
42         if (s[0] == '\0') {
43                 while (p[0] == '*')
44                         p++;
45                 return (p[0] != '\0');
46         }
47         switch (p[0]) {
48         case '[':
49                 {
50                         int not = 0;
51                         p++;
52                         if (p[0] == '!') {
53                                 not = 1;
54                                 p++;
55                         }
56                         while ((p[0] != '\0') && (p[0] != ']')) {
57                                 int match = 0;
58                                 if (p[1] == '-') {
59                                         if ((s[0] >= p[0]) && (s[0] <= p[2]))
60                                                 match = 1;
61                                         p += 3;
62                                 } else {
63                                         match = (p[0] == s[0]);
64                                         p++;
65                                 }
66                                 if (match ^ not) {
67                                         while ((p[0] != '\0') && (p[0] != ']'))
68                                                 p++;
69                                         if (p[0] == ']')
70                                                 return strcmp_pattern(p+1, s+1);
71                                 }
72                         }
73                 }
74                 break;
75         case '*':
76                 if (strcmp_pattern(p, s+1))
77                         return strcmp_pattern(p+1, s);
78                 return 0;
79         case '\0':
80                 if (s[0] == '\0') {
81                         return 0;
82                 }
83                 break;
84         default:
85                 if ((p[0] == s[0]) || (p[0] == '?'))
86                         return strcmp_pattern(p+1, s+1);
87                 break;
88         }
89         return 1;
90 }
91
92 int string_is_true(const char *str)
93 {
94         if (strcasecmp(str, "true") == 0)
95                 return 1;
96         if (strcasecmp(str, "yes") == 0)
97                 return 1;
98         if (strcasecmp(str, "1") == 0)
99                 return 1;
100         return 0;
101 }
102
103 int log_priority(const char *priority)
104 {
105         char *endptr;
106         int prio;
107
108         prio = strtol(priority, &endptr, 10);
109         if (endptr[0] == '\0')
110                 return prio;
111         if (strncasecmp(priority, "err", 3) == 0)
112                 return LOG_ERR;
113         if (strcasecmp(priority, "info") == 0)
114                 return LOG_INFO;
115         if (strcasecmp(priority, "debug") == 0)
116                 return LOG_DEBUG;
117         if (string_is_true(priority))
118                 return LOG_ERR;
119
120         return 0;
121 }
122
123 int kernel_release_satisfactory(unsigned int version, unsigned int patchlevel, unsigned int sublevel)
124 {
125         static unsigned int kversion = 0;
126         static unsigned int kpatchlevel;
127         static unsigned int ksublevel;
128
129         if (kversion == 0) {
130                 struct utsname uts;
131                 if (uname(&uts) != 0)
132                         return -1;
133
134                 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
135                         kversion = 0;
136                         return -1;
137                 }
138         }
139
140         if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
141                 return 1;
142         else
143                 return 0;
144 }
145
146 void replace_untrusted_chars(char *string)
147 {
148         size_t len;
149
150         for (len = 0; string[len] != '\0'; len++) {
151                 if (strchr(";,~\\()\'", string[len])) {
152                         info("replace '%c' in '%s'", string[len], string);
153                         string[len] = '_';
154                 }
155         }
156 }
157
158 void remove_trailing_char(char *path, char c)
159 {
160         size_t len;
161
162         len = strlen(path);
163         while (len > 0 && path[len-1] == c)
164                 path[--len] = '\0';
165 }
166
167 int name_list_add(struct list_head *name_list, const char *name, int sort)
168 {
169         struct name_entry *loop_name;
170         struct name_entry *new_name;
171
172         list_for_each_entry(loop_name, name_list, node) {
173                 /* avoid doubles */
174                 if (strcmp(loop_name->name, name) == 0) {
175                         dbg("'%s' is already in the list", name);
176                         return 0;
177                 }
178         }
179
180         if (sort)
181                 list_for_each_entry(loop_name, name_list, node) {
182                         if (sort && strcmp(loop_name->name, name) > 0)
183                                 break;
184                 }
185
186         new_name = malloc(sizeof(struct name_entry));
187         if (new_name == NULL) {
188                 dbg("error malloc");
189                 return -ENOMEM;
190         }
191
192         strlcpy(new_name->name, name, sizeof(new_name->name));
193         dbg("adding '%s'", new_name->name);
194         list_add_tail(&new_name->node, &loop_name->node);
195
196         return 0;
197 }
198
199 int name_list_key_add(struct list_head *name_list, const char *key, const char *value)
200 {
201         struct name_entry *loop_name;
202         struct name_entry *new_name;
203
204         list_for_each_entry(loop_name, name_list, node) {
205                 if (strncmp(loop_name->name, key, strlen(key)) == 0) {
206                         dbg("key already present '%s', replace it", loop_name->name);
207                         snprintf(loop_name->name, sizeof(loop_name->name), "%s=%s", key, value);
208                         loop_name->name[sizeof(loop_name->name)-1] = '\0';
209                         return 0;
210                 }
211         }
212
213         new_name = malloc(sizeof(struct name_entry));
214         if (new_name == NULL) {
215                 dbg("error malloc");
216                 return -ENOMEM;
217         }
218
219         snprintf(new_name->name, sizeof(new_name->name), "%s=%s", key, value);
220         new_name->name[sizeof(new_name->name)-1] = '\0';
221         dbg("adding '%s'", new_name->name);
222         list_add_tail(&new_name->node, &loop_name->node);
223
224         return 0;
225 }
226
227 /* calls function for every file found in specified directory */
228 int add_matching_files(struct list_head *name_list, const char *dirname, const char *suffix)
229 {
230         struct dirent *ent;
231         DIR *dir;
232         char *ext;
233         char filename[PATH_SIZE];
234
235         dbg("open directory '%s'", dirname);
236         dir = opendir(dirname);
237         if (dir == NULL) {
238                 dbg("unable to open '%s'", dirname);
239                 return -1;
240         }
241
242         while (1) {
243                 ent = readdir(dir);
244                 if (ent == NULL || ent->d_name[0] == '\0')
245                         break;
246
247                 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
248                         continue;
249
250                 /* look for file matching with specified suffix */
251                 ext = strrchr(ent->d_name, '.');
252                 if (ext == NULL)
253                         continue;
254
255                 if (strcmp(ext, suffix) != 0)
256                         continue;
257
258                 dbg("put file '%s/%s' in list", dirname, ent->d_name);
259
260                 snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name);
261                 filename[sizeof(filename)-1] = '\0';
262                 name_list_add(name_list, filename, 1);
263         }
264
265         closedir(dir);
266         return 0;
267 }