chiark / gitweb /
udevd: simplify udev database and fix DEVNAME handling
[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                 util_create_path(udev, filename);
38                 symlink(udev_device_get_devpath(dev), filename);
39         } else {
40                 unlink(filename);
41         }
42 }
43
44 int udev_device_tag_index(struct udev_device *dev, struct udev_device *dev_old, bool add)
45 {
46         struct udev_list_entry *list_entry;
47         bool found;
48
49         if (add && dev_old != NULL) {
50                 /* delete possible left-over tags */
51                 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev_old)) {
52                         const char *tag_old = udev_list_entry_get_name(list_entry);
53                         struct udev_list_entry *list_entry_current;
54
55                         found = false;
56                         udev_list_entry_foreach(list_entry_current, udev_device_get_tags_list_entry(dev)) {
57                                 const char *tag = udev_list_entry_get_name(list_entry_current);
58
59                                 if (strcmp(tag, tag_old) == 0) {
60                                         found = true;
61                                         break;
62                                 }
63                         }
64                         if (!found)
65                                 udev_device_tag(dev_old, tag_old, false);
66                 }
67         }
68
69         udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev))
70                 udev_device_tag(dev, udev_list_entry_get_name(list_entry), add);
71
72         return 0;
73 }
74
75 static bool device_has_info(struct udev_device *udev_device)
76 {
77         struct udev_list_entry *list_entry;
78
79         if (udev_device_get_devlinks_list_entry(udev_device) != NULL)
80                 return true;
81         if (udev_device_get_devlink_priority(udev_device) != 0)
82                 return true;
83         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
84                 if (udev_list_entry_get_flags(list_entry))
85                         return true;
86         if (udev_device_get_tags_list_entry(udev_device) != NULL)
87                 return true;
88         if (udev_device_get_watch_handle(udev_device) >= 0)
89                 return true;
90         return false;
91 }
92
93 int udev_device_update_db(struct udev_device *udev_device)
94 {
95         bool has_info;
96         const char *id;
97         struct udev *udev = udev_device_get_udev(udev_device);
98         char filename[UTIL_PATH_SIZE];
99         char filename_tmp[UTIL_PATH_SIZE];
100         FILE *f;
101         size_t devlen = strlen(udev_get_dev_path(udev))+1;
102
103         id = udev_device_get_id_filename(udev_device);
104         if (id == NULL)
105                 return -1;
106
107         has_info = device_has_info(udev_device);
108         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/", id, NULL);
109
110         /* do not store anything for otherwise empty devices */
111         if (!has_info && udev_device_get_devnode(udev_device) == NULL) {
112                 unlink(filename);
113                 return 0;
114         }
115
116         /* write a database file */
117         util_strscpyl(filename_tmp, sizeof(filename_tmp), filename, ".tmp", NULL);
118         util_create_path(udev, filename_tmp);
119         f = fopen(filename_tmp, "we");
120         if (f == NULL) {
121                 err(udev, "unable to create temporary db file '%s': %m\n", filename_tmp);
122                 return -1;
123         }
124
125         if (has_info) {
126                 struct udev_list_entry *list_entry;
127
128                 if (udev_device_get_devnode(udev_device) != NULL) {
129                         fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
130                         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
131                                 fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]);
132                 }
133                 if (udev_device_get_devlink_priority(udev_device) != 0)
134                         fprintf(f, "L:%i\n", udev_device_get_devlink_priority(udev_device));
135                 if (udev_device_get_watch_handle(udev_device) >= 0)
136                         fprintf(f, "W:%i\n", udev_device_get_watch_handle(udev_device));
137                 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
138                         if (!udev_list_entry_get_flags(list_entry))
139                                 continue;
140                         fprintf(f, "E:%s=%s\n",
141                                 udev_list_entry_get_name(list_entry),
142                                 udev_list_entry_get_value(list_entry));
143                 }
144                 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
145                         fprintf(f, "G:%s\n", udev_list_entry_get_name(list_entry));
146         }
147
148         fclose(f);
149         rename(filename_tmp, filename);
150         info(udev, "created %s file '%s' for '%s'\n", has_info ? "db" : "empty", 
151              filename, udev_device_get_devpath(udev_device));
152         return 0;
153 }
154
155 int udev_device_delete_db(struct udev_device *udev_device)
156 {
157         const char *id;
158         struct udev *udev = udev_device_get_udev(udev_device);
159         char filename[UTIL_PATH_SIZE];
160
161         id = udev_device_get_id_filename(udev_device);
162         if (id == NULL)
163                 return -1;
164         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/", id, NULL);
165         unlink(filename);
166         return 0;
167 }