~ubuntu-branches/ubuntu/precise/corosync/precise-proposed

« back to all changes in this revision

Viewing changes to exec/mainconfig.c

  • Committer: Bazaar Package Importer
  • Author(s): Ante Karamatic
  • Date: 2009-08-21 09:29:56 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090821092956-w9qxxxx3zeoh8dem
Tags: 1.0.0-4ubuntu2
* debian/control:
  - 'Ubuntu Developers' instead of 'Ubuntu Core Developers'
    as maintainer
  - Bump debhelper dependecy to 7

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Copyright (c) 2002-2005 MontaVista Software, Inc.
3
 
 * Copyright (c) 2006-2008 Red Hat, Inc.
 
3
 * Copyright (c) 2006-2009 Red Hat, Inc.
4
4
 *
5
5
 * All rights reserved.
6
6
 *
32
32
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33
33
 * THE POSSIBILITY OF SUCH DAMAGE.
34
34
 */
 
35
 
 
36
#include <config.h>
 
37
 
35
38
#include <stdio.h>
36
39
#include <string.h>
37
40
#include <stdlib.h>
38
41
#include <errno.h>
39
 
#include <assert.h>
40
42
#include <sys/socket.h>
41
43
#include <netinet/in.h>
42
44
#include <arpa/inet.h>
 
45
#include <pwd.h>
 
46
#include <grp.h>
 
47
#include <limits.h>
43
48
 
44
 
#include <corosync/saAis.h>
 
49
#include <corosync/corotypes.h>
45
50
#include <corosync/list.h>
46
51
#include <corosync/totem/totem.h>
47
52
#include <corosync/engine/logsys.h>
48
53
 
49
54
#include "util.h"
50
55
#include "mainconfig.h"
51
 
#include "mempool.h"
52
56
 
53
57
static char error_string_response[512];
54
58
 
 
59
static struct objdb_iface_ver0 *global_objdb;
 
60
 
 
61
DECLARE_LIST_INIT(uidgid_list_head);
 
62
 
 
63
 
55
64
/* This just makes the code below a little neater */
56
65
static inline int objdb_get_string (
57
 
        struct objdb_iface_ver0 *objdb,
58
 
        unsigned int object_service_handle,
59
 
        char *key, char **value)
 
66
        const struct objdb_iface_ver0 *objdb,
 
67
        hdb_handle_t object_service_handle,
 
68
        const char *key, char **value)
60
69
{
61
70
        int res;
62
71
 
75
84
}
76
85
 
77
86
static inline void objdb_get_int (
78
 
        struct objdb_iface_ver0 *objdb, unsigned int object_service_handle,
 
87
        const struct objdb_iface_ver0 *objdb,
 
88
        hdb_handle_t object_service_handle,
79
89
        char *key, unsigned int *intvalue)
80
90
{
81
91
        char *value = NULL;
92
102
        }
93
103
}
94
104
 
95
 
static struct logsys_config_struct {
96
 
        char subsys[6];
97
 
        unsigned int priority;
98
 
        unsigned int tags;
99
 
} logsys_logger;
100
 
 
101
 
int corosync_main_config_read (
102
 
        struct objdb_iface_ver0 *objdb,
103
 
        char **error_string,
104
 
        struct main_config *main_config)
105
 
