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 #include <bluetooth.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <sys/queue.h>
54 static int32_t drop_root (char const *user
, char const *group
);
55 static void sighandler (int32_t s
);
56 static void usage (void);
61 * Bluetooth Service Discovery Procotol (SDP) daemon
65 main(int argc
, char *argv
[])
68 char const *control
= SDP_LOCAL_PATH
;
69 char const *user
= "nobody", *group
= "nobody";
70 int32_t detach
= 1, opt
;
73 while ((opt
= getopt(argc
, argv
, "c:dg:hu:")) != -1) {
75 case 'c': /* control */
79 case 'd': /* do not detach */
98 log_open(SDPD
, !detach
);
100 /* Become daemon if required */
101 if (detach
&& daemon(0, 0) < 0) {
102 log_crit("Could not become daemon. %s (%d)",
103 strerror(errno
), errno
);
107 /* Set signal handlers */
108 memset(&sa
, 0, sizeof(sa
));
109 sa
.sa_handler
= sighandler
;
111 if (sigaction(SIGTERM
, &sa
, NULL
) < 0 ||
112 sigaction(SIGHUP
, &sa
, NULL
) < 0 ||
113 sigaction(SIGINT
, &sa
, NULL
) < 0) {
114 log_crit("Could not install signal handlers. %s (%d)",
115 strerror(errno
), errno
);
119 sa
.sa_handler
= SIG_IGN
;
120 if (sigaction(SIGPIPE
, &sa
, NULL
) < 0) {
121 log_crit("Could not install signal handlers. %s (%d)",
122 strerror(errno
), errno
);
126 /* Initialize server */
127 if (server_init(&server
, control
) < 0)
130 if ((user
!= NULL
|| group
!= NULL
) && drop_root(user
, group
) < 0)
133 for (done
= 0; !done
; ) {
134 if (server_do(&server
) != 0)
138 server_shutdown(&server
);
149 drop_root(char const *user
, char const *group
)
154 if ((uid
= getuid()) != 0) {
155 log_notice("Cannot set uid/gid. Not a superuser");
156 return (0); /* dont do anything unless root */
162 uid
= strtol(user
, &ep
, 10);
164 struct passwd
*pwd
= getpwnam(user
);
167 log_err("Could not find passwd entry for " \
177 gid
= strtol(group
, &ep
, 10);
179 struct group
*grp
= getgrnam(group
);
182 log_err("Could not find group entry for " \
191 if (setgid(gid
) < 0) {
192 log_err("Could not setgid(%s). %s (%d)",
193 group
, strerror(errno
), errno
);
197 if (setuid(uid
) < 0) {
198 log_err("Could not setuid(%s). %s (%d)",
199 user
, strerror(errno
), errno
);
211 sighandler(int32_t s
)
213 log_notice("Got signal %d. Total number of signals received %d",
218 * Display usage information and quit
225 "Usage: %s [options]\n" \
226 "Where options are:\n" \
227 " -c specify control socket name (default %s)\n" \
228 " -d do not detach (run in foreground)\n" \
229 " -g grp specify group\n" \
230 " -h display usage and exit\n" \
231 " -u usr specify user\n",
232 SDPD
, SDP_LOCAL_PATH
);