Implemented two angel "fake" functions

personal/stbuehler/wip
Stefan Bühler 14 years ago
parent 9d2b053659
commit 324224fe2e
  1. 9
      include/lighttpd/angel.h
  2. 1
      include/lighttpd/base.h
  3. 2
      src/CMakeLists.txt
  4. 13
      src/angel.c
  5. 103
      src/angel_fake.c
  6. 10
      src/log.c
  7. 74
      src/plugin_core.c

@ -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

@ -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>

@ -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

@ -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);
}

@ -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;
}

@ -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;

@ -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,72 +340,14 @@ 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));
}
}
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);
ipstr = val->data.string;
if (-1 == (s = angel_listen(srv, ipstr))) {
ERROR(srv, "%s", "angel_listen failed");
return FALSE;
}
server_listen(srv, s);
return TRUE;
}

Loading…
Cancel
Save