Remove pidfile in favour of control socket.
[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
39         inlen = read(fd, buf, sizeof buf);
40
41         if(inlen <= 0) {
42                 logger(LOG_DEBUG, _("Closing control socket"));
43                 event_del(event);
44                 splay_delete(control_socket_tree, event);
45                 close(fd);
46         }
47 }
48
49 static void handle_new_control_socket(int fd, short events, void *data) {
50         int newfd;
51         struct event *ev;
52
53         newfd = accept(fd, NULL, NULL);
54
55         if(newfd < 0) {
56                 logger(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
57                 event_del(&control_event);
58                 return;
59         }
60
61         ev = xmalloc(sizeof *ev);
62         event_set(ev, newfd, EV_READ | EV_PERSIST, handle_control_data, ev);
63         event_add(ev, NULL);
64         splay_insert(control_socket_tree, ev);
65
66         logger(LOG_DEBUG, _("Control socket connection accepted"));
67 }
68
69 static int control_compare(const struct event *a, const struct event *b) {
70         return a < b ? -1 : a > b ? 1 : 0;
71 }
72
73 bool init_control() {
74         int result;
75         struct sockaddr_un addr;
76
77         if(strlen(controlsocketname) >= sizeof addr.sun_path) {
78                 logger(LOG_ERR, _("Control socket filename too long!"));
79                 return false;
80         }
81
82         memset(&addr, 0, sizeof addr);
83         addr.sun_family = AF_UNIX;
84         strncpy(addr.sun_path, controlsocketname, sizeof addr.sun_path - 1);
85
86         control_socket = socket(PF_UNIX, SOCK_STREAM, 0);
87
88         if(control_socket < 0) {
89                 logger(LOG_ERR, _("Creating UNIX socket failed: %s"), strerror(errno));
90                 return false;
91         }
92
93         //unlink(controlsocketname);
94         result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
95         
96         if(result < 0 && errno == EADDRINUSE) {
97                 result = connect(control_socket, (struct sockaddr *)&addr, sizeof addr);
98                 if(result < 0) {
99                         logger(LOG_WARNING, _("Removing old control socket."));
100                         unlink(controlsocketname);
101                         result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
102                 } else {
103                         close(control_socket);
104                         if(netname)
105                                 logger(LOG_ERR, _("Another tincd is already running for net `%s'."), netname);
106                         else
107                                 logger(LOG_ERR, _("Another tincd is already running."));
108                         return false;
109                 }
110         }
111
112         if(result < 0) {
113                 logger(LOG_ERR, _("Can't bind to %s: %s\n"), controlsocketname, strerror(errno));
114                 close(control_socket);
115                 return false;
116         }
117
118         if(listen(control_socket, 3) < 0) {
119                 logger(LOG_ERR, _("Can't listen on %s: %s\n"), controlsocketname, strerror(errno));
120                 close(control_socket);
121                 return false;
122         }
123
124         control_socket_tree = splay_alloc_tree((splay_compare_t)control_compare, (splay_action_t)free);
125
126         event_set(&control_event, control_socket, EV_READ | EV_PERSIST, handle_new_control_socket, NULL);
127         event_add(&control_event, NULL);
128
129         return true;
130 }
131
132 void exit_control() {
133         event_del(&control_event);
134         close(control_socket);
135         unlink(controlsocketname);
136 }