4 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $Id: main.c,v 1.8 2004/01/13 19:31:54 max Exp $
32 #include <sys/select.h>
33 #define L2CAP_SOCKET_CHECKED
34 #include <bluetooth.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <sys/queue.h>
55 static int32_t drop_root (char const *user, char const *group);
56 static void sighandler (int32_t s);
57 static void usage (void);
62 * Bluetooth Service Discovery Procotol (SDP) daemon
66 main(int argc, char *argv[])
69 char const *control = SDP_LOCAL_PATH;
70 char const *user = "nobody", *group = "nobody";
71 int32_t detach = 1, opt;
74 while ((opt = getopt(argc, argv, "c:dg:hu:")) != -1) {
76 case 'c': /* control */
80 case 'd': /* do not detach */
99 log_open(SDPD, !detach);
101 /* Become daemon if required */
102 if (detach && daemon(0, 0) < 0) {
103 log_crit("Could not become daemon. %s (%d)",
104 strerror(errno), errno);
108 /* Set signal handlers */
109 memset(&sa, 0, sizeof(sa));
110 sa.sa_handler = sighandler;
112 if (sigaction(SIGTERM, &sa, NULL) < 0 ||
113 sigaction(SIGHUP, &sa, NULL) < 0 ||
114 sigaction(SIGINT, &sa, NULL) < 0) {
115 log_crit("Could not install signal handlers. %s (%d)",
116 strerror(errno), errno);
120 sa.sa_handler = SIG_IGN;
121 if (sigaction(SIGPIPE, &sa, NULL) < 0) {
122 log_crit("Could not install signal handlers. %s (%d)",
123 strerror(errno), errno);
127 /* Initialize server */
128 if (server_init(&server, control) < 0)
131 if ((user != NULL || group != NULL) && drop_root(user, group) < 0)
134 for (done = 0; !done; ) {
135 if (server_do(&server) != 0)
139 server_shutdown(&server);
150 drop_root(char const *user, char const *group)
155 if ((uid = getuid()) != 0) {
156 log_notice("Cannot set uid/gid. Not a superuser");
157 return (0); /* dont do anything unless root */
163 uid = strtol(user, &ep, 10);
165 struct passwd *pwd = getpwnam(user);
168 log_err("Could not find passwd entry for " \
178 gid = strtol(group, &ep, 10);
180 struct group *grp = getgrnam(group);
183 log_err("Could not find group entry for " \
192 if (setgid(gid) < 0) {
193 log_err("Could not setgid(%s). %s (%d)",
194 group, strerror(errno), errno);
198 if (setuid(uid) < 0) {
199 log_err("Could not setuid(%s). %s (%d)",
200 user, strerror(errno), errno);
212 sighandler(int32_t s)
214 log_notice("Got signal %d. Total number of signals received %d",
219 * Display usage information and quit
226 "Usage: %s [options]\n" \
227 "Where options are:\n" \
228 " -c specify control socket name (default %s)\n" \
229 " -d do not detach (run in foreground)\n" \
230 " -g grp specify group\n" \
231 " -h display usage and exit\n" \
232 " -u usr specify user\n",
233 SDPD, SDP_LOCAL_PATH);