kernel - Validate that previous pte was cleaned.
[dragonfly.git] / sbin / udevd / udevd_client.c
blob212fb3ad664e9f41b83f60416a9f0e46a8fbfbde
1 /*
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
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
34 #include <sys/types.h>
35 #include <sys/device.h>
36 #include <sys/wait.h>
37 #include <sys/socket.h>
38 #include <sys/ioctl.h>
39 #include <sys/poll.h>
40 #include <sys/queue.h>
41 #include <sys/un.h>
42 #include <cpu/inttypes.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <libgen.h>
48 #include <regex.h>
49 #include <signal.h>
50 #include <stdarg.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <syslog.h>
55 #include <unistd.h>
56 #include <pthread.h>
57 #include <assert.h>
59 #include <libprop/proplib.h>
60 #include <sys/udev.h>
61 #include "udevd.h"
63 struct cmd_function cmd_fn[] = {
64 { .cmd = "getdevs", .fn = client_cmd_getdevs},
65 { .cmd = "monitor", .fn = client_cmd_monitor},
66 {NULL, NULL}
69 static void *client_thread(void *arg);
71 void
72 handle_new_connection(int s)
74 struct client_info *cli_info;
75 struct sockaddr_un addr;
76 int fd;
77 socklen_t saddr_len = sizeof(struct sockaddr_un);
79 fd = accept(s, (struct sockaddr *)&addr, &saddr_len);
80 if (fd < 0) {
81 syslog(LOG_ERR, "uh, oh, accept failed with %d", errno);
82 return;
85 block_descriptor(fd);
86 cli_info = malloc(sizeof(struct client_info));
87 memset(cli_info, 0, sizeof(struct client_info));
89 cli_info->fd = fd;
90 pthread_create(&cli_info->tid, NULL, client_thread, (void *)cli_info);
94 static void *
95 client_thread(void *arg)
97 prop_dictionary_t dict;
98 prop_string_t ps;
99 prop_object_t po;
100 struct client_info *cli;
101 char *xml;
102 int r, n, error;
104 r = pthread_detach(pthread_self());
105 assert(r == 0);
107 r = ignore_signal(SIGPIPE);
108 if (r != 0)
109 err(1, "could not ignore_signal SIGPIPE");
111 cli = (struct client_info *)arg;
112 for (;;) {
113 n = read_xml(cli->fd, &xml);
114 if (n == 0)
115 goto cli_disconnect;
116 else if (n < 0)
117 goto error_out;
119 xml[n+1] = '\0';
121 dict = prop_dictionary_internalize(xml);
122 free(xml);
124 if (dict == NULL) {
125 syslog(LOG_ERR, "internalization of received XML failed");
126 goto error_out;
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);
133 continue;
136 ps = po;
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))
141 break;
144 if (cmd_fn[n].cmd != NULL) {
145 error = cmd_fn[n].fn(cli, dict);
146 if (error) {
147 prop_object_release(dict);
148 goto error_out;
151 prop_object_release(dict);
154 error_out:
156 cli_disconnect:
157 close(cli->fd);
158 cli->fd = -1;
159 free(cli);
160 return NULL;
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;
170 prop_object_t po;
171 prop_array_t pa;
172 char *xml;
173 ssize_t r;
174 int filters;
177 pa = NULL;
178 po = prop_dictionary_get(cli_dict, "filters");
179 if ((po != NULL) && prop_object_type(po) == PROP_TYPE_ARRAY) {
180 pa = po;
181 filters = 1;
182 } else {
183 filters = 0;
186 pae = pdev_array_entry_get_last();
187 if (pae == NULL)
188 return 1;
190 if (filters) {
191 udm = udev_monitor_init(cli, pa);
192 if (udm == NULL) {
193 pdev_array_entry_unref(pae);
194 return 1;
197 pa = prop_array_create_with_capacity(10);
199 iter = prop_array_iterator(pae->pdev_array);
200 if (iter == NULL) {
201 pdev_array_entry_unref(pae);
202 udev_monitor_free(udm);
203 return 1;
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);
214 } else {
215 pa = pae->pdev_array;
218 xml = prop_array_externalize(pa);
219 if (filters)
220 prop_object_release(pa);
222 pdev_array_entry_unref(pae);
224 if (xml == NULL)
225 return 1;
227 r = send_xml(cli->fd, xml);
228 if (r < 0)
229 syslog(LOG_DEBUG, "error while send_xml (cmd_getdevs)\n");
230 if (r == 0)
231 syslog(LOG_DEBUG, "EOF while send_xml (cmd_getdevs)\n");
233 free(xml);
235 return 0;