chiark / gitweb /
2b5683fda65b5801207fb8a878cac167fe32a758
[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 = DEV_BLOCK;
57                 else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
58                         udev->type = DEV_NET;
59                 else if (strncmp(udev->devpath, "/class/", 7) == 0)
60                         udev->type = DEV_CLASS;
61                 else if (strncmp(udev->devpath, "/devices/", 9) == 0)
62                         udev->type = DEV_DEVICE;
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 file_map(const char *filename, char **buf, size_t *bufsize)
181 {
182         struct stat stats;
183         int fd;
184
185         fd = open(filename, O_RDONLY);
186         if (fd < 0) {
187                 return -1;
188         }
189
190         if (fstat(fd, &stats) < 0) {
191                 close(fd);
192                 return -1;
193         }
194
195         *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
196         if (*buf == MAP_FAILED) {
197                 close(fd);
198                 return -1;
199         }
200         *bufsize = stats.st_size;
201
202         close(fd);
203
204         return 0;
205 }
206
207 void file_unmap(char *buf, size_t bufsize)
208 {
209         munmap(buf, bufsize);
210 }
211
212 /* return number of chars until the next newline, skip escaped newline */
213 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
214 {
215         int escape = 0;
216         size_t count;
217
218         for (count = cur; count < buflen; count++) {
219                 if (!escape && buf[count] == '\n')
220                         break;
221
222                 if (buf[count] == '\\')
223                         escape = 1;
224                 else
225                         escape = 0;
226         }
227
228         return count - cur;
229 }
230
231 void no_trailing_slash(char *path)
232 {
233         size_t len;
234
235         len = strlen(path);
236         while (len > 0 && path[len-1] == '/')
237                 path[--len] = '\0';
238 }
239
240 int name_list_add(struct list_head *name_list, const char *name, int sort)
241 {
242         struct name_entry *loop_name;
243         struct name_entry *new_name;
244
245         list_for_each_entry(loop_name, name_list, node) {
246                 /* avoid doubles */
247                 if (strcmp(loop_name->name, name) == 0) {
248                         dbg("'%s' is already in the list", name);
249                         return 0;
250                 }
251                 if (sort && strcmp(loop_name->name, name) > 0)
252                         break;
253         }
254
255         new_name = malloc(sizeof(struct name_entry));
256         if (new_name == NULL) {
257                 dbg("error malloc");
258                 return -ENOMEM;
259         }
260
261         strlcpy(new_name->name, name, sizeof(new_name->name));
262         list_add_tail(&new_name->node, &loop_name->node);
263
264         return 0;
265 }
266
267 /* calls function for every file found in specified directory */
268 int add_matching_files(struct list_head *name_list, const char *dirname, const char *suffix)
269 {
270         struct dirent *ent;
271         DIR *dir;
272         char *ext;
273         char filename[PATH_SIZE];
274
275         dbg("open directory '%s'", dirname);
276         dir = opendir(dirname);
277         if (dir == NULL) {
278                 dbg("unable to open '%s'", dirname);
279                 return -1;
280         }
281
282         while (1) {
283                 ent = readdir(dir);
284                 if (ent == NULL || ent->d_name[0] == '\0')
285                         break;
286
287                 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
288                         continue;
289
290                 /* look for file matching with specified suffix */
291                 ext = strrchr(ent->d_name, '.');
292                 if (ext == NULL)
293                         continue;
294
295                 if (strcmp(ext, suffix) != 0)
296                         continue;
297
298                 dbg("put file '%s/%s' in list", dirname, ent->d_name);
299
300                 snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name);
301                 filename[sizeof(filename)-1] = '\0';
302                 name_list_add(name_list, filename, 1);
303         }
304
305         closedir(dir);
306         return 0;
307 }