xref: /dragonfly/sbin/udevd/udevd_client.c (revision 655933d6)
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 
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <libgen.h>
47 #include <regex.h>
48 #include <signal.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <syslog.h>
54 #include <unistd.h>
55 #include <pthread.h>
56 #include <assert.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 = pthread_detach(pthread_self());
104 	assert(r == 0);
105 
106 	r = ignore_signal(SIGPIPE);
107 	if (r != 0)
108 		err(1, "could not ignore_signal SIGPIPE");
109 
110 	cli = (struct client_info *)arg;
111 	for (;;) {
112 		n = read_xml(cli->fd, &xml);
113 		if (n == 0)
114 			goto cli_disconnect;
115 		else if (n < 0)
116 			goto error_out;
117 
118 		xml[n+1] = '\0';
119 
120 		dict = prop_dictionary_internalize(xml);
121 		free(xml);
122 
123 		if (dict == NULL) {
124 			syslog(LOG_ERR, "internalization of received XML failed");
125 			goto error_out;
126 		}
127 
128 		po = prop_dictionary_get(dict, "command");
129 		if (po == NULL || prop_object_type(po) != PROP_TYPE_STRING) {
130 			syslog(LOG_ERR, "received dictionary doesn't contain a key 'command'");
131 			prop_object_release(dict);
132 			continue;
133 		}
134 
135 		ps = po;
136 
137 		syslog(LOG_DEBUG, "Received command: %s (from fd = %d)\n", prop_string_cstring_nocopy(ps), cli->fd);
138 		for(n = 0; cmd_fn[n].cmd != NULL; n++) {
139 			if (prop_string_equals_cstring(ps, cmd_fn[n].cmd))
140 				break;
141 		}
142 
143 		if (cmd_fn[n].cmd != NULL) {
144 			error = cmd_fn[n].fn(cli, dict);
145 			if (error) {
146 				prop_object_release(dict);
147 				goto error_out;
148 			}
149 		}
150 		prop_object_release(dict);
151 	}
152 
153 error_out:
154 
155 cli_disconnect:
156 	close(cli->fd);
157 	cli->fd = -1;
158 	free(cli);
159 	return NULL;
160 }
161 
162 int
163 client_cmd_getdevs(struct client_info *cli, prop_dictionary_t cli_dict)
164 {
165 	struct pdev_array_entry *pae;
166 	struct udev_monitor	*udm;
167 	prop_object_iterator_t	iter;
168 	prop_dictionary_t	dict;
169 	prop_object_t	po;
170 	prop_array_t	pa;
171 	char *xml;
172 	ssize_t r;
173 	int filters;
174 
175 
176 	pa = NULL;
177 	po = prop_dictionary_get(cli_dict, "filters");
178 	if ((po != NULL) && prop_object_type(po) == PROP_TYPE_ARRAY) {
179 		pa = po;
180 		filters = 1;
181 	} else {
182 		filters = 0;
183 	}
184 
185 	pae = pdev_array_entry_get_last();
186 	if (pae == NULL)
187 		return 1;
188 
189 	if (filters) {
190 		udm = udev_monitor_init(cli, pa);
191 		if (udm == NULL) {
192 			pdev_array_entry_unref(pae);
193 			return 1;
194 		}
195 
196 		pa = prop_array_create_with_capacity(10);
197 
198 		iter = prop_array_iterator(pae->pdev_array);
199 		if (iter == NULL) {
200 			pdev_array_entry_unref(pae);
201 			udev_monitor_free(udm);
202 			return 1;
203 		}
204 
205 		while ((dict = prop_object_iterator_next(iter)) != NULL) {
206 			if (match_event_filter(udm, dict)) {
207 				prop_array_add(pa, dict);
208 			}
209 		}
210 
211 		prop_object_iterator_release(iter);
212 		udev_monitor_free(udm);
213 	} else {
214 		pa = pae->pdev_array;
215 	}
216 
217 	xml = prop_array_externalize(pa);
218 	if (filters)
219 		prop_object_release(pa);
220 
221 	pdev_array_entry_unref(pae);
222 
223 	if (xml == NULL)
224 		return 1;
225 
226 	r = send_xml(cli->fd, xml);
227 	if (r < 0)
228 		syslog(LOG_DEBUG, "error while send_xml (cmd_getdevs)\n");
229 	if (r == 0)
230 		syslog(LOG_DEBUG, "EOF while send_xml (cmd_getdevs)\n");
231 
232 	free(xml);
233 
234 	return 0;
235 }
236