1 /* Copyright (C) 2009-2010, Martin Johansson <martin@fatbob.nu>
2 Copyright (C) 2005-2010, 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.
35 #include <sys/types.h>
37 #include <sys/utsname.h>
43 #ifdef _POSIX_PRIORITY_SCHEDULING
54 char system_string[64], version_string[64];
58 void lockfile(const char *pidfile)
63 lfp = open(pidfile, O_RDWR|O_CREAT|O_EXCL, 0640);
66 Log_fatal("Cannot open PID-file %s for writing", pidfile);
67 snprintf(str,16,"%d\n", getpid());
68 write(lfp, str, strlen(str)); /* record pid to lockfile */
70 Log_info("PID-file: %s", pidfile);
74 void signal_handler(int sig)
79 Log_info("HUP signal");
82 Log_info("TERM signal. Shutting down.");
93 return; /* already a daemon */
96 fprintf(stderr, "Fork error. Exiting\n");
97 exit(1); /* fork error */
100 exit(0); /* parent exits */
102 /* child (daemon) continues */
103 setsid(); /* obtain a new process group */
104 for (i = getdtablesize(); i >= 0; --i)
105 close(i); /* close all descriptors */
107 i = open("/dev/null",O_RDWR);
111 umask(027); /* set newly created file permissions */
116 #ifdef _POSIX_PRIORITY_SCHEDULING
120 struct sched_param sp;
122 sp.sched_priority = sched_get_priority_min(SCHED_RR); /* Should suffice */
123 Log_info("Setting SCHED_RR prio %d", sp.sched_priority);
124 rc = sched_setscheduler(0, SCHED_RR, &sp);
126 Log_warn("Failed to set scheduler: %s", strerror(errno));
132 printf("uMurmur version %s. Mumble protocol %d.%d.%d\n", UMURMUR_VERSION, PROTVER_MAJOR, PROTVER_MINOR, PROTVER_PATCH);
133 printf("Usage: umurmurd [-d] [-p <pidfile>] [-c <conf file>] [-h]\n");
134 printf(" -d - Do not daemonize\n");
135 printf(" -p <pidfile> - Write PID to this file\n");
136 printf(" -c <conf file> - Specify configuration file\n");
137 #ifdef _POSIX_PRIORITY_SCHEDULING
138 printf(" -r - Run with realtime priority\n");
140 printf(" -a <address> - Bind to IP address\n");
141 printf(" -b <port> - Bind to port\n");
142 printf(" -h - Print this help\n");
146 int main(int argc, char **argv)
148 bool_t nodaemon = false;
149 #ifdef _POSIX_PRIORITY_SCHEDULING
150 bool_t realtime = false;
152 char *conffile = NULL, *pidfile = NULL;
154 struct utsname utsbuf;
157 #ifdef _POSIX_PRIORITY_SCHEDULING
158 while ((c = getopt(argc, argv, "drp:c:a:b:h")) != EOF) {
160 while ((c = getopt(argc, argv, "dp:c:a:b:h")) != EOF) {
173 bindport = atoi(optarg);
181 #ifdef _POSIX_PRIORITY_SCHEDULING
187 fprintf(stderr, "Unrecognized option\n");
204 signal(SIGCHLD, SIG_IGN); /* ignore child */
205 signal(SIGTSTP, SIG_IGN); /* ignore tty signals */
206 signal(SIGTTOU, SIG_IGN);
207 signal(SIGTTIN, SIG_IGN);
208 signal(SIGPIPE, SIG_IGN);
209 signal(SIGHUP, signal_handler); /* catch hangup signal */
210 signal(SIGTERM, signal_handler); /* catch kill signal */
212 /* Build system string */
213 if (uname(&utsbuf) == 0) {
214 snprintf(system_string, 64, "%s %s", utsbuf.sysname, utsbuf.machine);
215 snprintf(version_string, 64, "%s", utsbuf.release);
218 snprintf(system_string, 64, "unknown unknown");
219 snprintf(version_string, 64, "unknown");
227 #ifdef _POSIX_PRIORITY_SCHEDULING