1 /* $NetBSD: main.c,v 1.3 2007/03/18 15:53:55 plunky Exp $ */
2 /* $DragonFly: src/usr.sbin/sdpd/main.c,v 1.1 2008/01/06 21:51:30 hasso Exp $ */
7 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $Id: main.c,v 1.2 2007/11/30 07:39:37 griffin Exp $
32 * $FreeBSD: src/usr.sbin/bluetooth/sdpd/main.c,v 1.1 2004/01/20 20:48:26 emax Exp $
35 #include <sys/select.h>
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 = "_sdpd", *group = "_sdpd";
73 char const *sgroup = NULL;
74 int32_t detach = 1, opt;
77 while ((opt = getopt(argc, argv, "c:dG:g:hu:")) != -1) {
79 case 'c': /* control */
83 case 'd': /* do not detach */
87 case 'G': /* super group */
106 log_open(SDPD, !detach);
108 /* Become daemon if required */
109 if (detach && daemon(0, 0) < 0) {
110 log_crit("Could not become daemon. %s (%d)",
111 strerror(errno), errno);
115 /* Set signal handlers */
116 memset(&sa, 0, sizeof(sa));
117 sa.sa_handler = sighandler;
119 if (sigaction(SIGTERM, &sa, NULL) < 0 ||
120 sigaction(SIGHUP, &sa, NULL) < 0 ||
121 sigaction(SIGINT, &sa, NULL) < 0) {
122 log_crit("Could not install signal handlers. %s (%d)",
123 strerror(errno), errno);
127 sa.sa_handler = SIG_IGN;
128 if (sigaction(SIGPIPE, &sa, NULL) < 0) {
129 log_crit("Could not install signal handlers. %s (%d)",
130 strerror(errno), errno);
134 /* Initialize server */
135 if (server_init(&server, control, sgroup) < 0)
138 if ((user != NULL || group != NULL) && drop_root(user, group) < 0)
141 for (done = 0; !done; ) {
142 if (server_do(&server) != 0)
146 server_shutdown(&server);
157 drop_root(char const *user, char const *group)
162 if ((uid = getuid()) != 0) {
163 log_notice("Cannot set uid/gid. Not a superuser");
164 return (0); /* dont do anything unless root */
170 uid = strtol(user, &ep, 10);
172 struct passwd *pwd = getpwnam(user);
175 log_err("Could not find passwd entry for " \
185 gid = strtol(group, &ep, 10);
187 struct group *grp = getgrnam(group);
190 log_err("Could not find group entry for " \
199 if (setgid(gid) < 0) {
200 log_err("Could not setgid(%s). %s (%d)",
201 group, strerror(errno), errno);
205 if (setuid(uid) < 0) {
206 log_err("Could not setuid(%s). %s (%d)",
207 user, strerror(errno), errno);
219 sighandler(int32_t s)
221 log_notice("Got signal %d. Total number of signals received %d",
226 * Display usage information and quit
233 "Usage: %s [options]\n" \
234 "Where options are:\n" \
235 " -c specify control socket name (default %s)\n" \
236 " -d do not detach (run in foreground)\n" \
237 " -G grp allow privileges to group\n" \
238 " -g grp specify group\n" \
239 " -h display usage and exit\n" \
240 " -u usr specify user\n",
241 SDPD, SDP_LOCAL_PATH);