chiark / gitweb /
[PATCH] move kernel name/number evaluation into udev_init_device()
[elogind.git] / udev_utils.c
1 /*
2  * udev_lib - generic stuff used by udev
3  *
4  * Copyright (C) 2004 Kay Sievers <kay@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 <sys/stat.h>
31 #include <sys/mman.h>
32 #include <sys/utsname.h>
33
34 #include "udev.h"
35 #include "logging.h"
36 #include "udev_utils.h"
37 #include "list.h"
38
39
40 int udev_init_device(struct udevice *udev, const char* devpath, const char *subsystem)
41 {
42         char *pos;
43
44         memset(udev, 0x00, sizeof(struct udevice));
45
46         if (devpath) {
47                 strfieldcpy(udev->devpath, devpath);
48                 no_trailing_slash(udev->devpath);
49         }
50         if (subsystem)
51                 strfieldcpy(udev->subsystem, subsystem);
52
53         if (strcmp(udev->subsystem, "block") == 0)
54                 udev->type = BLOCK;
55         else if (strcmp(udev->subsystem, "net") == 0)
56                 udev->type = NET;
57         else if (strncmp(udev->devpath, "/block/", 7) == 0)
58                 udev->type = BLOCK;
59         else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
60                 udev->type = NET;
61         else if (strncmp(udev->devpath, "/class/", 7) == 0)
62                 udev->type = CLASS;
63
64         udev->mode = 0660;
65         strcpy(udev->owner, "root");
66         strcpy(udev->group, "root");
67
68         /* get kernel name */
69         pos = strrchr(udev->devpath, '/');
70         if (pos == NULL)
71                 return -1;
72         strfieldcpy(udev->kernel_name, &pos[1]);
73
74         /* get kernel number */
75         pos = &udev->kernel_name[strlen(udev->kernel_name)];
76         while (isdigit(pos[-1]))
77                 pos--;
78         strfieldcpy(udev->kernel_number, pos);
79         dbg("kernel_number='%s'", udev->kernel_number);
80
81         /* Some block devices have '!' in their name, change that to '/' */
82         pos = udev->kernel_name;
83         while (pos[0] != '\0') {
84                 if (pos[0] == '!')
85                         pos[0] = '/';
86                 pos++;
87         }
88
89         dbg("kernel_name='%s'", udev->kernel_name);
90         return 0;
91 }
92
93 int kernel_release_satisfactory(unsigned int version, unsigned int patchlevel, unsigned int sublevel)
94 {
95         static unsigned int kversion = 0;
96         static unsigned int kpatchlevel;
97         static unsigned int ksublevel;
98
99         if (kversion == 0) {
100                 struct utsname uts;
101                 if (uname(&uts) != 0)
102                         return -1;
103
104                 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
105                         kversion = 0;
106                         return -1;
107                 }
108         }
109
110         if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
111                 return 1;
112         else
113                 return 0;
114 }
115
116 int create_path(const char *path)
117 {
118         char p[NAME_SIZE];
119         char *pos;
120         struct stat stats;
121
122         strcpy (p, path);
123         pos = strrchr(p, '/');
124         if (pos == p || pos == NULL)
125                 return 0;
126
127         while (pos[-1] == '/')
128                 pos--;
129
130         pos[0] = '\0';
131
132         dbg("stat '%s'\n", p);
133         if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
134                 return 0;
135
136         if (create_path (p) != 0)
137                 return -1;
138
139         dbg("mkdir '%s'\n", p);
140         return mkdir(p, 0755);
141 }
142
143 /* Reset permissions on the device node, before unlinking it to make sure,
144  * that permisions of possible hard links will be removed too.
145  */
146 int unlink_secure(const char *filename)
147 {
148         int retval;
149
150         retval = chown(filename, 0, 0);
151         if (retval)
152                 dbg("chown(%s, 0, 0) failed with error '%s'", filename, strerror(errno));
153
154         retval = chmod(filename, 0000);
155         if (retval)
156                 dbg("chmod(%s, 0000) failed with error '%s'", filename, strerror(errno));
157
158         retval = unlink(filename);
159         if (errno == ENOENT)
160                 retval = 0;
161
162         if (retval)
163                 dbg("unlink(%s) failed with error '%s'", filename, strerror(errno));
164
165         return retval;
166 }
167
168 int parse_get_pair(char **orig_string, char **left, char **right)
169 {
170         char *temp;
171         char *string = *orig_string;
172
173         if (!string)
174                 return -ENODEV;
175
176         /* eat any whitespace */
177         while (isspace(*string) || *string == ',')
178                 ++string;
179
180         /* split based on '=' */
181         temp = strsep(&string, "=");
182         *left = temp;
183         if (!string)
184                 return -ENODEV;
185
186         /* take the right side and strip off the '"' */
187         while (isspace(*string))
188                 ++string;
189         if (*string == '"')
190                 ++string;
191         else
192                 return -ENODEV;
193
194         temp = strsep(&string, "\"");
195         if (!string || *temp == '\0')
196                 return -ENODEV;
197         *right = temp;
198         *orig_string = string;
199         
200         return 0;
201 }
202
203 int file_map(const char *filename, char **buf, size_t *bufsize)
204 {
205         struct stat stats;
206         int fd;
207
208         fd = open(filename, O_RDONLY);
209         if (fd < 0) {
210                 return -1;
211         }
212
213         if (fstat(fd, &stats) < 0) {
214                 close(fd);
215                 return -1;
216         }
217
218         *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
219         if (*buf == MAP_FAILED) {
220                 close(fd);
221                 return -1;
222         }
223         *bufsize = stats.st_size;
224
225         close(fd);
226
227         return 0;
228 }
229
230 void file_unmap(char *buf, size_t bufsize)
231 {
232         munmap(buf, bufsize);
233 }
234
235 /* return number of chars until the next newline, skip escaped newline */
236 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
237 {
238         int escape = 0;
239         size_t count;
240
241         for (count = cur; count < buflen; count++) {
242                 if (!escape && buf[count] == '\n')
243                         break;
244
245                 if (buf[count] == '\\')
246                         escape = 1;
247                 else
248                         escape = 0;
249         }
250
251         return count - cur;
252 }
253
254 void no_trailing_slash(char *path)
255 {
256         size_t len;
257
258         len = strlen(path);
259         while (len > 0 && path[len-1] == '/')
260                 path[--len] = '\0';
261 }
262
263 struct name_entry {
264         struct list_head node;
265         char name[NAME_SIZE];
266 };
267
268 /* sort files in lexical order */
269 static int name_list_add(struct list_head *name_list, const char *name, int sort)
270 {
271         struct name_entry *loop_name;
272         struct name_entry *new_name;
273
274         list_for_each_entry(loop_name, name_list, node) {
275                 /* avoid doubles */
276                 if (strcmp(loop_name->name, name) == 0) {
277                         dbg("'%s' is already in the list", name);
278                         return 0;
279                 }
280                 if (sort && strcmp(loop_name->name, name) > 0)
281                         break;
282         }
283
284         new_name = malloc(sizeof(struct name_entry));
285         if (new_name == NULL) {
286                 dbg("error malloc");
287                 return -ENOMEM;
288         }
289
290         strfieldcpy(new_name->name, name);
291         list_add_tail(&new_name->node, &loop_name->node);
292         return 0;
293 }
294
295 /* calls function for every file found in specified directory */
296 int call_foreach_file(file_fnct_t fnct, const char *dirname,
297                       const char *suffix, void *data)
298 {
299         struct dirent *ent;
300         DIR *dir;
301         char *ext;
302         struct name_entry *loop_file;
303         struct name_entry *tmp_file;
304         LIST_HEAD(file_list);
305
306         dbg("open directory '%s'", dirname);
307         dir = opendir(dirname);
308         if (dir == NULL) {
309                 dbg("unable to open '%s'", dirname);
310                 return -1;
311         }
312
313         while (1) {
314                 ent = readdir(dir);
315                 if (ent == NULL || ent->d_name[0] == '\0')
316                         break;
317
318                 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
319                         continue;
320
321                 /* look for file with specified suffix */
322                 ext = strrchr(ent->d_name, '.');
323                 if (ext == NULL)
324                         continue;
325
326                 if (strcmp(ext, suffix) != 0)
327                         continue;
328
329                 dbg("put file '%s/%s' in list", dirname, ent->d_name);
330                 name_list_add(&file_list, ent->d_name, 1);
331         }
332
333         /* call function for every file in the list */
334         list_for_each_entry_safe(loop_file, tmp_file, &file_list, node) {
335                 char filename[NAME_SIZE];
336
337                 snprintf(filename, NAME_SIZE, "%s/%s", dirname, loop_file->name);
338                 filename[NAME_SIZE-1] = '\0';
339
340                 fnct(filename, data);
341
342                 list_del(&loop_file->node);
343                 free(loop_file);
344         }
345
346         closedir(dir);
347         return 0;
348 }