2
* Linux HA management common macros and functions
4
* Author: Huang Zhen <zhenhltc@cn.ibm.com>
5
* Copyright (C) 2005 International Business Machines
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.
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.
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
22
#include <lha_internal.h>
29
#include <sys/types.h>
30
#include <sys/socket.h>
31
#include <netinet/in.h>
32
#include <arpa/inet.h>
34
#include <mgmt/mgmt_common.h>
36
malloc_t malloc_f = NULL;
37
realloc_t realloc_f = NULL;
41
mgmt_new_msg(const char* type, ...)
47
/* count the total len of fields */
48
len = strnlen(type, MAX_STRLEN)+1;
51
char* arg = va_arg(ap, char*);
55
len += strnlen(arg, MAX_STRLEN)+1;
60
buf = (char*)mgmt_malloc(len+1);
65
/* assign the first field */
66
snprintf(buf, len, "%s", type);
71
char* arg = va_arg(ap, char*);
75
strncat(buf, "\n", len-strlen(buf)-1);
76
strncat(buf, arg, len-strlen(buf)-1);
83
mgmt_msg_append(char* msg, const char* append)
89
msg_len = strnlen(msg, MAX_MSGLEN);
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);
99
/* +2: one is the '\n', other is the end 0*/
101
msg = (char*)mgmt_realloc(msg, len);
102
strncat(msg, "\n", len-strlen(msg)-1);
107
mgmt_result_ok(char* msg)
110
char** args = mgmt_msg_args(msg, &num);
111
if (args == NULL || num ==0) {
114
else if (STRNCMP_CONST(args[0], MSG_OK)!=0) {
125
mgmt_msg_args(const char* msg, int* num)
138
len = strnlen(msg, MAX_MSGLEN);
139
buf = (char*)mgmt_malloc(len+1);
144
strncpy(buf, msg, len);
147
/* find out how many fields first */
161
/* malloc the array for args */
162
ret = (char**)mgmt_malloc(sizeof(char*)*n);
168
/* splite the string to fields */
170
for (i = 1; i < n; i++) {
171
ret[i] = strchr(ret[i-1],'\n');
182
mgmt_del_msg(char* msg)
189
mgmt_del_args(char** args)
192
if (args[0] != NULL) {
200
mgmt_set_mem_funcs(malloc_t m, realloc_t r, free_t f)
208
mgmt_malloc(size_t size)
210
if (malloc_f == NULL) {
213
return (*malloc_f)(size);
217
mgmt_realloc(void* oldval, size_t newsize)
219
if (realloc_f == NULL) {
220
return realloc(oldval, newsize);
222
return (*realloc_f)(oldval, newsize);
228
if (free_f == NULL) {