xref: /freebsd/usr.sbin/bluetooth/bthidd/bthidd.c (revision 63a938566d524836885917d95bd491aa4400b181)
1 /*
2  * bthidd.c
3  */
4 
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
7  *
8  * Copyright (c) 2006 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id: bthidd.c,v 1.8 2006/09/07 21:06:53 max Exp $
33  * $FreeBSD$
34  */
35 
36 #include <sys/time.h>
37 #include <sys/queue.h>
38 #include <assert.h>
39 #define L2CAP_SOCKET_CHECKED
40 #include <bluetooth.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <syslog.h>
48 #include <unistd.h>
49 #include <usbhid.h>
50 #include "bthid_config.h"
51 #include "bthidd.h"
52 
53 static int32_t	write_pid_file	(char const *file);
54 static int32_t	remove_pid_file	(char const *file);
55 static int32_t	elapsed		(int32_t tval);
56 static void	sighandler	(int32_t s);
57 static void	usage		(void);
58 
59 /*
60  * bthidd
61  */
62 
63 static int32_t	done = 0; /* are we done? */
64 
65 int32_t
66 main(int32_t argc, char *argv[])
67 {
68 	struct bthid_server	 srv;
69 	struct sigaction	 sa;
70 	char const		*pid_file = BTHIDD_PIDFILE;
71 	char			*ep;
72 	int32_t			 opt, detach, tval;
73 
74 	memset(&srv, 0, sizeof(srv));
75 	memset(&srv.bdaddr, 0, sizeof(srv.bdaddr));
76 	detach = 1;
77 	tval = 10; /* sec */
78 
79 	while ((opt = getopt(argc, argv, "a:c:dH:hp:t:")) != -1) {
80 		switch (opt) {
81 		case 'a': /* BDADDR */
82 			if (!bt_aton(optarg, &srv.bdaddr)) {
83 				struct hostent  *he;
84 
85 				if ((he = bt_gethostbyname(optarg)) == NULL)
86 					errx(1, "%s: %s", optarg, hstrerror(h_errno));
87 
88 				memcpy(&srv.bdaddr, he->h_addr, sizeof(srv.bdaddr));
89 			}
90 			break;
91 
92 		case 'c': /* config file */
93 			config_file = optarg;
94 			break;
95 
96 		case 'd': /* do not detach */
97 			detach = 0;
98 			break;
99 
100 		case 'H': /* hids file */
101 			hids_file = optarg;
102 			break;
103 
104 		case 'p': /* pid file */
105 			pid_file = optarg;
106 			break;
107 
108 		case 't': /* rescan interval */
109 			tval = strtol(optarg, (char **) &ep, 10);
110 			if (*ep != '\0' || tval <= 0)
111 				usage();
112 			break;
113 
114 		case 'h':
115 		default:
116 			usage();
117 			/* NOT REACHED */
118 		}
119 	}
120 
121 	openlog(BTHIDD_IDENT, LOG_PID|LOG_PERROR|LOG_NDELAY, LOG_USER);
122 
123 	/* Become daemon if required */
124 	if (detach && daemon(0, 0) < 0) {
125 		syslog(LOG_CRIT, "Could not become daemon. %s (%d)",
126 			strerror(errno), errno);
127 		exit(1);
128 	}
129 
130 	/* Install signal handler */
131 	memset(&sa, 0, sizeof(sa));
132 	sa.sa_handler = sighandler;
133 
134 	if (sigaction(SIGTERM, &sa, NULL) < 0 ||
135 	    sigaction(SIGHUP, &sa, NULL) < 0 ||
136 	    sigaction(SIGINT, &sa, NULL) < 0) {
137 		syslog(LOG_CRIT, "Could not install signal handlers. %s (%d)",
138 			strerror(errno), errno);
139 		exit(1);
140 	}
141 
142 	sa.sa_handler = SIG_IGN;
143 	if (sigaction(SIGPIPE, &sa, NULL) < 0) {
144 		syslog(LOG_CRIT, "Could not install signal handlers. %s (%d)",
145 			strerror(errno), errno);
146 		exit(1);
147 	}
148 
149 	sa.sa_handler = SIG_IGN;
150 	sa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT;
151 	if (sigaction(SIGCHLD, &sa, NULL) < 0) {
152 		syslog(LOG_CRIT, "Could not install signal handlers. %s (%d)",
153 			strerror(errno), errno);
154 		exit(1);
155 	}
156 
157 	if (read_config_file() < 0 || read_hids_file() < 0 ||
158 	    server_init(&srv) < 0 || write_pid_file(pid_file) < 0)
159 		exit(1);
160 
161 	for (done = 0; !done; ) {
162 		if (elapsed(tval))
163 			client_rescan(&srv);
164 
165 		if (server_do(&srv) < 0)
166 			break;
167 	}
168 
169 	server_shutdown(&srv);
170 	remove_pid_file(pid_file);
171 	clean_config();
172 	closelog();
173 
174 	return (0);
175 }
176 
177 /*
178  * Write pid file
179  */
180 
181 static int32_t
182 write_pid_file(char const *file)
183 {
184 	FILE	*pid;
185 
186 	assert(file != NULL);
187 
188 	if ((pid = fopen(file, "w")) == NULL) {
189 		syslog(LOG_ERR, "Could not open file %s. %s (%d)",
190 			file, strerror(errno), errno);
191 		return (-1);
192 	}
193 
194 	fprintf(pid, "%d", getpid());
195 	fclose(pid);
196 
197 	return (0);
198 }
199 
200 /*
201  * Remote pid file
202  */
203 
204 static int32_t
205 remove_pid_file(char const *file)
206 {
207 	assert(file != NULL);
208 
209 	if (unlink(file) < 0) {
210 		syslog(LOG_ERR, "Could not unlink file %s. %s (%d)",
211 			file, strerror(errno), errno);
212 		return (-1);
213 	}
214 
215 	return (0);
216 }
217 
218 /*
219  * Returns true if desired time interval has elapsed
220  */
221 
222 static int32_t
223 elapsed(int32_t tval)
224 {
225 	static struct timeval	last = { 0, 0 };
226 	struct timeval		now;
227 
228 	gettimeofday(&now, NULL);
229 
230 	if (now.tv_sec - last.tv_sec >= tval) {
231 		last = now;
232 		return (1);
233 	}
234 
235 	return (0);
236 }
237 
238 /*
239  * Signal handler
240  */
241 
242 static void
243 sighandler(int32_t s)
244 {
245 	syslog(LOG_NOTICE, "Got signal %d, total number of signals %d",
246 		s, ++ done);
247 }
248 
249 /*
250  * Display usage and exit
251  */
252 
253 static void
254 usage(void)
255 {
256 	fprintf(stderr,
257 "Usage: %s [options]\n" \
258 "Where options are:\n" \
259 "	-a address	specify address to listen on (default ANY)\n" \
260 "	-c file		specify config file name\n" \
261 "	-d		run in foreground\n" \
262 "	-H file		specify known HIDs file name\n" \
263 "	-h		display this message\n" \
264 "	-p file		specify PID file name\n" \
265 "	-t tval		specify client rescan interval (sec)\n" \
266 "", BTHIDD_IDENT);
267 	exit(255);
268 }
269 
270