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