~ampelbein/ubuntu/oneiric/heartbeat/lp-770743

« back to all changes in this revision

Viewing changes to cim/utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Ante Karamatic
  • Date: 2009-08-10 19:29:25 UTC
  • mfrom: (5.2.3 experimental)
  • Revision ID: james.westby@ubuntu.com-20090810192925-9zy2llcbgavbskf7
Tags: 2.99.2+sles11r9-5ubuntu1
* New upstream snapshot
* Adjusted heartbeat.install and rules for documentation path

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * utils.c: utilities
3
 
 * 
4
 
 * Author: Jia Ming Pan <jmltc@cn.ibm.com>
5
 
 * Copyright (c) 2005 International Business Machines
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; either version 2 of the License, or
10
 
 * (at your option) any later version.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
 
 *
21
 
 */
22
 
 
23
 
#include <lha_internal.h>
24
 
#include <unistd.h>
25
 
#include <stdlib.h>
26
 
#include <stdio.h>
27
 
#include <stdarg.h>
28
 
#include <string.h>
29
 
#include <sys/stat.h>
30
 
#include <sys/types.h>
31
 
#include <regex.h>
32
 
#include <hb_api.h>
33
 
#include <errno.h>
34
 
#include <heartbeat.h>
35
 
#include "cluster_info.h"
36
 
#include "utils.h"
37
 
 
38
 
int debug_level = 10;
39
 
 
40
 
static char *           pathname_encode(const char *);
41
 
 
42
 
int 
43
 
cim_init_logger(const char * entity)
44
 
{
45
 
        char * inherit_debuglevel;
46
 
        int debug_level = 2;
47
 
 
48
 
        inherit_debuglevel = getenv(HADEBUGVAL);
49
 
        if (inherit_debuglevel != NULL) {
50
 
                debug_level = atoi(inherit_debuglevel);
51
 
                if (debug_level > 2) {
52
 
                        debug_level = 2;
53
 
                }
54
 
        }
55
 
 
56
 
        cl_log_set_entity(entity);
57
 
        cl_log_enable_stderr(debug_level?TRUE:FALSE);
58
 
        cl_log_set_facility(HA_LOG_FACILITY);
59
 
        return HA_OK;
60
 
}
61
 
 
62
 
int 
63
 
run_shell_cmnd(const char *cmnd, int *ret, char ***out, char ***err)
64
 
                                /* err not used currently */
65
 
{
66
 
        FILE * fstream = NULL;
67
 
        char buffer [4096];
68
 
        int cmnd_rc, rc, i;
69
 
 
70
 
        DEBUG_ENTER();
71
 
        if ( (fstream = popen(cmnd, "r")) == NULL ){
72
 
                cl_log(LOG_ERR, "run_shell_cmnd: popen error: %s",
73
 
                        strerror(errno));       
74
 
                return HA_FAIL;
75
 
        }
76
 
 
77
 
        if ( (*out = cim_malloc(sizeof(char*)) ) == NULL ) {
78
 
                cl_log(LOG_ERR, "run_shell_cmnd: failed malloc.");
79
 
                return HA_FAIL;
80
 
        }
81
 
 
82
 
        (*out)[0] = NULL;
83
 
 
84
 
        i = 0;
85
 
        while (!feof(fstream)) {
86
 
                if ( fgets(buffer, 4096, fstream) != NULL ){
87
 
                        /** add buffer to out **/
88
 
                        *out = cim_realloc(*out, (i+2) * sizeof(char*));        
89
 
                        if ( *out == NULL ) {
90
 
                                rc = HA_FAIL;
91
 
                                goto exit;
92
 
                        }
93
 
                        (*out)[i] = cim_strdup(buffer);
94
 
                        (*out)[i+1] = NULL;             
95
 
                }else{
96
 
                        continue;
97
 
                }
98
 
                i++;
99
 
        }
100
 
        
101
 
        rc = HA_OK;
102
 
exit:
103
 
        if ( (cmnd_rc = pclose(fstream)) == -1 ){
104
 
                /*** WARNING log ***/
105
 
                cl_log(LOG_WARNING, "failed to close pipe.");
106
 
        }
107
 
        *ret = cmnd_rc;
108
 
 
109
 
        DEBUG_LEAVE();
110
 
        return rc;
111
 
}
112
 
 
113
 
