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

« back to all changes in this revision

Viewing changes to lib/mgmt/mgmt_common_lib.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
 
 * Linux HA management common macros and functions
3
 
 *
4
 
 * Author: Huang Zhen <zhenhltc@cn.ibm.com>
5
 
 * Copyright (C) 2005 International Business Machines
6
 
 *
7
 
 * This library is free software; you can redistribute it and/or
8
 
 * modify it under the terms of the GNU Lesser General Public
9
 
 * License as published by the Free Software Foundation; either
10
 
 * version 2.1 of the License, or (at your option) any later version.
11
 
 *
12
 
 * This library 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 GNU
15
 
 * Lesser General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU Lesser General Public
18
 
 * License along with this library; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 
 *
21
 
 */
22
 
#include <lha_internal.h>
23
 
#include <stdlib.h>
24
 
#include <unistd.h>
25
 
#include <stdarg.h>
26
 
#include <string.h>
27
 
#include <stdio.h>
28
 
#include <errno.h>
29
 
#include <sys/types.h>
30
 
#include <sys/socket.h>
31
 
#include <netinet/in.h>
32
 
#include <arpa/inet.h>
33
 
 
34
 
#include <mgmt/mgmt_common.h>
35
 
 
36
 
malloc_t        malloc_f = NULL;
37
 
realloc_t       realloc_f = NULL;
38
 
free_t          free_f = NULL;
39
 
 
40
 
char*
41
 
mgmt_new_msg(const char* type, ...)
42
 
{
43
 
        va_list ap;
44
 
        int len;
45
 
        char* buf;
46
 
        
47
 
        /* count the total len of fields */     
48
 
        len = strnlen(type, MAX_STRLEN)+1;
49
 
        va_start(ap,type);
50
 
        while(1) {
51
 
                char* arg = va_arg(ap, char*);
52
 
                if (arg == NULL) {
53
 
                        break;
54
 
                }
55
 
                len += strnlen(arg, MAX_STRLEN)+1;
56
 
        }
57
 
        va_end(ap);
58
 
        
59
 
        /* alloc memory */
60
 
        buf = (char*)mgmt_malloc(len+1);
61
 
        if (buf == NULL) {
62
 
                return NULL;
63
 
        }
64
 
 
65
 
        /* assign the first field */
66
 
        snprintf(buf, len, "%s", type);
67
 
        
68
 
        /* then the others */
69
 
        va_start(ap, type);
70
 
        while(1) {
71
 
                char* arg = va_arg(ap, char*);
72
 
                if (arg == NULL) {
73
 
                        break;
74
 
                }
75
 
                strncat(buf, "\n", len-strlen(buf)-1);
76
 
                strncat(buf, arg, len-strlen(buf)-1);
77
 
        }
78
 
        va_end(ap);
79
 
        
80
 
        return buf;
81
 
}
82
 
char*
83
 
mgmt_msg_append(char* msg, const char* append)
84
 
{
85
 
        int msg_len;
86
 
        int append_len;
87
 
        int len;
88
 
        
89
 
        msg_len = strnlen(msg, MAX_MSGLEN);
90
 
        if (append != NULL) {
91
 
                append_len = strnlen(append, MAX_STRLEN);
92
 
                /* +2: one is the '\n', other is the end 0*/
93
 
                len = msg_len+append_len+2;
94
 
                msg = (char*)mgmt_realloc(msg, len);
95
 
                strncat(msg, "\n", len-strlen(msg)-1);
96
 
                strncat(msg, append, len-strlen(msg)-1);
97
 
        }
98
 
        else {
99
 
                /* +2: one is the '\n', other is the end 0*/
100
 
                len = msg_len+2;
101
 
                msg = (char*)mgmt_realloc(msg, len);
102
 
                strncat(msg, "\n", len-strlen(msg)-1);
103
 
        }
104
 
        return msg;
105
 
}
106
 
int
107
 
mgmt_result_ok(char* msg)
108
 
{
109
 
        int ret, num;
110
 
        char** args = mgmt_msg_args(msg, &num);
111
 
        if (args == NULL || num ==0) {
112
 
                ret = 0;
113
 
        }
114
 
        else if (STRNCMP_CONST(args[0], MSG_OK)!=0) {
115
 
                ret = 0;
116
 
        }
117
 
        else {
118
 
                ret = 1;
119
 
        }
120
 
        mgmt_del_args(args);
121
 
        return ret;
122
 
}
123
 
 
124
 
char**
125
 
mgmt_msg_args(const char* msg, int* num)
126
 
{
127
 
        char* p;
128
 
        char* buf;
129
 
        char** ret = NULL;
130
 
        int i,n;
131
 
        int len;
132
 
        
133
 
        if (msg == NULL) {
134
 
                return NULL;
135
 
        }
136
 
        
137
 
        /* alloc memory */
138
 
        len = strnlen(msg, MAX_MSGLEN);
139
 
        buf = (char*)mgmt_malloc(len+1);
140
 
        if (buf == NULL) {
141
 
                return NULL;
142
 
        }
143
 
        
144
 
        strncpy(buf, msg, len);
145
 
        buf[len] = 0;
146
 
        
147
 
        /* find out how many fields first */
148
 
        p = buf;
149
 
        n = 1;
150
 
        while(1) {
151
 
                p=strchr(p,'\n');
152
 
                if (p != NULL) {
153
 
                        p++;
154
 
                        n++;
155
 
                }
156
 
                else {
157
 
                        break;
158
 
                }
159
 
        }
160
 
 
161
 
        /* malloc the array for args */
162
 
        ret = (char**)mgmt_malloc(sizeof(char*)*n);
163
 
        if (ret == NULL) {
164
 
                mgmt_free(buf);
165
 
                return NULL;
166
 
        }
167
 
 
168
 
        /* splite the string to fields */
169
 
        ret[0] = buf;
170
 
        for (i = 1; i < n; i++) {
171
 
                ret[i] = strchr(ret[i-1],'\n');
172
 
                *ret[i] = 0;
173
 
                ret[i]++;
174
 
        }
175
 
        if (num != NULL) {
176
 
                *num = n;
177
 
        }
178
 
        return ret;
179
 
}
180
 
 
181
 
void
182
 
mgmt_del_msg(char* msg)
183
 
{
184
 
        if (msg != NULL) {
185
 
                mgmt_free(msg);
186
 
        }
187
 
}
188
 
void
189
 
mgmt_del_args(char** args)
190
 
{
191
 
        if (args != NULL) {
192
 
                if (args[0] != NULL) {
193
 
                        mgmt_free(args[0]);
194
 
                }
195
 
                mgmt_free(args);
196
 
        }
197
 
}
198
 
 
199
 
void
200
 
mgmt_set_mem_funcs(malloc_t m, realloc_t r, free_t f)
201
 
{
202
 
        malloc_f = m;
203
 
        realloc_f = r;
204
 
        free_f = f;
205
 
}
206
 
 
207
 
void*
208
 
mgmt_malloc(size_t size)
209
 
{
210
 
        if (malloc_f == NULL) {
211
 
                return malloc(size);
212
 
        }
213
 
        return (*malloc_f)(size);
214
 
}
215
 
 
216
 
void*
217
 
mgmt_realloc(void* oldval, size_t newsize)
218
 
{
219
 
        if (realloc_f == NULL) {
220
 
                return realloc(oldval, newsize);
221
 
        }
222
 
        return (*realloc_f)(oldval, newsize);
223
 
}
224
 
 
225
 
void
226
 
mgmt_free(void *ptr)
227
 
{
228
 
        if (free_f == NULL) {
229
 
                free(ptr);
230
 
                return;
231
 
        }
232
 
        (*free_f)(ptr);
233
 
}