chiark / gitweb /
libudev: enumerate - allow to filter-out not-already-initialized devices
[elogind.git] / libudev / libudev-device-private.c
1 /*
2  * libudev - interface to udev device information
3  *
4  * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  */
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <stddef.h>
16 #include <stdbool.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <string.h>
20 #include <sys/stat.h>
21
22 #include "libudev.h"
23 #include "libudev-private.h"
24
25 static void udev_device_tag(struct udev_device *dev, const char *tag, bool add)
26 {
27         const char *id;
28         struct udev *udev = udev_device_get_udev(dev);
29         char filename[UTIL_PATH_SIZE];
30
31         id = udev_device_get_id_filename(dev);
32         if (id == NULL)
33                 return;
34         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/tags/", tag, "/", id, NULL);
35
36         if (add) {
37                 int fd;
38
39                 util_create_path(udev, filename);
40                 fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
41                 if (fd >= 0)
42                         close(fd);
43         } else {
44                 unlink(filename);
45         }
46 }
47
48 int udev_device_tag_index(struct udev_device *dev, struct udev_device *dev_old, bool add)
49 {
50         struct udev_list_entry *list_entry;
51         bool found;
52
53         if (add && dev_old != NULL) {
54                 /* delete possible left-over tags */
55                 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev_old)) {
56                         const char *tag_old = udev_list_entry_get_name(list_entry);
57                         struct udev_list_entry *list_entry_current;
58
59                         found = false;
60                         udev_list_entry_foreach(list_entry_current, udev_device_get_tags_list_entry(dev)) {
61                                 const char *tag = udev_list_entry_get_name(list_entry_current);
62
63                                 if (strcmp(tag, tag_old) == 0) {
64                                         found = true;
65                                         break;
66                                 }
67                         }
68                         if (!found)
69                                 udev_device_tag(dev_old, tag_old, false);
70                 }
71         }
72
73         udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev))
74                 udev_device_tag(dev, udev_list_entry_get_name(list_entry), add);
75
76         return 0;
77 }
78
79 static bool device_has_info(struct udev_device *udev_device)
80 {
81         struct udev_list_entry *list_entry;
82
83         if (udev_device_get_devlinks_list_entry(udev_device) != NULL)
84                 return true;
85         if (udev_device_get_devlink_priority(udev_device) != 0)
86                 return true;
87         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
88                 if (udev_list_entry_get_flags(list_entry))
89                         return true;
90         if (udev_device_get_tags_list_entry(udev_device) != NULL)
91                 return true;
92         if (udev_device_get_knodename(udev_device) != NULL)
93                 if (strcmp(udev_device_get_devnode(udev_device), udev_device_get_knodename(udev_device)) != 0)
94                         return true;
95         if (udev_device_get_watch_handle(udev_device) >= 0)
96                 return true;
97         return false;
98 }
99
100 int udev_device_update_db(struct udev_device *udev_device)
101 {
102         bool has_info;
103         const char *id;
104         struct udev *udev = udev_device_get_udev(udev_device);
105         char filename[UTIL_PATH_SIZE];
106         char filename_tmp[UTIL_PATH_SIZE];
107         FILE *f;
108
109         id = udev_device_get_id_filename(udev_device);
110         if (id == NULL)
111                 return -1;
112
113         has_info = device_has_info(udev_device);
114         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/", id, NULL);
115
116         /* do not store anything for otherwise empty devices */
117         if (!has_info &&
118             major(udev_device_get_devnum(udev_device)) == 0 &&
119             udev_device_get_ifindex(udev_device) == 0) {
120                 unlink(filename);
121                 return 0;
122         }
123
124         /* write a database file */
125         util_strscpyl(filename_tmp, sizeof(filename_tmp), filename, ".tmp", NULL);
126         util_create_path(udev, filename_tmp);
127         f = fopen(filename_tmp, "we");
128         if (f == NULL) {
129                 err(udev, "unable to create temporary db file '%s': %m\n", filename_tmp);
130                 return -1;
131         }
132
133         if (has_info) {
134                 size_t devlen = strlen(udev_get_dev_path(udev))+1;
135                 struct udev_list_entry *list_entry;
136
137                 if (udev_device_get_devnode(udev_device) != NULL) {
138                         fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
139                         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
140                                 fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]);
141                 }
142                 if (udev_device_get_devlink_priority(udev_device) != 0)
143                         fprintf(f, "L:%i\n", udev_device_get_devlink_priority(udev_device));
144                 if (udev_device_get_watch_handle(udev_device) >= 0)
145                         fprintf(f, "W:%i\n", udev_device_get_watch_handle(udev_device));
146                 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
147                         if (!udev_list_entry_get_flags(list_entry))
148                                 continue;
149                         fprintf(f, "E:%s=%s\n",
150                                 udev_list_entry_get_name(list_entry),
151                                 udev_list_entry_get_value(list_entry));
152                 }
153                 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
154                         fprintf(f, "G:%s\n", udev_list_entry_get_name(list_entry));
155         }
156
157         fclose(f);
158         rename(filename_tmp, filename);
159         info(udev, "created %s file '%s' for '%s'\n", has_info ? "db" : "empty", 
160              filename, udev_device_get_devpath(udev_device));
161         return 0;
162 }
163
164 int udev_device_delete_db(struct udev_device *udev_device)
165 {
166         const char *id;
167         struct udev *udev = udev_device_get_udev(udev_device);
168         char filename[UTIL_PATH_SIZE];
169
170         id = udev_device_get_id_filename(udev_device);
171         if (id == NULL)
172                 return -1;
173         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/", id, NULL);
174         unlink(filename);
175         return 0;
176 }