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>
62 struct cmd_function cmd_fn[] = {
63 { .cmd = "getdevs", .fn = client_cmd_getdevs},
64 { .cmd = "monitor", .fn = client_cmd_monitor},
68 static void *client_thread(void *arg);
71 handle_new_connection(int s)
73 struct client_info *cli_info;
74 struct sockaddr_un addr;
76 socklen_t saddr_len = sizeof(struct sockaddr_un);
78 fd = accept(s, (struct sockaddr *)&addr, &saddr_len);
80 syslog(LOG_ERR, "uh, oh, accept failed with %d", errno);
85 cli_info = malloc(sizeof(struct client_info));
86 memset(cli_info, 0, sizeof(struct client_info));
89 pthread_create(&cli_info->tid, NULL, client_thread, (void *)cli_info);
94 client_thread(void *arg)
96 prop_dictionary_t dict;
99 struct client_info *cli;
103 r = ignore_signal(SIGPIPE);
105 err(1, "could not ignore_signal SIGPIPE");
107 cli = (struct client_info *)arg;
108 xml = malloc(12*1024*1024); /* generous 12 MB */
110 n = read_xml(cli->fd, xml, 12*1024*1024);
118 dict = prop_dictionary_internalize(xml);
120 syslog(LOG_ERR, "internalization of received XML failed");
124 po = prop_dictionary_get(dict, "command");
125 if (po == NULL || prop_object_type(po) != PROP_TYPE_STRING) {
126 syslog(LOG_ERR, "received dictionary doesn't contain a key 'command'");
127 prop_object_release(dict);
133 syslog(LOG_DEBUG, "Received command: %s (from fd = %d)\n", prop_string_cstring_nocopy(ps), cli->fd);
134 for(n = 0; cmd_fn[n].cmd != NULL; n++) {
135 if (prop_string_equals_cstring(ps, cmd_fn[n].cmd))
139 if (cmd_fn[n].cmd != NULL) {
140 error = cmd_fn[n].fn(cli, dict);
142 prop_object_release(dict);
146 prop_object_release(dict);
160 client_cmd_getdevs(struct client_info *cli, prop_dictionary_t cli_dict)
162 struct pdev_array_entry *pae;
163 struct udev_monitor *udm;
164 prop_object_iterator_t iter;
165 prop_dictionary_t dict;
174 po = prop_dictionary_get(cli_dict, "filters");
175 if ((po != NULL) && prop_object_type(po) == PROP_TYPE_ARRAY) {
182 pae = pdev_array_entry_get_last();
187 udm = udev_monitor_init(cli, pa);
189 pdev_array_entry_unref(pae);
193 pa = prop_array_create_with_capacity(10);
195 iter = prop_array_iterator(pae->pdev_array);
197 pdev_array_entry_unref(pae);
198 udev_monitor_free(udm);
202 while ((dict = prop_object_iterator_next(iter)) != NULL) {
203 if (match_event_filter(udm, dict)) {
204 prop_array_add(pa, dict);
208 udev_monitor_free(udm);
210 pa = pae->pdev_array;
213 xml = prop_array_externalize(pa);
215 prop_object_release(pa);
217 pdev_array_entry_unref(pae);
222 r = send_xml(cli->fd, xml);
224 syslog(LOG_DEBUG, "error while send_xml (cmd_getdevs)\n");
226 syslog(LOG_DEBUG, "EOF while send_xml (cmd_getdevs)\n");