X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Ftincd.c;h=23845fff1f2942ada86b7e229c96ab307adf2347;hb=33f241d97852d7a171f1aaf1bda7f66356ff889e;hp=f6cda972e7fa62c0a682c85e2703d12b9821e277;hpb=d6c50eb73ad49bd2eac67214995dff76b7a20661;p=meshlink diff --git a/src/tincd.c b/src/tincd.c index f6cda972..23845fff 100644 --- a/src/tincd.c +++ b/src/tincd.c @@ -1,9 +1,11 @@ /* tincd.c -- the main file for tincd Copyright (C) 1998-2005 Ivo Timmermans - 2000-2009 Guus Sliepen + 2000-2011 Guus Sliepen 2008 Max Rijevski 2009 Michael Tokarev + 2010 Julien Muchembled + 2010 Timothy Redaelli This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -31,7 +33,15 @@ #include #endif +#include +#include +#include +#include +#include + +#ifdef HAVE_LZO #include LZO1X_H +#endif #ifndef HAVE_MINGW #include @@ -57,16 +67,16 @@ char *program_name = NULL; /* If nonzero, display usage information and exit. */ -bool show_help = false; +static bool show_help = false; /* If nonzero, print the version on standard output and exit. */ -bool show_version = false; +static bool show_version = false; /* If nonzero, use null ciphers and skip all key exchanges. */ bool bypass_security = false; /* If nonzero, disable swapping for this process. */ -bool do_mlock = false; +static bool do_mlock = false; /* If nonzero, chroot to netdir after startup. */ static bool do_chroot = false; @@ -103,6 +113,7 @@ static struct option const long_options[] = { #ifdef HAVE_MINGW static struct WSAData wsa_state; CRITICAL_SECTION mutex; +int main2(int argc, char **argv); #endif static void usage(bool status) { @@ -119,6 +130,7 @@ static void usage(bool status) { " --logfile[=FILENAME] Write log entries to a logfile.\n" " --controlcookie=FILENAME Write control socket cookie to FILENAME.\n" " --bypass-security Disables meta protocol security, for debugging.\n" + " -o [HOST.]KEY=VALUE Set global/host configuration value.\n" " -R, --chroot chroot to NET dir at startup.\n" " -U, --user=USER setuid to given USER at startup.\n" " --help Display this help and exit.\n" " --version Output version information and exit.\n\n"); @@ -127,10 +139,14 @@ static void usage(bool status) { } static bool parse_options(int argc, char **argv) { + config_t *cfg; int r; int option_index = 0; + int lineno = 0; - while((r = getopt_long(argc, argv, "c:DLd::n:RU:", long_options, &option_index)) != EOF) { + cmdline_conf = list_alloc((list_action_t)free_config); + + while((r = getopt_long(argc, argv, "c:DLd::n:o:RU:", long_options, &option_index)) != EOF) { switch (r) { case 0: /* long option */ break; @@ -160,7 +176,16 @@ static bool parse_options(int argc, char **argv) { break; case 'n': /* net name given */ - netname = xstrdup(optarg); + /* netname "." is special: a "top-level name" */ + netname = strcmp(optarg, ".") != 0 ? + xstrdup(optarg) : NULL; + break; + + case 'o': /* option */ + cfg = parse_config_line(optarg, NULL, ++lineno); + if (!cfg) + return false; + list_insert_tail(cmdline_conf, cfg); break; case 'R': /* chroot to NETNAME dir */ @@ -243,6 +268,9 @@ static void make_names(void) { if(!logfilename) xasprintf(&logfilename, LOCALSTATEDIR "/log/%s.log", identname); + if(!controlcookiename) + xasprintf(&controlcookiename, LOCALSTATEDIR "/run/%s.cookie", identname); + if(netname) { if(!confbase) xasprintf(&confbase, CONFDIR "/tinc/%s", netname); @@ -254,7 +282,7 @@ static void make_names(void) { } } -static void free_names() { +static void free_names(void) { if (identname) free(identname); if (netname) free(netname); if (controlcookiename) free(controlcookiename); @@ -262,7 +290,7 @@ static void free_names() { if (confbase) free(confbase); } -static bool drop_privs() { +static bool drop_privs(void) { #ifdef HAVE_MINGW if (switchuser) { logger(LOG_ERR, "%s not supported on this platform", "-U"); @@ -311,12 +339,12 @@ static bool drop_privs() { } #ifdef HAVE_MINGW -# define setpriority(level) SetPriorityClass(GetCurrentProcess(), level) +# define setpriority(level) !SetPriorityClass(GetCurrentProcess(), (level)) #else # define NORMAL_PRIORITY_CLASS 0 # define BELOW_NORMAL_PRIORITY_CLASS 10 # define HIGH_PRIORITY_CLASS -10 -# define setpriority(level) nice(level) +# define setpriority(level) (setpriority(PRIO_PROCESS, 0, (level))) #endif int main(int argc, char **argv) { @@ -330,7 +358,7 @@ int main(int argc, char **argv) { if(show_version) { printf("%s version %s (built %s %s, protocol %d)\n", PACKAGE, VERSION, __DATE__, __TIME__, PROT_CURRENT); - printf("Copyright (C) 1998-2009 Ivo Timmermans, Guus Sliepen and others.\n" + printf("Copyright (C) 1998-2011 Ivo Timmermans, Guus Sliepen and others.\n" "See the AUTHORS file for a complete list.\n\n" "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n" "and you are welcome to redistribute it under certain conditions;\n" @@ -370,10 +398,12 @@ int main(int argc, char **argv) { if(!read_server_config()) return 1; +#ifdef HAVE_LZO if(lzo_init() != LZO_E_OK) { logger(LOG_ERR, "Error initializing LZO compressor!"); return 1; } +#endif #ifdef HAVE_MINGW if(!do_detach || !init_service()) @@ -415,16 +445,28 @@ int main2(int argc, char **argv) { /* Change process priority */ - char *priority = 0; + char *priority = NULL; if(get_config_string(lookup_config(config_tree, "ProcessPriority"), &priority)) { - if(!strcasecmp(priority, "Normal")) - setpriority(NORMAL_PRIORITY_CLASS); - else if(!strcasecmp(priority, "Low")) - setpriority(BELOW_NORMAL_PRIORITY_CLASS); - else if(!strcasecmp(priority, "High")) - setpriority(HIGH_PRIORITY_CLASS); - else { + if(!strcasecmp(priority, "Normal")) { + if (setpriority(NORMAL_PRIORITY_CLASS) != 0) { + logger(LOG_ERR, "System call `%s' failed: %s", + "setpriority", strerror(errno)); + goto end; + } + } else if(!strcasecmp(priority, "Low")) { + if (setpriority(BELOW_NORMAL_PRIORITY_CLASS) != 0) { + logger(LOG_ERR, "System call `%s' failed: %s", + "setpriority", strerror(errno)); + goto end; + } + } else if(!strcasecmp(priority, "High")) { + if (setpriority(HIGH_PRIORITY_CLASS) != 0) { + logger(LOG_ERR, "System call `%s' failed: %s", + "setpriority", strerror(errno)); + goto end; + } + } else { logger(LOG_ERR, "Invalid priority `%s`!", priority); goto end; }