]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libudev/libudev-device-private.c
udev: check malloc return in collect/collect.c
[thirdparty/systemd.git] / src / libudev / libudev-device-private.c
CommitLineData
8dfc8dbe 1/*
4061ab9f 2 * libudev - interface to udev device information
8dfc8dbe 3 *
28460195 4 * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
55e9959b 5 *
4061ab9f
KS
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.
8dfc8dbe
GKH
10 */
11
8e41d35d
DS
12#include <stdlib.h>
13#include <stdio.h>
8ea84a8a 14#include <string.h>
c81b35c0 15#include <stddef.h>
28460195 16#include <stdbool.h>
845d4751 17#include <unistd.h>
8e41d35d
DS
18#include <fcntl.h>
19#include <string.h>
34bb5d05 20#include <sys/stat.h>
8e41d35d 21
44b49d37
KS
22#include "libudev.h"
23#include "libudev-private.h"
2b41e68a 24
c1dbe11d 25static void udev_device_tag(struct udev_device *dev, const char *tag, bool add)
28460195 26{
912541b0 27 const char *id;
912541b0
KS
28 char filename[UTIL_PATH_SIZE];
29
30 id = udev_device_get_id_filename(dev);
31 if (id == NULL)
32 return;
4cb72937 33 util_strscpyl(filename, sizeof(filename), "/run/udev/tags/", tag, "/", id, NULL);
912541b0
KS
34
35 if (add) {
36 int fd;
37
3cbd5f6b 38 mkdir_parents(filename, 0755);
912541b0
KS
39 fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
40 if (fd >= 0)
41 close(fd);
42 } else {
43 unlink(filename);
44 }
c1dbe11d 45}
28460195 46
c1dbe11d
KS
47int udev_device_tag_index(struct udev_device *dev, struct udev_device *dev_old, bool add)
48{
912541b0
KS
49 struct udev_list_entry *list_entry;
50 bool found;
51
52 if (add && dev_old != NULL) {
53 /* delete possible left-over tags */
54 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev_old)) {
55 const char *tag_old = udev_list_entry_get_name(list_entry);
56 struct udev_list_entry *list_entry_current;
57
58 found = false;
59 udev_list_entry_foreach(list_entry_current, udev_device_get_tags_list_entry(dev)) {
60 const char *tag = udev_list_entry_get_name(list_entry_current);
61
33502ffe 62 if (streq(tag, tag_old)) {
912541b0
KS
63 found = true;
64 break;
65 }
66 }
67 if (!found)
68 udev_device_tag(dev_old, tag_old, false);
69 }
70 }
71
72 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev))
73 udev_device_tag(dev, udev_list_entry_get_name(list_entry), add);
74
75 return 0;
28460195
KS
76}
77
24d10766
KS
78static bool device_has_info(struct udev_device *udev_device)
79{
912541b0
KS
80 struct udev_list_entry *list_entry;
81
82 if (udev_device_get_devlinks_list_entry(udev_device) != NULL)
83 return true;
84 if (udev_device_get_devlink_priority(udev_device) != 0)
85 return true;
86 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
87 if (udev_list_entry_get_num(list_entry))
88 return true;
89 if (udev_device_get_tags_list_entry(udev_device) != NULL)
90 return true;
91 if (udev_device_get_watch_handle(udev_device) >= 0)
92 return true;
93 return false;
24d10766
KS
94}
95
aa8734ff 96int udev_device_update_db(struct udev_device *udev_device)
ca999860 97{
9e13dbae 98 struct udev *udev = udev_device_get_udev(udev_device);
912541b0
KS
99 bool has_info;
100 const char *id;
912541b0
KS
101 char filename[UTIL_PATH_SIZE];
102 char filename_tmp[UTIL_PATH_SIZE];
103 FILE *f;
104
105 id = udev_device_get_id_filename(udev_device);
106 if (id == NULL)
107 return -1;
108
109 has_info = device_has_info(udev_device);
4cb72937 110 util_strscpyl(filename, sizeof(filename), "/run/udev/data/", id, NULL);
912541b0
KS
111
112 /* do not store anything for otherwise empty devices */
113 if (!has_info &&
114 major(udev_device_get_devnum(udev_device)) == 0 &&
115 udev_device_get_ifindex(udev_device) == 0) {
116 unlink(filename);
117 return 0;
118 }
119
120 /* write a database file */
121 util_strscpyl(filename_tmp, sizeof(filename_tmp), filename, ".tmp", NULL);
3cbd5f6b 122 mkdir_parents(filename_tmp, 0755);
912541b0
KS
123 f = fopen(filename_tmp, "we");
124 if (f == NULL) {
c8f8394a 125 udev_err(udev, "unable to create temporary db file '%s': %m\n", filename_tmp);
912541b0
KS
126 return -1;
127 }
128
129 /*
130 * set 'sticky' bit to indicate that we should not clean the
131 * database when we transition from initramfs to the real root
132 */
133 if (udev_device_get_db_persist(udev_device))
134 fchmod(fileno(f), 01644);
135
136 if (has_info) {
137 struct udev_list_entry *list_entry;
138
139 if (major(udev_device_get_devnum(udev_device)) > 0) {
912541b0 140 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
4cb72937 141 fprintf(f, "S:%s\n", udev_list_entry_get_name(list_entry) + strlen("/dev/"));
912541b0
KS
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 }
147
148 if (udev_device_get_usec_initialized(udev_device) > 0)
149 fprintf(f, "I:%llu\n", udev_device_get_usec_initialized(udev_device));
150
151 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
152 if (!udev_list_entry_get_num(list_entry))
153 continue;
154 fprintf(f, "E:%s=%s\n",
155 udev_list_entry_get_name(list_entry),
156 udev_list_entry_get_value(list_entry));
157 }
158
159 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
160 fprintf(f, "G:%s\n", udev_list_entry_get_name(list_entry));
161 }
162
163 fclose(f);
164 rename(filename_tmp, filename);
c8f8394a 165 udev_dbg(udev, "created %s file '%s' for '%s'\n", has_info ? "db" : "empty",
912541b0
KS
166 filename, udev_device_get_devpath(udev_device));
167 return 0;
ca999860
GKH
168}
169
aa8734ff 170int udev_device_delete_db(struct udev_device *udev_device)
8e41d35d 171{
912541b0 172 const char *id;
912541b0
KS
173 char filename[UTIL_PATH_SIZE];
174
175 id = udev_device_get_id_filename(udev_device);
176 if (id == NULL)
177 return -1;
4cb72937 178 util_strscpyl(filename, sizeof(filename), "/run/udev/data/", id, NULL);
912541b0
KS
179 unlink(filename);
180 return 0;
8e41d35d 181}