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