chiark / gitweb /
update internal variables if we see $DEVPATH during IMPORT
[elogind.git] / udev_db.c
1 /*
2  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
3  * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
4  *
5  *      This program is free software; you can redistribute it and/or modify it
6  *      under the terms of the GNU General Public License as published by the
7  *      Free Software Foundation version 2 of the License.
8  * 
9  *      This program is distributed in the hope that it will be useful, but
10  *      WITHOUT ANY WARRANTY; without even the implied warranty of
11  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *      General Public License for more details.
13  * 
14  *      You should have received a copy of the GNU General Public License along
15  *      with this program; if not, write to the Free Software Foundation, Inc.,
16  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <dirent.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32
33 #include "udev.h"
34
35
36 static size_t devpath_to_db_path(const char *devpath, char *filename, size_t len)
37 {
38         size_t start;
39
40         /* add location of db files */
41         strlcpy(filename, udev_root, len);
42         start = strlcat(filename, "/"DB_DIR, len);
43         strlcat(filename, devpath, len);
44         return path_encode(&filename[start+1], len - (start+1));
45 }
46
47 static int db_file_to_devpath(const char *filename, char *devpath, size_t len)
48 {
49         strlcpy(devpath, "/", len);
50         strlcat(devpath, filename, len);
51         return path_decode(devpath);
52 }
53
54 int udev_db_add_device(struct udevice *udev)
55 {
56         char filename[PATH_SIZE];
57
58         if (udev->test_run)
59                 return 0;
60
61         devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
62         create_path(filename);
63
64         /*
65          * create only a symlink with the name as the target
66          * if we don't have any interesting data to remember
67          */
68         if (list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
69             !udev->partitions && !udev->ignore_remove) {
70                 dbg("nothing interesting to store, create symlink");
71                 unlink(filename);
72                 if (symlink(udev->name, filename) != 0) {
73                         err("unable to create db link '%s': %s", filename, strerror(errno));
74                         return -1;
75                 }
76         } else {
77                 struct name_entry *name_loop;
78                 FILE *f;
79
80                 unlink(filename);
81                 f = fopen(filename, "w");
82                 if (f == NULL) {
83                         err("unable to create db file '%s': %s", filename, strerror(errno));
84                         return -1;
85                 }
86                 dbg("storing data for device '%s' in '%s'", udev->dev->devpath, filename);
87
88                 fprintf(f, "N:%s\n", udev->name);
89                 list_for_each_entry(name_loop, &udev->symlink_list, node)
90                         fprintf(f, "S:%s\n", name_loop->name);
91                 fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
92                 if (udev->partitions)
93                         fprintf(f, "A:%u\n", udev->partitions);
94                 if (udev->ignore_remove)
95                         fprintf(f, "R:%u\n", udev->ignore_remove);
96                 list_for_each_entry(name_loop, &udev->env_list, node)
97                         fprintf(f, "E:%s\n", name_loop->name);
98                 fclose(f);
99         }
100         return 0;
101 }
102
103 int udev_db_get_device(struct udevice *udev, const char *devpath)
104 {
105         struct stat stats;
106         char filename[PATH_SIZE];
107         char line[PATH_SIZE];
108         unsigned int maj, min;
109         char *bufline;
110         char *buf;
111         size_t bufsize;
112         size_t cur;
113         size_t count;
114
115         strlcpy(udev->dev->devpath, devpath, sizeof(udev->dev->devpath));
116         devpath_to_db_path(devpath, filename, sizeof(filename));
117
118         if (lstat(filename, &stats) != 0) {
119                 info("no db file to read %s: %s", filename, strerror(errno));
120                 return -1;
121         }
122         if ((stats.st_mode & S_IFMT) == S_IFLNK) {
123                 char target[NAME_SIZE];
124                 int target_len;
125
126                 info("found a symlink as db file");
127                 target_len = readlink(filename, target, sizeof(target));
128                 if (target_len > 0)
129                         target[target_len] = '\0';
130                 else {
131                         info("error reading db link %s: %s", filename, strerror(errno));
132                         return -1;
133                 }
134                 dbg("db link points to '%s'", target);
135                 strlcpy(udev->name, target, sizeof(udev->name));
136                 return 0;
137         }
138
139         if (file_map(filename, &buf, &bufsize) != 0) {
140                 info("error reading db file %s: %s", filename, strerror(errno));
141                 return -1;
142         }
143
144         cur = 0;
145         while (cur < bufsize) {
146                 count = buf_get_line(buf, bufsize, cur);
147                 bufline = &buf[cur];
148                 cur += count+1;
149
150                 switch(bufline[0]) {
151                 case 'N':
152                         if (count > sizeof(udev->name))
153                                 count = sizeof(udev->name);
154                         memcpy(udev->name, &bufline[2], count-2);
155                         udev->name[count-2] = '\0';
156                         break;
157                 case 'M':
158                         if (count > sizeof(line))
159                                 count = sizeof(line);
160                         memcpy(line, &bufline[2], count-2);
161                         line[count-2] = '\0';
162                         sscanf(line, "%u:%u", &maj, &min);
163                         udev->devt = makedev(maj, min);
164                         break;
165                 case 'S':
166                         if (count > sizeof(line))
167                                 count =  sizeof(line);
168                         memcpy(line, &bufline[2], count-2);
169                         line[count-2] = '\0';
170                         name_list_add(&udev->symlink_list, line, 0);
171                         break;
172                 case 'A':
173                         if (count > sizeof(line))
174                                 count =  sizeof(line);
175                         memcpy(line, &bufline[2], count-2);
176                         line[count-2] = '\0';
177                         udev->partitions = atoi(line);
178                         break;
179                 case 'R':
180                         if (count > sizeof(line))
181                                 count =  sizeof(line);
182                         memcpy(line, &bufline[2], count-2);
183                         line[count-2] = '\0';
184                         udev->ignore_remove = atoi(line);
185                         break;
186                 case 'E':
187                         if (count > sizeof(line))
188                                 count =  sizeof(line);
189                         memcpy(line, &bufline[2], count-2);
190                         line[count-2] = '\0';
191                         name_list_add(&udev->env_list, line, 0);
192                         break;
193                 }
194         }
195         file_unmap(buf, bufsize);
196
197         if (udev->name[0] == '\0')
198                 return -1;
199
200         return 0;
201 }
202
203 int udev_db_delete_device(struct udevice *udev)
204 {
205         char filename[PATH_SIZE];
206
207         devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
208         unlink(filename);
209
210         return 0;
211 }
212
213 int udev_db_lookup_name(const char *name, char *devpath, size_t len)
214 {
215         char dbpath[PATH_MAX];
216         DIR *dir;
217         int found = 0;
218
219         strlcpy(dbpath, udev_root, sizeof(dbpath));
220         strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
221         dir = opendir(dbpath);
222         if (dir == NULL) {
223                 info("no udev_db available '%s': %s", dbpath, strerror(errno));
224                 return -1;
225         }
226
227         while (!found) {
228                 struct dirent *ent;
229                 char filename[PATH_SIZE];
230                 char nodename[PATH_SIZE];
231                 struct stat stats;
232                 char *bufline;
233                 char *buf;
234                 size_t bufsize;
235                 size_t cur;
236                 size_t count;
237
238                 ent = readdir(dir);
239                 if (ent == NULL || ent->d_name[0] == '\0')
240                         break;
241                 if (ent->d_name[0] == '.')
242                         continue;
243
244                 snprintf(filename, sizeof(filename), "%s/%s", dbpath, ent->d_name);
245                 filename[sizeof(filename)-1] = '\0';
246                 dbg("looking at '%s'", filename);
247
248                 if (lstat(filename, &stats) != 0) {
249                         info("unable to read %s: %s", filename, strerror(errno));
250                         continue;
251                 }
252                 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
253                         char target[NAME_SIZE];
254                         int target_len;
255
256                         info("found a symlink as db file");
257                         target_len = readlink(filename, target, sizeof(target));
258                         if (target_len > 0)
259                                 target[target_len] = '\0';
260                         else {
261                                 info("error reading db link %s: %s", filename, strerror(errno));
262                                 return -1;
263                         }
264                         dbg("db link points to '%s'", target);
265                         if (strcmp(name, target) == 0) {
266                                 db_file_to_devpath(ent->d_name, devpath, len);
267                                 found =1;
268                         }
269                         continue;
270                 }
271
272                 if (file_map(filename, &buf, &bufsize) != 0) {
273                         info("unable to read db file '%s': %s", filename, strerror(errno));
274                         continue;
275                 }
276
277                 cur = 0;
278                 while (cur < bufsize && !found) {
279                         count = buf_get_line(buf, bufsize, cur);
280                         bufline = &buf[cur];
281                         cur += count+1;
282
283                         switch(bufline[0]) {
284                         case 'N':
285                         case 'S':
286                                 if (count > sizeof(nodename))
287                                         count = sizeof(nodename);
288                                 memcpy(nodename, &bufline[2], count-2);
289                                 nodename[count-2] = '\0';
290                                 dbg("compare '%s' '%s'", nodename, name);
291                                 if (strcmp(nodename, name) == 0) {
292                                         db_file_to_devpath(ent->d_name, devpath, len);
293                                         found = 1;
294                                 }
295                                 break;
296                         default:
297                                 continue;
298                         }
299                 }
300                 file_unmap(buf, bufsize);
301         }
302
303         closedir(dir);
304         if (found)
305                 return 0;
306         else
307                 return -1;
308 }
309
310 int udev_db_get_all_entries(struct list_head *name_list)
311 {
312         char dbpath[PATH_MAX];
313         DIR *dir;
314
315         strlcpy(dbpath, udev_root, sizeof(dbpath));
316         strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
317         dir = opendir(dbpath);
318         if (dir == NULL) {
319                 info("no udev_db available '%s': %s", dbpath, strerror(errno));
320                 return -1;
321         }
322
323         while (1) {
324                 struct dirent *ent;
325                 char device[PATH_SIZE];
326
327                 ent = readdir(dir);
328                 if (ent == NULL || ent->d_name[0] == '\0')
329                         break;
330                 if (ent->d_name[0] == '.')
331                         continue;
332
333                 db_file_to_devpath(ent->d_name, device, sizeof(device));
334                 name_list_add(name_list, device, 1);
335                 dbg("added '%s'", device);
336         }
337
338         closedir(dir);
339         return 0;
340 }