bthcid.c revision 1.1 1 1.1 gdamore /* $NetBSD: bthcid.c,v 1.1 2006/06/19 15:44:56 gdamore 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.1 gdamore __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc.\n"
34 1.1 gdamore "@(#) Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin (at) yahoo.com>\n"
35 1.1 gdamore "All rights reserved.\n");
36 1.1 gdamore __RCSID("$NetBSD: bthcid.c,v 1.1 2006/06/19 15:44:56 gdamore 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.1 gdamore const char *key_file = "/var/db/bthcid.keys";
53 1.1 gdamore const char *config_file = NULL;
54 1.1 gdamore const char *socket_name = BTHCID_SOCKET_NAME;
55 1.1 gdamore int detach = 1;
56 1.1 gdamore
57 1.1 gdamore static struct event sighup_ev;
58 1.1 gdamore static struct event sigint_ev;
59 1.1 gdamore static struct event sigterm_ev;
60 1.1 gdamore
61 1.1 gdamore static void process_signal(int, short, void *);
62 1.1 gdamore static void usage(void);
63 1.1 gdamore
64 1.1 gdamore int
65 1.1 gdamore main(int argc, char *argv[])
66 1.1 gdamore {
67 1.1 gdamore bdaddr_t bdaddr;
68 1.1 gdamore int ch;
69 1.1 gdamore mode_t mode;
70 1.1 gdamore
71 1.1 gdamore bdaddr_copy(&bdaddr, BDADDR_ANY);
72 1.1 gdamore mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
73 1.1 gdamore
74 1.1 gdamore while ((ch = getopt(argc, argv, "c:d:fm:ns:h")) != -1) {
75 1.1 gdamore switch (ch) {
76 1.1 gdamore case 'c':
77 1.1 gdamore config_file = optarg;
78 1.1 gdamore break;
79 1.1 gdamore
80 1.1 gdamore case 'd':
81 1.1 gdamore if (!bt_devaddr(optarg, &bdaddr))
82 1.1 gdamore err(EXIT_FAILURE, "%s", optarg);
83 1.1 gdamore break;
84 1.1 gdamore
85 1.1 gdamore case 'f':
86 1.1 gdamore detach = 0;
87 1.1 gdamore break;
88 1.1 gdamore
89 1.1 gdamore case 'm':
90 1.1 gdamore mode = atoi(optarg);
91 1.1 gdamore break;
92 1.1 gdamore
93 1.1 gdamore case 'n':
94 1.1 gdamore socket_name = NULL;
95 1.1 gdamore break;
96 1.1 gdamore
97 1.1 gdamore case 's':
98 1.1 gdamore socket_name = optarg;
99 1.1 gdamore break;
100 1.1 gdamore
101 1.1 gdamore case 'h':
102 1.1 gdamore default:
103 1.1 gdamore usage();
104 1.1 gdamore /* NOT REACHED */
105 1.1 gdamore }
106 1.1 gdamore }
107 1.1 gdamore
108 1.1 gdamore if (getuid() != 0)
109 1.1 gdamore errx(EXIT_FAILURE,
110 1.1 gdamore "** ERROR: You should run %s as privileged user!",
111 1.1 gdamore getprogname());
112 1.1 gdamore
113 1.1 gdamore if (detach)
114 1.1 gdamore if (daemon(0, 0) < 0)
115 1.1 gdamore err(EXIT_FAILURE, "Could not daemon()ize");
116 1.1 gdamore
117 1.1 gdamore openlog(getprogname(), LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_DAEMON);
118 1.1 gdamore
119 1.1 gdamore event_init();
120 1.1 gdamore
121 1.1 gdamore signal_set(&sigterm_ev, SIGTERM, process_signal, NULL);
122 1.1 gdamore if (signal_add(&sigterm_ev, NULL) < 0) {
123 1.1 gdamore syslog(LOG_ERR, "signal_add(sigterm_ev)");
124 1.1 gdamore exit(EXIT_FAILURE);
125 1.1 gdamore }
126 1.1 gdamore
127 1.1 gdamore signal_set(&sigint_ev, SIGINT, process_signal, NULL);
128 1.1 gdamore if (signal_add(&sigint_ev, NULL) < 0) {
129 1.1 gdamore syslog(LOG_ERR, "signal_add(sigint_ev)");
130 1.1 gdamore exit(EXIT_FAILURE);
131 1.1 gdamore }
132 1.1 gdamore
133 1.1 gdamore signal_set(&sighup_ev, SIGHUP, process_signal, NULL);
134 1.1 gdamore if (signal_add(&sighup_ev, NULL) < 0) {
135 1.1 gdamore syslog(LOG_ERR, "signal_add(sighup_ev)");
136 1.1 gdamore exit(EXIT_FAILURE);
137 1.1 gdamore }
138 1.1 gdamore
139 1.1 gdamore if (init_hci(&bdaddr) < 0) {
140 1.1 gdamore syslog(LOG_ERR, "init_hci(%s)", bt_ntoa(&bdaddr, NULL));
141 1.1 gdamore exit(EXIT_FAILURE);
142 1.1 gdamore }
143 1.1 gdamore
144 1.1 gdamore if (init_control(socket_name, mode) < 0) {
145 1.1 gdamore syslog(LOG_ERR, "init_control(%s)", socket_name);
146 1.1 gdamore exit(EXIT_FAILURE);
147 1.1 gdamore }
148 1.1 gdamore
149 1.1 gdamore if (detach && pidfile(NULL) < 0) {
150 1.1 gdamore syslog(LOG_ERR, "Could not create PID file. %s (%d)",
151 1.1 gdamore strerror(errno), errno);
152 1.1 gdamore
153 1.1 gdamore exit(EXIT_FAILURE);
154 1.1 gdamore }
155 1.1 gdamore
156 1.1 gdamore event_dispatch();
157 1.1 gdamore
158 1.1 gdamore /* NOTREACHED */
159 1.1 gdamore /* gcc fodder */
160 1.1 gdamore exit(EXIT_FAILURE);
161 1.1 gdamore }
162 1.1 gdamore
163 1.1 gdamore static void
164 1.1 gdamore process_signal(int s, short e, void *arg)
165 1.1 gdamore {
166 1.1 gdamore
167 1.1 gdamore syslog(LOG_DEBUG, "Exiting on signal %d", s);
168 1.1 gdamore
169 1.1 gdamore if (socket_name)
170 1.1 gdamore unlink(socket_name);
171 1.1 gdamore
172 1.1 gdamore closelog();
173 1.1 gdamore exit(EXIT_FAILURE);
174 1.1 gdamore
175 1.1 gdamore }
176 1.1 gdamore
177 1.1 gdamore /* Display usage and exit */
178 1.1 gdamore static void
179 1.1 gdamore usage(void)
180 1.1 gdamore {
181 1.1 gdamore
182 1.1 gdamore fprintf(stderr,
183 1.1 gdamore "Usage: %s [-fhn] [-c config] [-d devaddr] [-m mode] [-s path]\n"
184 1.1 gdamore "Where:\n"
185 1.1 gdamore "\t-c config specify config filename\n"
186 1.1 gdamore "\t-d device specify device address\n"
187 1.1 gdamore "\t-f run in foreground\n"
188 1.1 gdamore "\t-m mode specify socket permissions\n"
189 1.1 gdamore "\t-n do not listen for clients\n"
190 1.1 gdamore "\t-s path specify client socket pathname\n"
191 1.1 gdamore "\t-h display this message\n",
192 1.1 gdamore getprogname());
193 1.1 gdamore
194 1.1 gdamore exit(EXIT_FAILURE);
195 1.1 gdamore }
196