Implement "stop" command, and allow tincctl to retrieve a running tincd's PID.
[tinc] / src / control.c
1 /*
2     control.c -- Control socket handling.
3     Copyright (C) 2007 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19     $Id$
20 */
21
22 #include <sys/un.h>
23
24 #include "system.h"
25 #include "conf.h"
26 #include "control.h"
27 #include "logger.h"
28 #include "xalloc.h"
29
30 static int control_socket = -1;
31 static struct event control_event;
32 static splay_tree_t *control_socket_tree;
33 extern char *controlsocketname;
34
35 static void handle_control_data(int fd, short events, void *event) {
36         char buf[MAXBUFSIZE];
37         size_t inlen;
38         char *p;
39
40         inlen = read(fd, buf, sizeof buf);
41
42         if(inlen <= 0)
43                 goto close;
44
45         p = memchr(buf, '\n', sizeof buf);
46         if(!p || p - buf + 1 != inlen)
47                 goto malformed;
48
49         *p = 0;
50
51         if(!strcasecmp(buf, "stop")) {
52                 logger(LOG_NOTICE, _("Got stop command"));
53                 event_loopexit(NULL);
54                 return;
55         }
56
57 malformed:
58         logger(LOG_DEBUG, _("Malformed control command received"));
59
60 close:
61         logger(LOG_DEBUG, _("Closing control socket"));
62         event_del(event);
63         splay_delete(control_socket_tree, event);
64         close(fd);
65 }
66
67 static void handle_new_control_socket(int fd, short events, void *data) {
68         int newfd;
69         struct event *ev;
70
71         newfd = accept(fd, NULL, NULL);
72
73         if(newfd < 0) {
74                 logger(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
75                 event_del(&control_event);
76                 return;
77         }
78
79         ev = xmalloc(sizeof *ev);
80         event_set(ev, newfd, EV_READ | EV_PERSIST, handle_control_data, ev);
81         event_add(ev, NULL);
82         splay_insert(control_socket_tree, ev);
83
84         logger(LOG_DEBUG, _("Control socket connection accepted"));
85 }
86
87 static int control_compare(const struct event *a, const struct event *b) {
88         return a < b ? -1 : a > b ? 1 : 0;
89 }
90
91 bool init_control() {
92         int result;
93         struct sockaddr_un addr;
94
95         if(strlen(controlsocketname) >= sizeof addr.sun_path) {
96                 logger(LOG_ERR, _("Control socket filename too long!"));
97                 return false;
98         }
99
100         memset(&addr, 0, sizeof addr);
101         addr.sun_family = AF_UNIX;
102         strncpy(addr.sun_path, controlsocketname, sizeof addr.sun_path - 1);
103
104         control_socket = socket(PF_UNIX, SOCK_STREAM, 0);
105
106         if(control_socket < 0) {
107                 logger(LOG_ERR, _("Creating UNIX socket failed: %s"), strerror(errno));
108                 return false;
109         }
110
111         //unlink(controlsocketname);
112         result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
113         
114         if(result < 0 && errno == EADDRINUSE) {
115                 result = connect(control_socket, (struct sockaddr *)&addr, sizeof addr);
116                 if(result < 0) {
117                         logger(LOG_WARNING, _("Removing old control socket."));
118                         unlink(controlsocketname);
119                         result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
120                 } else {
121                         close(control_socket);
122                         if(netname)
123                                 logger(LOG_ERR, _("Another tincd is already running for net `%s'."), netname);
124                         else
125                                 logger(LOG_ERR, _("Another tincd is already running."));
126                         return false;
127                 }
128         }
129
130         if(result < 0) {
131                 logger(LOG_ERR, _("Can't bind to %s: %s\n"), controlsocketname, strerror(errno));
132                 close(control_socket);
133                 return false;
134         }
135
136         if(listen(control_socket, 3) < 0) {
137                 logger(LOG_ERR, _("Can't listen on %s: %s\n"), controlsocketname, strerror(errno));
138                 close(control_socket);
139                 return false;
140         }
141
142         control_socket_tree = splay_alloc_tree((splay_compare_t)control_compare, (splay_action_t)free);
143
144         event_set(&control_event, control_socket, EV_READ | EV_PERSIST, handle_new_control_socket, NULL);
145         event_add(&control_event, NULL);
146
147         return true;
148 }
149
150 void exit_control() {
151         event_del(&control_event);
152         close(control_socket);
153         unlink(controlsocketname);
154 }