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