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