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
72 udev_ref(struct udev
*udev_ctx
)
74 atomic_add_int(&udev_ctx
->refs
, 1);
80 udev_unref(struct udev
*udev_ctx
)
84 refcount
= atomic_fetchadd_int(&udev_ctx
->refs
, -1);
87 atomic_subtract_int(&udev_ctx
->refs
, 0x400); /* in destruction */
88 if (udev_ctx
->gp_fd
!= -1)
89 close (udev_ctx
->gp_fd
);
90 if (udev_ctx
->monitor_fd
!= -1)
91 close (udev_ctx
->monitor_fd
);
100 struct udev
*udev_ctx
;
103 ret
= conn_local_server(LISTEN_SOCKET_FILE
, SOCK_STREAM
, 0, &s
);
107 udev_ctx
= malloc(sizeof(struct udev
));
111 udev_ctx
->monitor_fd
= -1;
112 udev_ctx
->userdata
= NULL
;
117 const char *udev_get_dev_path(struct udev
*udev_ctx __unused
)
123 udev_get_userdata(struct udev
*udev_ctx
)
125 return udev_ctx
->userdata
;
129 udev_set_userdata(struct udev
*udev_ctx
, void *userdata
)
131 udev_ctx
->userdata
= userdata
;
135 udev_get_fd(struct udev
*udev_ctx
)
137 return udev_ctx
->gp_fd
;
141 send_xml(int s
, char *xml
)
146 sz
= strlen(xml
) + 1;
148 r
= send(s
, &sz
, sizeof(sz
), 0);
153 while (r
< (ssize_t
)sz
) {
154 n
= send(s
, xml
+r
, sz
-r
, 0);
164 read_xml(int s
, char **buf
)
172 n
= recv(s
, &sz
, sizeof(sz
), MSG_WAITALL
);
173 if ((n
<= 0) || (sz
> 12*1024*1024)) /* Arbitrary limit */
178 while (r
< (ssize_t
)sz
) {
179 n
= recv(s
, xml
+r
, sz
-r
, MSG_WAITALL
);
192 _udev_dict_set_cstr(prop_dictionary_t dict
, const char *key
, char *str
)
196 ps
= prop_string_create_cstring(str
);
200 if (prop_dictionary_set(dict
, key
, ps
) == false) {
201 prop_object_release(ps
);
205 prop_object_release(ps
);
210 _udev_dict_set_int(prop_dictionary_t dict
, const char *key
, int64_t val
)
214 pn
= prop_number_create_integer(val
);
218 if (prop_dictionary_set(dict
, key
, pn
) == false) {
219 prop_object_release(pn
);
223 prop_object_release(pn
);
228 _udev_dict_set_uint(prop_dictionary_t dict
, const char *key
, uint64_t val
)
232 pn
= prop_number_create_unsigned_integer(val
);
236 if (prop_dictionary_set(dict
, key
, pn
) == false) {
237 prop_object_release(pn
);
241 prop_object_release(pn
);
246 conn_local_server(const char *sockfile
, int socktype
, int nonblock __unused
,
250 struct sockaddr_un serv_addr
;
253 if ((s
= socket(AF_UNIX
, socktype
, 0)) < 0)
256 memset(&serv_addr
, 0, sizeof(serv_addr
));
257 serv_addr
.sun_family
= AF_UNIX
;
258 strncpy(serv_addr
.sun_path
, sockfile
, SOCKFILE_NAMELEN
);
259 serv_addr
.sun_path
[SOCKFILE_NAMELEN
- 1] = '\0';
262 return connect(s
, (struct sockaddr
*)&serv_addr
, sizeof(serv_addr
));
266 udevd_get_command_dict(char *command
)
268 prop_dictionary_t dict
;
271 dict
= prop_dictionary_create();
275 if ((error
= _udev_dict_set_cstr(dict
, "command", command
)))
281 prop_object_release(dict
);
286 udevd_request_devs(int s
, prop_array_t filters
)
289 prop_dictionary_t dict
;
294 dict
= udevd_get_command_dict(__DECONST(char *, "getdevs"));
298 /* Add filters to message, if available */
299 if (filters
!= NULL
) {
300 if (prop_dictionary_set(dict
, "filters", filters
) == false) {
301 prop_object_release(dict
);
306 xml
= prop_dictionary_externalize(dict
);
307 prop_object_release(dict
);
311 n
= send_xml(s
, xml
);
317 if ((n
= read_xml(s
, &xml
)) <= 0)
321 pa
= prop_array_internalize(xml
);