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