2009-10-05 16:34:26 +04:00
|
|
|
/* $NetBSD: bthcid.c,v 1.5 2009/10/05 12:34:26 plunky Exp $ */
|
2006-06-19 19:44:33 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2006 Itronix Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of Itronix Inc. may not be used to endorse
|
|
|
|
* or promote products derived from this software without specific
|
|
|
|
* prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2008-07-21 17:36:57 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc.\
|
|
|
|
Copyright (c) 2001-2002 Maksim Yevmenkin m_evmenkin@yahoo.com.\
|
|
|
|
All rights reserved.");
|
2009-10-05 16:34:26 +04:00
|
|
|
__RCSID("$NetBSD: bthcid.c,v 1.5 2009/10/05 12:34:26 plunky Exp $");
|
2006-06-19 19:44:33 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <bluetooth.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <event.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <util.h>
|
|
|
|
|
|
|
|
#include "bthcid.h"
|
|
|
|
|
|
|
|
const char *socket_name = BTHCID_SOCKET_NAME;
|
|
|
|
int detach = 1;
|
|
|
|
|
|
|
|
static struct event sighup_ev;
|
|
|
|
static struct event sigint_ev;
|
|
|
|
static struct event sigterm_ev;
|
|
|
|
|
|
|
|
static void process_signal(int, short, void *);
|
|
|
|
static void usage(void);
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2009-10-05 16:34:26 +04:00
|
|
|
const char *device;
|
2006-06-19 19:44:33 +04:00
|
|
|
int ch;
|
|
|
|
mode_t mode;
|
|
|
|
|
2009-10-05 16:34:26 +04:00
|
|
|
device = NULL;
|
2006-06-19 19:44:33 +04:00
|
|
|
mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
|
|
|
|
|
2006-07-26 15:00:07 +04:00
|
|
|
while ((ch = getopt(argc, argv, "d:fm:ns:h")) != -1) {
|
2006-06-19 19:44:33 +04:00
|
|
|
switch (ch) {
|
|
|
|
case 'd':
|
2009-10-05 16:34:26 +04:00
|
|
|
device = optarg;
|
2006-06-19 19:44:33 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
detach = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
mode = atoi(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
socket_name = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
socket_name = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOT REACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getuid() != 0)
|
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"** ERROR: You should run %s as privileged user!",
|
|
|
|
getprogname());
|
|
|
|
|
|
|
|
if (detach)
|
|
|
|
if (daemon(0, 0) < 0)
|
|
|
|
err(EXIT_FAILURE, "Could not daemon()ize");
|
|
|
|
|
|
|
|
openlog(getprogname(), LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_DAEMON);
|
|
|
|
|
|
|
|
event_init();
|
|
|
|
|
|
|
|
signal_set(&sigterm_ev, SIGTERM, process_signal, NULL);
|
|
|
|
if (signal_add(&sigterm_ev, NULL) < 0) {
|
|
|
|
syslog(LOG_ERR, "signal_add(sigterm_ev)");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
signal_set(&sigint_ev, SIGINT, process_signal, NULL);
|
|
|
|
if (signal_add(&sigint_ev, NULL) < 0) {
|
|
|
|
syslog(LOG_ERR, "signal_add(sigint_ev)");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
signal_set(&sighup_ev, SIGHUP, process_signal, NULL);
|
|
|
|
if (signal_add(&sighup_ev, NULL) < 0) {
|
|
|
|
syslog(LOG_ERR, "signal_add(sighup_ev)");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2009-10-05 16:34:26 +04:00
|
|
|
if (init_hci(device) < 0) {
|
|
|
|
syslog(LOG_ERR, "init_hci(%s)", device);
|
2006-06-19 19:44:33 +04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (init_control(socket_name, mode) < 0) {
|
|
|
|
syslog(LOG_ERR, "init_control(%s)", socket_name);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (detach && pidfile(NULL) < 0) {
|
2007-01-25 23:33:41 +03:00
|
|
|
syslog(LOG_ERR, "Could not create PID file: %m");
|
2006-06-19 19:44:33 +04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
event_dispatch();
|
|
|
|
|
|
|
|
/* NOTREACHED */
|
|
|
|
/* gcc fodder */
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
process_signal(int s, short e, void *arg)
|
|
|
|
{
|
|
|
|
|
|
|
|
syslog(LOG_DEBUG, "Exiting on signal %d", s);
|
|
|
|
|
|
|
|
if (socket_name)
|
|
|
|
unlink(socket_name);
|
|
|
|
|
|
|
|
closelog();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Display usage and exit */
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
fprintf(stderr,
|
2009-10-05 16:34:26 +04:00
|
|
|
"Usage: %s [-fhn] [-c config] [-d device] [-m mode] [-s path]\n"
|
2006-06-19 19:44:33 +04:00
|
|
|
"Where:\n"
|
|
|
|
"\t-c config specify config filename\n"
|
|
|
|
"\t-d device specify device address\n"
|
|
|
|
"\t-f run in foreground\n"
|
|
|
|
"\t-m mode specify socket permissions\n"
|
|
|
|
"\t-n do not listen for clients\n"
|
|
|
|
"\t-s path specify client socket pathname\n"
|
|
|
|
"\t-h display this message\n",
|
|
|
|
getprogname());
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|