2
0
Fork 0

Implemented two angel "fake" functions

personal/stbuehler/wip
Stefan Bühler 2008-12-20 16:25:02 +01:00
parent 9d2b053659
commit 324224fe2e
7 changed files with 143 additions and 69 deletions

View File

@ -3,6 +3,15 @@
/* interface to the angel; implementation needs to work without angel too */
/* listen to a socket */
LI_API int angel_listen(server *srv, GString *str);
/* send log messages while startup to angel, frees the string */
LI_API gboolean angel_log(server *srv, GString *str);
/* angle_fake definitions, only for internal use */
int angel_fake_listen(server *srv, GString *str);
gboolean angel_fake_log(server *srv, GString *str);
#endif

View File

@ -30,6 +30,7 @@
#include <lighttpd/server.h>
#include <lighttpd/worker.h>
#include <lighttpd/angel.h>
#include <lighttpd/condition.h>
#include <lighttpd/options.h>
#include <lighttpd/value.h>

View File

@ -271,6 +271,8 @@ ADD_DEFINITIONS(-DHAVE_CONFIG_H)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/include ${CMAKE_BINARY_DIR}/include)
SET(COMMON_SRC
angel.c
angel_fake.c
actions.c
base.c
chunk.c

13
src/angel.c Normal file
View File

@ -0,0 +1,13 @@
#include <lighttpd/base.h>
#include <lighttpd/angel.h>
/* listen to a socket */
int angel_listen(server *srv, GString *str) {
return angel_fake_listen(srv, str);
}
/* send log messages while startup to angel */
gboolean angel_log(server *srv, GString *str) {
return angel_fake_log(srv, str);
}

103
src/angel_fake.c Normal file
View File

@ -0,0 +1,103 @@
#include <lighttpd/base.h>
#include <lighttpd/angel.h>
/* listen to a socket */
int angel_fake_listen(server *srv, GString *str) {
guint32 ipv4;
#ifdef HAVE_IPV6
guint8 ipv6[16];
#endif
GString *ipstr;
guint16 port = 80;
if (str->str[0] == '[') {
/* ipv6 with port */
gchar *pos = g_strrstr(str->str, "]");
if (NULL == pos) {
ERROR(srv, "%s", "listen: bogus ipv6 format");
return -1;
}
if (pos[1] == ':') {
port = atoi(&pos[2]);
}
ipstr = g_string_new_len(&str->str[1], pos - &str->str[1]);
} else {
/* no brackets, search for :port */
gchar *pos = g_strrstr(str->str, ":");
if (NULL != pos) {
ipstr = g_string_new_len(str->str, pos - str->str);
port = atoi(&pos[1]);
} else {
/* no port, just plain ipv4 or ipv6 address */
ipstr = g_string_new_len(GSTR_LEN(str));
}
}
if (parse_ipv4(ipstr->str, &ipv4, NULL)) {
int s, v;
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = ipv4;
addr.sin_port = htons(port);
g_string_free(ipstr, TRUE);
if (-1 == (s = socket(AF_INET, SOCK_STREAM, 0))) {
ERROR(srv, "Couldn't open socket: %s", g_strerror(errno));
return -1;
}
v = 1;
if (-1 == setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v))) {
close(s);
ERROR(srv, "Couldn't setsockopt(SO_REUSEADDR) to '%s': %s", inet_ntoa(*(struct in_addr*)&ipv4), g_strerror(errno));
return -1;
}
if (-1 == bind(s, (struct sockaddr*)&addr, sizeof(addr))) {
close(s);
ERROR(srv, "Couldn't bind socket to '%s': %s", inet_ntoa(*(struct in_addr*)&ipv4), g_strerror(errno));
return -1;
}
if (-1 == listen(s, 1000)) {
close(s);
ERROR(srv, "Couldn't listen on '%s': %s", inet_ntoa(*(struct in_addr*)&ipv4), g_strerror(errno));
return -1;
}
TRACE(srv, "listen to ipv4: '%s' port: %d", inet_ntoa(*(struct in_addr*)&ipv4), port);
return s;
#ifdef HAVE_IPV6
} else if (parse_ipv6(ipstr->str, ipv6, NULL)) {
/* TODO: IPv6 */
g_string_free(ipstr, TRUE);
ERROR(srv, "%s", "IPv6 not supported yet");
return -1;
#endif
} else {
ERROR(srv, "Invalid ip: '%s'", ipstr->str);
g_string_free(ipstr, TRUE);
return -1;
}
}
/* send log messages while startup to angel */
gboolean angel_fake_log(server *srv, GString *str) {
const char *buf;
guint len;
ssize_t written;
UNUSED(srv);
g_string_prepend(str, "fake: ");
buf = str->str;
len = str->len;
while (len > 0) {
written = write(2, buf, len);
if (written < 0) {
g_string_free(str, TRUE);
return FALSE;
}
len -= written;
buf += written;
}
g_string_free(str, TRUE);
return TRUE;
}

