From: Guus Sliepen Date: Fri, 24 Nov 2000 23:13:07 +0000 (+0000) Subject: Another big & bad commit: X-Git-Tag: import-tinc-1.1~785^2~667 X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=cf49b2c0647554613874cce495e4a7937a9f7863;p=meshlink Another big & bad commit: - Added some extra search functions to rbl routines - Fix subnet_lookup() - Reorder some syslog messages to make more sense - daemon() is back - Don't let scripts execute in parallel (gives race conditions, and anyway something MIGHT just be configured which is necessary for further execution of tinc itself) - Accidently merged check_child() with execute_script(). - Small fixes --- diff --git a/configure.in b/configure.in index 891cc24e..7013b995 100644 --- a/configure.in +++ b/configure.in @@ -1,6 +1,6 @@ dnl Process this file with autoconf to produce a configure script. -dnl $Id: configure.in,v 1.13.2.23 2000/11/24 13:32:26 zarq Exp $ +dnl $Id: configure.in,v 1.13.2.24 2000/11/24 23:12:56 guus Exp $ AC_INIT(src/tincd.c) AM_INIT_AUTOMAKE(tinc, 1.0pre4-cvs) @@ -48,7 +48,7 @@ AC_FUNC_MEMCMP AC_FUNC_ALLOCA AC_TYPE_SIGNAL AC_CHECK_FUNCS([ftime socket select strtol strerror flock unsetenv \ -asprintf putenv strdup fcloseall]) +asprintf putenv strdup fcloseall daemon]) jm_FUNC_MALLOC jm_FUNC_REALLOC diff --git a/lib/Makefile.am b/lib/Makefile.am index 43629d38..7ac630a0 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -1,15 +1,15 @@ ## Process this file with automake to produce Makefile.in -# $Id: Makefile.am,v 1.2.4.3 2000/11/20 19:12:10 guus Exp $ +# $Id: Makefile.am,v 1.2.4.4 2000/11/24 23:12:58 guus Exp $ noinst_LIBRARIES = libvpn.a INCLUDES = -I. -I$(top_builddir) -I$(top_srcdir)/intl -libvpn_a_SOURCES = xmalloc.c pidfile.c utils.c getopt.c getopt1.c list.c rbl.c +libvpn_a_SOURCES = xmalloc.c pidfile.c utils.c getopt.c getopt1.c list.c rbl.c daemon.c libvpn_a_LIBADD = @LIBOBJS@ @ALLOCA@ libvpn_a_DEPENDENCIES = $(libvpn_a_LIBADD) -noinst_HEADERS = xalloc.h pidfile.h utils.h getopt.h list.h rbl.h +noinst_HEADERS = xalloc.h pidfile.h utils.h getopt.h list.h rbl.h daemon.h EXTRA_DIST = README diff --git a/lib/rbl.c b/lib/rbl.c index 1a405350..226e938b 100644 --- a/lib/rbl.c +++ b/lib/rbl.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: rbl.c,v 1.1.2.11 2000/11/22 19:14:08 guus Exp $ + $Id: rbl.c,v 1.1.2.12 2000/11/24 23:12:59 guus Exp $ */ #include "config.h" @@ -88,6 +88,38 @@ rbl_t *rbl_search_closest_rbl(rbltree_t *tree, void *data) return rbl; } +/* Search closest match in the tree */ +rbl_t *rbl_search_closest_greater_rbl(rbltree_t *tree, void *data) +{ + rbl_t *rbl; + + rbl = rbl_search_closest_rbl(tree, data); + + if(rbl) + { + if(tree->compare(data, rbl->data) > 0) + rbl = rbl->next; + } + + return rbl; +} + +/* Search closest match in the tree */ +rbl_t *rbl_search_closest_smaller_rbl(rbltree_t *tree, void *data) +{ + rbl_t *rbl; + + rbl = rbl_search_closest_rbl(tree, data); + + if(rbl) + { + if(tree->compare(data, rbl->data) < 0) + rbl = rbl->next; + } + + return rbl; +} + void *rbl_search_closest(rbltree_t *tree, void *data) { rbl_t *rbl; @@ -100,6 +132,30 @@ void *rbl_search_closest(rbltree_t *tree, void *data) return NULL; } +void *rbl_search_closest_greater(rbltree_t *tree, void *data) +{ + rbl_t *rbl; + + rbl = rbl_search_closest_greater_rbl(tree, data); + + if(rbl) + return rbl->data; + else + return NULL; +} + +void *rbl_search_closest_smaller(rbltree_t *tree, void *data) +{ + rbl_t *rbl; + + rbl = rbl_search_closest_smaller_rbl(tree, data); + + if(rbl) + return rbl->data; + else + return NULL; +} + /* Search exact match or return NULL pointer */ rbl_t *rbl_search_rbl(rbltree_t *tree, void *data) { diff --git a/lib/rbl.h b/lib/rbl.h index 14ef52b2..0a090bd7 100644 --- a/lib/rbl.h +++ b/lib/rbl.h @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: rbl.h,v 1.1.2.7 2000/11/20 19:12:10 guus Exp $ + $Id: rbl.h,v 1.1.2.8 2000/11/24 23:13:00 guus Exp $ */ #ifndef __RBL_H__ @@ -83,8 +83,12 @@ extern void free_rbl(rbl_t *); extern void *rbl_search(rbltree_t *, void *); extern void *rbl_search_closest(rbltree_t *, void *); +extern void *rbl_search_closest_greater(rbltree_t *, void *); +extern void *rbl_search_closest_smaller(rbltree_t *, void *); extern rbl_t *rbl_search_rbl(rbltree_t *, void *); extern rbl_t *rbl_search_closest_rbl(rbltree_t *, void *); +extern rbl_t *rbl_search_closest_greater_rbl(rbltree_t *, void *); +extern rbl_t *rbl_search_closest_smaller_rbl(rbltree_t *, void *); extern rbl_t *rbl_insert(rbltree_t *, void *); extern rbl_t *rbl_unlink(rbltree_t *, void *); extern void rbl_delete(rbltree_t *, void *); diff --git a/src/connection.c b/src/connection.c index 4d0b3a81..7f48697d 100644 --- a/src/connection.c +++ b/src/connection.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: connection.c,v 1.1.2.5 2000/11/22 22:18:03 guus Exp $ + $Id: connection.c,v 1.1.2.6 2000/11/24 23:13:01 guus Exp $ */ #include "config.h" @@ -71,11 +71,8 @@ void init_connections(void) connection_t *new_connection(void) { - connection_t *p = (connection_t *)xmalloc(sizeof(*p)); + connection_t *p = (connection_t *)xmalloc_and_zero(sizeof(*p)); cp - /* initialise all those stupid pointers at once */ - memset(p, '\0', sizeof(*p)); - p->subnet_tree = new_rbltree((rbl_compare_t)subnet_compare, NULL); cp return p; diff --git a/src/net.c b/src/net.c index c16a261b..5e8cfaff 100644 --- a/src/net.c +++ b/src/net.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net.c,v 1.35.4.80 2000/11/20 23:29:46 guus Exp $ + $Id: net.c,v 1.35.4.81 2000/11/24 23:13:02 guus Exp $ */ #include "config.h" @@ -806,8 +806,6 @@ cp myself->status.active = 1; syslog(LOG_NOTICE, _("Ready: listening on port %hd"), myself->port); - - child_pids = list_new(); cp return 0; } @@ -869,12 +867,12 @@ cp if(setup_tap_fd() < 0) return -1; - if(setup_myself() < 0) - return -1; - /* Run tinc-up script to further initialize the tap interface */ execute_script("tinc-up"); + if(setup_myself() < 0) + return -1; + if(!(cfg = get_config_val(config, config_connectto))) /* No upstream IP given, we're listen only. */ return 0; @@ -925,8 +923,6 @@ cp execute_script("tinc-down"); destroy_connection_tree(); - - syslog(LOG_NOTICE, _("Terminating")); cp return; } @@ -1417,8 +1413,6 @@ cp if(FD_ISSET(tap_fd, &fset)) handle_tap_input(); } - - check_children(); } cp } diff --git a/src/process.c b/src/process.c index 33828bbc..1319c0c0 100644 --- a/src/process.c +++ b/src/process.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: process.c,v 1.1.2.13 2000/11/24 12:44:39 zarq Exp $ + $Id: process.c,v 1.1.2.14 2000/11/24 23:13:05 guus Exp $ */ #include "config.h" @@ -47,25 +47,13 @@ #include "system.h" -/* A list containing all our children */ -list_t *child_pids = NULL; - /* If zero, don't detach from the terminal. */ int do_detach = 1; -static pid_t ppid; - extern char *identname; extern char *pidfilename; extern char **g_argv; -void init_processes(void) -{ -cp - child_pids = list_new(); -cp -} - void memory_full(int size) { syslog(LOG_ERR, _("Memory exhausted (couldn't allocate %d bytes), exiting."), size); @@ -87,44 +75,6 @@ int fcloseall(void) } #endif -int become_daemon(void) -{ - pid_t pid; - int fd; - - ppid = getpid(); - - if((pid = fork()) < 0) - { - perror("fork"); - return -1; - } - if(pid) /* parent process */ - { - signal(SIGTERM, parent_exit); - sleep(600); /* wait 10 minutes */ - exit(1); - } - - if((fd = open("/dev/tty", O_RDWR)) >= 0) - { - if(ioctl(fd, TIOCNOTTY, NULL)) - { - perror("ioctl"); - return -1; - } - close(fd); - } - - if(setsid() < 0) - return -1; - - kill(ppid, SIGTERM); - - chdir("/"); - fcloseall(); -} - /* Close network connections, and terminate neatly */ @@ -137,8 +87,9 @@ cp syslog(LOG_INFO, _("Total bytes written: tap %d, socket %d; bytes read: tap %d, socket %d"), total_tap_out, total_socket_out, total_tap_in, total_socket_in); + syslog(LOG_NOTICE, _("Terminating")); + closelog(); - kill(ppid, SIGTERM); exit(c); } @@ -199,13 +150,13 @@ int detach(void) cp setup_signals(); - if(do_detach) - if(become_daemon() < 0) - return -1; - if(write_pidfile()) return -1; + if(do_detach) + if(daemon(0, 0) < 0) + return -1; + openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON); if(debug_lvl > DEBUG_NOTHING) @@ -244,62 +195,21 @@ cp } #endif - if(chdir(confbase) < 0) - /* This cannot fail since we already read config files from this - directory. - Guus */ - /* Yes this can fail, somebody could have removed this directory - when we didn't pay attention. - Ivo */ - { - if(chdir("/") < 0) - /* Now if THIS fails, something wicked is going on. - Ivo */ - syslog(LOG_ERR, _("Couldn't chdir to `/': %m")); - - /* Continue anyway. */ - } + chdir("/"); asprintf(&scriptname, "%s/%s", confbase, name); /* Close all file descriptors */ - closelog(); + closelog(); /* <- this means we cannot use syslog() here anymore! */ fcloseall(); - /* Open standard input */ - if((fd = open("/dev/null", O_RDONLY)) < 0) - { - syslog(LOG_ERR, _("Opening `/dev/null' failed: %m")); - error = 1; - } - if(dup2(fd, 0) != 0) - { - syslog(LOG_ERR, _("Couldn't assign /dev/null to standard input: %m")); - error = 1; - } - - if(!error) - { - close(1); /* fd #1 should be the first available filedescriptor now. */ - /* Standard output directly goes to syslog */ - openlog(name, LOG_CONS | LOG_PID, LOG_DAEMON); - /* Standard error as well */ - if(dup2(1, 2) < 0) - { - syslog(LOG_ERR, _("System call `%s' failed: %m"), - "dup2"); - error = 1; - } - } - - if(error && debug_lvl > 1) - syslog(LOG_INFO, _("This means that any output the script generates will not be shown in syslog.")); - execl(scriptname, NULL); /* No return on success */ - if(errno != ENOENT) /* Ignore if the file does not exist */ - syslog(LOG_WARNING, _("Error executing `%s': %m"), scriptname); - - /* No need to free things */ - exit(0); + if(errno != ENOENT) /* Ignore if the file does not exist */ + exit(-1); /* Some error while trying execl(). */ + else + exit(0); } /* @@ -308,6 +218,7 @@ cp int execute_script(const char *name) { pid_t pid; + int status; cp if((pid = fork()) < 0) { @@ -318,55 +229,45 @@ cp if(pid) { - list_append(child_pids, &pid); - return 0; - } -cp - /* Child here */ - _execute_script(name); -} + if(debug_lvl >= DEBUG_STATUS) + syslog(LOG_INFO, _("Executing script %s"), name); -/* - Check a child (the pointer data is actually an integer, the PID of - that child. A non-zero return value means that the child has exited - and can be removed from our list. -*/ -int check_child(void *data) -{ - pid_t pid; - int status; -cp - pid = (pid_t) data; - pid = waitpid(pid, &status, WNOHANG); - if(WIFEXITED(status)) - { - if(WIFSIGNALED(status)) /* Child was killed by a signal */ - { - syslog(LOG_ERR, _("Child with PID %d was killed by signal %d (%s)"), - pid, WTERMSIG(status), strsignal(WTERMSIG(status))); - return -1; - } - if(WEXITSTATUS(status) != 0) - { - syslog(LOG_INFO, _("Child with PID %d exited with code %d"), - WEXITSTATUS(status)); - } - return -1; + if(waitpid(pid, &status, 0) == pid) + { + if(WIFEXITED(status)) /* Child exited by itself */ + { + if(WEXITSTATUS(status)) + { + syslog(LOG_ERR, _("Process %d (%s) exited with non-zero status %d"), pid, name, WEXITSTATUS(status)); + return -1; + } + else + return 0; + } + else if(WIFSIGNALED(status)) /* Child was killed by a signal */ + { + syslog(LOG_ERR, _("Process %d (%s) was killed by signal %d (%s)"), + pid, name, WTERMSIG(status), strsignal(WTERMSIG(status))); + return -1; + } + else /* Something strange happened */ + { + syslog(LOG_ERR, _("Process %d (%s) terminated abnormaly"), pid, name); + return -1; + } + } + else + { + syslog(LOG_ERR, _("System call `%s' failed: %m"), "waitpid"); + return -1; + } } cp - /* Child is still running */ - return 0; -} + /* Child here */ -/* - Check the status of all our children. -*/ -void check_children(void) -{ - list_forall_nodes(child_pids, check_child); + _execute_script(name); } - /* Signal handlers. */ @@ -392,6 +293,7 @@ RETSIGTYPE sigsegv_square(int a) { syslog(LOG_ERR, _("Got another SEGV signal: not restarting")); + cp_trace(); exit(0); } @@ -475,8 +377,3 @@ setup_signals(void) signal(SIGUSR2, sigusr2_handler); signal(SIGCHLD, SIG_IGN); } - -RETSIGTYPE parent_exit(int a) -{ - exit(0); -} diff --git a/src/process.h b/src/process.h index bd548138..751c1014 100644 --- a/src/process.h +++ b/src/process.h @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: process.h,v 1.1.2.4 2000/11/22 22:18:03 guus Exp $ + $Id: process.h,v 1.1.2.5 2000/11/24 23:13:06 guus Exp $ */ #ifndef __TINC_PROCESS_H__ @@ -26,13 +26,8 @@ #include "config.h" #include -extern list_t *child_pids; - -extern RETSIGTYPE parent_exit(int a); -extern void init_processes(void); extern void setup_signals(void); extern int execute_script(const char *); -extern void check_children(void); extern int detach(void); extern int kill_other(void); extern void cleanup_and_exit(int); diff --git a/src/subnet.c b/src/subnet.c index 8e506462..574e7a06 100644 --- a/src/subnet.c +++ b/src/subnet.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: subnet.c,v 1.1.2.14 2000/11/20 22:13:13 guus Exp $ + $Id: subnet.c,v 1.1.2.15 2000/11/24 23:13:06 guus Exp $ */ #include "config.h" @@ -140,6 +140,7 @@ void subnet_del(subnet_t *subnet) { cp rbl_delete(subnet->owner->subnet_tree, subnet); +cp rbl_delete(subnet_tree, subnet); cp } @@ -257,21 +258,35 @@ cp subnet_t *lookup_subnet_mac(mac_t address) { - subnet_t subnet; + subnet_t subnet, *p; cp subnet.type = SUBNET_MAC; subnet.net.mac.address = address; - return (subnet_t *)rbl_search_closest(subnet_tree, &subnet); + + p = (subnet_t *)rbl_search_closest(subnet_tree, &subnet); +cp + if(p && !memcmp(&address, &p->net.mac.address, sizeof(mac_t))) + return p; + else + return NULL; } subnet_t *lookup_subnet_ipv4(ipv4_t address) { - subnet_t subnet; + subnet_t subnet, *p; cp subnet.type = SUBNET_IPV4; subnet.net.ipv4.address = address; subnet.net.ipv4.mask = 0xFFFFFFFF; - return (subnet_t *)rbl_search_closest(subnet_tree, &subnet); + + p = (subnet_t *)rbl_search_closest_greater(subnet_tree, &subnet); + + /* Check if the found subnet REALLY matches */ +cp + if(p && ((address & p->net.ipv4.mask) == p->net.ipv4.address)) + return p; + else + return NULL; } subnet_t *lookup_subnet_ipv6(ipv6_t address) @@ -281,6 +296,9 @@ cp subnet.type = SUBNET_IPV6; subnet.net.ipv6.address = address; memset(&subnet.net.ipv6.mask, 0xFF, 16); + +/* FIXME: check if it REALLY matches */ + return (subnet_t *)rbl_search_closest(subnet_tree, &subnet); } diff --git a/src/tincd.c b/src/tincd.c index 18791cc8..f98afe41 100644 --- a/src/tincd.c +++ b/src/tincd.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: tincd.c,v 1.10.4.34 2000/11/22 19:14:09 guus Exp $ + $Id: tincd.c,v 1.10.4.35 2000/11/24 23:13:07 guus Exp $ */ #include "config.h" @@ -316,8 +316,6 @@ main(int argc, char **argv, char **envp) if(read_server_config()) return 1; - init_processes(); - if(detach()) exit(0);