xpc: add support for logging over XPC channels

This commit is contained in:
Martin Willi
2013-07-18 12:17:55 +02:00
parent fbc89786b5
commit 1a3f71d97a
4 changed files with 174 additions and 1 deletions
+11 -1
View File
@@ -14,6 +14,7 @@
*/
#include "xpc_channels.h"
#include "xpc_logger.h"
#include <credentials/sets/callback_cred.h>
#include <collections/hashtable.h>
@@ -58,6 +59,8 @@ typedef struct {
uintptr_t sa;
/* did we already ask for a password? */
bool passworded;
/* channel specific logger */
xpc_logger_t *logger;
} entry_t;
/**
@@ -65,6 +68,8 @@ typedef struct {
*/
static void destroy_entry(entry_t *entry)
{
charon->bus->remove_logger(charon->bus, &entry->logger->logger);
entry->logger->destroy(entry->logger);
xpc_connection_suspend(entry->conn);
xpc_connection_cancel(entry->conn);
xpc_release(entry->conn);
@@ -110,6 +115,7 @@ METHOD(xpc_channels_t, add, void,
INIT(entry,
.conn = conn,
.sa = ike_sa,
.logger = xpc_logger_create(conn),
);
xpc_connection_set_event_handler(entry->conn, ^(xpc_object_t event) {
@@ -117,7 +123,7 @@ METHOD(xpc_channels_t, add, void,
if (event == XPC_ERROR_CONNECTION_INVALID ||
event == XPC_ERROR_CONNECTION_INTERRUPTED)
{
remove_conn(this, entry->conn);
remove_conn(this, conn);
}
else
{
@@ -125,6 +131,9 @@ METHOD(xpc_channels_t, add, void,
}
});
entry->logger->set_ike_sa(entry->logger, entry->sa);
charon->bus->add_logger(charon->bus, &entry->logger->logger);
this->lock->write_lock(this->lock);
this->channels->put(this->channels, (void*)entry->sa, entry);
this->lock->unlock(this->lock);
@@ -144,6 +153,7 @@ METHOD(listener_t, ike_rekey, bool,
if (entry)
{
entry->sa = new->get_unique_id(new);
entry->logger->set_ike_sa(entry->logger, entry->sa);
this->channels->put(this->channels, (void*)entry->sa, entry);
}
this->lock->unlock(this->lock);
+96
View File
@@ -0,0 +1,96 @@
/*
* Copyright (C) 2013 Martin Willi
* Copyright (C) 2013 revosec AG
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "xpc_logger.h"
typedef struct private_xpc_logger_t private_xpc_logger_t;
/**
* Private data of an xpc_logger_t object.
*/
struct private_xpc_logger_t {
/**
* Public xpc_logger_t interface.
*/
xpc_logger_t public;
/**
* XPC channel to send logging messages to
*/
xpc_connection_t conn;
/**
* IKE_SA we log for
*/
u_int32_t ike_sa;
};
METHOD(logger_t, log_, void,
private_xpc_logger_t *this, debug_t group, level_t level, int thread,
ike_sa_t* ike_sa, const char *message)
{
if (ike_sa && ike_sa->get_unique_id(ike_sa) == this->ike_sa)
{
xpc_object_t msg;
msg = xpc_dictionary_create(NULL, NULL, 0);
xpc_dictionary_set_string(msg, "type", "event");
xpc_dictionary_set_string(msg, "event", "log");
xpc_dictionary_set_string(msg, "message", message);
xpc_connection_send_message(this->conn, msg);
xpc_release(msg);
}
}
METHOD(logger_t, get_level, level_t,
private_xpc_logger_t *this, debug_t group)
{
return LEVEL_CTRL;
}
METHOD(xpc_logger_t, set_ike_sa, void,
private_xpc_logger_t *this, u_int32_t ike_sa)
{
this->ike_sa = ike_sa;
}
METHOD(xpc_logger_t, destroy, void,
private_xpc_logger_t *this)
{
free(this);
}
/**
* See header
*/
xpc_logger_t *xpc_logger_create(xpc_connection_t conn)
{
private_xpc_logger_t *this;
INIT(this,
.public = {
.logger = {
.log = _log_,
.get_level = _get_level,
},
.set_ike_sa = _set_ike_sa,
.destroy = _destroy,
},
.conn = conn,
);
return &this->public;
}
+61
View File
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2013 Martin Willi
* Copyright (C) 2013 revosec AG
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
/**
* @defgroup xpc_logger xpc_logger
* @{ @ingroup xpc
*/
#ifndef XPC_LOGGER_H_
#define XPC_LOGGER_H_
#include <xpc/xpc.h>
#include <daemon.h>
typedef struct xpc_logger_t xpc_logger_t;
/**
* Connection specific logger over XPC.
*/
struct xpc_logger_t {
/**
* Implements logger_t.
*/
logger_t logger;
/**
* Set the IKE_SA unique identifier this logger logs for.
*
* @param ike_sa IKE_SA unique identifier
*/
void (*set_ike_sa)(xpc_logger_t *this, u_int32_t ike_sa);
/**
* Destroy a xpc_logger_t.
*/
void (*destroy)(xpc_logger_t *this);
};
/**
* Create a xpc_logger instance.
*
* @param conn XPC connection to send logging events to
* @return XPC logger
*/
xpc_logger_t *xpc_logger_create(xpc_connection_t conn);
#endif /** XPC_LOGGER_H_ @}*/