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