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