chiark / gitweb /
[PATCH] add MANAGED_EVENT to the forked udev environment
[elogind.git] / udev_lib.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 <dirent.h>
29 #include <sys/stat.h>
30 #include <sys/mman.h>
31 #include <sys/utsname.h>
32
33 #include "udev.h"
34 #include "logging.h"
35 #include "udev_lib.h"
36 #include "list.h"
37
38
39 void udev_set_values(struct udevice *udev, const char* devpath,
40                      const char *subsystem, const char* action)
41 {
42         memset(udev, 0x00, sizeof(struct udevice));
43
44         if (devpath)
45                 strfieldcpy(udev->devpath, devpath);
46         if (subsystem)
47                 strfieldcpy(udev->subsystem, subsystem);
48         if (action)
49                 strfieldcpy(udev->action, action);
50
51         if (strcmp(udev->subsystem, "block") == 0)
52                 udev->type = 'b';
53         else if (strcmp(udev->subsystem, "net") == 0)
54                 udev->type = 'n';
55         else if (strncmp(udev->devpath, "/block/", 7) == 0)
56                 udev->type = 'b';
57         else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
58                 udev->type = 'n';
59         else if (strncmp(udev->devpath, "/class/", 7) == 0)
60                 udev->type = 'c';
61 }
62
63 int kernel_release_satisfactory(int version, int patchlevel, int sublevel)
64 {
65         static int kversion = 0;
66         static int kpatchlevel;
67         static int ksublevel;
68
69         if (kversion == 0) {
70                 struct utsname uts;
71                 if (uname(&uts) != 0)
72                         return -1;
73
74                 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
75                         kversion = 0;
76                         return -1;
77                 }
78         }
79
80         if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
81                 return 1;
82         else
83                 return 0;
84 }
85
86 int create_path(const char *path)
87 {
88         char p[NAME_SIZE];
89         char *pos;
90         struct stat stats;
91
92         strcpy (p, path);
93         pos = strrchr(p, '/');
94         if (pos == p || pos == NULL)
95                 return 0;
96
97         while (pos[-1] == '/')
98                 pos--;
99
100         pos[0] = '\0';
101
102         dbg("stat '%s'\n", p);
103         if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
104                 return 0;
105
106         if (create_path (p) != 0)
107                 return -1;
108
109         dbg("mkdir '%s'\n", p);
110         return mkdir(p, 0755);
111 }
112
113 int file_map(const char *filename, char **buf, size_t *bufsize)
114 {
115         struct stat stats;
116         int fd;
117
118         fd = open(filename, O_RDONLY);
119         if (fd < 0) {
120                 return -1;
121         }
122
123         if (fstat(fd, &stats) < 0) {
124                 close(fd);
125                 return -1;
126         }
127
128         *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
129         if (*buf == MAP_FAILED) {
130                 close(fd);
131                 return -1;
132         }
133         *bufsize = stats.st_size;
134
135         close(fd);
136
137         return 0;
138 }
139
140 void file_unmap(char *buf, size_t bufsize)
141 {
142         munmap(buf, bufsize);
143 }
144
145 size_t buf_get_line(char *buf, size_t buflen, size_t cur)
146 {
147         size_t count = 0;
148
149         for (count = cur; count < buflen && buf[count] != '\n'; count++);
150
151         return count - cur;
152 }
153
154 void no_trailing_slash(char *path)
155 {
156         int len;
157
158         len = strlen(path);
159         if (len > 0 && path[len-1] == '/')
160                 path[len-1] = '\0';
161 }
162
163 struct files {
164         struct list_head list;
165         char name[NAME_SIZE];
166 };
167
168 /* sort files in lexical order */
169 static int file_list_insert(char *filename, struct list_head *file_list)
170 {
171         struct files *loop_file;
172         struct files *new_file;
173
174         list_for_each_entry(loop_file, file_list, list) {
175                 if (strcmp(loop_file->name, filename) > 0) {
176                         break;
177                 }
178         }
179
180         new_file = malloc(sizeof(struct files));
181         if (new_file == NULL) {
182                 dbg("error malloc");
183                 return -ENOMEM;
184         }
185
186         strfieldcpy(new_file->name, filename);
187         list_add_tail(&new_file->list, &loop_file->list);
188         return 0;
189 }
190
191 /* calls function for every file found in specified directory */
192 int call_foreach_file(file_fnct_t fnct, const char *dirname,
193                       const char *suffix, void *data)
194 {
195         struct dirent *ent;
196         DIR *dir;
197         char *ext;
198         struct files *loop_file;
199         struct files *tmp_file;
200         LIST_HEAD(file_list);
201
202         dbg("open directory '%s'", dirname);
203         dir = opendir(dirname);
204         if (dir == NULL) {
205                 dbg("unable to open '%s'", dirname);
206                 return -1;
207         }
208
209         while (1) {
210                 ent = readdir(dir);
211                 if (ent == NULL || ent->d_name[0] == '\0')
212                         break;
213
214                 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
215                         continue;
216
217                 /* look for file with specified suffix */
218                 ext = strrchr(ent->d_name, '.');
219                 if (ext == NULL)
220                         continue;
221
222                 if (strcmp(ext, suffix) != 0)
223                         continue;
224
225                 dbg("put file '%s/%s' in list", dirname, ent->d_name);
226                 file_list_insert(ent->d_name, &file_list);
227         }
228
229         /* call function for every file in the list */
230         list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) {
231                 char filename[NAME_SIZE];
232
233                 snprintf(filename, NAME_SIZE, "%s/%s", dirname, loop_file->name);
234                 filename[NAME_SIZE-1] = '\0';
235
236                 fnct(filename, data);
237
238                 list_del(&loop_file->list);
239                 free(loop_file);
240         }
241
242         closedir(dir);
243         return 0;
244 }