1 1.6 joerg /* $NetBSD: bthcid.c,v 1.6 2011/08/27 22:26:05 joerg Exp $ */ 2 1.1 gdamore 3 1.1 gdamore /*- 4 1.1 gdamore * Copyright (c) 2006 Itronix Inc. 5 1.1 gdamore * All rights reserved. 6 1.1 gdamore * 7 1.1 gdamore * Redistribution and use in source and binary forms, with or without 8 1.1 gdamore * modification, are permitted provided that the following conditions 9 1.1 gdamore * are met: 10 1.1 gdamore * 1. Redistributions of source code must retain the above copyright 11 1.1 gdamore * notice, this list of conditions and the following disclaimer. 12 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 gdamore * notice, this list of conditions and the following disclaimer in the 14 1.1 gdamore * documentation and/or other materials provided with the distribution. 15 1.1 gdamore * 3. The name of Itronix Inc. may not be used to endorse 16 1.1 gdamore * or promote products derived from this software without specific 17 1.1 gdamore * prior written permission. 18 1.1 gdamore * 19 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND 20 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 gdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 gdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY 23 1.1 gdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 1.1 gdamore * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 1.1 gdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 1.1 gdamore * ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 gdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 gdamore * POSSIBILITY OF SUCH DAMAGE. 30 1.1 gdamore */ 31 1.1 gdamore 32 1.1 gdamore #include <sys/cdefs.h> 33 1.4 lukem __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc.\ 34 1.4 lukem Copyright (c) 2001-2002 Maksim Yevmenkin m_evmenkin (at) yahoo.com.\ 35 1.4 lukem All rights reserved."); 36 1.6 joerg __RCSID("$NetBSD: bthcid.c,v 1.6 2011/08/27 22:26:05 joerg Exp $"); 37 1.1 gdamore 38 1.1 gdamore #include <sys/param.h> 39 1.1 gdamore #include <sys/stat.h> 40 1.1 gdamore #include <bluetooth.h> 41 1.1 gdamore #include <err.h> 42 1.1 gdamore #include <errno.h> 43 1.1 gdamore #include <event.h> 44 1.1 gdamore #include <stdlib.h> 45 1.1 gdamore #include <string.h> 46 1.1 gdamore #include <syslog.h> 47 1.1 gdamore #include <unistd.h> 48 1.1 gdamore #include <util.h> 49 1.1 gdamore 50 1.1 gdamore #include "bthcid.h" 51 1.1 gdamore 52 1.6 joerg static const char *socket_name = BTHCID_SOCKET_NAME; 53 1.6 joerg static int detach = 1; 54 1.1 gdamore 55 1.1 gdamore static struct event sighup_ev; 56 1.1 gdamore static struct event sigint_ev; 57 1.1 gdamore static struct event sigterm_ev; 58 1.1 gdamore 59 1.6 joerg __dead static void process_signal(int, short, void *); 60 1.6 joerg __dead static void usage(void); 61 1.1 gdamore 62 1.1 gdamore int 63 1.1 gdamore main(int argc, char *argv[]) 64 1.1 gdamore { 65 1.5 plunky const char *device; 66 1.1 gdamore int ch; 67 1.1 gdamore mode_t mode; 68 1.1 gdamore 69 1.5 plunky device = NULL; 70 1.1 gdamore mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; 71 1.1 gdamore 72 1.2 tron while ((ch = getopt(argc, argv, "d:fm:ns:h")) != -1) { 73 1.1 gdamore switch (ch) { 74 1.1 gdamore case 'd': 75 1.5 plunky device = optarg; 76 1.1 gdamore break; 77 1.1 gdamore 78 1.1 gdamore case 'f': 79 1.1 gdamore detach = 0; 80 1.1 gdamore break; 81 1.1 gdamore 82 1.1 gdamore case 'm': 83 1.1 gdamore mode = atoi(optarg); 84 1.1 gdamore break; 85 1.1 gdamore 86 1.1 gdamore case 'n': 87 1.1 gdamore socket_name = NULL; 88 1.1 gdamore break; 89 1.1 gdamore 90 1.1 gdamore case 's': 91 1.1 gdamore socket_name = optarg; 92 1.1 gdamore break; 93 1.1 gdamore 94 1.1 gdamore case 'h': 95 1.1 gdamore default: 96 1.1 gdamore usage(); 97 1.1 gdamore /* NOT REACHED */ 98 1.1 gdamore } 99 1.1 gdamore } 100 1.1 gdamore 101 1.1 gdamore if (getuid() != 0) 102 1.1 gdamore errx(EXIT_FAILURE, 103 1.1 gdamore "** ERROR: You should run %s as privileged user!", 104 1.1 gdamore getprogname()); 105 1.1 gdamore 106 1.1 gdamore if (detach) 107 1.1 gdamore if (daemon(0, 0) < 0) 108 1.1 gdamore err(EXIT_FAILURE, "Could not daemon()ize"); 109 1.1 gdamore 110 1.1 gdamore openlog(getprogname(), LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_DAEMON); 111 1.1 gdamore 112 1.1 gdamore event_init(); 113 1.1 gdamore 114 1.1 gdamore signal_set(&sigterm_ev, SIGTERM, process_signal, NULL); 115 1.1 gdamore if (signal_add(&sigterm_ev, NULL) < 0) { 116 1.1 gdamore syslog(LOG_ERR, "signal_add(sigterm_ev)"); 117 1.1 gdamore exit(EXIT_FAILURE); 118 1.1 gdamore } 119 1.1 gdamore 120 1.1 gdamore signal_set(&sigint_ev, SIGINT, process_signal, NULL); 121 1.1 gdamore if (signal_add(&sigint_ev, NULL) < 0) { 122 1.1 gdamore syslog(LOG_ERR, "signal_add(sigint_ev)"); 123 1.1 gdamore exit(EXIT_FAILURE); 124 1.1 gdamore } 125 1.1 gdamore 126 1.1 gdamore signal_set(&sighup_ev, SIGHUP, process_signal, NULL); 127 1.1 gdamore if (signal_add(&sighup_ev, NULL) < 0) { 128 1.1 gdamore syslog(LOG_ERR, "signal_add(sighup_ev)"); 129 1.1 gdamore exit(EXIT_FAILURE); 130 1.1 gdamore } 131 1.1 gdamore 132 1.5 plunky if (init_hci(device) < 0) { 133 1.5 plunky syslog(LOG_ERR, "init_hci(%s)", device); 134 1.1 gdamore exit(EXIT_FAILURE); 135 1.1 gdamore } 136 1.1 gdamore 137 1.1 gdamore if (init_control(socket_name, mode) < 0) { 138 1.1 gdamore syslog(LOG_ERR, "init_control(%s)", socket_name); 139 1.1 gdamore exit(EXIT_FAILURE); 140 1.1 gdamore } 141 1.1 gdamore 142 1.1 gdamore if (detach && pidfile(NULL) < 0) { 143 1.3 plunky syslog(LOG_ERR, "Could not create PID file: %m"); 144 1.1 gdamore exit(EXIT_FAILURE); 145 1.1 gdamore } 146 1.1 gdamore 147 1.1 gdamore event_dispatch(); 148 1.1 gdamore 149 1.1 gdamore /* NOTREACHED */ 150 1.1 gdamore /* gcc fodder */ 151 1.1 gdamore exit(EXIT_FAILURE); 152 1.1 gdamore } 153 1.1 gdamore 154 1.1 gdamore static void 155 1.1 gdamore process_signal(int s, short e, void *arg) 156 1.1 gdamore { 157 1.1 gdamore 158 1.1 gdamore syslog(LOG_DEBUG, "Exiting on signal %d", s); 159 1.1 gdamore 160 1.1 gdamore if (socket_name) 161 1.1 gdamore unlink(socket_name); 162 1.1 gdamore 163 1.1 gdamore closelog(); 164 1.1 gdamore exit(EXIT_FAILURE); 165 1.1 gdamore 166 1.1 gdamore } 167 1.1 gdamore 168 1.1 gdamore /* Display usage and exit */ 169 1.1 gdamore static void 170 1.1 gdamore usage(void) 171 1.1 gdamore { 172 1.1 gdamore 173 1.1 gdamore fprintf(stderr, 174 1.5 plunky "Usage: %s [-fhn] [-c config] [-d device] [-m mode] [-s path]\n" 175 1.1 gdamore "Where:\n" 176 1.1 gdamore "\t-c config specify config filename\n" 177 1.1 gdamore "\t-d device specify device address\n" 178 1.1 gdamore "\t-f run in foreground\n" 179 1.1 gdamore "\t-m mode specify socket permissions\n" 180 1.1 gdamore "\t-n do not listen for clients\n" 181 1.1 gdamore "\t-s path specify client socket pathname\n" 182 1.1 gdamore "\t-h display this message\n", 183 1.1 gdamore getprogname()); 184 1.1 gdamore 185 1.1 gdamore exit(EXIT_FAILURE); 186 1.1 gdamore } 187