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