| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2007-2010, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
* |
|---|
| 5 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 6 |
* modification, are permitted provided that the following conditions are |
|---|
| 7 |
* met: |
|---|
| 8 |
* |
|---|
| 9 |
* * Redistributions of source code must retain the above copyright |
|---|
| 10 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 11 |
* * Redistributions in binary form must reproduce the above |
|---|
| 12 |
* copyright notice, this list of conditions and the following |
|---|
| 13 |
* disclaimer in the documentation and/or other materials provided |
|---|
| 14 |
* with the distribution. |
|---|
| 15 |
* * Neither the name OmniTI Computer Consulting, Inc. nor the names |
|---|
| 16 |
* of its contributors may be used to endorse or promote products |
|---|
| 17 |
* derived from this software without specific prior written |
|---|
| 18 |
* permission. |
|---|
| 19 |
* |
|---|
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 31 |
*/ |
|---|
| 32 |
#include "noit_defines.h" |
|---|
| 33 |
|
|---|
| 34 |
#include <assert.h> |
|---|
| 35 |
#include <stdio.h> |
|---|
| 36 |
#include <stdlib.h> |
|---|
| 37 |
#include <unistd.h> |
|---|
| 38 |
#include <errno.h> |
|---|
| 39 |
#include <fcntl.h> |
|---|
| 40 |
#include <sys/mman.h> |
|---|
| 41 |
#include <signal.h> |
|---|
| 42 |
#ifdef HAVE_SYS_WAIT_H |
|---|
| 43 |
#include <sys/wait.h> |
|---|
| 44 |
#endif |
|---|
| 45 |
|
|---|
| 46 |
#include "noit_main.h" |
|---|
| 47 |
#include "eventer/eventer.h" |
|---|
| 48 |
#include "utils/noit_log.h" |
|---|
| 49 |
#include "utils/noit_hash.h" |
|---|
| 50 |
#include "utils/noit_security.h" |
|---|
| 51 |
#include "utils/noit_watchdog.h" |
|---|
| 52 |
#include "utils/noit_lockfile.h" |
|---|
| 53 |
#include "noit_listener.h" |
|---|
| 54 |
#include "noit_console.h" |
|---|
| 55 |
#include "noit_jlog_listener.h" |
|---|
| 56 |
#include "noit_rest.h" |
|---|
| 57 |
#include "noit_check_rest.h" |
|---|
| 58 |
#include "noit_livestream_listener.h" |
|---|
| 59 |
#include "noit_capabilities_listener.h" |
|---|
| 60 |
#include "noit_module.h" |
|---|
| 61 |
#include "noit_conf.h" |
|---|
| 62 |
#include "noit_conf_checks.h" |
|---|
| 63 |
#include "noit_filters.h" |
|---|
| 64 |
|
|---|
| 65 |
#define APPNAME "noit" |
|---|
| 66 |
#define CHILD_WATCHDOG_TIMEOUT 5 /*seconds*/ |
|---|
| 67 |
|
|---|
| 68 |
static char *config_file = ETC_DIR "/" APPNAME ".conf"; |
|---|
| 69 |
static const char *droptouser = NULL; |
|---|
| 70 |
static const char *droptogroup = NULL; |
|---|
| 71 |
static const char *chrootpath = NULL; |
|---|
| 72 |
static int foreground = 0; |
|---|
| 73 |
static int debug = 0; |
|---|
| 74 |
static int strict_module_load = 0; |
|---|
| 75 |
static char *glider = NULL; |
|---|
| 76 |
|
|---|
| 77 |
#include "man/noitd.usage.h" |
|---|
| 78 |
static void usage(const char *progname) { |
|---|
| 79 |
printf("Usage for %s:\n", progname); |
|---|
| 80 |
#ifdef NOITD_USAGE |
|---|
| 81 |
assert(write(STDOUT_FILENO, |
|---|
| 82 |
NOITD_USAGE, |
|---|
| 83 |
sizeof(NOITD_USAGE)-1) == sizeof(NOITD_USAGE)-1); |
|---|
| 84 |
#else |
|---|
| 85 |
printf("\nError in usage, build problem.\n"); |
|---|
| 86 |
#endif |
|---|
| 87 |
return; |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
void parse_clargs(int argc, char **argv) { |
|---|
| 91 |
int c; |
|---|
| 92 |
while((c = getopt(argc, argv, "Mhc:dDu:g:n:t:l:L:G:")) != EOF) { |
|---|
| 93 |
switch(c) { |
|---|
| 94 |
case 'G': |
|---|
| 95 |
glider = strdup(optarg); |
|---|
| 96 |
break; |
|---|
| 97 |
case 'M': |
|---|
| 98 |
strict_module_load = 1; |
|---|
| 99 |
break; |
|---|
| 100 |
case 'h': |
|---|
| 101 |
usage(argv[0]); |
|---|
| 102 |
exit(1); |
|---|
| 103 |
break; |
|---|
| 104 |
case 'l': |
|---|
| 105 |
noit_main_enable_log(optarg); |
|---|
| 106 |
break; |
|---|
| 107 |
case 'L': |
|---|
| 108 |
noit_main_disable_log(optarg); |
|---|
| 109 |
break; |
|---|
| 110 |
case 'n': |
|---|
| 111 |
{ |
|---|
| 112 |
char *cp = strchr(optarg, ':'); |
|---|
| 113 |
if(!cp) noit_listener_skip(optarg, 0); |
|---|
| 114 |
else { |
|---|
| 115 |
if(cp == optarg) optarg = NULL; |
|---|
| 116 |
*cp++ = '\0'; |
|---|
| 117 |
noit_listener_skip(optarg, atoi(cp)); |
|---|
| 118 |
} |
|---|
| 119 |
} |
|---|
| 120 |
break; |
|---|
| 121 |
case 'u': |
|---|
| 122 |
droptouser = strdup(optarg); |
|---|
| 123 |
break; |
|---|
| 124 |
case 'g': |
|---|
| 125 |
droptogroup = strdup(optarg); |
|---|
| 126 |
break; |
|---|
| 127 |
case 't': |
|---|
| 128 |
chrootpath = strdup(optarg); |
|---|
| 129 |
break; |
|---|
| 130 |
case 'c': |
|---|
| 131 |
config_file = strdup(optarg); |
|---|
| 132 |
break; |
|---|
| 133 |
case 'D': |
|---|
| 134 |
foreground++; |
|---|
| 135 |
break; |
|---|
| 136 |
case 'd': |
|---|
| 137 |
debug++; |
|---|
| 138 |
break; |
|---|
| 139 |
default: |
|---|
| 140 |
break; |
|---|
| 141 |
} |
|---|
| 142 |
} |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
static int __reload_needed = 0; |
|---|
| 146 |
static void request_conf_reload(int sig) { |
|---|
| 147 |
if(sig == SIGHUP) { |
|---|
| 148 |
__reload_needed = 1; |
|---|
| 149 |
} |
|---|
| 150 |
} |
|---|
| 151 |
static int noitice_hup(eventer_t e, int mask, void *unused, struct timeval *now) { |
|---|
| 152 |
if(__reload_needed) { |
|---|
| 153 |
noitL(noit_error, "SIGHUP received, performing reload\n"); |
|---|
| 154 |
if(noit_conf_load(config_file) == -1) { |
|---|
| 155 |
noitL(noit_error, "Cannot load config: '%s'\n", config_file); |
|---|
| 156 |
exit(-1); |
|---|
| 157 |
} |
|---|
| 158 |
noit_poller_reload(NULL); |
|---|
| 159 |
__reload_needed = 0; |
|---|
| 160 |
} |
|---|
| 161 |
return 0; |
|---|
| 162 |
} |
|---|
| 163 |
static int child_main() { |
|---|
| 164 |
eventer_t e; |
|---|
| 165 |
|
|---|
| 166 |
/* Load our config... |
|---|
| 167 |
* to ensure it is current w.r.t. to this child starting */ |
|---|
| 168 |
if(noit_conf_load(config_file) == -1) { |
|---|
| 169 |
noitL(noit_error, "Cannot load config: '%s'\n", config_file); |
|---|
| 170 |
exit(2); |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
noit_log_reopen_all(); |
|---|
| 174 |
noitL(noit_notice, "process starting: %d\n", (int)getpid()); |
|---|
| 175 |
|
|---|
| 176 |
signal(SIGHUP, request_conf_reload); |
|---|
| 177 |
|
|---|
| 178 |
/* initialize the eventer */ |
|---|
| 179 |
if(eventer_init() == -1) { |
|---|
| 180 |
noitL(noit_stderr, "Cannot initialize eventer\n"); |
|---|
| 181 |
exit(-1); |
|---|
| 182 |
} |
|---|
| 183 |
/* rotation init requires, eventer_init() */ |
|---|
| 184 |
noit_conf_log_init_rotate(APPNAME, noit_false); |
|---|
| 185 |
|
|---|
| 186 |
/* Setup our heartbeat */ |
|---|
| 187 |
noit_watchdog_child_eventer_heartbeat(); |
|---|
| 188 |
|
|---|
| 189 |
e = eventer_alloc(); |
|---|
| 190 |
e->mask = EVENTER_RECURRENT; |
|---|
| 191 |
e->callback = noitice_hup; |
|---|
| 192 |
eventer_add_recurrent(e); |
|---|
| 193 |
|
|---|
| 194 |
/* Initialize all of our listeners */ |
|---|
| 195 |
noit_console_init(APPNAME); |
|---|
| 196 |
noit_console_conf_init(); |
|---|
| 197 |
noit_console_conf_checks_init(); |
|---|
| 198 |
noit_capabilities_listener_init(); |
|---|
| 199 |
noit_jlog_listener_init(); |
|---|
| 200 |
noit_http_rest_init(); |
|---|
| 201 |
noit_check_rest_init(); |
|---|
| 202 |
noit_filters_rest_init(); |
|---|
| 203 |
noit_livestream_listener_init(); |
|---|
| 204 |
|
|---|
| 205 |
noit_module_init(); |
|---|
| 206 |
if(strict_module_load && noit_module_load_failures() > 0) { |
|---|
| 207 |
noitL(noit_stderr, "Failed to load some modules and -M given.\n"); |
|---|
| 208 |
exit(2); |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
/* Drop privileges */ |
|---|
| 212 |
if(chrootpath && noit_security_chroot(chrootpath)) { |
|---|
| 213 |
noitL(noit_stderr, "Failed to chroot(), exiting.\n"); |
|---|
| 214 |
exit(2); |
|---|
| 215 |
} |
|---|
| 216 |
if(noit_security_usergroup(droptouser, droptogroup, noit_false)) { |
|---|
| 217 |
noitL(noit_stderr, "Failed to drop privileges, exiting.\n"); |
|---|
| 218 |
exit(2); |
|---|
| 219 |
} |
|---|
| 220 |
|
|---|
| 221 |
/* Prepare for launch... */ |
|---|
| 222 |
noit_filters_init(); |
|---|
| 223 |
noit_poller_init(); |
|---|
| 224 |
noit_listener_init(APPNAME); |
|---|
| 225 |
|
|---|
| 226 |
/* Write our log out, and setup a watchdog to write it out on change. */ |
|---|
| 227 |
noit_conf_write_log(NULL); |
|---|
| 228 |
noit_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */ |
|---|
| 229 |
noit_conf_watch_and_journal_watchdog(noit_conf_write_log, NULL); |
|---|
| 230 |
|
|---|
| 231 |
eventer_loop(); |
|---|
| 232 |
return 0; |
|---|
| 233 |
} |
|---|
| 234 |
|
|---|
| 235 |
int main(int argc, char **argv) { |
|---|
| 236 |
parse_clargs(argc, argv); |
|---|
| 237 |
return noit_main(APPNAME, config_file, debug, foreground, |
|---|
| 238 |
glider, droptouser, droptogroup, child_main); |
|---|
| 239 |
} |
|---|