6 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
8 * Copyright (c) 2006 Maksim Yevmenkin <m_evmenkin@yahoo.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
32 * $Id: bthidd.c,v 1.8 2006/09/07 21:06:53 max Exp $
37 #include <sys/queue.h>
39 #define L2CAP_SOCKET_CHECKED
40 #include <bluetooth.h>
50 #include "bthid_config.h"
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);
63 static int32_t done = 0; /* are we done? */
66 main(int32_t argc, char *argv[])
68 struct bthid_server srv;
70 char const *pid_file = BTHIDD_PIDFILE;
72 int32_t opt, detach, tval;
74 memset(&srv, 0, sizeof(srv));
75 memset(&srv.bdaddr, 0, sizeof(srv.bdaddr));
79 while ((opt = getopt(argc, argv, "a:c:dH:hp:t:")) != -1) {
81 case 'a': /* BDADDR */
82 if (!bt_aton(optarg, &srv.bdaddr)) {
85 if ((he = bt_gethostbyname(optarg)) == NULL)
86 errx(1, "%s: %s", optarg, hstrerror(h_errno));
88 memcpy(&srv.bdaddr, he->h_addr, sizeof(srv.bdaddr));
92 case 'c': /* config file */
96 case 'd': /* do not detach */
100 case 'H': /* hids file */
104 case 'p': /* pid file */
108 case 't': /* rescan interval */
109 tval = strtol(optarg, (char **) &ep, 10);
110 if (*ep != '\0' || tval <= 0)
121 openlog(BTHIDD_IDENT, LOG_PID|LOG_PERROR|LOG_NDELAY, LOG_USER);
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);
130 /* Install signal handler */
131 memset(&sa, 0, sizeof(sa));
132 sa.sa_handler = sighandler;
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);
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);
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);
157 if (read_config_file() < 0 || read_hids_file() < 0 ||
158 server_init(&srv) < 0 || write_pid_file(pid_file) < 0)
161 for (done = 0; !done; ) {
165 if (server_do(&srv) < 0)
169 server_shutdown(&srv);
170 remove_pid_file(pid_file);
182 write_pid_file(char const *file)
186 assert(file != NULL);
188 if ((pid = fopen(file, "w")) == NULL) {
189 syslog(LOG_ERR, "Could not open file %s. %s (%d)",
190 file, strerror(errno), errno);
194 fprintf(pid, "%d", getpid());
205 remove_pid_file(char const *file)
207 assert(file != NULL);
209 if (unlink(file) < 0) {
210 syslog(LOG_ERR, "Could not unlink file %s. %s (%d)",
211 file, strerror(errno), errno);
219 * Returns true if desired time interval has elapsed
223 elapsed(int32_t tval)
225 static struct timeval last = { 0, 0 };
228 gettimeofday(&now, NULL);
230 if (now.tv_sec - last.tv_sec >= tval) {
243 sighandler(int32_t s)
245 syslog(LOG_NOTICE, "Got signal %d, total number of signals %d",
250 * Display usage and exit
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" \