Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / usr.sbin / bluetooth / sdpd / main.c
blob1df3bf0d5887b2cbb86625daa476ac564a6f98b4
1 /*
2 * main.c
4 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
26 * SUCH DAMAGE.
28 * $Id: main.c,v 1.8 2004/01/13 19:31:54 max Exp $
29 * $FreeBSD$
32 #include <sys/select.h>
33 #include <bluetooth.h>
34 #include <errno.h>
35 #include <grp.h>
36 #include <pwd.h>
37 #include <signal.h>
38 #include <sdp.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include "log.h"
44 #include "server.h"
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <sys/queue.h>
49 #include "profile.h"
50 #include "provider.h"
52 #define SDPD "sdpd"
54 static int32_t drop_root (char const *user, char const *group);
55 static void sighandler (int32_t s);
56 static void usage (void);
58 static int32_t done;
61 * Bluetooth Service Discovery Procotol (SDP) daemon
64 int
65 main(int argc, char *argv[])
67 server_t server;
68 char const *control = SDP_LOCAL_PATH;
69 char const *user = "nobody", *group = "nobody";
70 int32_t detach = 1, opt;
71 struct sigaction sa;
73 while ((opt = getopt(argc, argv, "c:dg:hu:")) != -1) {
74 switch (opt) {
75 case 'c': /* control */
76 control = optarg;
77 break;
79 case 'd': /* do not detach */
80 detach = 0;
81 break;
83 case 'g': /* group */
84 group = optarg;
85 break;
87 case 'u': /* user */
88 user = optarg;
89 break;
91 case 'h':
92 default:
93 usage();
94 /* NOT REACHED */
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);
104 exit(1);
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);
116 exit(1);
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);
123 exit(1);
126 /* Initialize server */
127 if (server_init(&server, control) < 0)
128 exit(1);
130 if ((user != NULL || group != NULL) && drop_root(user, group) < 0)
131 exit(1);
133 for (done = 0; !done; ) {
134 if (server_do(&server) != 0)
135 done ++;
138 server_shutdown(&server);
139 log_close();
141 return (0);
145 * Drop root
148 static int32_t
149 drop_root(char const *user, char const *group)
151 int uid, gid;
152 char *ep;
154 if ((uid = getuid()) != 0) {
155 log_notice("Cannot set uid/gid. Not a superuser");
156 return (0); /* dont do anything unless root */
159 gid = getgid();
161 if (user != NULL) {
162 uid = strtol(user, &ep, 10);
163 if (*ep != '\0') {
164 struct passwd *pwd = getpwnam(user);
166 if (pwd == NULL) {
167 log_err("Could not find passwd entry for " \
168 "user %s", user);
169 return (-1);
172 uid = pwd->pw_uid;
176 if (group != NULL) {
177 gid = strtol(group, &ep, 10);
178 if (*ep != '\0') {
179 struct group *grp = getgrnam(group);
181 if (grp == NULL) {
182 log_err("Could not find group entry for " \
183 "group %s", group);
184 return (-1);
187 gid = grp->gr_gid;
191 if (setgid(gid) < 0) {
192 log_err("Could not setgid(%s). %s (%d)",
193 group, strerror(errno), errno);
194 return (-1);
197 if (setuid(uid) < 0) {
198 log_err("Could not setuid(%s). %s (%d)",
199 user, strerror(errno), errno);
200 return (-1);
203 return (0);
207 * Signal handler
210 static void
211 sighandler(int32_t s)
213 log_notice("Got signal %d. Total number of signals received %d",
214 s, ++ done);
218 * Display usage information and quit
221 static void
222 usage(void)
224 fprintf(stderr,
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);
233 exit(255);