Implement "stop" command, and allow tincctl to retrieve a running tincd's PID.
This commit is contained in:
parent
e9043e17c7
commit
8c6131deda
2 changed files with 66 additions and 7 deletions
|
@ -35,16 +35,34 @@ extern char *controlsocketname;
|
||||||
static void handle_control_data(int fd, short events, void *event) {
|
static void handle_control_data(int fd, short events, void *event) {
|
||||||
char buf[MAXBUFSIZE];
|
char buf[MAXBUFSIZE];
|
||||||
size_t inlen;
|
size_t inlen;
|
||||||
|
char *p;
|
||||||
|
|
||||||
inlen = read(fd, buf, sizeof buf);
|
inlen = read(fd, buf, sizeof buf);
|
||||||
|
|
||||||
if(inlen <= 0) {
|
if(inlen <= 0)
|
||||||
|
goto close;
|
||||||
|
|
||||||
|
p = memchr(buf, '\n', sizeof buf);
|
||||||
|
if(!p || p - buf + 1 != inlen)
|
||||||
|
goto malformed;
|
||||||
|
|
||||||
|
*p = 0;
|
||||||
|
|
||||||
|
if(!strcasecmp(buf, "stop")) {
|
||||||
|
logger(LOG_NOTICE, _("Got stop command"));
|
||||||
|
event_loopexit(NULL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
malformed:
|
||||||
|
logger(LOG_DEBUG, _("Malformed control command received"));
|
||||||
|
|
||||||
|
close:
|
||||||
logger(LOG_DEBUG, _("Closing control socket"));
|
logger(LOG_DEBUG, _("Closing control socket"));
|
||||||
event_del(event);
|
event_del(event);
|
||||||
splay_delete(control_socket_tree, event);
|
splay_delete(control_socket_tree, event);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static void handle_new_control_socket(int fd, short events, void *data) {
|
static void handle_new_control_socket(int fd, short events, void *data) {
|
||||||
int newfd;
|
int newfd;
|
||||||
|
|
|
@ -80,6 +80,7 @@ static void usage(bool status) {
|
||||||
" stop Stop tincd.\n"
|
" stop Stop tincd.\n"
|
||||||
" restart Restart tincd.\n"
|
" restart Restart tincd.\n"
|
||||||
" reload Reload configuration of running tincd.\n"
|
" reload Reload configuration of running tincd.\n"
|
||||||
|
" pid Show PID of currently running tincd.\n"
|
||||||
" generate-keys [bits] Generate a new public/private keypair.\n"
|
" generate-keys [bits] Generate a new public/private keypair.\n"
|
||||||
" dump Dump a list of one of the following things:\n"
|
" dump Dump a list of one of the following things:\n"
|
||||||
" nodes - all known nodes in the VPN\n"
|
" nodes - all known nodes in the VPN\n"
|
||||||
|
@ -326,7 +327,7 @@ static void make_names(void) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char *argv[], char *envp[]) {
|
||||||
int fd;
|
int fd;
|
||||||
struct sockaddr_un addr;
|
struct sockaddr_un addr;
|
||||||
program_name = argv[0];
|
program_name = argv[0];
|
||||||
|
@ -363,10 +364,21 @@ int main(int argc, char **argv) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// First handle commands that don't involve connecting to a running tinc daemon.
|
||||||
|
|
||||||
if(!strcasecmp(argv[optind], "generate-keys")) {
|
if(!strcasecmp(argv[optind], "generate-keys")) {
|
||||||
return !keygen(optind > argc ? atoi(argv[optind + 1]) : 1024);
|
return !keygen(optind > argc ? atoi(argv[optind + 1]) : 1024);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!strcasecmp(argv[optind], "start")) {
|
||||||
|
argv[optind] = NULL;
|
||||||
|
execve("tincd", argv, envp);
|
||||||
|
fprintf(stderr, _("Could not start tincd: %s"), strerror(errno));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now handle commands that do involve connecting to a running tinc daemon.
|
||||||
|
|
||||||
if(strlen(controlsocketname) >= sizeof addr.sun_path) {
|
if(strlen(controlsocketname) >= sizeof addr.sun_path) {
|
||||||
fprintf(stderr, _("Control socket filename too long!\n"));
|
fprintf(stderr, _("Control socket filename too long!\n"));
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -387,7 +399,36 @@ int main(int argc, char **argv) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Connected to %s.\n", controlsocketname);
|
struct ucred cred;
|
||||||
|
socklen_t credlen = sizeof cred;
|
||||||
|
|
||||||
|
if(getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &credlen) < 0) {
|
||||||
|
fprintf(stderr, _("Could not obtain PID: %s\n"), strerror(errno));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!strcasecmp(argv[optind], "pid")) {
|
||||||
|
printf("%d\n", cred.pid);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!strcasecmp(argv[optind], "stop")) {
|
||||||
|
write(fd, "stop\n", 5);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!strcasecmp(argv[optind], "reload")) {
|
||||||
|
write(fd, "reload\n", 7);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!strcasecmp(argv[optind], "restart")) {
|
||||||
|
write(fd, "restart\n", 8);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, _("Unknown command `%s'.\n"), argv[optind]);
|
||||||
|
usage(true);
|
||||||
|
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue