Make pid files backwards compatible and add address of listening socket.
The pid is now written first, so that a version 1.0.x tincd can be used to stop a running version 1.1 tincd. Getsockname() is used to determine the address of the first listening socket, so that tincctl can connect to the local tincd even if AddressFamily = ipv6, or if BindToAddress or BindToInterface is used.
This commit is contained in:
parent
a05fa7f882
commit
ab4d289faf
2 changed files with 17 additions and 9 deletions
|
|
@ -26,6 +26,7 @@
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
#include "meta.h"
|
#include "meta.h"
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
|
#include "netutl.h"
|
||||||
#include "protocol.h"
|
#include "protocol.h"
|
||||||
#include "route.h"
|
#include "route.h"
|
||||||
#include "splay_tree.h"
|
#include "splay_tree.h"
|
||||||
|
|
@ -146,8 +147,20 @@ bool init_control(void) {
|
||||||
#else
|
#else
|
||||||
chmod(pidfilename, 0600);
|
chmod(pidfilename, 0600);
|
||||||
#endif
|
#endif
|
||||||
|
// Get the address and port of the first listening socket
|
||||||
|
|
||||||
fprintf(f, "%s %s %d\n", controlcookie, myport, getpid());
|
char *localhost = NULL;
|
||||||
|
sockaddr_t sa;
|
||||||
|
socklen_t len = sizeof sa;
|
||||||
|
|
||||||
|
if(getsockname(listen_socket[0].tcp, (struct sockaddr *)&sa, &len))
|
||||||
|
xasprintf(&localhost, "127.0.0.1 port %d", myport);
|
||||||
|
else
|
||||||
|
localhost = sockaddr2hostname(&sa);
|
||||||
|
|
||||||
|
fprintf(f, "%d %s %s\n", (int)getpid(), controlcookie, localhost);
|
||||||
|
|
||||||
|
free(localhost);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,6 @@ static char *pidfilename = NULL; /* pid file location */
|
||||||
static char controlcookie[1024];
|
static char controlcookie[1024];
|
||||||
char *netname = NULL;
|
char *netname = NULL;
|
||||||
char *confbase = NULL;
|
char *confbase = NULL;
|
||||||
static char *host = NULL;
|
|
||||||
|
|
||||||
#ifdef HAVE_MINGW
|
#ifdef HAVE_MINGW
|
||||||
static struct WSAData wsa_state;
|
static struct WSAData wsa_state;
|
||||||
|
|
@ -52,7 +51,6 @@ static struct WSAData wsa_state;
|
||||||
|
|
||||||
static struct option const long_options[] = {
|
static struct option const long_options[] = {
|
||||||
{"config", required_argument, NULL, 'c'},
|
{"config", required_argument, NULL, 'c'},
|
||||||
{"host", required_argument, NULL, 'h'},
|
|
||||||
{"net", required_argument, NULL, 'n'},
|
{"net", required_argument, NULL, 'n'},
|
||||||
{"help", no_argument, NULL, 1},
|
{"help", no_argument, NULL, 1},
|
||||||
{"version", no_argument, NULL, 2},
|
{"version", no_argument, NULL, 2},
|
||||||
|
|
@ -104,7 +102,7 @@ static bool parse_options(int argc, char **argv) {
|
||||||
int r;
|
int r;
|
||||||
int option_index = 0;
|
int option_index = 0;
|
||||||
|
|
||||||
while((r = getopt_long(argc, argv, "c:n:h:", long_options, &option_index)) != EOF) {
|
while((r = getopt_long(argc, argv, "c:n:", long_options, &option_index)) != EOF) {
|
||||||
switch (r) {
|
switch (r) {
|
||||||
case 0: /* long option */
|
case 0: /* long option */
|
||||||
break;
|
break;
|
||||||
|
|
@ -113,10 +111,6 @@ static bool parse_options(int argc, char **argv) {
|
||||||
confbase = xstrdup(optarg);
|
confbase = xstrdup(optarg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'h': /* alternative host to connect to */
|
|
||||||
host = xstrdup(optarg);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'n': /* net name given */
|
case 'n': /* net name given */
|
||||||
netname = xstrdup(optarg);
|
netname = xstrdup(optarg);
|
||||||
break;
|
break;
|
||||||
|
|
@ -429,6 +423,7 @@ void pcap(int fd, FILE *out) {
|
||||||
int main(int argc, char *argv[], char *envp[]) {
|
int main(int argc, char *argv[], char *envp[]) {
|
||||||
int fd;
|
int fd;
|
||||||
int result;
|
int result;
|
||||||
|
char host[128];
|
||||||
char port[128];
|
char port[128];
|
||||||
int pid;
|
int pid;
|
||||||
|
|
||||||
|
|
@ -487,7 +482,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||||
fprintf(stderr, "Could not open pid file %s: %s\n", pidfilename, strerror(errno));
|
fprintf(stderr, "Could not open pid file %s: %s\n", pidfilename, strerror(errno));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if(fscanf(f, "%1024s %128s %20d", controlcookie, port, &pid) != 3) {
|
if(fscanf(f, "%20d %1024s %128s port %128s", &pid, controlcookie, host, port) != 4) {
|
||||||
fprintf(stderr, "Could not parse pid file %s\n", pidfilename);
|
fprintf(stderr, "Could not parse pid file %s\n", pidfilename);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue