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