]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev-queue.c
4d1a0adbf21a8150fb1ed672dadb08f617c6e55a
[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 <stdlib.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <sys/inotify.h>
26
27 #include "fd-util.h"
28 #include "libudev-private.h"
29
30 /**
31 * SECTION:libudev-queue
32 * @short_description: access to currently active events
33 *
34 * This exports the current state of the udev processing queue.
35 */
36
37 /**
38 * udev_queue:
39 *
40 * Opaque object representing the current event queue in the udev daemon.
41 */
42 struct udev_queue {
43 struct udev *udev;
44 int refcount;
45 int fd;
46 };
47
48 /**
49 * udev_queue_new:
50 * @udev: udev library context
51 *
52 * The initial refcount is 1, and needs to be decremented to
53 * release the resources of the udev queue context.
54 *
55 * Returns: the udev queue context, or #NULL on error.
56 **/
57 _public_ struct udev_queue *udev_queue_new(struct udev *udev)
58 {
59 struct udev_queue *udev_queue;
60
61 if (udev == NULL)
62 return NULL;
63
64 udev_queue = new0(struct udev_queue, 1);
65 if (udev_queue == NULL)
66 return NULL;
67
68 udev_queue->refcount = 1;
69 udev_queue->udev = udev;
70 udev_queue->fd = -1;
71 return udev_queue;
72 }
73
74 /**
75 * udev_queue_ref:
76 * @udev_queue: udev queue context
77 *
78 * Take a reference of a udev queue context.
79 *
80 * Returns: the same udev queue context.
81 **/
82 _public_ struct udev_queue *udev_queue_ref(struct udev_queue *udev_queue)
83 {
84 if (udev_queue == NULL)
85 return NULL;
86
87 udev_queue->refcount++;
88 return udev_queue;
89 }
90
91 /**
92 * udev_queue_unref:
93 * @udev_queue: udev queue context
94 *
95 * Drop a reference of a udev queue context. If the refcount reaches zero,
96 * the resources of the queue context will be released.
97 *
98 * Returns: #NULL
99 **/
100 _public_ struct udev_queue *udev_queue_unref(struct udev_queue *udev_queue)
101 {
102 if (udev_queue == NULL)
103 return NULL;
104
105 udev_queue->refcount--;
106 if (udev_queue->refcount > 0)
107 return NULL;
108
109 safe_close(udev_queue->fd);
110
111 free(udev_queue);
112 return NULL;
113 }
114
115 /**
116 * udev_queue_get_udev:
117 * @udev_queue: udev queue context
118 *
119 * Retrieve the udev library context the queue context was created with.
120 *
121 * Returns: the udev library context.
122 **/
123 _public_ struct udev *udev_queue_get_udev(struct udev_queue *udev_queue)
124 {
125 if (udev_queue == NULL)
126 return NULL;
127 return udev_queue->udev;
128 }
129
130 /**
131 * udev_queue_get_kernel_seqnum:
132 * @udev_queue: udev queue context
133 *
134 * This function is deprecated.
135 *
136 * Returns: 0.
137 **/
138 _public_ unsigned long long int udev_queue_get_kernel_seqnum(struct udev_queue *udev_queue)
139 {
140 return 0;
141 }
142
143 /**
144 * udev_queue_get_udev_seqnum:
145 * @udev_queue: udev queue context
146 *
147 * This function is deprecated.
148 *
149 * Returns: 0.
150 **/
151 _public_ unsigned long long int udev_queue_get_udev_seqnum(struct udev_queue *udev_queue)
152 {
153 return 0;
154 }
155
156 /**
157 * udev_queue_get_udev_is_active:
158 * @udev_queue: udev queue context
159 *
160 * Check if udev is active on the system.
161 *
162 * Returns: a flag indicating if udev is active.
163 **/
164 _public_ int udev_queue_get_udev_is_active(struct udev_queue *udev_queue)
165 {
166 return access("/run/udev/control", F_OK) >= 0;
167 }
168
169 /**
170 * udev_queue_get_queue_is_empty:
171 * @udev_queue: udev queue context
172 *
173 * Check if udev is currently processing any events.
174 *
175 * Returns: a flag indicating if udev is currently handling events.
176 **/
177 _public_ int udev_queue_get_queue_is_empty(struct udev_queue *udev_queue)
178 {
179 return access("/run/udev/queue", F_OK) < 0;
180 }
181
182 /**
183 * udev_queue_get_seqnum_sequence_is_finished:
184 * @udev_queue: udev queue context
185 * @start: first event sequence number
186 * @end: last event sequence number
187 *
188 * This function is deprecated, it just returns the result of
189 * udev_queue_get_queue_is_empty().
190 *
191 * Returns: a flag indicating if udev is currently handling events.
192 **/
193 _public_ int udev_queue_get_seqnum_sequence_is_finished(struct udev_queue *udev_queue,
194 unsigned long long int start, unsigned long long int end)
195 {
196 return udev_queue_get_queue_is_empty(udev_queue);
197 }
198
199 /**
200 * udev_queue_get_seqnum_is_finished:
201 * @udev_queue: udev queue context
202 * @seqnum: sequence number
203 *
204 * This function is deprecated, it just returns the result of
205 * udev_queue_get_queue_is_empty().
206 *
207 * Returns: a flag indicating if udev is currently handling events.
208 **/
209 _public_ int udev_queue_get_seqnum_is_finished(struct udev_queue *udev_queue, unsigned long long int seqnum)
210 {
211 return udev_queue_get_queue_is_empty(udev_queue);
212 }
213
214 /**
215 * udev_queue_get_queued_list_entry:
216 * @udev_queue: udev queue context
217 *
218 * This function is deprecated.
219 *
220 * Returns: NULL.
221 **/
222 _public_ struct udev_list_entry *udev_queue_get_queued_list_entry(struct udev_queue *udev_queue)
223 {
224 return NULL;
225 }
226
227 /**
228 * udev_queue_get_fd:
229 * @udev_queue: udev queue context
230 *
231 * Returns: a file descriptor to watch for a queue to become empty.
232 */
233 _public_ int udev_queue_get_fd(struct udev_queue *udev_queue) {
234 int fd;
235 int r;
236
237 if (udev_queue->fd >= 0)
238 return udev_queue->fd;
239
240 fd = inotify_init1(IN_CLOEXEC);
241 if (fd < 0)
242 return -errno;
243
244 r = inotify_add_watch(fd, "/run/udev" , IN_DELETE);
245 if (r < 0) {
246 r = -errno;
247 close(fd);
248 return r;
249 }
250
251 udev_queue->fd = fd;
252 return fd;
253 }
254
255 /**
256 * udev_queue_flush:
257 * @udev_queue: udev queue context
258 *
259 * Returns: the result of clearing the watch for queue changes.
260 */
261 _public_ int udev_queue_flush(struct udev_queue *udev_queue) {
262 if (udev_queue->fd < 0)
263 return -EINVAL;
264
265 return flush_fd(udev_queue->fd);
266 }