chiark / gitweb /
add TAG= to improve event filtering and device enumeration
[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 int udev_device_tag_index(struct udev_device *udev_device, bool add)
26 {
27         struct udev *udev = udev_device_get_udev(udev_device);
28         struct udev_list_entry *list_entry;
29
30         udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device)) {
31                 char filename[UTIL_PATH_SIZE];
32
33                 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/tags/",
34                               udev_list_entry_get_name(list_entry), "/",
35                               udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
36
37                 if (add) {
38                         util_create_path(udev, filename);
39                         symlink(udev_device_get_devpath(udev_device), filename);
40                         if (udev_device_get_sysname_old(udev_device) != NULL) {
41                                 char filename_old[UTIL_PATH_SIZE];
42
43                                 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/tags/",
44                                               udev_list_entry_get_name(list_entry),
45                                               udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname_old(udev_device), NULL);
46                                 unlink(filename_old);
47                         }
48                 } else {
49                         unlink(filename);
50                         util_delete_path(udev, filename);
51                 }
52         }
53         return 0;
54 }
55
56 int udev_device_update_db(struct udev_device *udev_device)
57 {
58         struct udev *udev = udev_device_get_udev(udev_device);
59         char filename[UTIL_PATH_SIZE];
60         char filename_tmp[UTIL_PATH_SIZE];
61         FILE *f;
62         char target[232]; /* on 64bit, tmpfs inlines up to 239 bytes */
63         size_t devlen = strlen(udev_get_dev_path(udev))+1;
64         char *s;
65         size_t l;
66         struct udev_list_entry *list_entry;
67         int ret;
68
69         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/",
70                       udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
71         util_strscpyl(filename_tmp, sizeof(filename_tmp), filename, ".tmp", NULL);
72
73         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
74                 if (udev_list_entry_get_flags(list_entry))
75                         goto file;
76         if (udev_device_get_tags_list_entry(udev_device) != NULL)
77                 goto file;
78         if (udev_device_get_devlink_priority(udev_device) != 0)
79                 goto file;
80         if (udev_device_get_event_timeout(udev_device) >= 0)
81                 goto file;
82         if (udev_device_get_watch_handle(udev_device) >= 0)
83                 goto file;
84         if (udev_device_get_devnode(udev_device) == NULL)
85                 goto out;
86
87         /*
88          * if we have only the node and symlinks to store, try not to waste
89          * tmpfs memory -- store values, if they fit, in a symlink target
90          */
91         s = target;
92         l = util_strpcpy(&s, sizeof(target), &udev_device_get_devnode(udev_device)[devlen]);
93         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) {
94                 l = util_strpcpyl(&s, l, " ", &udev_list_entry_get_name(list_entry)[devlen], NULL);
95                 if (l == 0) {
96                         info(udev, "size of links too large, create file\n");
97                         goto file;
98                 }
99         }
100         udev_selinux_setfscreatecon(udev, filename_tmp, S_IFLNK);
101         util_create_path(udev, filename_tmp);
102         ret = symlink(target, filename_tmp);
103         udev_selinux_resetfscreatecon(udev);
104         if (ret != 0)
105                 goto file;
106         ret = rename(filename_tmp, filename);
107         if (ret != 0)
108                 goto file;
109         info(udev, "created db link (%s)\n", target);
110         goto out;
111 file:
112         util_create_path(udev, filename_tmp);
113         f = fopen(filename_tmp, "w");
114         if (f == NULL) {
115                 err(udev, "unable to create temporary db file '%s': %m\n", filename_tmp);
116                 return -1;
117         }
118
119         if (udev_device_get_devnode(udev_device) != NULL) {
120                 fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
121                 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
122                         fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]);
123         }
124         if (udev_device_get_devlink_priority(udev_device) != 0)
125                 fprintf(f, "L:%i\n", udev_device_get_devlink_priority(udev_device));
126         if (udev_device_get_event_timeout(udev_device) >= 0)
127                 fprintf(f, "T:%i\n", udev_device_get_event_timeout(udev_device));
128         if (udev_device_get_watch_handle(udev_device) >= 0)
129                 fprintf(f, "W:%i\n", udev_device_get_watch_handle(udev_device));
130         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
131                 if (!udev_list_entry_get_flags(list_entry))
132                         continue;
133                 fprintf(f, "E:%s=%s\n",
134                         udev_list_entry_get_name(list_entry),
135                         udev_list_entry_get_value(list_entry));
136         }
137         udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
138                 fprintf(f, "G:%s\n", udev_list_entry_get_name(list_entry));
139         fclose(f);
140         rename(filename_tmp, filename);
141         info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
142 out:
143         udev_device_tag_index(udev_device, true);
144         return 0;
145 }
146
147 int udev_device_delete_db(struct udev_device *udev_device)
148 {
149         struct udev *udev = udev_device_get_udev(udev_device);
150         char filename[UTIL_PATH_SIZE];
151
152         udev_device_tag_index(udev_device, false);
153         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/",
154                       udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
155         unlink(filename);
156         return 0;
157 }
158
159 int udev_device_rename_db(struct udev_device *udev_device)
160 {
161         struct udev *udev = udev_device_get_udev(udev_device);
162         char filename_old[UTIL_PATH_SIZE];
163         char filename[UTIL_PATH_SIZE];
164
165         if (strcmp(udev_device_get_sysname(udev_device), udev_device_get_sysname_old(udev_device)) == 0)
166                 return 0;
167
168         util_strscpyl(filename_old, sizeof(filename_old), udev_get_dev_path(udev), "/.udev/db/",
169                       udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname_old(udev_device), NULL);
170         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/",
171                       udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
172         udev_device_tag_index(udev_device, true);
173         return rename(filename_old, filename);
174 }