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