char **
114
 
regex_search(const char * reg, const char * str, int * len)
115
 
{
116
 
        regex_t regexp;
117
 
        const int maxmatch = 16;
118
 
        regmatch_t pm[16];
119
 
        int i, ret, nmatch = 0;
120
 
        char **match = NULL;
121
 
 
122
 
        DEBUG_ENTER();
123
 
 
124
 
        *len = 0;
125
 
        ret = regcomp(&regexp, reg, REG_EXTENDED);
126
 
        if ( ret != 0) {
127
 
                cl_log(LOG_ERR, "regex_search: error regcomp regex %s.", reg);
128
 
                return HA_FAIL;
129
 
        }
130
 
 
131
 
        ret = regexec(&regexp, str, nmatch, pm, 0);
132
 
        if ( ret == REG_NOMATCH ){
133
 
                regfree(&regexp);
134
 
                cl_log(LOG_ERR, "regex_search: no match.");
135
 
                return NULL;
136
 
        }else if (ret != 0){
137
 
                cl_log(LOG_ERR, "regex_search: error regexec.\n");
138
 
                regfree(&regexp);
139
 
                return NULL;
140
 
        }
141
 
 
142
 
        for(nmatch=0; pm[nmatch].rm_so != -1 && nmatch < maxmatch; nmatch++);
143
 
 
144
 
        if ( (match = cim_malloc(nmatch*sizeof(char *))) == NULL ) {
145
 
                cl_log(LOG_ERR, "regex_search: alloc_failed.");
146
 
                regfree(&regexp);
147
 
                return NULL;
148
 
        }
149
 
        
150
 
        *len = nmatch;
151
 
        for(i = 0; i < maxmatch && i < nmatch; i++){
152
 
                int str_len = pm[i].rm_eo - pm[i].rm_so;
153
 
                if ((match[i] = cim_malloc(str_len + 1))) {
154
 
                        strncpy( match[i], str + pm[i].rm_so, str_len);
155
 
                        match[i][str_len] = EOS;
156
 
                }
157
 
        }
158
 
        regfree(&regexp);
159
 
        DEBUG_LEAVE();
160
 
        return match;
161
 
162
 
 
163
 
void
164
 
free_2d_zarray(void *zarray, cim_free_t free)
165
 
{
166
 
        void ** z = (void **)zarray;
167
 
        int i = 0;
168
 
        void * p;
169
 
 
170
 
        while ((p=z[i++])) {
171
 
                free(p);
172
 
        }
173
 
        cim_free(z);
174
 
}
175
 
 
176
 
void
177
 
free_2d_array(void * a, int len, cim_free_t free)
178
 
{
179
 
        int     i;
180
 
        void ** array = (void **)a;
181
 
 
182
 
        for (i=0; i<len; i++){
183
 
                if (array[i]) {
184
 
                        free(array[i]);
185
 
                }
186
 
        }
187
 
        cim_free(array);
188
 
}
189
 
 
190
 
char * 
191
 
uuid_to_str(const cl_uuid_t * uuid){
192
 
        int i, len = 0;
193
 
        char * str = cim_malloc(256);
194
 
 
195
 
        if ( str == NULL ) {
196
 
                return NULL;
197
 
        }
198
 
 
199
 
        memset(str, 0, 256);
200
 
 
201
 
        for ( i = 0; i < sizeof(cl_uuid_t); i++){
202
 
                len += snprintf(str + len, 2, "%.2X", uuid->uuid[i]);
203
 
        }
204
 
        return str;
205
 
}
206
 
 
207
 
void
208
 
cim_assert(const char * assertion, int line, const char * file)
209
 
{
210
 
        cl_log(LOG_ERR, "Assertion \"%s\" failed on line %d in file \"%s\""
211
 
        ,       assertion, line, file);
212
 
        exit(1);
213
 
}
214
 
 
215
 
 
216
 
char **
217
 
split_string(const char* string, int *len, const char *delim)
218
 
{
219
 
        char **strings = NULL;
220
 
        const char *p;
221
 
 
222
 
        *len = 0;
223
 
        while(*string){
224
 
                char * token = NULL;
225
 
                /* eat up delim chars */
226
 
                while (*string && strchr(delim, *string)) {
227
 
                        string++;
228
 
                        continue;
229
 
                }       
230
 
                if(!*string) {
231
 
                        break;
232
 
                }
233
 
                
234
 
                /* reach a delim char */
235
 
                p = string;
236
 
                while ( *p && !strchr(delim, *p) ) {
237
 
                        p ++;
238
 
                        continue;
239
 
                }
240
 
                
241
 
                /* copy string~(p-1) to token */
242
 
                token = cim_malloc(p-string+1);
243
 
                if ( token == NULL ) {
244
 
                        return strings;
245
 
                }
246
 
                memcpy(token, string, p - string); 
247
 
                token[p-string] = EOS;
248
 
 
249
 
                strings = cim_realloc(strings, (*len+1) * sizeof(char *));
250
 
                if ( strings == NULL ) {
251
 
                        return NULL;
252
 
                }
253
 
                strings[*len] = token;
254
 
                *len = *len + 1;
255
 
                string = p;
256
 
        }
257
 
        return strings; 
258
 
}
259
 
 
260
 
int 
261
 
cim_msg2disk(const char *objpathname, struct ha_msg *msg)
262
 
{
263
 
        struct stat st;
264
 
        FILE *fobj; 
265
 
        char pathname[MAXLEN], *buf;
266
 
        char *msgstr = NULL;
267
 
 
268
 
        if ( stat(HA_CIM_VARDIR, &st) < 0 ) {
269
 
                if ( errno == ENOENT ) {        /* not exist, create */
270
 
                        if (  mkdir(HA_CIM_VARDIR, 0660) < 0){
271
 
                                cl_log(LOG_ERR,"msg2disk: mkdir failed.");              
272
 
                                cl_log(LOG_ERR,"reason:%s",strerror(errno)); 
273
 
                                return HA_FAIL;
274
 
                        }
275
 
 
276
 
                } else {
277
 
                        cl_log(LOG_ERR, "msg2disk: stat faild.");
278
 
                        cl_log(LOG_ERR,"reason:%d(%s)",errno,strerror(errno)); 
279
 
                        return HA_FAIL;
280
 
                }
281
 
        } 
282
 
 
283
 
        /* check stat */
284
 
 
285
 
        /* write msg*/
286
 
        if((buf = pathname_encode(objpathname))== NULL ) {
287
 
                return HA_FAIL;
288
 
        }
289
 
        snprintf(pathname, MAXLEN, "%s/%s", HA_CIM_VARDIR, buf);
290
 
        cim_free(buf);
291
 
 
292
 
        if ( ( fobj = fopen(pathname, "w") ) == NULL ) {
293
 
                cl_log(LOG_WARNING, "msg2disk: can't open file.");
294
 
                cl_log(LOG_WARNING, "reason:%d(%s)", errno, strerror(errno));
295
 
                return HA_FAIL;
296
 
        }
297
 
        if ( msg->nfields == 0 ) {
298
 
                fclose(fobj);
299
 
                return HA_OK;
300
 
        }
301
 
 
302
 
        if ((msgstr = msg2string(msg)) == NULL ) {
303
 
                cl_log(LOG_ERR, "cim_msg2disk: msgstr NULL.");
304
 
                return HA_FAIL;
305
 
        }
306
 
 
307
 
        fprintf(fobj, "%s", msgstr);
308
 
        fclose(fobj); 
309
 
        return HA_OK;
310
 
}
311
 
 
312
 
struct ha_msg*
313
 
cim_disk2msg(const char *objpathname)
314
 
{
315
 
        char pathname[MAXLEN], *buf;
316
 
        FILE *fobj = NULL;
317
 
        int ret;
318
 
        int bytes = 0;
319
 
        struct stat st;
320
 
        struct ha_msg *msg = NULL;
321
 
 
322
 
        if((buf = pathname_encode(objpathname))== NULL ) {
323
 
                return NULL;
324
 
        }
325
 
        snprintf(pathname, MAXLEN, "%s/%s", HA_CIM_VARDIR, buf);
326
 
        cim_free(buf);
327
 
 
328
 
        if ( ( ret = stat(pathname, &st)) < 0 ) {
329
 
                cl_log(LOG_WARNING, "disk2msg: stat faild for %s.", pathname);
330
 
                cl_log(LOG_WARNING,"reason:%d(%s)",errno,strerror(errno)); 
331
 
                return NULL;
332
 
        } 
333
 
 
334
 
        if (st.st_size == 0 ) {
335
 
                cl_log(LOG_WARNING, "disk2msg: size of %s is zero.", objpathname);
336
 
                return NULL;
337
 
        }
338
 
 
339
 
        if ((buf = cim_malloc(st.st_size)) == NULL ) {
340
 
                cl_log(LOG_ERR, "disk2msg: alloc msg failed for %s.", objpathname);
341
 
                return NULL;
342
 
        }
343
 
        
344
 
        if ( (fobj = fopen(pathname, "r")) == NULL ) {
345
 
                cl_log(LOG_WARNING, "msg2disk: can't open file %s.", pathname);
346
 
                cl_log(LOG_WARNING, "reason:%d(%s)", errno, strerror(errno));
347
 
                return NULL;
348
 
        }
349
 
 
350
 
        while ( (ret = fread(buf, st.st_size, 1, fobj))){
351
 
                bytes += ret*st.st_size;
352
 
        }
353
 
        
354
 
        if ( !feof(fobj) ) {
355
 
                cl_log(LOG_ERR, "msg2disk: read error for %s.", objpathname);
356
 
                cl_log(LOG_ERR, "reason: %d(%s).", errno, strerror(errno));             
357
 
                cim_free(msg);
358
 
                return NULL;
359
 
        }
360
 
 
361
 
        if ( bytes != st.st_size ) {
362
 
                cl_log(LOG_ERR, "msg2disk: incompete read:");
363
 
                cl_log(LOG_ERR, "read: %d vs size: %d.", bytes, (int)st.st_size);
364
 
                cim_free(msg);
365
 
                        return NULL;
366
 
        }
367
 
        msg = string2msg(buf, bytes);
368
 
        cim_free(buf);
369
 
        fclose(fobj);
370
 
        return msg;
371
 
}
372
 
 
373
 
 
374
 
int
375
 
cim_disk_msg_del(const char *objpathname)
376
 
{
377
 
        char fullpathname[MAXLEN];
378
 
        char * pathname = pathname_encode(objpathname);
379
 
        snprintf(fullpathname, MAXLEN, "%s/%s", HA_CIM_VARDIR, pathname);
380
 
        cim_debug2(LOG_INFO, "%s: unlink %s", __FUNCTION__, fullpathname);
381
 
        unlink(fullpathname);
382
 
        cim_free(pathname);
383
 
        return HA_OK;
384
 
}
385
 
 
386
 
        
387
 
char*
388
 
cim_dbget(const char *pathname, const char*key)
389
 
{
390
 
        struct ha_msg *db = cim_disk2msg(pathname);
391
 
        const char * value;
392
 
 
393
 
        if ( db == NULL ) {
394
 
                return NULL;
395
 
        }
396
 
        
397
 
        value = cl_get_string(db, key);
398
 
        if ( value == NULL || strncmp(value, "null", MAXLEN) == 0) {
399
 
                return NULL;
400
 
        }
401
 
        return cim_strdup(value);
402
 
}
403
 
 
404
 
int
405
 
cim_dbput(const char *pathname, const char*key, const char*value)
406
 
{
407
 
        int ret;
408
 
        struct ha_msg* db = cim_disk2msg(pathname);
409
 
        if ( db == NULL ) {
410
 
                if ( (db = ha_msg_new(1)) == NULL ) {
411
 
                        cl_log(LOG_ERR, "cim_dbput: alloc db failed.");
412
 
                        return HA_FAIL;
413
 
                }
414
 
        }
415
 
 
416
 
        if ((cl_msg_modstring(db, key, value?value:"null")) != HA_OK ) {
417
 
                ha_msg_del(db);
418
 
                cl_log(LOG_ERR, "cim_dbput: put value failed.");
419
 
                return HA_FAIL;
420
 
        }
421
 
 
422
 
        ret = cim_msg2disk(pathname, db);
423
 
        ha_msg_del(db);
424
 
        return HA_OK;   
425
 
}
426
 
 
427
 
int
428
 
cim_dbdel(const char *pathname, const char*key)
429
 
{
430
 
        int ret;
431
 
        struct ha_msg* db = cim_disk2msg(pathname);
432
 
        if ( db == NULL ) {
433
 
                cl_log(LOG_ERR, "cim_dbdel: get db failed.");
434
 
                return HA_FAIL;
435
 
        }
436
 
 
437
 
        if ((cl_msg_remove(db, key)) != HA_OK ) {
438
 
                ha_msg_del(db);
439
 
                cl_log(LOG_ERR, "cim_dbdel: remove failed.");
440
 
                return HA_FAIL;
441
 
        }
442
 
 
443
 
        ret = cim_msg2disk(pathname, db);
444
 
        ha_msg_del(db);
445
 
        return HA_OK;   
446
 
}
447
 
 
448
 
struct ha_msg* 
449
 
cim_dbkeys(const char *pathname)
450
 
{
451
 
        struct ha_msg * db = cim_disk2msg(pathname);
452
 
        struct ha_msg * list;
453
 
        int i;
454
 
 
455
 
        if ( db == NULL ) {
456
 
                cl_log(LOG_ERR, "cim_dbkeys: get db failed.");
457
 
                return NULL;
458
 
        }
459
 
        
460
 
        if ( (list = ha_msg_new(1)) == NULL ) {
461
 
                ha_msg_del(db);
462
 
                cl_log(LOG_ERR, "cim_dbkeys: alloc list failed.");
463
 
                return NULL;
464
 
        }
465
 
        
466
 
        for (i = 0; i < db->nfields; i++) {
467
 
                cim_list_add(list, db->names[i]);
468
 
        }
469
 
 
470
 
        ha_msg_del(db);
471
 
        return list;
472
 
}
473
 
 
474
 
static char*   
475
 
pathname_encode(const char *pathname)
476
 
{
477
 
        char *new_pathname = NULL;
478
 
        char ch, *p;
479
 
        if ((new_pathname = cim_malloc(strlen(pathname)+1)) == NULL ) {
480
 
                cl_log(LOG_ERR, "pathname_enocde: alloc pathname failed.");
481
 
                return NULL;
482
 
        }
483
 
        p = new_pathname;
484
 
        while( (ch = *(pathname++)) ) {
485
 
                if (ch == '\\' || ch == '/')  {
486
 
                        *(p++) = '_';
487
 
                } else {
488
 
                        *(p++) = ch;
489
 
                }
490
 
        }
491
 
        *p = EOS;
492
 
        return new_pathname;
493
 
}
494
 
 
495
 
 
496
 
/****************************************************
497
 
 * msg
498
 
 ****************************************************/
499
 
int
500
 
cim_msg_children_count(struct ha_msg *parent)
501
 
{
502
 
        int i, count = 0;
503
 
        for (i = 0; i < parent->nfields; i++) {
504
 
                if ( parent->types[i] == FT_STRUCT ) {
505
 
                        count++;
506
 
                }
507
 
        }
508
 
        return count;
509
 
}
510
 
 
511
 
 
512
 
const char *
513
 
cim_msg_child_name(struct ha_msg *parent, int index)
514
 
{       
515
 
        int i, current = 0;
516
 
        for (i = 0; i < parent->nfields; i++) {
517
 
                if ( parent->types[i] == FT_STRUCT ) {
518
 
                        if ( index == current) {
519
 
                                return parent->names[i];
520
 
                        }
521
 
                        current++;
522
 
                }
523
 
        }
524
 
        return NULL;
525
 
}
526
 
 
527
 
 
528
 
struct ha_msg * 
529
 
cim_msg_child_index(struct ha_msg *parent, int index)
530
 
{
531
 
        int i, current = 0;
532
 
        for (i = 0; i < parent->nfields; i++) {
533
 
                if ( parent->types[i] == FT_STRUCT ) {
534
 
                        if ( index == current) {
535
 
                                return parent->values[i];
536
 
                        }
537
 
                        current++;
538
 
                }
539
 
        }
540
 
        return NULL;
541
 
}
542
 
 
543
 
 
544
 
int
545
 
cim_list_find(struct ha_msg *list, const char *value)
546
 
{
547
 
        int len = cim_list_length(list);
548
 
        int i = 0;
549
 
        for (i = 0; i<len; i++) {
550
 
                char * v = cim_list_index(list, i);
551
 
                if ( v && (strncmp(v, value, MAXLEN) == 0)) {
552
 
                        return TRUE;
553
 
                }
554
 
        }
555
 
        return FALSE;
556
 
}