Sync usage with man page.
[netbsd-mini2440.git] / usr.sbin / sdpd / main.c
blobdd22809a9d319bbf454972178b2d3786ab3bc5e1
1 /* $NetBSD: main.c,v 1.6 2009/08/13 17:50:41 drochner Exp $ */
3 /*-
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
27 * SUCH DAMAGE.
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 $");
39 #include <errno.h>
40 #include <grp.h>
41 #include <pwd.h>
42 #include <signal.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
48 #include "sdpd.h"
50 #define SDPD "sdpd"
52 static bool drop_root (char const *user, char const *group);
53 static void sighandler (int s);
54 static void usage (void);
56 static bool done;
59 * Bluetooth Service Discovery Procotol (SDP) daemon
62 int
63 main(int argc, char *argv[])
65 server_t server;
66 char const *control = SDP_LOCAL_PATH;
67 char const *user = "_sdpd", *group = "_sdpd";
68 char const *sgroup = NULL;
69 int opt;
70 bool detach = true;
71 struct sigaction sa;
73 while ((opt = getopt(argc, argv, "c:dG:g:hu:")) != -1) {
74 switch (opt) {
75 case 'c': /* control */
76 control = optarg;
77 break;
79 case 'd': /* do not detach */
80 detach = false;
81 break;
83 case 'G': /* super group */
84 sgroup = optarg;
85 break;
87 case 'g': /* group */
88 group = optarg;
89 break;
91 case 'u': /* user */
92 user = optarg;
93 break;
95 case 'h':
96 default:
97 usage();
98 /* NOT REACHED */
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);
109 exit(EXIT_FAILURE);
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);
122 exit(EXIT_FAILURE);
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);
130 exit(EXIT_FAILURE);
133 /* Initialize server */
134 if (!server_init(&server, control, sgroup))
135 exit(EXIT_FAILURE);
137 if ((user != NULL || group != NULL) && !drop_root(user, group))
138 exit(EXIT_FAILURE);
140 for (done = 0; !done; ) {
141 if (!server_do(&server))
142 done++;
145 server_shutdown(&server);
146 log_close();
148 exit(EXIT_SUCCESS);
152 * Drop root
155 static bool
156 drop_root(char const *user, char const *group)
158 int uid, gid;
159 char *ep;
161 if ((uid = getuid()) != 0) {
162 log_notice("Cannot set uid/gid. Not a superuser");
163 return true; /* dont do anything unless root */
166 gid = getgid();
168 if (user != NULL) {
169 uid = strtol(user, &ep, 10);
170 if (*ep != '\0') {
171 struct passwd *pwd = getpwnam(user);
173 if (pwd == NULL) {
174 log_err("No passwd entry for user %s", user);
175 return false;
178 uid = pwd->pw_uid;
182 if (group != NULL) {
183 gid = strtol(group, &ep, 10);
184 if (*ep != '\0') {
185 struct group *grp = getgrnam(group);
187 if (grp == NULL) {
188 log_err("No group entry for group %s", group);
189 return false;
192 gid = grp->gr_gid;
196 if (setgid(gid) < 0) {
197 log_err("Could not setgid(%s). %s (%d)", group,
198 strerror(errno), errno);
200 return false;
203 if (setgroups(0, NULL) < 0) {
204 log_err("Could not setgroups(0). %s (%d)",
205 strerror(errno), errno);
207 return false;
210 if (setuid(uid) < 0) {
211 log_err("Could not setuid(%s). %s (%d)", user,
212 strerror(errno), errno);
214 return false;
217 return true;
221 * Signal handler
224 static void
225 sighandler(int s)
228 log_notice("Got signal %d. Total number of signals received %d",
229 s, ++done);
233 * Display usage information and quit
236 static void
237 usage(void)
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);
250 exit(EXIT_FAILURE);