{
106
 
        unsigned int object_service_handle;
107
 
        unsigned int object_logger_subsys_handle;
108
 
        char *value;
109
 
        char *error_reason = error_string_response;
110
 
        unsigned int object_find_handle;
111
 
        unsigned int object_find_logsys_handle;
112
 
        int global_debug = 0;
113
 
 
114
 
 
115
 
        memset (main_config, 0, sizeof (struct main_config));
 
105
/**
 
106
 * insert_into_buffer
 
107
 * @target_buffer: a buffer where to write results
 
108
 * @bufferlen: tell us the size of the buffer to avoid overflows
 
109
 * @entry: entry that needs to be added to the buffer
 
110
 * @after: can either be NULL or set to a string.
 
111
 *         if NULL, @entry is prependend to logsys_format_get buffer.
 
112
 *         if set, @entry is added immediately after @after.
 
113
 *
 
114
 * Since the function is specific to logsys_format_get handling, it is implicit
 
115
 * that source is logsys_format_get();
 
116
 *
 
117
 * In case of failure, target_buffer could be left dirty. So don't trust
 
118
 * any data leftover in it.
 
119
 *
 
120
 * Searching for "after" assumes that there is only entry of "after"
 
121
 * in the source. Afterall we control the string here and for logging format
 
122
 * it makes little to no sense to have duplicate format entries.
 
123
 *
 
124
 * Returns: 0 on success, -1 on failure
 
125
 **/
 
126
static int insert_into_buffer(
 
127
        char *target_buffer,
 
128
        size_t bufferlen,
 
129
        const char *entry,
 
130
        const char *after)
 
131
{
 
132
        const char *current_format = NULL;
 
133
 
 
134
        current_format = logsys_format_get();
 
135
 
 
136
        /* if the entry is already in the format we don't add it again */
 
137
        if (strstr(current_format, entry) != NULL) {
 
138
                return -1;
 
139
        }
 
140
 
 
141
        /* if there is no "after", simply prepend the requested entry
 
142
         * otherwise go for beautiful string manipulation.... </sarcasm> */
 
143
        if (!after) {
 
144
                if (snprintf(target_buffer, bufferlen - 1, "%s%s",
 
145
                                entry,
 
146
                                current_format) >= bufferlen - 1) {
 
147
                        return -1;
 
148
                }
 
149
        } else {
 
150
                const char *afterpos;
 
151
                size_t afterlen;
 
152
                size_t templen;
 
153
 
 
154
                /* check if after is contained in the format
 
155
                 * and afterlen has a meaning or return an error */
 
156
                afterpos = strstr(current_format, after);
 
157
                afterlen = strlen(after);
 
158
                if ((!afterpos) || (!afterlen)) {
 
159
                        return -1;
 
160
                }
 
161
 
 
162
                templen = afterpos - current_format + afterlen;
 
163
                if (snprintf(target_buffer, templen + 1, "%s", current_format)
 
164
                                >= bufferlen - 1) {
 
165
                        return -1;
 
166
                }
 
167
                if (snprintf(target_buffer + templen, bufferlen - ( templen + 1 ),
 
168
                                "%s%s", entry, current_format + templen)
 
169
                                >= bufferlen - ( templen + 1 )) {
 
170
                        return -1;
 
171
                }
 
172
        }
 
173
        return 0;
 
174
}
 
175
 
 
176
/*
 
177
 * format set is the only global specific option that
 
178
 * doesn't apply at system/subsystem level.
 
179
 */
 
180
static int corosync_main_config_format_set (
 
181
        struct objdb_iface_ver0 *objdb,
 
182
        hdb_handle_t object_handle,
 
183
        const char **error_string)
 
184
{
 
185
        const char *error_reason;
 
186
        char new_format_buffer[PATH_MAX];
 
187
        char *value;
 
188
        int err = 0;
 
189
 
 
190
        if (!objdb_get_string (objdb,object_handle, "fileline", &value)) {
 
191
                if (strcmp (value, "on") == 0) {
 
192
                        if (!insert_into_buffer(new_format_buffer,
 
193
                                        sizeof(new_format_buffer),
 
194
                                        " %f:%l", "s]")) {
 
195
                                err = logsys_format_set(new_format_buffer);
 
196
                        } else
 
197
                        if (!insert_into_buffer(new_format_buffer,
 
198
                                        sizeof(new_format_buffer),
 
199
                                        "%f:%l", NULL)) {
 
200
                                err = logsys_format_set(new_format_buffer);
 
201
                        }
 
202
                } else
 
203
                if (strcmp (value, "off") == 0) {
 
204
                        /* nothing to do here */
 
205
                } else {
 
206
                        error_reason = "unknown value for fileline";
 
207
                        goto parse_error;
 
208
                }
 
209
        }
 
210
        if (!objdb_get_string (objdb,object_handle, "function_name", &value)) {
 
211
                if (strcmp (value, "on") == 0) {
 
212
                        if (!insert_into_buffer(new_format_buffer,
 
213
                                        sizeof(new_format_buffer),
 
214
                                        "%n:", "f:")) {
 
215
                                err = logsys_format_set(new_format_buffer);
 
216
                        } else
 
217
                        if (!insert_into_buffer(new_format_buffer,
 
218
                                        sizeof(new_format_buffer),
 
219
                                        " %n", "s]")) {
 
220
                                err = logsys_format_set(new_format_buffer);
 
221
                        }
 
222
                } else
 
223
                if (strcmp (value, "off") == 0) {
 
224
                        /* nothing to do here */
 
225
                } else {
 
226
                        error_reason = "unknown value for function_name";
 
227
                        goto parse_error;
 
228
                }
 
229
        }
 
230
        if (!objdb_get_string (objdb,object_handle, "timestamp", &value)) {
 
231
                if (strcmp (value, "on") == 0) {
 
232
                        if(!insert_into_buffer(new_format_buffer,
 
233
                                        sizeof(new_format_buffer),
 
234
                                        "%t ", NULL)) {
 
235
                                err = logsys_format_set(new_format_buffer);
 
236
                        }
 
237
                } else
 
238
                if (strcmp (value, "off") == 0) {
 
239
                        /* nothing to do here */
 
240
                } else {
 
241
                        error_reason = "unknown value for timestamp";
 
242
                        goto parse_error;
 
243
                }
 
244
        }
 
245
        if (err) {
 
246
                error_reason = "exhausted virtual memory";
 
247
                goto parse_error;
 
248
        }
 
249
 
 
250
        return (0);
 
251
 
 
252
parse_error:
 
253
        *error_string = error_reason;
 
254
 
 
255
        return (-1);
 
256
}
 
257
 
 
258
static int corosync_main_config_set (
 
259
        struct objdb_iface_ver0 *objdb,
 
260
        hdb_handle_t object_handle,
 
261
        const char *subsys,
 
262
        const char **error_string)
 
263
{
 
264
        const char *error_reason = error_string_response;
 
265
        char *value;
 
266
        unsigned int mode;
 
267
 
 
268
        /*
 
269
         * this bit abuses the internal logsys exported API
 
270
         * to guarantee that all configured subsystems are
 
271
         * initialized too.
 
272
         *
 
273
         * using this approach avoids some headaches caused
 
274
         * by IPC and TOTEM that have a special logging
 
275
         * handling requirements
 
276
         */
 
277
        if (subsys != NULL) {
 
278
                if (_logsys_subsys_create(subsys) < 0) {
 
279
                        error_reason = "unable to create new logging subsystem";
 
280
                        goto parse_error;
 
281
                }
 
282
        }
 
283
 
 
284
        mode = logsys_config_mode_get(subsys);
 
285
        if (mode < 0) {
 
286
                error_reason = "unable to get mode";
 
287
                goto parse_error;
 
288
        }
 
289
 
 
290
        if (!objdb_get_string (objdb,object_handle, "to_file", &value)) {
 
291
 
 
292
                log_printf(LOGSYS_LEVEL_WARNING,
 
293
                 "Warning: the to_file config paramater has been obsoleted."
 
294
                 " See corosync.conf man page to_logfile directive.");
 
295
 
 
296
                if (strcmp (value, "yes") == 0) {
 
297
                        mode |= LOGSYS_MODE_OUTPUT_FILE;
 
298
                        if (logsys_config_mode_set(subsys, mode) < 0) {
 
299
                                error_reason = "unable to set mode to_file";
 
300
                                goto parse_error;
 
301
                        }
 
302
                } else
 
303
                if (strcmp (value, "no") == 0) {
 
304
                        mode &= ~LOGSYS_MODE_OUTPUT_FILE;
 
305
                        if (logsys_config_mode_set(subsys, mode) < 0) {
 
306
                                error_reason = "unable to unset mode to_file";
 
307
                                goto parse_error;
 
308
                        }
 
309
                } else {
 
310
                        error_reason = "unknown value for to_file";
 
311
                        goto parse_error;
 
312
                }
 
313
        }
 
314
 
 
315
        if (!objdb_get_string (objdb,object_handle, "to_logfile", &value)) {
 
316
                if (strcmp (value, "yes") == 0) {
 
317
                        mode |= LOGSYS_MODE_OUTPUT_FILE;
 
318
                        if (logsys_config_mode_set(subsys, mode) < 0) {
 
319
                                error_reason = "unable to set mode to_logfile";
 
320
                                goto parse_error;
 
321
                        }
 
322
                } else
 
323
                if (strcmp (value, "no") == 0) {
 
324
                        mode &= ~LOGSYS_MODE_OUTPUT_FILE;
 
325
                        if (logsys_config_mode_set(subsys, mode) < 0) {
 
326
                                error_reason = "unable to unset mode to_logfile";
 
327
                                goto parse_error;
 
328
                        }
 
329
                } else {
 
330
                        error_reason = "unknown value for to_logfile";
 
331
                        goto parse_error;
 
332
                }
 
333
        }
 
334
 
 
335
        if (!objdb_get_string (objdb,object_handle, "to_syslog", &value)) {
 
336
                if (strcmp (value, "yes") == 0) {
 
337
                        mode |= LOGSYS_MODE_OUTPUT_SYSLOG;
 
338
                        if (logsys_config_mode_set(subsys, mode) < 0) {
 
339
                                error_reason = "unable to set mode to_syslog";
 
340
                                goto parse_error;
 
341
                        }
 
342
                } else
 
343
                if (strcmp (value, "no") == 0) {
 
344
                        mode &= ~LOGSYS_MODE_OUTPUT_SYSLOG;
 
345
                        if (logsys_config_mode_set(subsys, mode) < 0) {
 
346
                                error_reason = "unable to unset mode to_syslog";
 
347
                                goto parse_error;
 
348
                        }
 
349
                } else {
 
350
                        error_reason = "unknown value for to_syslog";
 
351
                        goto parse_error;
 
352
                }
 
353
        }
 
354
 
 
355
        if (!objdb_get_string (objdb,object_handle, "to_stderr", &value)) {
 
356
                if (strcmp (value, "yes") == 0) {
 
357
                        mode |= LOGSYS_MODE_OUTPUT_STDERR;
 
358
                        if (logsys_config_mode_set(subsys, mode) < 0) {
 
359
                                error_reason = "unable to set mode to_stderr";
 
360
                                goto parse_error;
 
361
                        }
 
362
                } else
 
363
                if (strcmp (value, "no") == 0) {
 
364
                        mode &= ~LOGSYS_MODE_OUTPUT_STDERR;
 
365
                        if (logsys_config_mode_set(subsys, mode) < 0) {
 
366
                                error_reason = "unable to unset mode to_stderr";
 
367
                                goto parse_error;
 
368
                        }
 
369
                } else {
 
370
                        error_reason = "unknown value for to_syslog";
 
371
                        goto parse_error;
 
372
                }
 
373
        }
 
374
 
 
375
        if (!objdb_get_string (objdb,object_handle, "syslog_facility", &value)) {
 
376
                int syslog_facility;
 
377
 
 
378
                syslog_facility = logsys_facility_id_get(value);
 
379
                if (syslog_facility < 0) {
 
380
                        error_reason = "unknown syslog facility specified";
 
381
                        goto parse_error;
 
382
                }
 
383
                if (logsys_config_syslog_facility_set(subsys,
 
384
                                                syslog_facility) < 0) {
 
385
                        error_reason = "unable to set syslog facility";
 
386
                        goto parse_error;
 
387
                }
 
388
        }
 
389
 
 
390
        if (!objdb_get_string (objdb,object_handle, "syslog_level", &value)) {
 
391
                int syslog_priority;
 
392
 
 
393
                log_printf(LOGSYS_LEVEL_WARNING,
 
394
                 "Warning: the syslog_level config paramater has been obsoleted."
 
395
                 " See corosync.conf man page syslog_priority directive.");
 
396
 
 
397
                syslog_priority = logsys_priority_id_get(value);
 
398
                if (syslog_priority < 0) {
 
399
                        error_reason = "unknown syslog level specified";
 
400
                        goto parse_error;
 
401
                }
 
402
                if (logsys_config_syslog_priority_set(subsys,
 
403
                                                syslog_priority) < 0) {
 
404
                        error_reason = "unable to set syslog level";
 
405
                        goto parse_error;
 
406
                }
 
407
        }
 
408
 
 
409
        if (!objdb_get_string (objdb,object_handle, "syslog_priority", &value)) {
 
410
                int syslog_priority;
 
411
 
 
412
                syslog_priority = logsys_priority_id_get(value);
 
413
                if (syslog_priority < 0) {
 
414
                        error_reason = "unknown syslog priority specified";
 
415
                        goto parse_error;
 
416
                }
 
417
                if (logsys_config_syslog_priority_set(subsys,
 
418
                                                syslog_priority) < 0) {
 
419
                        error_reason = "unable to set syslog priority";
 
420
                        goto parse_error;
 
421
                }
 
422
        }
 
423
 
 
424
        if (!objdb_get_string (objdb,object_handle, "logfile", &value)) {
 
425
                if (logsys_config_file_set (subsys, error_string, value) < 0) {
 
426
                        goto parse_error;
 
427
                }
 
428
        }
 
429
 
 
430
        if (!objdb_get_string (objdb,object_handle, "logfile_priority", &value)) {
 
431
                int logfile_priority;
 
432
 
 
433
                logfile_priority = logsys_priority_id_get(value);
 
434
                if (logfile_priority < 0) {
 
435
                        error_reason = "unknown logfile priority specified";
 
436
                        goto parse_error;
 
437
                }
 
438
                if (logsys_config_logfile_priority_set(subsys,
 
439
                                                logfile_priority) < 0) {
 
440
                        error_reason = "unable to set logfile priority";
 
441
                        goto parse_error;
 
442
                }
 
443
        }
 
444
 
 
445
        if (!objdb_get_string (objdb, object_handle, "debug", &value)) {
 
446
                if (strcmp (value, "on") == 0) {
 
447
                        if (logsys_config_debug_set (subsys, 1) < 0) {
 
448
                                error_reason = "unable to set debug on";
 
449
                                goto parse_error;
 
450
                        }
 
451
                } else
 
452
                if (strcmp (value, "off") == 0) {
 
453
                        if (logsys_config_debug_set (subsys, 0) < 0) {
 
454
                                error_reason = "unable to set debug off";
 
455
                                goto parse_error;
 
456
                        }
 
457
                } else {
 
458
                        error_reason = "unknown value for debug";
 
459
                        goto parse_error;
 
460
                }
 
461
        }
 
462
 
 
463
        return (0);
 
464
 
 
465
parse_error:
 
466
        *error_string = error_reason;
 
467
 
 
468
        return (-1);
 
469
}
 
470
 
 
471
static int corosync_main_config_read_logging (
 
472
        struct objdb_iface_ver0 *objdb,
 
473
        const char **error_string)
 
474
{
 
475
        hdb_handle_t object_service_handle;
 
476
        hdb_handle_t object_logger_subsys_handle;
 
477
        hdb_handle_t object_find_handle;
 
478
        hdb_handle_t object_find_logsys_handle;
 
479
        const char *error_reason;
 
480
        char *value;
116
481
 
117
482
        objdb->object_find_create (
118
483
                OBJECT_PARENT_HANDLE,
120
485
                strlen ("logging"),
121
486
                &object_find_handle);
122
487
 
123
 
        main_config->logmode = LOG_MODE_FLUSH_AFTER_CONFIG;
124
488
        if (objdb->object_find_next (
125
489
                object_find_handle,
126
490
                &object_service_handle) == 0) {
127
491
 
128
 
                if (!objdb_get_string (objdb,object_service_handle, "to_file", &value)) {
129
 
                        if (strcmp (value, "yes") == 0) {
130
 
                                main_config->logmode |= LOG_MODE_OUTPUT_FILE;
131
 
                        } else
132
 
                        if (strcmp (value, "no") == 0) {
133
 
                                main_config->logmode &= ~LOG_MODE_OUTPUT_FILE;
134
 
                        }
135
 
                }
136
 
                if (!objdb_get_string (objdb,object_service_handle, "to_syslog", &value)) {
137
 
                        if (strcmp (value, "yes") == 0) {
138
 
                                main_config->logmode |= LOG_MODE_OUTPUT_SYSLOG_THREADED;
139
 
                        } else
140
 
                        if (strcmp (value, "no") == 0) {
141
 
                                main_config->logmode &= ~LOG_MODE_OUTPUT_SYSLOG_THREADED;
142
 
                        }
143
 
                }
144
 
                if (!objdb_get_string (objdb,object_service_handle, "to_stderr", &value)) {
145
 
                        if (strcmp (value, "yes") == 0) {
146
 
                                main_config->logmode |= LOG_MODE_OUTPUT_STDERR;
147
 
                        } else
148
 
                        if (strcmp (value, "no") == 0) {
149
 
                                main_config->logmode &= ~LOG_MODE_OUTPUT_STDERR;
150
 
                        }
151
 
                }
152
 
 
153
 
                if (!objdb_get_string (objdb,object_service_handle, "debug", &value)) {
154
 
                        if (strcmp (value, "on") == 0) {
155
 
                                global_debug = 1;
156
 
                        } else
157
 
                        if (strcmp (value, "off") == 0) {
158
 
                                global_debug = 0;
159
 
                        } else {
160
 
                                goto parse_error;
161
 
                        }
162
 
                }
163
 
                if (!objdb_get_string (objdb,object_service_handle, "timestamp", &value)) {
164
 
                        if (strcmp (value, "on") == 0) {
165
 
                                main_config->logmode |= LOG_MODE_DISPLAY_TIMESTAMP;
166
 
                        } else
167
 
                        if (strcmp (value, "off") == 0) {
168
 
                                main_config->logmode &= ~LOG_MODE_DISPLAY_TIMESTAMP;
169
 
                        } else {
170
 
                                goto parse_error;
171
 
                        }
172
 
                }
173
 
                if (!objdb_get_string (objdb,object_service_handle, "logfile", &value)) {
174
 
                        main_config->logfile = strdup (value);
175
 
                }
176
 
 
177
 
                if (!objdb_get_string (objdb,object_service_handle, "fileline", &value)) {
178
 
                        if (strcmp (value, "on") == 0) {
179
 
                                main_config->logmode |= LOG_MODE_DISPLAY_FILELINE;
180
 
                        } else
181
 
                        if (strcmp (value, "off") == 0) {
182
 
                                main_config->logmode &= ~LOG_MODE_DISPLAY_FILELINE;
183
 
                        } else {
184
 
                                goto parse_error;
185
 
                        }
186
 
                }
187
 
 
188
 
                if (!objdb_get_string (objdb,object_service_handle, "syslog_facility", &value)) {
189
 
                        if (strcmp (value, "daemon") == 0) {
190
 
                                main_config->syslog_facility = LOG_DAEMON;
191
 
                        } else
192
 
                        if (strcmp (value, "local0") == 0) {
193
 
                                main_config->syslog_facility = LOG_LOCAL0;
194
 
                        } else
195
 
                        if (strcmp (value, "local1") == 0) {
196
 
                                main_config->syslog_facility = LOG_LOCAL1;
197
 
                        } else
198
 
                        if (strcmp (value, "local2") == 0) {
199
 
                                main_config->syslog_facility = LOG_LOCAL2;
200
 
                        } else
201
 
                        if (strcmp (value, "local3") == 0) {
202
 
                                main_config->syslog_facility = LOG_LOCAL3;
203
 
                        } else
204
 
                        if (strcmp (value, "local4") == 0) {
205
 
                                main_config->syslog_facility = LOG_LOCAL4;
206
 
                        } else
207
 
                        if (strcmp (value, "local5") == 0) {
208
 
                                main_config->syslog_facility = LOG_LOCAL5;
209
 
                        } else
210
 
                        if (strcmp (value, "local6") == 0) {
211
 
                                main_config->syslog_facility = LOG_LOCAL6;
212
 
                        } else
213
 
                        if (strcmp (value, "local7") == 0) {
214
 
                                main_config->syslog_facility = LOG_LOCAL7;
215
 
                        } else {
216
 
                                error_reason = "unknown syslog facility specified";
217
 
                                goto parse_error;
218
 
                        }
219
 
                }
 
492
                /* format set is supported only for toplevel */
 
493
                if (corosync_main_config_format_set (objdb,
 
494
                                                       object_service_handle,
 
495
                                                       &error_reason) < 0) {
 
496
                        goto parse_error;
 
497
                }
 
498
 
 
499
                if (corosync_main_config_set (objdb,
 
500
                                                object_service_handle,
 
501
                                                NULL,
 
502
                                                &error_reason) < 0) {
 
503
                        goto parse_error;
 
504
                }
 
505
 
 
506
                /* we will need 2 of these to compensate for new logging
 
507
                 * config format */
220
508
 
221
509
                objdb->object_find_create (
222
510
                        object_service_handle,
232
520
                                object_logger_subsys_handle,
233
521
                                "subsys", &value)) {
234
522
 
235
 
                                strncpy (logsys_logger.subsys, value,
236
 
                                        sizeof (logsys_logger.subsys));
 
523
                                if (corosync_main_config_set (objdb,
 
524
                                                object_logger_subsys_handle,
 
525
                                                value,
 
526
                                                &error_reason) < 0) {
 
527
                                        goto parse_error;
 
528
                                }
237
529
                        }
238
530
                        else {
239
531
                                error_reason = "subsys required for logger directive";
240
532
                                goto parse_error;
241
533
                        }
242
 
                        if (!objdb_get_string (objdb, object_logger_subsys_handle, "debug", &value)) {
243
 
                                if (strcmp (value, "on") == 0) {
244
 
                                        logsys_logger.priority = LOG_LEVEL_DEBUG;
245
 
                                } else
246
 
                                if (strcmp (value, "off") == 0) {
247
 
                                        logsys_logger.priority &= ~LOG_LEVEL_DEBUG;
248
 
                                } else {
249
 
                                        goto parse_error;
250
 
                                }
251
 
                        }
252
 
                        if (!objdb_get_string (objdb, object_logger_subsys_handle, "tags", &value)) {
253
 
                                char *token = strtok (value, "|");
254
 
 
255
 
                                while (token != NULL) {
256
 
                                        if (strcmp (token, "enter") == 0) {
257
 
                                                logsys_logger.tags |= LOGSYS_TAG_ENTER;
258
 
                                        } else if (strcmp (token, "leave") == 0) {
259
 
                                                logsys_logger.tags |= LOGSYS_TAG_LEAVE;
260
 
                                        } else if (strcmp (token, "trace1") == 0) {
261
 
                                                logsys_logger.tags |= LOGSYS_TAG_TRACE1;
262
 
                                        } else if (strcmp (token, "trace2") == 0) {
263
 
                                                logsys_logger.tags |= LOGSYS_TAG_TRACE2;
264
 
                                        } else if (strcmp (token, "trace3") == 0) {
265
 
                                                logsys_logger.tags |= LOGSYS_TAG_TRACE3;
266
 
                                        } else if (strcmp (token, "trace4") == 0) {
267
 
                                                logsys_logger.tags |= LOGSYS_TAG_TRACE4;
268
 
                                        } else if (strcmp (token, "trace5") == 0) {
269
 
                                                logsys_logger.tags |= LOGSYS_TAG_TRACE5;
270
 
                                        } else if (strcmp (token, "trace6") == 0) {
271
 
                                                logsys_logger.tags |= LOGSYS_TAG_TRACE6;
272
 
                                        } else if (strcmp (token, "trace7") == 0) {
273
 
                                                logsys_logger.tags |= LOGSYS_TAG_TRACE7;
274
 
                                        } else if (strcmp (token, "trace8") == 0) {
275
 
                                                logsys_logger.tags |= LOGSYS_TAG_TRACE8;
276
 
                                        } else {
277
 
                                                error_reason = "bad tags value";
 
534
                }
 
535
                objdb->object_find_destroy (object_find_logsys_handle);
 
536
 
 
537
                objdb->object_find_create (
 
538
                        object_service_handle,
 
539
                        "logging_daemon",
 
540
                        strlen ("logging_daemon"),
 
541
                        &object_find_logsys_handle);
 
542
 
 
543
                while (objdb->object_find_next (
 
544
                        object_find_logsys_handle,
 
545
                        &object_logger_subsys_handle) == 0) {
 
546
 
 
547
                        if (!objdb_get_string (objdb,
 
548
                                object_logger_subsys_handle,
 
549
                                "name", &value)) {
 
550
 
 
551
                                if ((strcmp(value, "corosync") == 0) &&
 
552
                                   (!objdb_get_string (objdb,
 
553
                                        object_logger_subsys_handle,
 
554
                                        "subsys", &value))) {
 
555
 
 
556
                                        if (corosync_main_config_set (objdb,
 
557
                                                        object_logger_subsys_handle,
 
558
                                                        value,
 
559
                                                        &error_reason) < 0) {
278
560
                                                goto parse_error;
279
561
                                        }
280
 
 
281
 
                                        token = strtok(NULL, "|");
282
 
                                }
283
 
                        }
284
 
                        /*
285
 
                         * set individual logger configurations
286
 
                         */
287
 
                        logsys_config_subsys_set (
288
 
                                logsys_logger.subsys,
289
 
                                logsys_logger.tags,
290
 
                                logsys_logger.priority);
291
 
                        
 
562
                                }
 
563
                                else {
 
564
                                        error_reason = "subsys required for logging_daemon directive";
 
565
                                        goto parse_error;
 
566
                                }
 
567
                        }
 
568
                        else {
 
569
                                error_reason = "name required for logging_daemon directive";
 
570
                                goto parse_error;
 
571
                        }
292
572
                }
293
573
                objdb->object_find_destroy (object_find_logsys_handle);
294
574
        }
295
 
 
296
575
        objdb->object_find_destroy (object_find_handle);
297
576
 
 
577
        return 0;
 
578
 
 
579
parse_error:
 
580
        *error_string = error_reason;
 
581
 
 
582
        return (-1);
 
583
}
 
