xref: /dflybsd-src/sbin/udevd/udevd_client.c (revision 2e7bf158f373428dba2c765c927f14d9e94f00a4)
1 /*
2  * Copyright (c) 2010 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Alex Hornung <ahornung@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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.
20  *
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.
33  */
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>
43 
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 
58 #include <libprop/proplib.h>
59 #include <sys/udev.h>
60 #include "udevd.h"
61 
62 struct cmd_function cmd_fn[] = {
63 		{ .cmd = "getdevs", .fn = client_cmd_getdevs},
64 		{ .cmd = "monitor", .fn = client_cmd_monitor},
65 		{NULL, NULL}
66 };
67 
68 static void *client_thread(void *arg);
69 
70 void
71 handle_new_connection(int s)
72 {
73 	struct client_info *cli_info;
74 	struct sockaddr_un addr;
75 	int fd;
76 	socklen_t saddr_len = sizeof(struct sockaddr_un);
77 
78 	fd = accept(s, (struct sockaddr *)&addr, &saddr_len);
79 	if (fd < 0) {
80 		syslog(LOG_ERR, "uh, oh, accept failed with %d", errno);
81 		return;
82 	}
83 
84 	block_descriptor(fd);
85 	cli_info = malloc(sizeof(struct client_info));
86 	memset(cli_info, 0, sizeof(struct client_info));
87 
88 	cli_info->fd = fd;
89 	pthread_create(&cli_info->tid, NULL, client_thread, (void *)cli_info);
90 }
91 
92 
93 static void *
94 client_thread(void *arg)
95 {
96 	prop_dictionary_t	dict;
97 	prop_string_t		ps;
98 	prop_object_t		po;
99 	struct client_info	*cli;
100 	char	*xml;
101 	int	r, n, error;
102 
103 	r = ignore_signal(SIGPIPE);
104 	if (r != 0)
105 		err(1, "could not ignore_signal SIGPIPE");
106 
107 	cli = (struct client_info *)arg;
108 	xml = malloc(12*1024*1024); /* generous 12 MB */
109 	for (;;) {
110 		n = read_xml(cli->fd, xml, 12*1024*1024);
111 		if (n == 0)
112 			goto cli_disconnect;
113 		else if (n < 0)
114 			goto error_out;
115 
116 		xml[n+1] = '\0';
117 
118 		dict = prop_dictionary_internalize(xml);
119 		if (dict == NULL) {
120 			syslog(LOG_ERR, "internalization of received XML failed");
121 			goto error_out;
122 		}
123 
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);
128 			continue;
129 		}
130 
131 		ps = po;
132 
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))
136 				break;
137 		}
138 
139 		if (cmd_fn[n].cmd != NULL) {
140 			error = cmd_fn[n].fn(cli, dict);
141 			if (error) {
142 				prop_object_release(dict);
143 				goto error_out;
144 			}
145 		}
146 		prop_object_release(dict);
147 	}
148 
149 error_out:
150 
151 cli_disconnect:
152 	close(cli->fd);
153 	free(xml);
154 	cli->fd = -1;
155 	free(cli);
156 	return NULL;
157 }
158 
159 int
160 client_cmd_getdevs(struct client_info *cli, prop_dictionary_t cli_dict)
161 {
162 	struct pdev_array_entry *pae;
163 	struct udev_monitor	*udm;
164 	prop_object_iterator_t	iter;
165 	prop_dictionary_t	dict;
166 	prop_object_t	po;
167 	prop_array_t	pa;
168 	char *xml;
169 	ssize_t r;
170 	int filters;
171 
172 
173 	pa = NULL;
174 	po = prop_dictionary_get(cli_dict, "filters");
175 	if ((po != NULL) && prop_object_type(po) == PROP_TYPE_ARRAY) {
176 		pa = po;
177 		filters = 1;
178 	} else {
179 		filters = 0;
180 	}
181 
182 	pae = pdev_array_entry_get_last();
183 	if (pae == NULL)
184 		return 1;
185 
186 	if (filters) {
187 		udm = udev_monitor_init(cli, pa);
188 		if (udm == NULL) {
189 			pdev_array_entry_unref(pae);
190 			return 1;
191 		}
192 
193 		pa = prop_array_create_with_capacity(10);
194 
195 		iter = prop_array_iterator(pae->pdev_array);
196 		if (iter == NULL) {
197 			pdev_array_entry_unref(pae);
198 			udev_monitor_free(udm);
199 			return 1;
200 		}
201 
202 		while ((dict = prop_object_iterator_next(iter)) != NULL) {
203 			if (match_event_filter(udm, dict)) {
204 				prop_array_add(pa, dict);
205 			}
206 		}
207 
208 		udev_monitor_free(udm);
209 	} else {
210 		pa = pae->pdev_array;
211 	}
212 
213 	xml = prop_array_externalize(pa);
214 	if (filters)
215 		prop_object_release(pa);
216 
217 	pdev_array_entry_unref(pae);
218 
219 	if (xml == NULL)
220 		return 1;
221 
222 	r = send_xml(cli->fd, xml);
223 	if (r < 0)
224 		syslog(LOG_DEBUG, "error while send_xml (cmd_getdevs)\n");
225 	if (r == 0)
226 		syslog(LOG_DEBUG, "EOF while send_xml (cmd_getdevs)\n");
227 
228 	free(xml);
229 
230 	return 0;
231 }
232