4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: main.c,v 1.8 2004/01/13 19:31:54 max Exp $
34 #include <sys/select.h>
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
51 #include <sys/queue.h>
57 static int32_t drop_root (char const *user, char const *group);
58 static void sighandler (int32_t s);
59 static void usage (void);
64 * Bluetooth Service Discovery Procotol (SDP) daemon
68 main(int argc, char *argv[])
71 char const *control = SDP_LOCAL_PATH;
72 char const *user = "nobody", *group = "nobody";
73 int32_t detach = 1, opt;
76 while ((opt = getopt(argc, argv, "c:dg:hu:")) != -1) {
78 case 'c': /* control */
82 case 'd': /* do not detach */
101 log_open(SDPD, !detach);
103 /* Become daemon if required */
104 if (detach && daemon(0, 0) < 0) {
105 log_crit("Could not become daemon. %s (%d)",
106 strerror(errno), errno);
110 /* Set signal handlers */
111 memset(&sa, 0, sizeof(sa));
112 sa.sa_handler = sighandler;
114 if (sigaction(SIGTERM, &sa, NULL) < 0 ||
115 sigaction(SIGHUP, &sa, NULL) < 0 ||
116 sigaction(SIGINT, &sa, NULL) < 0) {
117 log_crit("Could not install signal handlers. %s (%d)",
118 strerror(errno), errno);
122 sa.sa_handler = SIG_IGN;
123 if (sigaction(SIGPIPE, &sa, NULL) < 0) {
124 log_crit("Could not install signal handlers. %s (%d)",
125 strerror(errno), errno);
129 /* Initialize server */
130 if (server_init(&server, control) < 0)
133 if ((user != NULL || group != NULL) && drop_root(user, group) < 0)
136 for (done = 0; !done; ) {
137 if (server_do(&server) != 0)
141 server_shutdown(&server);
152 drop_root(char const *user, char const *group)
157 if ((uid = getuid()) != 0) {
158 log_notice("Cannot set uid/gid. Not a superuser");
159 return (0); /* dont do anything unless root */
165 uid = strtol(user, &ep, 10);
167 struct passwd *pwd = getpwnam(user);
170 log_err("Could not find passwd entry for " \
180 gid = strtol(group, &ep, 10);
182 struct group *grp = getgrnam(group);
185 log_err("Could not find group entry for " \
194 if (setgid(gid) < 0) {
195 log_err("Could not setgid(%s). %s (%d)",
196 group, strerror(errno), errno);
200 if (setuid(uid) < 0) {
201 log_err("Could not setuid(%s). %s (%d)",
202 user, strerror(errno), errno);
214 sighandler(int32_t s)
216 log_notice("Got signal %d. Total number of signals received %d",
221 * Display usage information and quit
228 "Usage: %s [options]\n" \
229 "Where options are:\n" \
230 " -c specify control socket name (default %s)\n" \
231 " -d do not detach (run in foreground)\n" \
232 " -g grp specify group\n" \
233 " -h display usage and exit\n" \
234 " -u usr specify user\n",
235 SDPD, SDP_LOCAL_PATH);