584
 
 
585
static int uid_determine (const char *req_user)
 
586
{
 
587
        struct passwd *passwd;
 
588
        int ais_uid = 0;
 
589
 
 
590
        passwd = getpwnam(req_user);
 
591
        if (passwd == 0) {
 
592
                log_printf (LOGSYS_LEVEL_ERROR, "ERROR: The '%s' user is not found in /etc/passwd, please read the documentation.\n", req_user);
 
593
                corosync_exit_error (AIS_DONE_UID_DETERMINE);
 
594
        }
 
595
        ais_uid = passwd->pw_uid;
 
596
        endpwent ();
 
597
        return ais_uid;
 
598
}
 
599
 
 
600
static int gid_determine (const char *req_group)
 
601
{
 
602
        struct group *group;
 
603
        int ais_gid = 0;
 
604
 
 
605
        group = getgrnam (req_group);
 
606
        if (group == 0) {
 
607
                log_printf (LOGSYS_LEVEL_ERROR, "ERROR: The '%s' group is not found in /etc/group, please read the documentation.\n", req_group);
 
608
                corosync_exit_error (AIS_DONE_GID_DETERMINE);
 
609
        }
 
610
        ais_gid = group->gr_gid;
 
611
        endgrent ();
 
612
        return ais_gid;
 
613
}
 
