added mutexes for server und connection structs; log_write_() proposal added
This commit is contained in:
parent
6b8bbdabde
commit
acf42d82b4
|
@ -25,6 +25,10 @@ struct server {
|
|||
size_t option_count;
|
||||
GHashTable *options;
|
||||
gpointer *option_def_values;
|
||||
|
||||
GMutex *mutex;
|
||||
|
||||
gint error_log_fd;
|
||||
};
|
||||
|
||||
struct connection {
|
||||
|
@ -36,6 +40,10 @@ struct connection {
|
|||
|
||||
request request;
|
||||
physical physical;
|
||||
|
||||
GMutex *mutex;
|
||||
|
||||
gint error_log_fd;
|
||||
};
|
||||
|
||||
server* server_new();
|
||||
|
|
|
@ -32,8 +32,8 @@ int main(int argc, char *argv[]) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
if (show_version)
|
||||
{
|
||||
/* -v, show version and exit */
|
||||
if (show_version) {
|
||||
g_print("%s-%s - a fast and lightweight webserver\n", PACKAGE_NAME, PACKAGE_VERSION);
|
||||
g_print("Build date: %s\n", PACKAGE_BUILD_DATE);
|
||||
return 0;
|
||||
|
@ -50,8 +50,7 @@ int main(int argc, char *argv[]) {
|
|||
if (!luaconfig) {
|
||||
/* standard config frontend */
|
||||
GList *cpd_stack = NULL;
|
||||
if (!config_parser_file(srv, &cpd_stack, config_path))
|
||||
{
|
||||
if (!config_parser_file(srv, &cpd_stack, config_path)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
|
62
src/log.c
62
src/log.c
|
@ -29,3 +29,65 @@ int log_write(server* UNUSED_PARAM(srv), connection* UNUSED_PARAM(con), const ch
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
gboolean log_write_(server *srv, connection *con, const char *fmt, ...) {
|
||||
va_list ap;
|
||||
GString *log_line;
|
||||
static GStaticMutex log_mutex = G_STATIC_MUTEX_INIT;
|
||||
gint log_fd;
|
||||
gssize bytes_written;
|
||||
gssize write_res;
|
||||
|
||||
if (con != NULL) {
|
||||
/* get fd from connection */
|
||||
g_mutex_lock(con->mutex);
|
||||
log_fd = con->error_log_fd;
|
||||
g_mutex_unlock(con->mutex);
|
||||
}
|
||||
else {
|
||||
/* get fd from server */
|
||||
g_mutex_lock(srv->mutex);
|
||||
log_fd = srv->error_log_fd;
|
||||
g_mutex_unlock(srv->mutex);
|
||||
}
|
||||
|
||||
log_line = g_string_sized_new(0);
|
||||
va_start(ap, fmt);
|
||||
g_string_vprintf(log_line, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
g_string_append_len(log_line, CONST_STR_LEN("\r\n"));
|
||||
|
||||
bytes_written = 0;
|
||||
|
||||
/* lock to ensure that multiple threads don't mess up the logs */
|
||||
g_static_mutex_lock(&log_mutex);
|
||||
while (bytes_written < (gssize)log_line->len) {
|
||||
write_res = write(log_fd, log_line->str + bytes_written, log_line->len - bytes_written);
|
||||
|
||||
assert(write_res <= (gssize) log_line->len);
|
||||
|
||||
/* write() failed, check why */
|
||||
if (write_res == -1) {
|
||||
switch (errno) {
|
||||
case EAGAIN:
|
||||
case EINTR:
|
||||
continue;
|
||||
}
|
||||
|
||||
/* the error is serious, unlock mutex and return as we can't seem to write to the log */
|
||||
g_static_mutex_unlock(&log_mutex);
|
||||
g_string_free(log_line, TRUE);
|
||||
return FALSE;
|
||||
}
|
||||
else {
|
||||
bytes_written += write_res;
|
||||
assert(bytes_written <= (gssize) log_line->len);
|
||||
}
|
||||
}
|
||||
|
||||
g_static_mutex_unlock(&log_mutex);
|
||||
g_string_free(log_line, TRUE);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ struct option {
|
|||
option_type type;
|
||||
union {
|
||||
gboolean opt_bool;
|
||||
gint opt_int;
|
||||
gint64 opt_int;
|
||||
GString *opt_string;
|
||||
/* array of option */
|
||||
GArray *opt_list;
|
||||
|
|
|
@ -6,6 +6,9 @@ server* server_new() {
|
|||
server* srv = g_slice_new0(server);
|
||||
srv->plugins = g_hash_table_new(g_str_hash, g_str_equal);
|
||||
srv->options = g_hash_table_new(g_str_hash, g_str_equal);
|
||||
srv->mutex = g_mutex_new();
|
||||
srv->error_log_fd = STDERR_FILENO;
|
||||
|
||||
return srv;
|
||||
}
|
||||
|
||||
|
@ -15,5 +18,7 @@ void server_free(server* srv) {
|
|||
|
||||
g_hash_table_destroy(srv->plugins);
|
||||
g_hash_table_destroy(srv->options);
|
||||
g_mutex_free(srv->mutex);
|
||||
|
||||
g_slice_free(server, srv);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue