1 /* Copyright (C) 2009-2011, Martin Johansson <martin@fatbob.nu>
2 Copyright (C) 2005-2011, Thorvald Natvig <thorvald@natvig.com>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 - Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15 - Neither the name of the Developers nor the names of its contributors may
16 be used to endorse or promote products derived from this software without
17 specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
36 #include <sys/utsname.h>
44 #ifdef _POSIX_PRIORITY_SCHEDULING
55 char system_string[64], version_string[64];
59 void lockfile(const char *pidfile)
64 /* Don't use O_TRUNC here -- we want to leave the PID file
65 * unmodified if we cannot lock it.
67 lfp = open(pidfile, O_WRONLY|O_CREAT, 0640);
70 Log_fatal("Cannot open PID-file %s for writing", pidfile);
72 /* Try to lock the file. */
73 if (lockf(lfp, F_TLOCK, 0) < 0) {
76 if (errno == EACCES || errno == EAGAIN)
77 Log_fatal("PID file is locked -- uMurmur already running?");
79 Log_fatal("Cannot lock PID file: %s", strerror(errno));
82 /* Now that we locked the file, erase its contents. */
83 if (ftruncate(lfp, 0) < 0) {
85 Log_fatal("Cannot truncate PID file: %s", strerror(errno));
88 snprintf(str,16,"%d\n", getpid());
89 write(lfp, str, strlen(str)); /* record pid to lockfile */
90 Log_info("PID-file: %s", pidfile);
92 /* If uMurmur ever starts to fork()+exec(), we don't want it to
93 * leak the fd to the forked process though. Set the close-on-exec
94 * flag to prevent leakage.
96 flags = fcntl(lfp, F_GETFD, 0);
98 fcntl(lfp, F_SETFD, (long) flags);
100 /* Don't close(lfp) here!
101 * We want the fd to remain opened so the lock is held until the
107 /* Drops privileges (if configured to do so). */
108 static void switch_user(void)
111 struct group *grp = NULL;
112 const char *username, *groupname;
115 username = getStrConf(USERNAME);
116 groupname = getStrConf(GROUPNAME);
119 /* It's an error to specify groupname
120 * but leave username empty.
123 Log_fatal("username missing");
129 pwd = getpwnam(username);
131 Log_fatal("Unknown user '%s'", username);
136 grp = getgrnam(groupname);
139 Log_fatal("Unknown group '%s'", groupname);
144 if (initgroups(pwd->pw_name, gid))
145 Log_fatal("initgroups() failed: %s", strerror(errno));
148 Log_fatal("setgid() failed: %s", strerror(errno));
150 if (setuid(pwd->pw_uid))
151 Log_fatal("setuid() failed: %s", strerror(errno));
156 Log_fatal("getgrgid() failed: %s", strerror(errno));
158 Log_info("Switch to user '%s' group '%s'", pwd->pw_name, grp->gr_name);
161 void signal_handler(int sig)
165 Log_info("HUP signal received.");
169 Log_info("TERM signal. Shutting down.");
180 return; /* already a daemon */
183 fprintf(stderr, "Fork error. Exiting\n");
184 exit(1); /* fork error */
187 exit(0); /* parent exits */
189 /* child (daemon) continues */
190 setsid(); /* obtain a new process group */
191 for (i = getdtablesize(); i >= 0; --i)
192 close(i); /* close all descriptors */
194 i = open("/dev/null",O_RDWR);
198 umask(027); /* set newly created file permissions */
203 #ifdef _POSIX_PRIORITY_SCHEDULING
207 struct sched_param sp;
209 sp.sched_priority = sched_get_priority_min(SCHED_RR); /* Should suffice */
210 Log_info("Setting SCHED_RR prio %d", sp.sched_priority);
211 rc = sched_setscheduler(0, SCHED_RR, &sp);
213 Log_warn("Failed to set scheduler: %s", strerror(errno));
219 printf("uMurmur version %s. Mumble protocol %d.%d.%d\n", UMURMUR_VERSION, PROTVER_MAJOR, PROTVER_MINOR, PROTVER_PATCH);
220 printf("Usage: umurmurd [-d] [-r] [-h] [-p <pidfile>] [-c <conf file>] [-a <addr>] [-b <port>]\n");
221 printf(" -d - Do not daemonize - run in foreground.\n");
222 #ifdef _POSIX_PRIORITY_SCHEDULING
223 printf(" -r - Run with realtime priority\n");
225 printf(" -h - Print this help\n");
226 printf(" -p <pidfile> - Write PID to this file\n");
227 printf(" -c <conf file> - Specify configuration file (default %s)\n", DEFAULT_CONFIG);
228 printf(" -a <address> - Bind to IP address\n");
229 printf(" -b <port> - Bind to port\n");
233 int main(int argc, char **argv)
235 bool_t nodaemon = false;
236 #ifdef _POSIX_PRIORITY_SCHEDULING
237 bool_t realtime = false;
239 char *conffile = NULL, *pidfile = NULL;
241 struct utsname utsbuf;
244 #ifdef _POSIX_PRIORITY_SCHEDULING
245 while ((c = getopt(argc, argv, "drp:c:a:b:h")) != EOF) {
247 while ((c = getopt(argc, argv, "dp:c:a:b:h")) != EOF) {
260 bindport = atoi(optarg);
268 #ifdef _POSIX_PRIORITY_SCHEDULING
274 fprintf(stderr, "Unrecognized option\n");
280 /* Initialize the config subsystem early;
281 * switch_user() will need to read some config variables as well as logging.
285 /* Logging to terminal if not daemonizing, otherwise to syslog or log file.
295 /* Reopen log file. If user switch results in access denied, we catch
302 signal(SIGCHLD, SIG_IGN); /* ignore child */
303 signal(SIGTSTP, SIG_IGN); /* ignore tty signals */
304 signal(SIGTTOU, SIG_IGN);
305 signal(SIGTTIN, SIG_IGN);
306 signal(SIGPIPE, SIG_IGN);
307 signal(SIGHUP, signal_handler); /* catch hangup signal */
308 signal(SIGTERM, signal_handler); /* catch kill signal */
310 /* Build system string */
311 if (uname(&utsbuf) == 0) {
312 snprintf(system_string, 64, "%s %s", utsbuf.sysname, utsbuf.machine);
313 snprintf(version_string, 64, "%s", utsbuf.release);
316 snprintf(system_string, 64, "unknown unknown");
317 snprintf(version_string, 64, "unknown");
325 #ifdef _POSIX_PRIORITY_SCHEDULING