1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
/*
* Copyright (C) 2012 Martin Willi
* Copyright (C) 2012 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 "error_notify_socket.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <errno.h>
#include <daemon.h>
#include <threading/thread.h>
#include <threading/mutex.h>
#include <collections/linked_list.h>
#include <processing/jobs/callback_job.h>
#include "error_notify_msg.h"
typedef struct private_error_notify_socket_t private_error_notify_socket_t;
/**
* Private data of an error_notify_socket_t object.
*/
struct private_error_notify_socket_t {
/**
* Public error_notify_socket_t interface.
*/
error_notify_socket_t public;
/**
* Service accepting connections
*/
stream_service_t *service;
/**
* List of connected clients, as stream_t
*/
linked_list_t *connected;
/**
* Mutex to lock clients list
*/
mutex_t *mutex;
};
METHOD(error_notify_socket_t, has_listeners, bool,
private_error_notify_socket_t *this)
{
int count;
this->mutex->lock(this->mutex);
count = this->connected->get_count(this->connected);
this->mutex->unlock(this->mutex);
return count != 0;
}
METHOD(error_notify_socket_t, notify, void,
private_error_notify_socket_t *this, error_notify_msg_t *msg)
{
enumerator_t *enumerator;
stream_t *stream;
this->mutex->lock(this->mutex);
enumerator = this->connected->create_enumerator(this->connected);
while (enumerator->enumerate(enumerator, &stream))
{
if (!stream->write_all(stream, msg, sizeof(*msg)))
{
switch (errno)
{
case ECONNRESET:
case EPIPE:
/* disconnect, remove this listener */
this->connected->remove_at(this->connected, enumerator);
stream->destroy(stream);
break;
default:
DBG1(DBG_CFG, "sending notify failed: %s", strerror(errno));
break;
}
}
}
enumerator->destroy(enumerator);
this->mutex->unlock(this->mutex);
}
/**
* Accept client connections
*/
static bool on_accept(private_error_notify_socket_t *this, stream_t *stream)
{
this->mutex->lock(this->mutex);
this->connected->insert_last(this->connected, stream);
this->mutex->unlock(this->mutex);
return TRUE;
}
METHOD(error_notify_socket_t, destroy, void,
private_error_notify_socket_t *this)
{
DESTROY_IF(this->service);
this->connected->destroy_offset(this->connected, offsetof(stream_t, destroy));
this->mutex->destroy(this->mutex);
free(this);
}
/**
* See header
*/
error_notify_socket_t *error_notify_socket_create()
{
private_error_notify_socket_t *this;
char *uri;
INIT(this,
.public = {
.notify = _notify,
.has_listeners = _has_listeners,
.destroy = _destroy,
},
.connected = linked_list_create(),
.mutex = mutex_create(MUTEX_TYPE_DEFAULT),
);
uri = lib->settings->get_str(lib->settings,
"%s.plugins.error-notify.socket", "unix://" ERROR_NOTIFY_SOCKET,
lib->ns);
this->service = lib->streams->create_service(lib->streams, uri, 10);
if (!this->service)
{
DBG1(DBG_CFG, "creating error-notify socket failed");
destroy(this);
return NULL;
}
this->service->on_accept(this->service, (stream_service_cb_t)on_accept,
this, JOB_PRIO_CRITICAL, 1);
return &this->public;
}
|