View File

@ -20,6 +20,11 @@ const char *remove_path(const char *path) {
void log_write(server *srv, log_t *log, GString *msg) {
log_entry_t *log_entry;
if (g_atomic_int_get(&srv->state) == SERVER_STARTING) {
angel_log(srv, msg);
return;
}
log_ref(srv, log);
log_entry = g_slice_new(log_entry_t);
@ -118,7 +123,10 @@ gboolean log_write_(server *srv, connection *con, log_level_t log_level, guint f
g_string_append_len(log_line, CONST_STR_LEN("\r\n"));
if (g_atomic_int_get(&srv->state) == SERVER_STARTING) {
log_unref(srv, log);
return angel_log(srv, log_line);
}
log_entry = g_slice_new(log_entry_t);
log_entry->log = log;
log_entry->msg = log_line;

View File

@ -331,12 +331,8 @@ static action* core_profile_mem(server *srv, plugin* p, value *val) {
}
static gboolean core_listen(server *srv, plugin* p, value *val) {
guint32 ipv4;
#ifdef HAVE_IPV6
guint8 ipv6[16];
#endif
GString *ipstr;
guint16 port = 80;
int s;
UNUSED(p);
if (val->type != VALUE_STRING) {
@ -344,71 +340,13 @@ static gboolean core_listen(server *srv, plugin* p, value *val) {
return FALSE;
}
if (val->data.string->str[0] == '[') {
/* ipv6 with port */
gchar *pos = g_strrstr(val->data.string->str, "]");
if (NULL == pos) {
ERROR(srv, "%s", "listen: bogus ipv6 format");
return FALSE;
}
if (pos[1] == ':') {
port = atoi(&pos[2]);
}
ipstr = g_string_new_len(&val->data.string->str[1], pos - &val->data.string->str[1]);
} else {
/* no brackets, search for :port */
gchar *pos = g_strrstr(val->data.string->str, ":");
if (NULL != pos) {
ipstr = g_string_new_len(val->data.string->str, pos - val->data.string->str);
port = atoi(&pos[1]);
} else {
/* no port, just plain ipv4 or ipv6 address */
ipstr = g_string_new_len(GSTR_LEN(val->data.string));
}
ipstr = val->data.string;
if (-1 == (s = angel_listen(srv, ipstr))) {
ERROR(srv, "%s", "angel_listen failed");
return FALSE;
}
if (parse_ipv4(ipstr->str, &ipv4, NULL)) {
int s, v;
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = ipv4;
addr.sin_port = htons(port);
g_string_free(ipstr, TRUE);
if (-1 == (s = socket(AF_INET, SOCK_STREAM, 0))) {
ERROR(srv, "Couldn't open socket: %s", g_strerror(errno));
return FALSE;
}
v = 1;
if (-1 == setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v))) {
close(s);
ERROR(srv, "Couldn't setsockopt(SO_REUSEADDR) to '%s': %s", inet_ntoa(*(struct in_addr*)&ipv4), g_strerror(errno));
return FALSE;
}
if (-1 == bind(s, (struct sockaddr*)&addr, sizeof(addr))) {
close(s);
ERROR(srv, "Couldn't bind socket to '%s': %s", inet_ntoa(*(struct in_addr*)&ipv4), g_strerror(errno));
return FALSE;
}
if (-1 == listen(s, 1000)) {
close(s);
ERROR(srv, "Couldn't listen on '%s': %s", inet_ntoa(*(struct in_addr*)&ipv4), g_strerror(errno));
return FALSE;
}
server_listen(srv, s);
TRACE(srv, "listen to ipv4: '%s' port: %d", inet_ntoa(*(struct in_addr*)&ipv4), port);
#ifdef HAVE_IPV6
} else if (parse_ipv6(ipstr->str, ipv6, NULL)) {
/* TODO: IPv6 */
g_string_free(ipstr, TRUE);
ERROR(srv, "%s", "IPv6 not supported yet");
return FALSE;
#endif
} else {
ERROR(srv, "Invalid ip: '%s'", ipstr->str);
g_string_free(ipstr, TRUE);
return FALSE;
}
server_listen(srv, s);
return TRUE;
}