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>
59 #include <libprop/proplib.h>
63 struct cmd_function cmd_fn
[] = {
64 { .cmd
= "getdevs", .fn
= client_cmd_getdevs
},
65 { .cmd
= "monitor", .fn
= client_cmd_monitor
},
69 static void *client_thread(void *arg
);
72 handle_new_connection(int s
)
74 struct client_info
*cli_info
;
75 struct sockaddr_un addr
;
77 socklen_t saddr_len
= sizeof(struct sockaddr_un
);
79 fd
= accept(s
, (struct sockaddr
*)&addr
, &saddr_len
);
81 syslog(LOG_ERR
, "uh, oh, accept failed with %d", errno
);
86 cli_info
= malloc(sizeof(struct client_info
));
87 memset(cli_info
, 0, sizeof(struct client_info
));
90 pthread_create(&cli_info
->tid
, NULL
, client_thread
, (void *)cli_info
);
95 client_thread(void *arg
)
97 prop_dictionary_t dict
;
100 struct client_info
*cli
;
104 r
= pthread_detach(pthread_self());
107 r
= ignore_signal(SIGPIPE
);
109 err(1, "could not ignore_signal SIGPIPE");
111 cli
= (struct client_info
*)arg
;
113 n
= read_xml(cli
->fd
, &xml
);
121 dict
= prop_dictionary_internalize(xml
);
125 syslog(LOG_ERR
, "internalization of received XML failed");
129 po
= prop_dictionary_get(dict
, "command");
130 if (po
== NULL
|| prop_object_type(po
) != PROP_TYPE_STRING
) {
131 syslog(LOG_ERR
, "received dictionary doesn't contain a key 'command'");
132 prop_object_release(dict
);
138 syslog(LOG_DEBUG
, "Received command: %s (from fd = %d)\n", prop_string_cstring_nocopy(ps
), cli
->fd
);
139 for(n
= 0; cmd_fn
[n
].cmd
!= NULL
; n
++) {
140 if (prop_string_equals_cstring(ps
, cmd_fn
[n
].cmd
))
144 if (cmd_fn
[n
].cmd
!= NULL
) {
145 error
= cmd_fn
[n
].fn(cli
, dict
);
147 prop_object_release(dict
);
151 prop_object_release(dict
);
164 client_cmd_getdevs(struct client_info
*cli
, prop_dictionary_t cli_dict
)
166 struct pdev_array_entry
*pae
;
167 struct udev_monitor
*udm
;
168 prop_object_iterator_t iter
;
169 prop_dictionary_t dict
;
178 po
= prop_dictionary_get(cli_dict
, "filters");
179 if ((po
!= NULL
) && prop_object_type(po
) == PROP_TYPE_ARRAY
) {
186 pae
= pdev_array_entry_get_last();
191 udm
= udev_monitor_init(cli
, pa
);
193 pdev_array_entry_unref(pae
);
197 pa
= prop_array_create_with_capacity(10);
199 iter
= prop_array_iterator(pae
->pdev_array
);
201 pdev_array_entry_unref(pae
);
202 udev_monitor_free(udm
);
206 while ((dict
= prop_object_iterator_next(iter
)) != NULL
) {
207 if (match_event_filter(udm
, dict
)) {
208 prop_array_add(pa
, dict
);
212 prop_object_iterator_release(iter
);
213 udev_monitor_free(udm
);
215 pa
= pae
->pdev_array
;
218 xml
= prop_array_externalize(pa
);
220 prop_object_release(pa
);
222 pdev_array_entry_unref(pae
);
227 r
= send_xml(cli
->fd
, xml
);
229 syslog(LOG_DEBUG
, "error while send_xml (cmd_getdevs)\n");
231 syslog(LOG_DEBUG
, "EOF while send_xml (cmd_getdevs)\n");