614
 
 
615
static void main_objdb_reload_notify(objdb_reload_notify_type_t type, int flush,
 
616
                                     void *priv_data_pt)
 
617
{
 
618
        const char *error_string;
 
619
 
 
620
        if (type == OBJDB_RELOAD_NOTIFY_END) {
 
621
 
 
622
                /*
 
623
                 * Reload the logsys configuration
 
624
                 */
 
625
                logsys_format_set(NULL);
 
626
                corosync_main_config_read_logging(global_objdb,
 
627
                                                  &error_string);
 
628
        }
 
629
}
 
630
 
 
631
static void add_logsys_config_notification(
 
632
        struct objdb_iface_ver0 *objdb)
 
633
{
 
634
 
 
635
        global_objdb = objdb;
 
636
 
 
637
        objdb->object_track_start(OBJECT_PARENT_HANDLE,
 
638
                                  1,
 
639
                                  NULL,
 
640
                                  NULL,
 
641
                                  NULL,
 
642
                                  main_objdb_reload_notify,
 
643
                                  NULL);
 
644
 
 
645
}
 
646
 
 
647
static int corosync_main_config_read_uidgid (
 
648
        struct objdb_iface_ver0 *objdb,
 
649
        const char **error_string)
 
650
{
 
651
        hdb_handle_t object_find_handle;
 
652
        hdb_handle_t object_service_handle;
 
653
        char *value;
 
654
        int uid, gid;
 
655
        struct uidgid_item *ugi;
 
656
 
298
657
        objdb->object_find_create (
299
658
                OBJECT_PARENT_HANDLE,
300
 
                "aisexec",
301
 
                strlen ("aisexec"),
 
659
                "uidgid",
 
660
                strlen ("uidgid"),
302
661
                &object_find_handle);
303
662
 
304
 
        if (objdb->object_find_next (
 
663
        while (objdb->object_find_next (
305
664
                object_find_handle,
306
665
                &object_service_handle) == 0) {
307
 
 
308
 
                if (!objdb_get_string (objdb,object_service_handle, "user", &value)) {
309
 
                        main_config->user = strdup(value);
310
 
                }
311
 
                if (!objdb_get_string (objdb,object_service_handle, "group", &value)) {
312
 
                        main_config->group = strdup(value);
 
666
                uid = -1;
 
667
                gid = -1;
 
668
 
 
669
                if (!objdb_get_string (objdb,object_service_handle, "uid", &value)) {
 
670
                        uid = uid_determine(value);
 
671
                }
 
672
 
 
673
                if (!objdb_get_string (objdb,object_service_handle, "gid", &value)) {
 
674
                        gid = gid_determine(value);
 
675
                }
 
676
 
 
677
                if (uid > -1 || gid > -1) {
 
678
                        ugi = malloc (sizeof (*ugi));
 
679
                        if (ugi == NULL) {
 
680
                                _corosync_out_of_memory_error();
 
681
                        }
 
682
                        ugi->uid = uid;
 
683
                        ugi->gid = gid;
 
684
                        list_init (&ugi->list);
 
685
                        list_add (&ugi->list, &uidgid_list_head);
313
686
                }
314
687
        }
315
 
 
316
688
        objdb->object_find_destroy (object_find_handle);
317
689
 
318
 
        /* Default user/group */
319
 
        if (!main_config->user)
320
 
                main_config->user = "ais";
321
 
 
322
 
        if (!main_config->group)
323
 
                main_config->group = "ais";
324
 
 
325
 
        if ((main_config->logmode & LOG_MODE_OUTPUT_FILE) &&
326
 
                (main_config->logfile == NULL)) {
327
 
                error_reason = "logmode set to 'file' but no logfile specified";
 
690
        return 0;
 
691
}
 
692
 
 
693
int corosync_main_config_read (
 
694
        struct objdb_iface_ver0 *objdb,
 
695
        const char **error_string)
 
696
{
 
697
        const char *error_reason = error_string_response;
 
698
 
 
699
        if (corosync_main_config_read_logging(objdb, error_string) < 0) {
 
700
                error_reason = *error_string;
328
701
                goto parse_error;
329
702
        }
330
703
 
331
 
        if (main_config->syslog_facility == 0)
332
 
                main_config->syslog_facility = LOG_DAEMON;
 
704
        corosync_main_config_read_uidgid (objdb, error_string);
 
705
 
 
706
        add_logsys_config_notification(objdb);
333
707
 
334
708
        return 0;
335
709
 
336
710
parse_error:
337
 
        sprintf (error_string_response,
 
711
        snprintf (error_string_response, sizeof(error_string_response),
338
712
                 "parse error in config: %s.\n",
339
713
                 error_reason);
340
714
 
341
715
        *error_string = error_string_response;
342
716
        return (-1);
343
717
}
 
718
 
 
719
int corosync_main_config_compatibility_read (
 
720
        struct objdb_iface_ver0 *objdb,
 
721
        enum cs_sync_mode *minimum_sync_mode,
 
722
        const char **error_string)
 
723
{
 
724
        const char *error_reason = error_string_response;
 
725
        char *value;
 
726
 
 
727
        *minimum_sync_mode = CS_SYNC_V1;
 
728
        if (!objdb_get_string (objdb, OBJECT_PARENT_HANDLE, "compatibility", &value)) {
 
729
 
 
730
                if (strcmp (value, "whitetank") == 0) {
 
731
                        *minimum_sync_mode = CS_SYNC_V1;
 
732
                } else
 
733
                if (strcmp (value, "none") == 0) {
 
734
                        *minimum_sync_mode = CS_SYNC_V2;
 
735
                } else {
 
736
 
 
737
                        snprintf (error_string_response, sizeof (error_string_response),
 
738
                                "Invalid compatibility option '%s' specified, must be none or whitetank.\n", value);
 
739
                        goto parse_error;
 
740
                }
 
741
        }
 
742
 
 
743
        return 0;
 
744
 
 
745
parse_error:
 
746
        *error_string = error_reason;
 
747
 
 
748
        return (-1);
 
749
}