From f5bb64b36ae0807cdd3f241f81a8e933065437f6 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Thu, 17 Jan 2013 16:39:02 +0100 Subject: [PATCH] Move make_names() and related variables to its own source file. --- src/Makefile.am | 4 +- src/bsd/device.c | 1 + src/conf.c | 4 +- src/conf.h | 2 - src/control.c | 2 +- src/cygwin/device.c | 1 + src/graph.c | 1 + src/linux/device.c | 1 + src/logger.c | 2 +- src/mingw/device.c | 1 + src/names.c | 94 ++++++++++++++++++++++++++++++++++++++++++++ src/names.h | 35 +++++++++++++++++ src/net.c | 1 + src/net_setup.c | 1 + src/net_socket.c | 1 + src/process.c | 6 +-- src/solaris/device.c | 1 + src/subnet.c | 1 + src/tincctl.c | 75 +++-------------------------------- src/tincd.c | 67 +------------------------------ src/top.c | 1 + src/uml_device.c | 2 +- src/vde_device.c | 2 +- 23 files changed, 155 insertions(+), 151 deletions(-) create mode 100644 src/names.c create mode 100644 src/names.h diff --git a/src/Makefile.am b/src/Makefile.am index 6a8737b6..ea4da6df 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -9,7 +9,7 @@ tincd_SOURCES = \ buffer.c conf.c connection.c control.c edge.c graph.c logger.c meta.c net.c net_packet.c net_setup.c \ net_socket.c netutl.c node.c process.c protocol.c protocol_auth.c protocol_edge.c protocol_misc.c \ protocol_key.c protocol_subnet.c route.c sptps.c subnet.c subnet_parse.c event.c tincd.c \ - dummy_device.c raw_socket_device.c multicast_device.c + dummy_device.c raw_socket_device.c multicast_device.c names.c if UML tincd_SOURCES += uml_device.c @@ -24,7 +24,7 @@ nodist_tincd_SOURCES = \ tincctl_SOURCES = \ utils.c getopt.c getopt1.c dropin.c \ - info.c list.c subnet_parse.c tincctl.c top.c + info.c list.c subnet_parse.c tincctl.c top.c names.c nodist_tincctl_SOURCES = \ ecdsagen.c rsagen.c diff --git a/src/bsd/device.c b/src/bsd/device.c index dd351459..fa580cac 100644 --- a/src/bsd/device.c +++ b/src/bsd/device.c @@ -24,6 +24,7 @@ #include "conf.h" #include "device.h" #include "logger.h" +#include "names.h" #include "net.h" #include "route.h" #include "utils.h" diff --git a/src/conf.c b/src/conf.c index b9bfbf67..46fd81d5 100644 --- a/src/conf.c +++ b/src/conf.c @@ -28,6 +28,7 @@ #include "conf.h" #include "list.h" #include "logger.h" +#include "names.h" #include "netutl.h" /* for str2address */ #include "protocol.h" #include "utils.h" /* for cp */ @@ -37,11 +38,8 @@ splay_tree_t *config_tree; int pinginterval = 0; /* seconds between pings */ int pingtimeout = 0; /* seconds to wait for response */ -char *confbase = NULL; /* directory in which all config files are */ -char *netname = NULL; /* name of the vpn network */ list_t *cmdline_conf = NULL; /* global/host configuration values given at the command line */ - static int config_compare(const config_t *a, const config_t *b) { int result; diff --git a/src/conf.h b/src/conf.h index 20a78a91..5395322e 100644 --- a/src/conf.h +++ b/src/conf.h @@ -39,8 +39,6 @@ extern int pinginterval; extern int pingtimeout; extern int maxtimeout; extern bool bypass_security; -extern char *confbase; -extern char *netname; extern list_t *cmdline_conf; extern void init_configuration(splay_tree_t **); diff --git a/src/control.c b/src/control.c index 83a9d79c..cc5bbb0a 100644 --- a/src/control.c +++ b/src/control.c @@ -25,6 +25,7 @@ #include "graph.h" #include "logger.h" #include "meta.h" +#include "names.h" #include "net.h" #include "netutl.h" #include "protocol.h" @@ -33,7 +34,6 @@ #include "xalloc.h" char controlcookie[65]; -extern char *pidfilename; static bool control_return(connection_t *c, int type, int error) { return send_request(c, "%d %d %d", CONTROL, type, error); diff --git a/src/cygwin/device.c b/src/cygwin/device.c index 6266e87e..c028cbf9 100644 --- a/src/cygwin/device.c +++ b/src/cygwin/device.c @@ -26,6 +26,7 @@ #include "conf.h" #include "device.h" #include "logger.h" +#include "names.h" #include "net.h" #include "route.h" #include "utils.h" diff --git a/src/graph.c b/src/graph.c index 4cf30eb7..e5f70902 100644 --- a/src/graph.c +++ b/src/graph.c @@ -51,6 +51,7 @@ #include "graph.h" #include "list.h" #include "logger.h" +#include "names.h" #include "netutl.h" #include "node.h" #include "process.h" diff --git a/src/linux/device.c b/src/linux/device.c index 18f1b6e5..45df6382 100644 --- a/src/linux/device.c +++ b/src/linux/device.c @@ -26,6 +26,7 @@ #include "conf.h" #include "device.h" #include "logger.h" +#include "names.h" #include "net.h" #include "route.h" #include "utils.h" diff --git a/src/logger.c b/src/logger.c index e01980e9..6fb972d1 100644 --- a/src/logger.c +++ b/src/logger.c @@ -22,6 +22,7 @@ #include "conf.h" #include "meta.h" +#include "names.h" #include "logger.h" #include "connection.h" #include "control_common.h" @@ -30,7 +31,6 @@ debug_t debug_level = DEBUG_NOTHING; static logmode_t logmode = LOGMODE_STDERR; static pid_t logpid; -extern char *logfilename; static FILE *logfile = NULL; #ifdef HAVE_MINGW static HANDLE loghandle = NULL; diff --git a/src/mingw/device.c b/src/mingw/device.c index 3b1458c6..00644dba 100644 --- a/src/mingw/device.c +++ b/src/mingw/device.c @@ -26,6 +26,7 @@ #include "conf.h" #include "device.h" #include "logger.h" +#include "names.h" #include "net.h" #include "route.h" #include "utils.h" diff --git a/src/names.c b/src/names.c new file mode 100644 index 00000000..5a5270a9 --- /dev/null +++ b/src/names.c @@ -0,0 +1,94 @@ +/* + names.c -- generate commonly used (file)names + Copyright (C) 1998-2005 Ivo Timmermans + 2000-2013 Guus Sliepen + + 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 + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "system.h" + +#include "logger.h" +#include "xalloc.h" + +char *netname = NULL; +char *confdir = NULL; /* base configuration directory */ +char *confbase = NULL; /* base configuration directory for this instance of tinc */ +char *identname = NULL; /* program name for syslog */ +char *logfilename = NULL; /* log file location */ +char *pidfilename = NULL; +char *program_name = NULL; + +/* + Set all files and paths according to netname +*/ +void make_names(void) { +#ifdef HAVE_MINGW + HKEY key; + char installdir[1024] = ""; + long len = sizeof installdir; +#endif + + if(netname) + xasprintf(&identname, "tinc.%s", netname); + else + identname = xstrdup("tinc"); + +#ifdef HAVE_MINGW + if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) { + if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) { + confdir = xstrdup(installdir); + if(!logfilename) + xasprintf(&logfilename, "%s" SLASH "log" SLASH "%s.log", installdir, identname); + if(!confbase) { + if(netname) + xasprintf(&confbase, "%s" SLASH "%s", installdir, netname); + else + xasprintf(&confbase, "%s", installdir); + } + if(!pidfilename) + xasprintf(&pidfilename, "%s" SLASH "pid", confbase); + } + RegCloseKey(key); + } +#endif + if(!confdir) + confdir = xstrdup(CONFDIR); + + if(!logfilename) + xasprintf(&logfilename, LOCALSTATEDIR SLASH "log" SLASH "%s.log", identname); + + if(!pidfilename) + xasprintf(&pidfilename, LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname); + + if(netname) { + if(!confbase) + xasprintf(&confbase, CONFDIR SLASH "tinc" SLASH "%s", netname); + else + logger(DEBUG_ALWAYS, LOG_INFO, "Both netname and configuration directory given, using the latter..."); + } else { + if(!confbase) + xasprintf(&confbase, CONFDIR SLASH "tinc"); + } +} + +void free_names(void) { + free(identname); + free(netname); + free(pidfilename); + free(logfilename); + free(confbase); + free(confdir); +} diff --git a/src/names.h b/src/names.h new file mode 100644 index 00000000..aef8b45a --- /dev/null +++ b/src/names.h @@ -0,0 +1,35 @@ +/* + names.h -- header for names.c + Copyright (C) 1998-2005 Ivo Timmermans + 2000-2013 Guus Sliepen + + 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 + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef __TINC_NAMES_H__ +#define __TINC_NAMES_H__ + +extern char *confdir; +extern char *confbase; +extern char *netname; +extern char *identname; +extern char *logfilename; +extern char *pidfilename; +extern char *program_name; + +extern void make_names(void); +extern void free_names(void); + +#endif /* __TINC_NAMES_H__ */ diff --git a/src/net.c b/src/net.c index 343ac717..f34c8c63 100644 --- a/src/net.c +++ b/src/net.c @@ -29,6 +29,7 @@ #include "graph.h" #include "logger.h" #include "meta.h" +#include "names.h" #include "net.h" #include "netutl.h" #include "process.h" diff --git a/src/net_setup.c b/src/net_setup.c index c5c83e7b..0e424eef 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -31,6 +31,7 @@ #include "ecdsa.h" #include "graph.h" #include "logger.h" +#include "names.h" #include "net.h" #include "netutl.h" #include "process.h" diff --git a/src/net_socket.c b/src/net_socket.c index 212649ba..051955bf 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -27,6 +27,7 @@ #include "list.h" #include "logger.h" #include "meta.h" +#include "names.h" #include "net.h" #include "netutl.h" #include "protocol.h" diff --git a/src/process.c b/src/process.c index 2fd3d936..04c19ed9 100644 --- a/src/process.c +++ b/src/process.c @@ -27,6 +27,7 @@ #include "edge.h" #include "event.h" #include "logger.h" +#include "names.h" #include "net.h" #include "node.h" #include "process.h" @@ -38,17 +39,12 @@ bool do_detach = true; bool sigalrm = false; -extern char *identname; extern char **g_argv; extern bool use_logfile; /* Some functions the less gifted operating systems might lack... */ #ifdef HAVE_MINGW -extern char *identname; -extern char *program_name; -extern char **g_argv; - static SC_HANDLE manager = NULL; static SC_HANDLE service = NULL; static SERVICE_STATUS status = {0}; diff --git a/src/solaris/device.c b/src/solaris/device.c index cb2ece79..03f7fe9e 100644 --- a/src/solaris/device.c +++ b/src/solaris/device.c @@ -28,6 +28,7 @@ #include "conf.h" #include "device.h" #include "logger.h" +#include "names.h" #include "net.h" #include "utils.h" #include "xalloc.h" diff --git a/src/subnet.c b/src/subnet.c index 4b6c0686..06fee32f 100644 --- a/src/subnet.c +++ b/src/subnet.c @@ -25,6 +25,7 @@ #include "device.h" #include "hash.h" #include "logger.h" +#include "names.h" #include "net.h" #include "netutl.h" #include "node.h" diff --git a/src/tincctl.c b/src/tincctl.c index 6b36aa74..1576ba86 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -31,6 +31,7 @@ #include "control_common.h" #include "ecdsagen.h" #include "info.h" +#include "names.h" #include "rsagen.h" #include "utils.h" #include "tincctl.h" @@ -40,10 +41,6 @@ #define mkdir(a, b) mkdir(a) #endif - -/* The name this program was run with. */ -static char *program_name = NULL; - static char **orig_argv; static int orig_argc; @@ -54,12 +51,7 @@ static bool show_help = false; static bool show_version = false; static char *name = NULL; -static char *identname = NULL; /* program name for syslog */ -static char *pidfilename = NULL; /* pid file location */ -static char *confdir = NULL; static char controlcookie[1025]; -char *netname = NULL; -char *confbase = NULL; static char *tinc_conf = NULL; static char *hosts_dir = NULL; struct timeval now; @@ -107,10 +99,9 @@ static void version(void) { } static void usage(bool status) { - if(status) - fprintf(stderr, "Try `%s --help\' for more information.\n", - program_name); - else { + if(status) { + fprintf(stderr, "Try `%s --help\' for more information.\n", program_name); + } else { printf("Usage: %s [options] command\n\n", program_name); printf("Valid options are:\n" " -c, --config=DIR Read configuration options from DIR.\n" @@ -455,62 +446,6 @@ static bool rsa_keygen(int bits, bool ask) { return true; } -/* - Set all files and paths according to netname -*/ -static void make_names(void) { -#ifdef HAVE_MINGW - HKEY key; - char installdir[1024] = ""; - long len = sizeof installdir; -#endif - - if(netname) - xasprintf(&identname, "tinc.%s", netname); - else - identname = xstrdup("tinc"); - -#ifdef HAVE_MINGW - if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) { - if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) { - if(!confbase) { - if(netname) - xasprintf(&confbase, "%s" SLASH "%s", installdir, netname); - else - xasprintf(&confbase, "%s", installdir); - } - } - if(!pidfilename) - xasprintf(&pidfilename, "%s" SLASH "pid", confbase); - RegCloseKey(key); - } - - if(!*installdir) { -#endif - confdir = xstrdup(CONFDIR); - - if(!pidfilename) - xasprintf(&pidfilename, "%s" SLASH "run" SLASH "%s.pid", LOCALSTATEDIR, identname); - - if(netname) { - if(!confbase) - xasprintf(&confbase, CONFDIR SLASH "tinc" SLASH "%s", netname); - else - fprintf(stderr, "Both netname and configuration directory given, using the latter...\n"); - } else { - if(!confbase) - xasprintf(&confbase, CONFDIR SLASH "tinc"); - } - -#ifdef HAVE_MINGW - } else - confdir = xstrdup(installdir); -#endif - - xasprintf(&tinc_conf, "%s" SLASH "tinc.conf", confbase); - xasprintf(&hosts_dir, "%s" SLASH "hosts", confbase); -} - static char buffer[4096]; static size_t blen = 0; @@ -2285,6 +2220,8 @@ int main(int argc, char *argv[]) { return 1; make_names(); + xasprintf(&tinc_conf, "%s" SLASH "tinc.conf", confbase); + xasprintf(&hosts_dir, "%s" SLASH "hosts", confbase); if(show_version) { version(); diff --git a/src/tincd.c b/src/tincd.c index 5d715891..cd49400b 100644 --- a/src/tincd.c +++ b/src/tincd.c @@ -56,6 +56,7 @@ #include "crypto.h" #include "device.h" #include "logger.h" +#include "names.h" #include "net.h" #include "netutl.h" #include "process.h" @@ -63,9 +64,6 @@ #include "utils.h" #include "xalloc.h" -/* The name this program was run with. */ -char *program_name = NULL; - /* If nonzero, display usage information and exit. */ static bool show_help = false; @@ -87,9 +85,6 @@ static const char *switchuser = NULL; /* If nonzero, write log entries to a separate file. */ bool use_logfile = false; -char *identname = NULL; /* program name for syslog */ -char *logfilename = NULL; /* log file location */ -char *pidfilename = NULL; char **g_argv; /* a copy of the cmdline arguments */ static int status = 1; @@ -256,66 +251,6 @@ static bool parse_options(int argc, char **argv) { return true; } -/* - Set all files and paths according to netname -*/ -static void make_names(void) { -#ifdef HAVE_MINGW - HKEY key; - char installdir[1024] = ""; - long len = sizeof installdir; -#endif - - if(netname) - xasprintf(&identname, "tinc.%s", netname); - else - identname = xstrdup("tinc"); - -#ifdef HAVE_MINGW - if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) { - if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) { - if(!logfilename) - xasprintf(&logfilename, "%s" SLASH "log" SLASH "%s.log", identname); - if(!confbase) { - if(netname) - xasprintf(&confbase, "%s" SLASH "%s", installdir, netname); - else - xasprintf(&confbase, "%s", installdir); - } - if(!pidfilename) - xasprintf(&pidfilename, "%s" SLASH "pid", confbase); - } - RegCloseKey(key); - if(*installdir) - return; - } -#endif - - if(!logfilename) - xasprintf(&logfilename, LOCALSTATEDIR SLASH "log" SLASH "%s.log", identname); - - if(!pidfilename) - xasprintf(&pidfilename, LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname); - - if(netname) { - if(!confbase) - xasprintf(&confbase, CONFDIR SLASH "tinc" SLASH "%s", netname); - else - logger(DEBUG_ALWAYS, LOG_INFO, "Both netname and configuration directory given, using the latter..."); - } else { - if(!confbase) - xasprintf(&confbase, CONFDIR SLASH "tinc"); - } -} - -static void free_names(void) { - if (identname) free(identname); - if (netname) free(netname); - if (pidfilename) free(pidfilename); - if (logfilename) free(logfilename); - if (confbase) free(confbase); -} - static bool drop_privs(void) { #ifdef HAVE_MINGW return false; diff --git a/src/top.c b/src/top.c index 28ab56f4..aafc2a44 100644 --- a/src/top.c +++ b/src/top.c @@ -25,6 +25,7 @@ #include "control_common.h" #include "list.h" +#include "names.h" #include "tincctl.h" #include "top.h" #include "xalloc.h" diff --git a/src/uml_device.c b/src/uml_device.c index afdded5e..b5f9c12b 100644 --- a/src/uml_device.c +++ b/src/uml_device.c @@ -24,6 +24,7 @@ #include "conf.h" #include "device.h" +#include "names.h" #include "net.h" #include "logger.h" #include "utils.h" @@ -37,7 +38,6 @@ static int write_fd = -1; static int state = 0; static char *device_info; -extern char *identname; extern volatile bool running; static uint64_t device_total_in = 0; diff --git a/src/vde_device.c b/src/vde_device.c index 815b956f..64394af9 100644 --- a/src/vde_device.c +++ b/src/vde_device.c @@ -23,6 +23,7 @@ #include "conf.h" #include "device.h" +#include "names.h" #include "net.h" #include "logger.h" #include "utils.h" @@ -35,7 +36,6 @@ static int port = 0; static char *group = NULL; static char *device_info; -extern char *identname; extern volatile bool running; static uint64_t device_total_in = 0; -- 2.20.1