1 /* $NetBSD: main.c,v 1.6 2009/08/13 17:50:41 drochner Exp $ */
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/usr.sbin/bluetooth/sdpd/main.c,v 1.1 2004/01/20 20:48:26 emax Exp $
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc.\
34 Copyright (c) 2006 Itronix, Inc.\
35 Copyright (c) 2004 Maksim Yevmenkin m_evmenkin@yahoo.com.\
36 All rights reserved.");
37 __RCSID("$NetBSD: main.c,v 1.6 2009/08/13 17:50:41 drochner Exp $");
52 static bool drop_root (char const *user
, char const *group
);
53 static void sighandler (int s
);
54 static void usage (void);
59 * Bluetooth Service Discovery Procotol (SDP) daemon
63 main(int argc
, char *argv
[])
66 char const *control
= SDP_LOCAL_PATH
;
67 char const *user
= "_sdpd", *group
= "_sdpd";
68 char const *sgroup
= NULL
;
73 while ((opt
= getopt(argc
, argv
, "c:dG:g:hu:")) != -1) {
75 case 'c': /* control */
79 case 'd': /* do not detach */
83 case 'G': /* super group */
102 log_open(SDPD
, !detach
);
104 /* Become daemon if required */
105 if (detach
&& daemon(0, 0) < 0) {
106 log_crit("Could not become daemon. %s (%d)",
107 strerror(errno
), errno
);
112 /* Set signal handlers */
113 memset(&sa
, 0, sizeof(sa
));
114 sa
.sa_handler
= sighandler
;
116 if (sigaction(SIGTERM
, &sa
, NULL
) < 0
117 || sigaction(SIGHUP
, &sa
, NULL
) < 0
118 || sigaction(SIGINT
, &sa
, NULL
) < 0) {
119 log_crit("Could not install signal handlers. %s (%d)",
120 strerror(errno
), errno
);
125 sa
.sa_handler
= SIG_IGN
;
126 if (sigaction(SIGPIPE
, &sa
, NULL
) < 0) {
127 log_crit("Could not install signal handlers. %s (%d)",
128 strerror(errno
), errno
);
133 /* Initialize server */
134 if (!server_init(&server
, control
, sgroup
))
137 if ((user
!= NULL
|| group
!= NULL
) && !drop_root(user
, group
))
140 for (done
= 0; !done
; ) {
141 if (!server_do(&server
))
145 server_shutdown(&server
);
156 drop_root(char const *user
, char const *group
)
161 if ((uid
= getuid()) != 0) {
162 log_notice("Cannot set uid/gid. Not a superuser");
163 return true; /* dont do anything unless root */
169 uid
= strtol(user
, &ep
, 10);
171 struct passwd
*pwd
= getpwnam(user
);
174 log_err("No passwd entry for user %s", user
);
183 gid
= strtol(group
, &ep
, 10);
185 struct group
*grp
= getgrnam(group
);
188 log_err("No group entry for group %s", group
);
196 if (setgid(gid
) < 0) {
197 log_err("Could not setgid(%s). %s (%d)", group
,
198 strerror(errno
), errno
);
203 if (setgroups(0, NULL
) < 0) {
204 log_err("Could not setgroups(0). %s (%d)",
205 strerror(errno
), errno
);
210 if (setuid(uid
) < 0) {
211 log_err("Could not setuid(%s). %s (%d)", user
,
212 strerror(errno
), errno
);
228 log_notice("Got signal %d. Total number of signals received %d",
233 * Display usage information and quit
240 fprintf(stderr
, "Usage: %s [options]\n"
241 "Where options are:\n"
242 "\t-c specify control socket name (default %s)\n"
243 "\t-d do not detach (run in foreground)\n"
244 "\t-G grp allow privileges to group\n"
245 "\t-g grp specify group\n"
246 "\t-h display usage and exit\n"
247 "\t-u usr specify user\n"
248 "", SDPD
, SDP_LOCAL_PATH
);