Add a load-tester control socket to manually trigger initiation
[strongswan.git] / src / libcharon / plugins / load_tester / load_tester_control.c
1 /*
2 * Copyright (C) 2012 Martin Willi
3 * Copyright (C) 2012 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "load_tester_control.h"
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <unistd.h>
23 #include <errno.h>
24
25 #include <daemon.h>
26 #include <threading/thread.h>
27 #include <processing/jobs/callback_job.h>
28
29 typedef struct private_load_tester_control_t private_load_tester_control_t;
30
31 /**
32 * Private data of an load_tester_control_t object.
33 */
34 struct private_load_tester_control_t {
35
36 /**
37 * Public load_tester_control_t interface.
38 */
39 load_tester_control_t public;
40
41 /**
42 * Load tester unix socket file descriptor
43 */
44 int socket;
45 };
46
47 /**
48 * Open load-tester listening socket
49 */
50 static bool open_socket(private_load_tester_control_t *this)
51 {
52 struct sockaddr_un addr;
53 mode_t old;
54
55 addr.sun_family = AF_UNIX;
56 strcpy(addr.sun_path, LOAD_TESTER_SOCKET);
57
58 this->socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
59 if (this->socket == -1)
60 {
61 DBG1(DBG_CFG, "creating load-tester socket failed");
62 return FALSE;
63 }
64 unlink(addr.sun_path);
65 old = umask(~(S_IRWXU | S_IRWXG));
66 if (bind(this->socket, (struct sockaddr*)&addr, sizeof(addr)) < 0)
67 {
68 DBG1(DBG_CFG, "binding load-tester socket failed: %s", strerror(errno));
69 close(this->socket);
70 return FALSE;
71 }
72 umask(old);
73 if (chown(addr.sun_path, charon->caps->get_uid(charon->caps),
74 charon->caps->get_gid(charon->caps)) != 0)
75 {
76 DBG1(DBG_CFG, "changing load-tester socket permissions failed: %s",
77 strerror(errno));
78 }
79 if (listen(this->socket, 10) < 0)
80 {
81 DBG1(DBG_CFG, "listening on load-tester socket failed: %s", strerror(errno));
82 close(this->socket);
83 unlink(addr.sun_path);
84 return FALSE;
85 }
86 return TRUE;
87 }
88
89 /**
90 * Initiate load-test, write progress to stream
91 */
92 static job_requeue_t initiate(FILE *stream)
93 {
94 enumerator_t *enumerator;
95 peer_cfg_t *peer_cfg;
96 child_cfg_t *child_cfg;
97 u_int i, count;
98 char buf[16] = "";
99
100 fflush(stream);
101 if (fgets(buf, sizeof(buf), stream) == NULL)
102 {
103 return JOB_REQUEUE_NONE;
104 }
105 if (sscanf(buf, "%u", &count) != 1)
106 {
107 return JOB_REQUEUE_NONE;
108 }
109
110 peer_cfg = charon->backends->get_peer_cfg_by_name(charon->backends,
111 "load-test");
112 if (!peer_cfg)
113 {
114 return JOB_REQUEUE_NONE;
115 }
116 enumerator = peer_cfg->create_child_cfg_enumerator(peer_cfg);
117 if (!enumerator->enumerate(enumerator, &child_cfg))
118 {
119 enumerator->destroy(enumerator);
120 return JOB_REQUEUE_NONE;
121 }
122 enumerator->destroy(enumerator);
123
124 for (i = 0; i < count; i++)
125 {
126 if (charon->controller->initiate(charon->controller,
127 peer_cfg->get_ref(peer_cfg),
128 child_cfg->get_ref(child_cfg),
129 controller_cb_empty, NULL, 0) == SUCCESS)
130 {
131 fprintf(stream, ".");
132 }
133 else
134 {
135 fprintf(stream, "!");
136 }
137 fflush(stream);
138 }
139 peer_cfg->destroy(peer_cfg);
140 fprintf(stream, "\n");
141
142 return JOB_REQUEUE_NONE;
143 }
144
145 /**
146 * Accept load-tester control connections, dispatch
147 */
148 static job_requeue_t receive(private_load_tester_control_t *this)
149 {
150 struct sockaddr_un addr;
151 int fd, len = sizeof(addr);
152 bool oldstate;
153 FILE *stream;
154
155 oldstate = thread_cancelability(TRUE);
156 fd = accept(this->socket, (struct sockaddr*)&addr, &len);
157 thread_cancelability(oldstate);
158
159 if (fd != -1)
160 {
161 stream = fdopen(fd, "r+");
162 if (stream)
163 {
164 DBG1(DBG_CFG, "client connected");
165 lib->processor->queue_job(lib->processor,
166 (job_t*)callback_job_create_with_prio(
167 (callback_job_cb_t)initiate, stream, (void*)fclose,
168 (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
169 }
170 else
171 {
172 close(fd);
173 }
174 }
175 return JOB_REQUEUE_FAIR;
176 }
177
178 METHOD(load_tester_control_t, destroy, void,
179 private_load_tester_control_t *this)
180 {
181 if (this->socket != -1)
182 {
183 close(this->socket);
184 }
185 free(this);
186 }
187
188 /**
189 * See header
190 */
191 load_tester_control_t *load_tester_control_create()
192 {
193 private_load_tester_control_t *this;
194
195 INIT(this,
196 .public = {
197 .destroy = _destroy,
198 },
199 );
200
201 if (open_socket(this))
202 {
203 lib->processor->queue_job(lib->processor, (job_t*)
204 callback_job_create_with_prio((callback_job_cb_t)receive, this, NULL,
205 (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
206 }
207 else
208 {
209 this->socket = -1;
210 }
211
212 return &this->public;
213 }