bthcid.c revision 1.2 1 1.2 tron /* $NetBSD: bthcid.c,v 1.2 2006/07/26 11:00:07 tron 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.2 tron __RCSID("$NetBSD: bthcid.c,v 1.2 2006/07/26 11:00:07 tron 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 *socket_name = BTHCID_SOCKET_NAME;
53 1.1 gdamore 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.1 gdamore static void process_signal(int, short, void *);
60 1.1 gdamore 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.1 gdamore bdaddr_t bdaddr;
66 1.1 gdamore int ch;
67 1.1 gdamore mode_t mode;
68 1.1 gdamore
69 1.1 gdamore bdaddr_copy(&bdaddr, BDADDR_ANY);
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.1 gdamore if (!bt_devaddr(optarg, &bdaddr))
76 1.1 gdamore err(EXIT_FAILURE, "%s", optarg);
77 1.1 gdamore break;
78 1.1 gdamore
79 1.1 gdamore case 'f':
80 1.1 gdamore detach = 0;
81 1.1 gdamore break;
82 1.1 gdamore
83 1.1 gdamore case 'm':
84 1.1 gdamore mode = atoi(optarg);
85 1.1 gdamore break;
86 1.1 gdamore
87 1.1 gdamore case 'n':
88 1.1 gdamore socket_name = NULL;
89 1.1 gdamore break;
90 1.1 gdamore
91 1.1 gdamore case 's':
92 1.1 gdamore socket_name = optarg;
93 1.1 gdamore break;
94 1.1 gdamore
95 1.1 gdamore case 'h':
96 1.1 gdamore default:
97 1.1 gdamore usage();
98 1.1 gdamore /* NOT REACHED */
99 1.1 gdamore }
100 1.1 gdamore }
101 1.1 gdamore
102 1.1 gdamore if (getuid() != 0)
103 1.1 gdamore errx(EXIT_FAILURE,
104 1.1 gdamore "** ERROR: You should run %s as privileged user!",
105 1.1 gdamore getprogname());
106 1.1 gdamore
107 1.1 gdamore if (detach)
108 1.1 gdamore if (daemon(0, 0) < 0)
109 1.1 gdamore err(EXIT_FAILURE, "Could not daemon()ize");
110 1.1 gdamore
111 1.1 gdamore openlog(getprogname(), LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_DAEMON);
112 1.1 gdamore
113 1.1 gdamore event_init();
114 1.1 gdamore
115 1.1 gdamore signal_set(&sigterm_ev, SIGTERM, process_signal, NULL);
116 1.1 gdamore if (signal_add(&sigterm_ev, NULL) < 0) {
117 1.1 gdamore syslog(LOG_ERR, "signal_add(sigterm_ev)");
118 1.1 gdamore exit(EXIT_FAILURE);
119 1.1 gdamore }
120 1.1 gdamore
121 1.1 gdamore signal_set(&sigint_ev, SIGINT, process_signal, NULL);
122 1.1 gdamore if (signal_add(&sigint_ev, NULL) < 0) {
123 1.1 gdamore syslog(LOG_ERR, "signal_add(sigint_ev)");
124 1.1 gdamore exit(EXIT_FAILURE);
125 1.1 gdamore }
126 1.1 gdamore
127 1.1 gdamore signal_set(&sighup_ev, SIGHUP, process_signal, NULL);
128 1.1 gdamore if (signal_add(&sighup_ev, NULL) < 0) {
129 1.1 gdamore syslog(LOG_ERR, "signal_add(sighup_ev)");
130 1.1 gdamore exit(EXIT_FAILURE);
131 1.1 gdamore }
132 1.1 gdamore
133 1.1 gdamore if (init_hci(&bdaddr) < 0) {
134 1.1 gdamore syslog(LOG_ERR, "init_hci(%s)", bt_ntoa(&bdaddr, NULL));
135 1.1 gdamore exit(EXIT_FAILURE);
136 1.1 gdamore }
137 1.1 gdamore
138 1.1 gdamore if (init_control(socket_name, mode) < 0) {
139 1.1 gdamore syslog(LOG_ERR, "init_control(%s)", socket_name);
140 1.1 gdamore exit(EXIT_FAILURE);
141 1.1 gdamore }
142 1.1 gdamore
143 1.1 gdamore if (detach && pidfile(NULL) < 0) {
144 1.1 gdamore syslog(LOG_ERR, "Could not create PID file. %s (%d)",
145 1.1 gdamore strerror(errno), errno);
146 1.1 gdamore
147 1.1 gdamore exit(EXIT_FAILURE);
148 1.1 gdamore }
149 1.1 gdamore
150 1.1 gdamore event_dispatch();
151 1.1 gdamore
152 1.1 gdamore /* NOTREACHED */
153 1.1 gdamore /* gcc fodder */
154 1.1 gdamore exit(EXIT_FAILURE);
155 1.1 gdamore }
156 1.1 gdamore
157 1.1 gdamore static void
158 1.1 gdamore process_signal(int s, short e, void *arg)
159 1.1 gdamore {
160 1.1 gdamore
161 1.1 gdamore syslog(LOG_DEBUG, "Exiting on signal %d", s);
162 1.1 gdamore
163 1.1 gdamore if (socket_name)
164 1.1 gdamore unlink(socket_name);
165 1.1 gdamore
166 1.1 gdamore closelog();
167 1.1 gdamore exit(EXIT_FAILURE);
168 1.1 gdamore
169 1.1 gdamore }
170 1.1 gdamore
171 1.1 gdamore /* Display usage and exit */
172 1.1 gdamore static void
173 1.1 gdamore usage(void)
174 1.1 gdamore {
175 1.1 gdamore
176 1.1 gdamore fprintf(stderr,
177 1.1 gdamore "Usage: %s [-fhn] [-c config] [-d devaddr] [-m mode] [-s path]\n"
178 1.1 gdamore "Where:\n"
179 1.1 gdamore "\t-c config specify config filename\n"
180 1.1 gdamore "\t-d device specify device address\n"
181 1.1 gdamore "\t-f run in foreground\n"
182 1.1 gdamore "\t-m mode specify socket permissions\n"
183 1.1 gdamore "\t-n do not listen for clients\n"
184 1.1 gdamore "\t-s path specify client socket pathname\n"
185 1.1 gdamore "\t-h display this message\n",
186 1.1 gdamore getprogname());
187 1.1 gdamore
188 1.1 gdamore exit(EXIT_FAILURE);
189 1.1 gdamore }
190