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