| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
|---|
| 2 |
|
|---|
| 3 |
/* Cherokee |
|---|
| 4 |
* |
|---|
| 5 |
* Authors: |
|---|
| 6 |
* Alvaro Lopez Ortega <alvaro@alobbs.com> |
|---|
| 7 |
* |
|---|
| 8 |
* Copyright (C) 2001-2008 Alvaro Lopez Ortega |
|---|
| 9 |
* |
|---|
| 10 |
* This program is free software; you can redistribute it and/or |
|---|
| 11 |
* modify it under the terms of version 2 of the GNU General Public |
|---|
| 12 |
* License as published by the Free Software Foundation. |
|---|
| 13 |
* |
|---|
| 14 |
* This program is distributed in the hope that it will be useful, |
|---|
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 |
* GNU General Public License for more details. |
|---|
| 18 |
* |
|---|
| 19 |
* You should have received a copy of the GNU General Public License |
|---|
| 20 |
* along with this program; if not, write to the Free Software |
|---|
| 21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|---|
| 22 |
* USA |
|---|
| 23 |
*/ |
|---|
| 24 |
|
|---|
| 25 |
#include "common-internal.h" |
|---|
| 26 |
#include "admin_server.h" |
|---|
| 27 |
|
|---|
| 28 |
#include "server-protected.h" |
|---|
| 29 |
#include "connection-protected.h" |
|---|
| 30 |
#include "connection_info.h" |
|---|
| 31 |
#include "util.h" |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
/* Server Port |
|---|
| 35 |
*/ |
|---|
| 36 |
ret_t |
|---|
| 37 |
cherokee_admin_server_reply_get_port (cherokee_handler_admin_t *ahdl, cherokee_buffer_t *question, cherokee_buffer_t *reply) |
|---|
| 38 |
{ |
|---|
| 39 |
cherokee_server_t *srv = HANDLER_SRV(ahdl); |
|---|
| 40 |
|
|---|
| 41 |
UNUSED(question); |
|---|
| 42 |
|
|---|
| 43 |
cherokee_buffer_add_va (reply, "server.port is %d\n", srv->port); |
|---|
| 44 |
return ret_ok; |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
ret_t |
|---|
| 49 |
cherokee_admin_server_reply_set_port (cherokee_handler_admin_t *ahdl, cherokee_buffer_t *question, cherokee_buffer_t *reply) |
|---|
| 50 |
{ |
|---|
| 51 |
cherokee_server_t *srv = HANDLER_SRV(ahdl); |
|---|
| 52 |
|
|---|
| 53 |
UNUSED(question); |
|---|
| 54 |
|
|---|
| 55 |
srv = srv; /* TODO */ |
|---|
| 56 |
cherokee_buffer_add_str (reply, "ok\n"); |
|---|
| 57 |
return ret_ok; |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
/* Server Port TLS |
|---|
| 62 |
*/ |
|---|
| 63 |
ret_t |
|---|
| 64 |
cherokee_admin_server_reply_get_port_tls (cherokee_handler_admin_t *ahdl, cherokee_buffer_t *question, cherokee_buffer_t *reply) |
|---|
| 65 |
{ |
|---|
| 66 |
cherokee_server_t *srv = HANDLER_SRV(ahdl); |
|---|
| 67 |
|
|---|
| 68 |
UNUSED(question); |
|---|
| 69 |
|
|---|
| 70 |
cherokee_buffer_add_va (reply, "server.port_tls is %d\n", srv->port_tls); |
|---|
| 71 |
return ret_ok; |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
ret_t |
|---|
| 75 |
cherokee_admin_server_reply_set_port_tls (cherokee_handler_admin_t *ahdl, cherokee_buffer_t *question, cherokee_buffer_t *reply) |
|---|
| 76 |
{ |
|---|
| 77 |
cherokee_server_t *srv = HANDLER_SRV(ahdl); |
|---|
| 78 |
|
|---|
| 79 |
UNUSED(question); |
|---|
| 80 |
|
|---|
| 81 |
srv = srv; /* TODO */ |
|---|
| 82 |
cherokee_buffer_add_str (reply, "ok\n"); |
|---|
| 83 |
return ret_ok; |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
/* Get RX/TX |
|---|
| 88 |
*/ |
|---|
| 89 |
ret_t |
|---|
| 90 |
cherokee_admin_server_reply_get_tx (cherokee_handler_admin_t *ahdl, cherokee_buffer_t *question, cherokee_buffer_t *reply) |
|---|
| 91 |
{ |
|---|
| 92 |
ret_t ret; |
|---|
| 93 |
size_t rx, tx; |
|---|
| 94 |
cherokee_server_t *srv = HANDLER_SRV(ahdl); |
|---|
| 95 |
|
|---|
| 96 |
UNUSED(question); |
|---|
| 97 |
|
|---|
| 98 |
ret = cherokee_server_get_total_traffic (srv, &rx, &tx); |
|---|
| 99 |
if (unlikely (ret != ret_ok)) return ret; |
|---|
| 100 |
|
|---|
| 101 |
ret = cherokee_buffer_add_str (reply, "server.tx is "); |
|---|
| 102 |
if (unlikely (ret != ret_ok)) return ret; |
|---|
| 103 |
|
|---|
| 104 |
ret = cherokee_buffer_add_fsize (reply, tx); |
|---|
| 105 |
if (unlikely (ret != ret_ok)) return ret; |
|---|
| 106 |
|
|---|
| 107 |
return ret_ok; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
ret_t |
|---|
| 112 |
cherokee_admin_server_reply_get_rx (cherokee_handler_admin_t *ahdl, cherokee_buffer_t *question, cherokee_buffer_t *reply) |
|---|
| 113 |
{ |
|---|
| 114 |
ret_t ret; |
|---|
| 115 |
size_t rx, tx; |
|---|
| 116 |
cherokee_server_t *srv = HANDLER_SRV(ahdl); |
|---|
| 117 |
|
|---|
| 118 |
UNUSED(question); |
|---|
| 119 |
|
|---|
| 120 |
ret = cherokee_server_get_total_traffic (srv, &rx, &tx); |
|---|
| 121 |
if (unlikely (ret != ret_ok)) return ret; |
|---|
| 122 |
|
|---|
| 123 |
ret = cherokee_buffer_add_str (reply, "server.rx is "); |
|---|
| 124 |
if (unlikely (ret != ret_ok)) return ret; |
|---|
| 125 |
|
|---|
| 126 |
ret = cherokee_buffer_add_fsize (reply, rx); |
|---|
| 127 |
if (unlikely (ret != ret_ok)) return ret; |
|---|
| 128 |
|
|---|
| 129 |
return ret_ok; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
/* Get connections |
|---|
| 134 |
*/ |
|---|
| 135 |
static void |
|---|
| 136 |
serialize_connection (cherokee_connection_info_t *info, cherokee_buffer_t *buf) |
|---|
| 137 |
{ |
|---|
| 138 |
cherokee_buffer_add_va (buf, "[id=%s,ip=%s,phase=%s", |
|---|
| 139 |
info->id.buf, |
|---|
| 140 |
info->ip.buf, |
|---|
| 141 |
info->phase.buf); |
|---|
| 142 |
|
|---|
| 143 |
if (!cherokee_buffer_is_empty(&info->rx)) { |
|---|
| 144 |
cherokee_buffer_add_va (buf, ",rx=%s", info->rx.buf); |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
if (!cherokee_buffer_is_empty(&info->tx)) { |
|---|
| 148 |
cherokee_buffer_add_va (buf, ",tx=%s", info->tx.buf); |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
if (!cherokee_buffer_is_empty(&info->request)) { |
|---|
| 152 |
cherokee_buffer_add_va (buf, ",request=%s", info->request.buf); |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
if (!cherokee_buffer_is_empty(&info->handler)) { |
|---|
| 156 |
cherokee_buffer_add_va (buf, ",handler=%s", info->handler.buf); |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
if (!cherokee_buffer_is_empty(&info->total_size)) { |
|---|
| 160 |
cherokee_buffer_add_va (buf, ",total_size=%s", info->total_size.buf); |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
if (!cherokee_buffer_is_empty(&info->percent)) { |
|---|
| 164 |
cherokee_buffer_add_va (buf, ",percent=%s", info->percent.buf); |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
if (!cherokee_buffer_is_empty(&info->icon)) { |
|---|
| 168 |
cherokee_buffer_add_va (buf, ",icon=%s", info->icon.buf); |
|---|
| 169 |
} |
|---|
| 170 |
|
|---|
| 171 |
cherokee_buffer_add_str (buf, "]"); |
|---|
| 172 |
} |
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
ret_t |
|---|
| 176 |
cherokee_admin_server_reply_get_connections (cherokee_handler_admin_t *ahdl, cherokee_buffer_t *question, cherokee_buffer_t *reply) |
|---|
| 177 |
{ |
|---|
| 178 |
ret_t ret; |
|---|
| 179 |
cherokee_list_t *i, *tmp; |
|---|
| 180 |
cherokee_list_t connections = LIST_HEAD_INIT(connections); |
|---|
| 181 |
cherokee_server_t *server = HANDLER_SRV(ahdl); |
|---|
| 182 |
|
|---|
| 183 |
UNUSED(question); |
|---|
| 184 |
|
|---|
| 185 |
/* Get the connection info list |
|---|
| 186 |
*/ |
|---|
| 187 |
ret = cherokee_connection_info_list_server (&connections, server, HANDLER(ahdl)); |
|---|
| 188 |
switch (ret) { |
|---|
| 189 |
case ret_ok: |
|---|
| 190 |
break; |
|---|
| 191 |
case ret_not_found: |
|---|
| 192 |
cherokee_buffer_add_str (reply, "server.connections are \n"); |
|---|
| 193 |
return ret_ok; |
|---|
| 194 |
case ret_error: |
|---|
| 195 |
return ret_error; |
|---|
| 196 |
default: |
|---|
| 197 |
RET_UNKNOWN(ret); |
|---|
| 198 |
return ret_error; |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
/* Build the string |
|---|
| 202 |
*/ |
|---|
| 203 |
cherokee_buffer_add_str (reply, "server.connections are "); |
|---|
| 204 |
list_for_each (i, &connections) { |
|---|
| 205 |
cherokee_connection_info_t *conn = CONN_INFO(i); |
|---|
| 206 |
|
|---|
| 207 |
/* It won't include details about the admin requests |
|---|
| 208 |
*/ |
|---|
| 209 |
if (!cherokee_buffer_is_empty (&conn->handler) && |
|---|
| 210 |
(!strcmp(conn->handler.buf, "admin"))) { |
|---|
| 211 |
continue; |
|---|
| 212 |
} |
|---|
| 213 |
|
|---|
| 214 |
serialize_connection (conn, reply); |
|---|
| 215 |
} |
|---|
| 216 |
|
|---|
| 217 |
cherokee_buffer_add_str (reply, "\n"); |
|---|
| 218 |
|
|---|
| 219 |
/* Free the connection info objects |
|---|
| 220 |
*/ |
|---|
| 221 |
list_for_each_safe (i, tmp, &connections) { |
|---|
| 222 |
cherokee_connection_info_free (CONN_INFO(i)); |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
return ret_ok; |
|---|
| 226 |
} |
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
ret_t |
|---|
| 230 |
cherokee_admin_server_reply_del_connection (cherokee_handler_admin_t *ahdl, cherokee_buffer_t *question, cherokee_buffer_t *reply) |
|---|
| 231 |
{ |
|---|
| 232 |
ret_t ret; |
|---|
| 233 |
char *begin; |
|---|
| 234 |
cherokee_server_t *server = HANDLER_SRV(ahdl); |
|---|
| 235 |
|
|---|
| 236 |
if (strncmp (question->buf, "del server.connection ", 22)) { |
|---|
| 237 |
return ret_error; |
|---|
| 238 |
} |
|---|
| 239 |
|
|---|
| 240 |
begin = question->buf + 22; |
|---|
| 241 |
ret = cherokee_server_del_connection (server, begin); |
|---|
| 242 |
|
|---|
| 243 |
cherokee_buffer_add_va (reply, "server.connection %s has been deleted\n", begin); |
|---|
| 244 |
return ret_ok; |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
ret_t |
|---|
| 249 |
cherokee_admin_server_reply_get_thread_num (cherokee_handler_admin_t *ahdl, cherokee_buffer_t *question, cherokee_buffer_t *reply) |
|---|
| 250 |
{ |
|---|
| 251 |
cherokee_server_t *srv = HANDLER_SRV(ahdl); |
|---|
| 252 |
|
|---|
| 253 |
UNUSED(question); |
|---|
| 254 |
|
|---|
| 255 |
cherokee_buffer_add_va (reply, "server.thread_num is %d\n", srv->thread_num); |
|---|
| 256 |
return ret_ok; |
|---|
| 257 |
} |
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
ret_t |
|---|
| 261 |
cherokee_admin_server_reply_set_backup_mode (cherokee_handler_admin_t *ahdl, cherokee_buffer_t *question, cherokee_buffer_t *reply) |
|---|
| 262 |
{ |
|---|
| 263 |
ret_t ret; |
|---|
| 264 |
cherokee_server_t *srv = HANDLER_SRV(ahdl); |
|---|
| 265 |
cherokee_boolean_t active; |
|---|
| 266 |
cherokee_boolean_t mode; |
|---|
| 267 |
|
|---|
| 268 |
/* Read if the resquest if for turning it on or off |
|---|
| 269 |
*/ |
|---|
| 270 |
if (cherokee_buffer_cmp_str (question, "set server.backup_mode on") == 0) { |
|---|
| 271 |
mode = true; |
|---|
| 272 |
} else if (cherokee_buffer_cmp_str (question, "set server.backup_mode off") == 0) { |
|---|
| 273 |
mode = false; |
|---|
| 274 |
} else { |
|---|
| 275 |
return ret_error; |
|---|
| 276 |
} |
|---|
| 277 |
|
|---|
| 278 |
/* Do it |
|---|
| 279 |
*/ |
|---|
| 280 |
ret = cherokee_server_set_backup_mode (srv, mode); |
|---|
| 281 |
if (ret != ret_ok) return ret; |
|---|
| 282 |
|
|---|
| 283 |
/* Build the reply |
|---|
| 284 |
*/ |
|---|
| 285 |
cherokee_server_get_backup_mode (srv, &active); |
|---|
| 286 |
|
|---|
| 287 |
if (active) |
|---|
| 288 |
cherokee_buffer_add_str (reply, "server.backup_mode is on\n"); |
|---|
| 289 |
else |
|---|
| 290 |
cherokee_buffer_add_str (reply, "server.backup_mode is off\n"); |
|---|
| 291 |
|
|---|
| 292 |
return ret_ok; |
|---|
| 293 |
} |
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
/* Trace |
|---|
| 297 |
*/ |
|---|
| 298 |
|
|---|
| 299 |
ret_t |
|---|
| 300 |
cherokee_admin_server_reply_get_trace (cherokee_handler_admin_t *ahdl, cherokee_buffer_t *question, cherokee_buffer_t *reply) |
|---|
| 301 |
{ |
|---|
| 302 |
ret_t ret; |
|---|
| 303 |
cherokee_buffer_t *modules_ref = NULL; |
|---|
| 304 |
|
|---|
| 305 |
UNUSED(ahdl); |
|---|
| 306 |
UNUSED(question); |
|---|
| 307 |
|
|---|
| 308 |
ret = cherokee_trace_get_trace (&modules_ref); |
|---|
| 309 |
if (ret != ret_ok) return ret; |
|---|
| 310 |
|
|---|
| 311 |
if (cherokee_buffer_is_empty (modules_ref)) { |
|---|
| 312 |
cherokee_buffer_add_str (reply, "server.trace is None\n"); |
|---|
| 313 |
} else { |
|---|
| 314 |
cherokee_buffer_add_va (reply, "server.trace is %s\n", modules_ref->buf); |
|---|
| 315 |
} |
|---|
| 316 |
|
|---|
| 317 |
return ret_ok; |
|---|
| 318 |
} |
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 |
ret_t |
|---|
| 322 |
cherokee_admin_server_reply_set_trace (cherokee_handler_admin_t *ahdl, cherokee_buffer_t *question, cherokee_buffer_t *reply) |
|---|
| 323 |
{ |
|---|
| 324 |
ret_t ret; |
|---|
| 325 |
|
|---|
| 326 |
UNUSED(ahdl); |
|---|
| 327 |
|
|---|
| 328 |
ret = cherokee_trace_set_modules (question); |
|---|
| 329 |
if (ret != ret_ok) return ret; |
|---|
| 330 |
|
|---|
| 331 |
cherokee_buffer_add_str (reply, "ok\n"); |
|---|
| 332 |
return ret_ok; |
|---|
| 333 |
} |
|---|