Update copyright year. Fix a typo in logging.
[umurmur.git] / src / main.c
1 /* Copyright (C) 2010, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2010, Thorvald Natvig <thorvald@natvig.com>
3
4    All rights reserved.
5
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9
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.
18
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.
30 */
31
32
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <signal.h>
39 #include <sched.h>
40 #include <errno.h>
41
42 #include "server.h"
43 #include "ssl.h"
44 #include "channel.h"
45 #include "log.h"
46 #include "client.h"
47 #include "conf.h"
48 #include "version.h"
49
50 void lockfile(const char *pidfile)
51 {
52         int lfp;
53         char str[16];
54         
55         lfp = open(pidfile, O_RDWR|O_CREAT, 0640);
56         
57         if (lfp < 0)
58                 Log_fatal("Cannot open PID-file %s for writing", pidfile);
59         sprintf(str,"%d\n", getpid());
60         write(lfp, str, strlen(str)); /* record pid to lockfile */
61         Log_info("PID-file: %s", pidfile);
62 }
63
64
65 void signal_handler(int sig)
66 {
67         switch(sig) {
68         case SIGHUP:
69                 /* XXX - do stuff? */
70                 Log_info("HUP signal");
71                 break;
72         case SIGTERM:
73                 Log_info("TERM signal. Shutting down.");
74                 Server_shutdown();
75                 break;
76         }
77 }
78
79 void daemonize()
80 {
81         int i;
82         
83         if (getppid() == 1)
84                 return; /* already a daemon */
85         i = fork();
86         if ( i < 0) {
87                 fprintf(stderr, "Fork error. Exiting\n");
88                 exit(1); /* fork error */
89         }
90         if ( i > 0)
91                 exit(0); /* parent exits */
92         
93         /* child (daemon) continues */
94         setsid(); /* obtain a new process group */
95         for (i = getdtablesize(); i >= 0; --i)
96                 close(i); /* close all descriptors */
97         
98         i = open("/dev/null",O_RDWR);
99         dup(i);
100         dup(i);
101         
102         umask(027); /* set newly created file permissions */
103         chdir("/");
104                 
105 }
106
107 void setscheduler()
108 {
109         int rc;
110         struct sched_param sp;
111
112         sp.sched_priority = sched_get_priority_min(SCHED_RR); /* Should suffice */
113         Log_info("Setting SCHED_RR prio %d", sp.sched_priority);
114         rc = sched_setscheduler(0, SCHED_RR, &sp);
115         if (rc < 0)
116                 Log_warn("Failed to set scheduler: %s", strerror(errno));
117 }
118
119 void printhelp()
120 {
121         printf("uMurmur version %s. Mumble protocol %d.%d.%d\n", UMURMUR_VERSION, PROTVER_MAJOR, PROTVER_MINOR, PROTVER_PATCH);
122         printf("Usage: umurmurd [-d] [-p <pidfile>] [-c <conf file>] [-h]\n");
123         printf("       -d             - Do not deamonize\n");
124         printf("       -p <pidfile>   - Write PID to this file\n");
125         printf("       -c <conf file> - Specify configuration file\n");
126         printf("       -r             - Run with realtime priority\n");
127         printf("       -h             - Print this help\n");
128         exit(0);
129 }
130
131 int main(int argc, char **argv)
132 {
133         bool_t nodaemon = false;
134         bool_t realtime = false;
135         char *conffile = NULL, *pidfile = NULL;
136         int c;
137         
138         /* Arguments */
139         while ((c = getopt(argc, argv, "drp:c:h")) != EOF) {
140                 switch(c) {
141                 case 'c':
142                         conffile = optarg;
143                         break;
144                 case 'p':
145                         pidfile = optarg;
146                         break;
147                 case 'd':
148                         nodaemon = true;
149                         break;
150                 case 'h':
151                         printhelp();
152                         break;
153                 case 'r':
154                         realtime = true;
155                         break;
156                 default:
157                         fprintf(stderr, "Unrecognized option\n");
158                         printhelp();
159                         break;
160                 }
161         }
162         
163         if (Conf_init(conffile) != 0) {
164                 fprintf(stderr, "Configuration error\n");
165                 exit(1);
166         }
167                 
168         if (!nodaemon) {
169                 Log_init(false);
170                 daemonize();
171                 if (pidfile != NULL)
172                         lockfile(pidfile);
173         }
174         else
175                 Log_init(true);
176         
177         signal(SIGCHLD, SIG_IGN); /* ignore child */
178         signal(SIGTSTP, SIG_IGN); /* ignore tty signals */
179         signal(SIGTTOU, SIG_IGN);
180         signal(SIGTTIN, SIG_IGN);
181         signal(SIGHUP, signal_handler); /* catch hangup signal */
182         signal(SIGTERM, signal_handler); /* catch kill signal */
183
184         /* Initializing */
185         SSL_init();
186         Chan_init();
187         Client_init();
188
189         if (realtime)
190                 setscheduler();
191         
192         Server_run();
193         
194         SSL_deinit();
195         Chan_free();
196         Log_free();
197         Conf_deinit();
198         
199         if (pidfile != NULL)
200                 unlink(pidfile);
201         
202         return 0;
203 }