summaryrefslogtreecommitdiffstats
path: root/sinks/file/elapi_sink_file.c
blob: 660aba36bd755626f8953e28f09b9f8a9e438042 (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
    ELAPI

    Implemenation of the file sink.

    Copyright (C) Dmitri Pal <dpal@redhat.com> 2009

    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 3 of the License, or
    (at your option) any later version.
    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.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <errno.h>
#include <malloc.h>
#include <syslog.h>
#include "elapi_sink.h"
#include "elapi_collection.h"
#include "elapi_debug.h"
#include "elapi_util.h"
#include "elapi_ini.h"

/* FIXME - this should be taken from the config.h generated by autotools */
#define FILE_RETRY    60
/* 
#define FILE_AUDIT_CONFIG  "/etc/elapi/file_defaults.conf"
#define FILE_AUDIT_DIR     "/etc/elapi/file_defaults.d"
*/
#define FILE_AUDIT_CONFIG  "/home/dpal/IPA/Code/elapi/etc/file_defaults.conf"
#define FILE_AUDIT_DIR     "/home/dpal/IPA/Code/elapi/etc/file_defaults.d"


/* FIXME there is currently no code 
 * to make sure we do not call syslog functions from multiple dispatchers.
 * We probably should create a mutex at the load of the library and
 * set a flag when the init function is called the first time
 * and not call the openlog any more times.
 * But I guess syslog can deal with this internally.
 */ 


/* Default conmfiguration for syslog */
struct file_conf {
    int keep_open;    
    char *file_name;
    FILE *file;
};

/* Internal function to intialize configuration */   
static int init_config(struct data_descriptor *dblock) 
{
    struct file_conf *conf_data;
    struct collection_item *config_from_file = (struct collection_item *)(NULL);
    int found = 0;
    int *keep_open_cfg = (int *)(NULL);
    char *file_name_cfg = NULL;
    int error;

    DEBUG_STRING("init_config","Entry");

    /* Allocate configuration data */
    conf_data = (struct file_conf *)(malloc(sizeof(struct file_conf)));
    if(conf_data == (struct file_conf *)(NULL)) return errno;

    /* Read configuration from the configuration file if any */
    (void)config_to_collection(dblock->appname, FILE_AUDIT_CONFIG, FILE_AUDIT_DIR, &config_from_file); 
    
    conf_data->keep_open = 1;
    conf_data->file_name = NULL;

    DEBUG_NUMBER("Keep open:",conf_data->keep_open);
    DEBUG_STRING("File name:",conf_data->file_name);

    /* Update defaults with settings from the file */
    error = get_value_from_config((void *)(&keep_open_cfg),ELAPI_TYPE_INTEGER, INI_DEFAULT_SECTION,"keep_open",config_from_file);
    if(error != EOK) {
        /* There is fundamentally something wrong */
        DEBUG_NUMBER("Attempt to get option returned error",error);        
        free((void *)(conf_data));       
        return error; 
    }

    /* Get the value */
    if(keep_open_cfg != (int *)(NULL)) {
        conf_data->keep_open = *keep_open_cfg;
        free((void *)(keep_open_cfg));
    } 
        
    error = get_value_from_config((void *)(&file_name_cfg),ELAPI_TYPE_STRING, INI_DEFAULT_SECTION,"file_name",config_from_file);
    if(error != EOK) {
        /* There is fundamentally something wrong */
        DEBUG_NUMBER("Attempt to get option returned error",error);        
        free((void *)(conf_data));       
        return error; 
    }

    /* Get the value */
    if(file_name_cfg != (char *)(NULL)) {
        conf_data->file_name = file_name_cfg;
    } 

    DEBUG_NUMBER("Keep open (after conf file):",conf_data->keep_open);
    DEBUG_STRING("File name (after conf file):",conf_data->file_name);

    dblock->config = (void *)(conf_data);

    if((conf_data->file_name != NULL) && (conf_data->keep_open != 0)) {
        file_conf->file = fopen(conf_data->file_name,"w");
    }
    else conf_data->file_name = stderr;

    DEBUG_STRING("init_config","Entry");
    return EOK;
}

/***** Standard functions each facility has to provide *****/
/* Initialize facility - open files, establish connnectons, etc... */
static int file_sink_init(struct data_descriptor *dblock)
{
    struct serial_data *event_storage;
    int error;

    DEBUG_STRING("file_sink_init","Entry");

    /* Prepare the block where the format function will store its data */
    errno = 0;
    dblock->internal_data = NULL;
    event_storage = (struct serail_data *)(malloc(sizeof(struct serial_data)));
    if(event_storage == (struct serail_data *)(NULL)) return errno;

    event_storage->buffer = NULL;
    event_storage->size = 0;
    event_storage->length = 0;
    event_storage->nest_level = 0;

    dblock->internal_data = (void *)(event_storage);
    
    /* Prepare the configuration data block */
    if((error = init_config(dblock))) {
        DEBUG_NUMBER("Failed to init config",error);
        free(dblock->internal_data);
        dblock->internal_data = NULL;        
        return error;
    }

    DEBUG_NUMBER("DBLOCK in init",dblock);
    DEBUG_NUMBER("internal data in init",dblock->internal_data);

    DEBUG_STRING("file_sink_init","Exit");
    return EOK;
}


/* Formatting calback */ 
static int file_sink_format(struct data_descriptor *dblock,
                              char *format_str,
                              struct collection_item *event)
{
    struct serial_data *serialized_data;
    int error = EOK;

    DEBUG_STRING("file_sink_format","Entry");
    DEBUG_NUMBER("DBLOCK in format",dblock);
    DEBUG_NUMBER("internal data in format",dblock->internal_data);

    serialized_data = (struct serial_data *)(dblock->internal_data);

    if(format_str != NULL) {
        /* Use format string */
        DEBUG_STRING("Using format:",format_str);
        error = serialize_with_format(event,format_str,serialized_data);
    }
    else {
        /* Traverse collection */
        DEBUG_STRING("Using default serialization callback","");
        error = traverse_collection(event,ELAPI_TRAVERSE_DEFAULT | ELAPI_TRAVERSE_END ,serialize,(void *)(serialized_data));
    }
    if(error) {
        DEBUG_NUMBER("Serialization returned error",error);
        return error;
    }

    DEBUG_STRING("stderr_sink_format","Exit");
    return EOK;
}


/* Cleanup per event internal data after a failure */
static void file_sink_cleanup(struct data_descriptor *dblock)
{
    struct serial_data *event_storage;

    DEBUG_STRING("file_sink_cleanup","Entry");

    event_storage = (struct file_event *)(dblock->internal_data);

    if(event_storage->buffer != NULL) {
        free(event_storage->buffer);
        event_storage->buffer = NULL;
        event_storage->size = 0;
        event_storage->length = 0;
        event_storage->nest_level = 0;
    }

    DEBUG_STRING("file_sink_cleanup","Exit");
}

/* Close facility */ 
static void file_sink_close(struct data_descriptor *dblock)
{
    struct file_conf *config;

    DEBUG_STRING("file_sink_close","Entry");

    if(dblock->internal_data != NULL) {
        file_sink_cleanup(dblock);
        free(dblock->internal_data);
        dblock->internal_data=NULL;
    }

    if(dblock->config != NULL) {
        config = (struct file_conf *)(dblock->config);

        if((conf_data->file_name != NULL) && (conf_data->keep_open != 0)) {
            close(file_conf->file);
        }

        if(config->file_name != NULL) free(config->file_name);
        free(dblock->config);
        dblock->config=NULL;
    }

    DEBUG_STRING("file_sink_close","Exit");
}


/* Logging calback */ 
static int file_sink_submit(struct data_descriptor *dblock)
{
    struct serial_data *event_storage;
    struct file_conf *config;

    DEBUG_STRING("file_sink_submit","Entry");
    DEBUG_NUMBER("DBLOCK in submit",dblock);
    DEBUG_NUMBER("internal data in submit",dblock->internal_data);

    event_storage = (struct file_event *)(dblock->internal_data);
    config = (struct file_conf *)(dblock->config);
   
    DEBUG_STRING("OUTPUT:",event_storage->buffer);

    if((conf_data->file_name != NULL) && (conf_data->keep_open == 0)) {
        file_conf->file = fopen(conf_data->file_name,"w");
    }

    fprintf(file_conf->file, "%s\n", event_storage->buffer);

    if((conf_data->file_name != NULL) && (conf_data->keep_open == 0)) {
        close(file_conf->file);
    }

    file_sink_cleanup(dblock);

    DEBUG_STRING("file_sink_submit","Exit");
    return EOK;
}

/* Return a filled in structure */
void get_sink_info(struct sink_capability *sink_cpb_block)
{
    DEBUG_STRING("get_sink_info","Entry");

    sink_cpb_block->retry_interval = FILE_RETRY;
    sink_cpb_block->flags = SINK_FLAG_NO_LIMIT;
    sink_cpb_block->instance = 0;
    sink_cpb_block->init_cb = file_sink_init;
    sink_cpb_block->cleanup_cb = file_sink_cleanup;
    sink_cpb_block->format_cb = file_sink_format;
    sink_cpb_block->submit_cb = file_sink_submit;
    sink_cpb_block->close_cb = file_sink_close;

    DEBUG_STRING("get_sink_info","Exit");    
}