2 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
35 #include <sys/device.h>
37 #include <sys/socket.h>
38 #include <sys/ioctl.h>
40 #include <sys/queue.h>
42 #include <cpu/inttypes.h>
58 #include <libprop/proplib.h>
60 #define LIBDEVATTR_INTERNAL
63 struct udev_enumerate
{
64 struct udev
*udev_ctx
;
68 TAILQ_HEAD(, udev_list_entry
) list_entries
;
71 struct udev_list_entry
{
72 struct udev
*udev_ctx
;
73 prop_dictionary_t dict
;
74 TAILQ_ENTRY(udev_list_entry
) link
;
77 struct udev_enumerate
*
78 udev_enumerate_new(struct udev
*udev_ctx
)
80 struct udev_enumerate
*udev_enum
;
82 udev_enum
= malloc(sizeof(struct udev_enumerate
));
85 udev_enum
->ev_filt
= NULL
;
87 TAILQ_INIT(&udev_enum
->list_entries
);
88 udev_enum
->udev_ctx
= udev_ref(udev_ctx
);
93 struct udev_enumerate
*
94 udev_enumerate_ref(struct udev_enumerate
*udev_enum
)
96 atomic_add_int(&udev_enum
->refs
, 1);
102 udev_enumerate_unref(struct udev_enumerate
*udev_enum
)
104 struct udev_list_entry
*le
;
107 refcount
= atomic_fetchadd_int(&udev_enum
->refs
, -1);
110 atomic_subtract_int(&udev_enum
->refs
, 0x400); /* in destruction */
111 if (udev_enum
->pa
!= NULL
)
112 prop_object_release(udev_enum
->pa
);
113 if (udev_enum
->ev_filt
!= NULL
)
114 prop_object_release(udev_enum
->ev_filt
);
116 while (!TAILQ_EMPTY(&udev_enum
->list_entries
)) {
117 le
= TAILQ_FIRST(&udev_enum
->list_entries
);
118 TAILQ_REMOVE(&udev_enum
->list_entries
, le
, link
);
119 prop_object_release(le
->dict
);
122 udev_unref(udev_enum
->udev_ctx
);
128 udev_enumerate_get_udev(struct udev_enumerate
*udev_enum
)
130 return udev_enum
->udev_ctx
;
134 udev_enumerate_scan_devices(struct udev_enumerate
*udev_enum
)
138 if (udev_get_fd(udev_enum
->udev_ctx
) == -1)
141 pa
= udevd_request_devs(udev_get_fd(udev_enum
->udev_ctx
), udev_enum
->ev_filt
);
145 prop_object_retain(pa
);
147 if (udev_enum
->pa
!= NULL
)
148 prop_object_release(udev_enum
->pa
);
155 struct udev_list_entry
*
156 udev_enumerate_get_list_entry(struct udev_enumerate
*udev_enum
)
158 struct udev_list_entry
*le
;
159 prop_object_iterator_t iter
;
160 prop_dictionary_t dict
;
162 /* If the list is not empty, assume it was populated in an earlier call */
163 if (!TAILQ_EMPTY(&udev_enum
->list_entries
))
164 return TAILQ_FIRST(&udev_enum
->list_entries
);
166 iter
= prop_array_iterator(udev_enum
->pa
);
170 while ((dict
= prop_object_iterator_next(iter
)) != NULL
) {
171 le
= malloc(sizeof(struct udev_list_entry
));
175 prop_object_retain(dict
);
177 le
->udev_ctx
= udev_enum
->udev_ctx
;
178 TAILQ_INSERT_TAIL(&udev_enum
->list_entries
, le
, link
);
181 le
= TAILQ_FIRST(&udev_enum
->list_entries
);
184 prop_object_iterator_release(iter
);
189 udev_enumerate_get_array(struct udev_enumerate
*udev_enum
)
191 return udev_enum
->pa
;
194 struct udev_list_entry
*
195 udev_list_entry_get_next(struct udev_list_entry
*list_entry
)
197 return TAILQ_NEXT(list_entry
, link
);
201 udev_list_entry_get_dictionary(struct udev_list_entry
*list_entry
)
203 return list_entry
->dict
;
207 udev_list_entry_get_device(struct udev_list_entry
*list_entry
)
209 struct udev_device
*udev_dev
;
211 udev_dev
= udev_device_new_from_dictionary(list_entry
->udev_ctx
,
218 udev_enumerate_add_match_subsystem(struct udev_enumerate
*udev_enum
,
219 const char *subsystem
)
223 ret
= _udev_enumerate_filter_add_match_gen(udev_enum
,
224 EVENT_FILTER_TYPE_WILDCARD
,
227 __DECONST(char *, subsystem
));
233 udev_enumerate_add_nomatch_subsystem(struct udev_enumerate
*udev_enum
,
234 const char *subsystem
)
238 ret
= _udev_enumerate_filter_add_match_gen(udev_enum
,
239 EVENT_FILTER_TYPE_WILDCARD
,
242 __DECONST(char *, subsystem
));
248 udev_enumerate_add_match_expr(struct udev_enumerate
*udev_enum
,
254 ret
= _udev_enumerate_filter_add_match_gen(udev_enum
,
255 EVENT_FILTER_TYPE_WILDCARD
,
264 udev_enumerate_add_nomatch_expr(struct udev_enumerate
*udev_enum
,
270 ret
= _udev_enumerate_filter_add_match_gen(udev_enum
,
271 EVENT_FILTER_TYPE_WILDCARD
,
280 udev_enumerate_add_match_regex(struct udev_enumerate
*udev_enum
,
286 ret
= _udev_enumerate_filter_add_match_gen(udev_enum
,
287 EVENT_FILTER_TYPE_REGEX
,
296 udev_enumerate_add_nomatch_regex(struct udev_enumerate
*udev_enum
,
302 ret
= _udev_enumerate_filter_add_match_gen(udev_enum
,
303 EVENT_FILTER_TYPE_REGEX
,
312 _udev_enumerate_filter_add_match_gen(struct udev_enumerate
*udev_enum
,
321 if (udev_enum
->ev_filt
== NULL
) {
322 pa
= prop_array_create_with_capacity(5);
326 udev_enum
->ev_filt
= pa
;
329 error
= _udev_filter_add_match_gen(udev_enum
->ev_filt
, type
, neg
, key
, expr
);