X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fcontrol.c;h=550384f20bdc3d3f22ff48fe4671aa646fd3edc9;hb=c45a3fd7319d03bd147448a017f5aaed3b46fdfe;hp=175f2bd019f66771350086e560eb0f7d1e26713d;hpb=0871c3095151bce6a4031a2662aa51b7193b855c;p=tinc diff --git a/src/control.c b/src/control.c index 175f2bd0..550384f2 100644 --- a/src/control.c +++ b/src/control.c @@ -18,7 +18,6 @@ */ #include "system.h" -#include "crypto.h" #include "conf.h" #include "control.h" #include "control_common.h" @@ -30,6 +29,8 @@ #include "route.h" #include "utils.h" #include "xalloc.h" +#include "random.h" +#include "pidfile.h" char controlcookie[65]; @@ -129,11 +130,15 @@ bool control_h(connection_t *c, const char *request) { pcap = true; return true; - case REQ_LOG: - sscanf(request, "%*d %*d %d", &c->outcompression); + case REQ_LOG: { + int level = 0, colorize = 0; + sscanf(request, "%*d %*d %d %d", &level, &colorize); + c->log_level = CLAMP(level, DEBUG_UNSET, DEBUG_SCARY_THINGS); c->status.log = true; + c->status.log_color = colorize; logcontrol = true; return true; + } default: return send_request(c, "%d %d", CONTROL, REQ_INVALID); @@ -144,16 +149,6 @@ bool init_control(void) { randomize(controlcookie, sizeof(controlcookie) / 2); bin2hex(controlcookie, controlcookie, sizeof(controlcookie) / 2); - mode_t mask = umask(0); - umask(mask | 077); - FILE *f = fopen(pidfilename, "w"); - umask(mask); - - if(!f) { - logger(DEBUG_ALWAYS, LOG_ERR, "Cannot write control socket cookie file %s: %s", pidfilename, strerror(errno)); - return false; - } - // Get the address and port of the first listening socket char *localhost = NULL; @@ -163,7 +158,7 @@ bool init_control(void) { // Make sure we have a valid address, and map 0.0.0.0 and :: to 127.0.0.1 and ::1. if(getsockname(listen_socket[0].tcp.fd, &sa.sa, &len)) { - xasprintf(&localhost, "127.0.0.1 port %s", myport); + xasprintf(&localhost, "127.0.0.1 port %s", myport.tcp); } else { if(sa.sa.sa_family == AF_INET) { if(sa.in.sin_addr.s_addr == 0) { @@ -180,12 +175,15 @@ bool init_control(void) { localhost = sockaddr2hostname(&sa); } - fprintf(f, "%d %s %s\n", (int)getpid(), controlcookie, localhost); - + bool wrote = write_pidfile(controlcookie, localhost); free(localhost); - fclose(f); -#ifndef HAVE_MINGW + if(!wrote) { + logger(DEBUG_ALWAYS, LOG_ERR, "Cannot write control socket cookie file %s: %s", pidfilename, strerror(errno)); + return false; + } + +#ifndef HAVE_WINDOWS int unix_fd = socket(AF_UNIX, SOCK_STREAM, 0); if(unix_fd < 0) { @@ -211,6 +209,7 @@ bool init_control(void) { unlink(unixsocketname); + mode_t mask = umask(0); umask(mask | 077); int result = bind(unix_fd, (struct sockaddr *)&sa_un, sizeof(sa_un)); umask(mask); @@ -232,7 +231,7 @@ bool init_control(void) { } void exit_control(void) { -#ifndef HAVE_MINGW +#ifndef HAVE_WINDOWS unlink(unixsocketname); io_del(&unix_socket); close(unix_socket.fd);