• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***
2   This file is part of systemd.
3 
4   Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
5 
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10 
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15 
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19 
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stddef.h>
24 #include <stdbool.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <sys/sysmacros.h>
29 
30 #include "libudev.h"
31 #include "libudev-private.h"
32 
udev_device_tag(struct udev_device * dev,const char * tag,bool add)33 static void udev_device_tag(struct udev_device *dev, const char *tag, bool add)
34 {
35         const char *id;
36         char filename[UTIL_PATH_SIZE];
37 
38         id = udev_device_get_id_filename(dev);
39         if (id == NULL)
40                 return;
41         strscpyl(filename, sizeof(filename), UDEV_ROOT_RUN "/udev/tags/", tag, "/", id, NULL);
42 
43         if (add) {
44                 int fd;
45 
46                 mkdir_parents(filename, 0755);
47                 fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
48                 if (fd >= 0)
49                         close(fd);
50         } else {
51                 unlink(filename);
52         }
53 }
54 
udev_device_tag_index(struct udev_device * dev,struct udev_device * dev_old,bool add)55 int udev_device_tag_index(struct udev_device *dev, struct udev_device *dev_old, bool add)
56 {
57         struct udev_list_entry *list_entry;
58         bool found;
59 
60         if (add && dev_old != NULL) {
61                 /* delete possible left-over tags */
62                 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev_old)) {
63                         const char *tag_old = udev_list_entry_get_name(list_entry);
64                         struct udev_list_entry *list_entry_current;
65 
66                         found = false;
67                         udev_list_entry_foreach(list_entry_current, udev_device_get_tags_list_entry(dev)) {
68                                 const char *tag = udev_list_entry_get_name(list_entry_current);
69 
70                                 if (streq(tag, tag_old)) {
71                                         found = true;
72                                         break;
73                                 }
74                         }
75                         if (!found)
76                                 udev_device_tag(dev_old, tag_old, false);
77                 }
78         }
79 
80         udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev))
81                 udev_device_tag(dev, udev_list_entry_get_name(list_entry), add);
82 
83         return 0;
84 }
85 
device_has_info(struct udev_device * udev_device)86 static bool device_has_info(struct udev_device *udev_device)
87 {
88         struct udev_list_entry *list_entry;
89 
90         if (udev_device_get_devlinks_list_entry(udev_device) != NULL)
91                 return true;
92         if (udev_device_get_devlink_priority(udev_device) != 0)
93                 return true;
94         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
95                 if (udev_list_entry_get_num(list_entry))
96                         return true;
97         if (udev_device_get_tags_list_entry(udev_device) != NULL)
98                 return true;
99         if (udev_device_get_watch_handle(udev_device) >= 0)
100                 return true;
101         return false;
102 }
103 
udev_device_update_db(struct udev_device * udev_device)104 int udev_device_update_db(struct udev_device *udev_device)
105 {
106         bool has_info;
107         const char *id;
108         char filename[UTIL_PATH_SIZE];
109         char filename_tmp[UTIL_PATH_SIZE];
110         FILE *f;
111         int r;
112 
113         id = udev_device_get_id_filename(udev_device);
114         if (id == NULL)
115                 return -1;
116 
117         has_info = device_has_info(udev_device);
118         strscpyl(filename, sizeof(filename), UDEV_ROOT_RUN "/udev/data/", id, NULL);
119 
120         /* do not store anything for otherwise empty devices */
121         if (!has_info &&
122             major(udev_device_get_devnum(udev_device)) == 0 &&
123             udev_device_get_ifindex(udev_device) == 0) {
124                 unlink(filename);
125                 return 0;
126         }
127 
128         /* write a database file */
129         strscpyl(filename_tmp, sizeof(filename_tmp), filename, ".tmp", NULL);
130         mkdir_parents(filename_tmp, 0755);
131         f = fopen(filename_tmp, "we");
132         if (f == NULL)
133                 return log_debug_errno(errno, "unable to create temporary db file '%s': %m", filename_tmp);
134 
135         /*
136          * set 'sticky' bit to indicate that we should not clean the
137          * database when we transition from initramfs to the real root
138          */
139         if (udev_device_get_db_persist(udev_device))
140                 fchmod(fileno(f), 01644);
141 
142         if (has_info) {
143                 struct udev_list_entry *list_entry;
144 
145                 if (major(udev_device_get_devnum(udev_device)) > 0) {
146                         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
147                                 fprintf(f, "S:%s\n", udev_list_entry_get_name(list_entry) + strlen("/dev/"));
148                         if (udev_device_get_devlink_priority(udev_device) != 0)
149                                 fprintf(f, "L:%i\n", udev_device_get_devlink_priority(udev_device));
150                         if (udev_device_get_watch_handle(udev_device) >= 0)
151                                 fprintf(f, "W:%i\n", udev_device_get_watch_handle(udev_device));
152                 }
153 
154                 if (udev_device_get_usec_initialized(udev_device) > 0)
155                         fprintf(f, "I:"USEC_FMT"\n", udev_device_get_usec_initialized(udev_device));
156 
157                 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
158                         if (!udev_list_entry_get_num(list_entry))
159                                 continue;
160                         fprintf(f, "E:%s=%s\n",
161                                 udev_list_entry_get_name(list_entry),
162                                 udev_list_entry_get_value(list_entry));
163                 }
164 
165                 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
166                         fprintf(f, "G:%s\n", udev_list_entry_get_name(list_entry));
167         }
168 
169         fclose(f);
170         r = rename(filename_tmp, filename);
171         if (r < 0)
172                 return -1;
173         log_debug("created %s file '%s' for '%s'", has_info ? "db" : "empty",
174              filename, udev_device_get_devpath(udev_device));
175         return 0;
176 }
177 
udev_device_delete_db(struct udev_device * udev_device)178 int udev_device_delete_db(struct udev_device *udev_device)
179 {
180         const char *id;
181         char filename[UTIL_PATH_SIZE];
182 
183         id = udev_device_get_id_filename(udev_device);
184         if (id == NULL)
185                 return -1;
186         strscpyl(filename, sizeof(filename), UDEV_ROOT_RUN "/udev/data/", id, NULL);
187 
188         unlink(filename);
189         return 0;
190 }
191