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