]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev-queue.c
tree-wide: sort includes
[thirdparty/systemd.git] / src / libudev / libudev-queue.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
5 Copyright 2009 Alan Jenkins <alan-jenkins@tuffmail.co.uk>
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <sys/inotify.h>
25 #include <unistd.h>
26
27 #include "alloc-util.h"
28 #include "fd-util.h"
29 #include "io-util.h"
30 #include "libudev-private.h"
31
32 /**
33 * SECTION:libudev-queue
34 * @short_description: access to currently active events
35 *
36 * This exports the current state of the udev processing queue.
37 */
38
39 /**
40 * udev_queue:
41 *
42 * Opaque object representing the current event queue in the udev daemon.
43 */
44 struct udev_queue {
45 struct udev *udev;
46 int refcount;
47 int fd;
48 };
49
50 /**
51 * udev_queue_new:
52 * @udev: udev library context
53 *
54 * The initial refcount is 1, and needs to be decremented to
55 * release the resources of the udev queue context.
56 *
57 * Returns: the udev queue context, or #NULL on error.
58 **/
59 _public_ struct udev_queue *udev_queue_new(struct udev *udev)
60 {
61 struct udev_queue *udev_queue;
62
63 if (udev == NULL)
64 return NULL;
65
66 udev_queue = new0(struct udev_queue, 1);
67 if (udev_queue == NULL)
68 return NULL;
69
70 udev_queue->refcount = 1;
71 udev_queue->udev = udev;
72 udev_queue->fd = -1;
73 return udev_queue;
74 }
75
76 /**
77 * udev_queue_ref:
78 * @udev_queue: udev queue context
79 *
80 * Take a reference of a udev queue context.
81 *
82 * Returns: the same udev queue context.
83 **/
84 _public_ struct udev_queue *udev_queue_ref(struct udev_queue *udev_queue)
85 {
86 if (udev_queue == NULL)
87 return NULL;
88
89 udev_queue->refcount++;
90 return udev_queue;
91 }
92
93 /**
94 * udev_queue_unref:
95 * @udev_queue: udev queue context
96 *
97 * Drop a reference of a udev queue context. If the refcount reaches zero,
98 * the resources of the queue context will be released.
99 *
100 * Returns: #NULL
101 **/
102 _public_ struct udev_queue *udev_queue_unref(struct udev_queue *udev_queue)
103 {
104 if (udev_queue == NULL)
105 return NULL;
106
107 udev_queue->refcount--;
108 if (udev_queue->refcount > 0)
109 return NULL;
110
111 safe_close(udev_queue->fd);
112
113 free(udev_queue);
114 return NULL;
115 }
116
117 /**
118 * udev_queue_get_udev:
119 * @udev_queue: udev queue context
120 *
121 * Retrieve the udev library context the queue context was created with.
122 *
123 * Returns: the udev library context.
124 **/
125 _public_ struct udev *udev_queue_get_udev(struct udev_queue *udev_queue)
126 {
127 if (udev_queue == NULL)
128 return NULL;
129 return udev_queue->udev;
130 }
131
132 /**
133 * udev_queue_get_kernel_seqnum:
134 * @udev_queue: udev queue context
135 *
136 * This function is deprecated.
137 *
138 * Returns: 0.
139 **/
140 _public_ unsigned long long int udev_queue_get_kernel_seqnum(struct udev_queue *udev_queue)
141 {
142 return 0;
143 }
144
145 /**
146 * udev_queue_get_udev_seqnum:
147 * @udev_queue: udev queue context
148 *
149 * This function is deprecated.
150 *
151 * Returns: 0.
152 **/
153 _public_ unsigned long long int udev_queue_get_udev_seqnum(struct udev_queue *udev_queue)
154 {
155 return 0;
156 }
157
158 /**
159 * udev_queue_get_udev_is_active:
160 * @udev_queue: udev queue context
161 *
162 * Check if udev is active on the system.
163 *
164 * Returns: a flag indicating if udev is active.
165 **/
166 _public_ int udev_queue_get_udev_is_active(struct udev_queue *udev_queue)
167 {
168 return access("/run/udev/control", F_OK) >= 0;
169 }
170
171 /**
172 * udev_queue_get_queue_is_empty:
173 * @udev_queue: udev queue context
174 *
175 * Check if udev is currently processing any events.
176 *
177 * Returns: a flag indicating if udev is currently handling events.
178 **/
179 _public_ int udev_queue_get_queue_is_empty(struct udev_queue *udev_queue)
180 {
181 return access("/run/udev/queue", F_OK) < 0;
182 }
183
184 /**
185 * udev_queue_get_seqnum_sequence_is_finished:
186 * @udev_queue: udev queue context
187 * @start: first event sequence number
188 * @end: last event sequence number
189 *
190 * This function is deprecated, it just returns the result of
191 * udev_queue_get_queue_is_empty().
192 *
193 * Returns: a flag indicating if udev is currently handling events.
194 **/
195 _public_ int udev_queue_get_seqnum_sequence_is_finished(struct udev_queue *udev_queue,
196 unsigned long long int start, unsigned long long int end)
197 {
198 return udev_queue_get_queue_is_empty(udev_queue);
199 }
200
201 /**
202 * udev_queue_get_seqnum_is_finished:
203 * @udev_queue: udev queue context
204 * @seqnum: sequence number
205 *
206 * This function is deprecated, it just returns the result of
207 * udev_queue_get_queue_is_empty().
208 *
209 * Returns: a flag indicating if udev is currently handling events.
210 **/
211 _public_ int udev_queue_get_seqnum_is_finished(struct udev_queue *udev_queue, unsigned long long int seqnum)
212 {
213 return udev_queue_get_queue_is_empty(udev_queue);
214 }
215
216 /**
217 * udev_queue_get_queued_list_entry:
218 * @udev_queue: udev queue context
219 *
220 * This function is deprecated.
221 *
222 * Returns: NULL.
223 **/
224 _public_ struct udev_list_entry *udev_queue_get_queued_list_entry(struct udev_queue *udev_queue)
225 {
226 return NULL;
227 }
228
229 /**
230 * udev_queue_get_fd:
231 * @udev_queue: udev queue context
232 *
233 * Returns: a file descriptor to watch for a queue to become empty.
234 */
235 _public_ int udev_queue_get_fd(struct udev_queue *udev_queue) {
236 int fd;
237 int r;
238
239 if (udev_queue->fd >= 0)
240 return udev_queue->fd;
241
242 fd = inotify_init1(IN_CLOEXEC);
243 if (fd < 0)
244 return -errno;
245
246 r = inotify_add_watch(fd, "/run/udev" , IN_DELETE);
247 if (r < 0) {
248 r = -errno;
249 close(fd);
250 return r;
251 }
252
253 udev_queue->fd = fd;
254 return fd;
255 }
256
257 /**
258 * udev_queue_flush:
259 * @udev_queue: udev queue context
260 *
261 * Returns: the result of clearing the watch for queue changes.
262 */
263 _public_ int udev_queue_flush(struct udev_queue *udev_queue) {
264 if (udev_queue->fd < 0)
265 return -EINVAL;
266
267 return flush_fd(udev_queue->fd);
268 }