1 /* Copyright (C) 2009-2012, Martin Johansson <martin@fatbob.nu>
2 Copyright (C) 2005-2012, 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
45 #if (_POSIX_PRIORITY_SCHEDULING > 0)
46 #define POSIX_PRIORITY_SCHEDULING
58 char system_string[64], version_string[64];
62 void lockfile(const char *pidfile)
67 /* Don't use O_TRUNC here -- we want to leave the PID file
68 * unmodified if we cannot lock it.
70 lfp = open(pidfile, O_WRONLY|O_CREAT, 0640);
73 Log_fatal("Cannot open PID-file %s for writing", pidfile);
75 /* Try to lock the file. */
76 if (lockf(lfp, F_TLOCK, 0) < 0) {
79 if (errno == EACCES || errno == EAGAIN)
80 Log_fatal("PID file is locked -- uMurmur already running?");
82 Log_fatal("Cannot lock PID file: %s", strerror(errno));
85 /* Now that we locked the file, erase its contents. */
86 if (ftruncate(lfp, 0) < 0) {
88 Log_fatal("Cannot truncate PID file: %s", strerror(errno));
91 snprintf(str,16,"%d\n", getpid());
92 write(lfp, str, strlen(str)); /* record pid to lockfile */
93 Log_info("PID-file: %s", pidfile);
95 /* If uMurmur ever starts to fork()+exec(), we don't want it to
96 * leak the fd to the forked process though. Set the close-on-exec
97 * flag to prevent leakage.
99 flags = fcntl(lfp, F_GETFD, 0);
101 fcntl(lfp, F_SETFD, (long) flags);
103 /* Don't close(lfp) here!
104 * We want the fd to remain opened so the lock is held until the
110 /* Drops privileges (if configured to do so). */
111 static void switch_user(void)
114 struct group *grp = NULL;
115 const char *username, *groupname;
118 username = getStrConf(USERNAME);
119 groupname = getStrConf(GROUPNAME);
122 /* It's an error to specify groupname
123 * but leave username empty.
126 Log_fatal("username missing");
132 pwd = getpwnam(username);
134 Log_fatal("Unknown user '%s'", username);
139 grp = getgrnam(groupname);
142 Log_fatal("Unknown group '%s'", groupname);
147 if (initgroups(pwd->pw_name, gid))
148 Log_fatal("initgroups() failed: %s", strerror(errno));
151 Log_fatal("setgid() failed: %s", strerror(errno));
153 if (setuid(pwd->pw_uid))
154 Log_fatal("setuid() failed: %s", strerror(errno));
159 Log_fatal("getgrgid() failed: %s", strerror(errno));
161 Log_info("Switch to user '%s' group '%s'", pwd->pw_name, grp->gr_name);
164 void signal_handler(int sig)
168 Log_info("HUP signal received.");
172 Log_info("TERM signal. Shutting down.");
183 return; /* already a daemon */
186 fprintf(stderr, "Fork error. Exiting\n");
187 exit(1); /* fork error */
190 exit(0); /* parent exits */
192 /* child (daemon) continues */
193 setsid(); /* obtain a new process group */
194 for (i = getdtablesize(); i >= 0; --i)
195 close(i); /* close all descriptors */
197 i = open("/dev/null",O_RDWR);
201 umask(027); /* set newly created file permissions */
206 #ifdef POSIX_PRIORITY_SCHEDULING
210 struct sched_param sp;
212 sp.sched_priority = sched_get_priority_min(SCHED_RR); /* Should suffice */
213 Log_info("Setting SCHED_RR prio %d", sp.sched_priority);
214 rc = sched_setscheduler(0, SCHED_RR, &sp);
216 Log_warn("Failed to set scheduler: %s", strerror(errno));
222 printf("uMurmur version %s ('%s'). Mumble protocol %d.%d.%d\n", UMURMUR_VERSION,
223 UMURMUR_CODENAME, PROTVER_MAJOR, PROTVER_MINOR, PROTVER_PATCH);
224 printf("Usage: umurmurd [-d] [-r] [-h] [-p <pidfile>] [-t] [-c <conf file>] [-a <addr>] [-b <port>]\n");
225 printf(" -d - Do not daemonize - run in foreground.\n");
226 #ifdef POSIX_PRIORITY_SCHEDULING
227 printf(" -r - Run with realtime priority\n");
229 printf(" -p <pidfile> - Write PID to this file\n");
230 printf(" -c <conf file> - Specify configuration file (default %s)\n", DEFAULT_CONFIG);
231 printf(" -t - Test config. Error message to stderr + non-zero exit code on error\n");
232 printf(" -a <address> - Bind to IP address\n");
233 printf(" -b <port> - Bind to port\n");
234 printf(" -h - Print this help\n");
238 int main(int argc, char **argv)
240 bool_t nodaemon = false;
241 #ifdef POSIX_PRIORITY_SCHEDULING
242 bool_t realtime = false;
244 bool_t testconfig = false;
245 char *conffile = NULL, *pidfile = NULL;
247 struct utsname utsbuf;
250 #ifdef POSIX_PRIORITY_SCHEDULING
251 while ((c = getopt(argc, argv, "drp:c:a:b:ht")) != EOF) {
253 while ((c = getopt(argc, argv, "dp:c:a:b:ht")) != EOF) {
266 bindport = atoi(optarg);
277 #ifdef POSIX_PRIORITY_SCHEDULING
283 fprintf(stderr, "Unrecognized option\n");
290 if (!Conf_ok(conffile))
296 /* Initialize the config subsystem early;
297 * switch_user() will need to read some config variables as well as logging.
301 /* Logging to terminal if not daemonizing, otherwise to syslog or log file.
311 /* Reopen log file. If user switch results in access denied, we catch
318 signal(SIGCHLD, SIG_IGN); /* ignore child */
319 signal(SIGTSTP, SIG_IGN); /* ignore tty signals */
320 signal(SIGTTOU, SIG_IGN);
321 signal(SIGTTIN, SIG_IGN);
322 signal(SIGPIPE, SIG_IGN);
323 signal(SIGHUP, signal_handler); /* catch hangup signal */
324 signal(SIGTERM, signal_handler); /* catch kill signal */
326 /* Build system string */
327 if (uname(&utsbuf) == 0) {
328 snprintf(system_string, 64, "%s %s", utsbuf.sysname, utsbuf.machine);
329 snprintf(version_string, 64, "%s", utsbuf.release);
332 snprintf(system_string, 64, "unknown unknown");
333 snprintf(version_string, 64, "unknown");
342 #ifdef POSIX_PRIORITY_SCHEDULING