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