forked from membase/libconflate
/
adhoc_commands.c
150 lines (125 loc) · 5.03 KB
/
adhoc_commands.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include "conflate.h"
#include "conflate_internal.h"
#include "conflate_convenience.h"
bool commands_initialized = false;
static enum conflate_mgmt_cb_result process_serverlist(void *opaque,
conflate_handle_t *handle,
const char *cmd,
bool direct,
kvpair_t *conf,
conflate_form_result *r)
{
/* If we have "config_is_private" set to "yes" we should only
process this if it's direct (i.e. ignore pubsub) */
if (!direct) {
char *priv = conflate_get_private(handle, "config_is_private",
handle->conf->save_path);
if (priv && strcmp(priv, "yes") == 0) {
CONFLATE_LOG(handle, INFO,
"Currently using a private config, ignoring update.");
return RV_OK;
}
free(priv);
}
CONFLATE_LOG(handle, INFO, "Processing a serverlist");
/* Persist the config lists */
if (!save_kvpairs(handle, conf, handle->conf->save_path)) {
CONFLATE_LOG(handle, ERROR, "Can not save config to %s",
handle->conf->save_path);
}
/* Send the config to the callback */
handle->conf->new_config(handle->conf->userdata, conf);
return RV_OK;
}
static enum conflate_mgmt_cb_result process_set_private(void *opaque,
conflate_handle_t *handle,
const char *cmd,
bool direct,
kvpair_t *form,
conflate_form_result *r)
{
/* Only direct stat requests are handled. */
assert(direct);
enum conflate_mgmt_cb_result rv = RV_ERROR;
char *key = get_simple_kvpair_val(form, "key");
char *value = get_simple_kvpair_val(form, "value");
if (key && value) {
if (conflate_save_private(handle, key, value,
handle->conf->save_path)) {
rv = RV_OK;
}
} else {
rv = RV_BADARG;
}
return rv;
}
static enum conflate_mgmt_cb_result process_get_private(void *opaque,
conflate_handle_t *handle,
const char *cmd,
bool direct,
kvpair_t *form,
conflate_form_result *r)
{
/* Only direct stat requests are handled. */
assert(direct);
enum conflate_mgmt_cb_result rv = RV_ERROR;
char *key = get_simple_kvpair_val(form, "key");
if (key) {
/* Initialize the form so there's always one there */
conflate_init_form(r);
char *value = conflate_get_private(handle, key,
handle->conf->save_path);
if (value) {
conflate_add_field(r, key, value);
free(value);
}
rv = RV_OK;
} else {
rv = RV_BADARG;
}
return rv;
}
static enum conflate_mgmt_cb_result process_delete_private(void *opaque,
conflate_handle_t *handle,
const char *cmd,
bool direct,
kvpair_t *form,
conflate_form_result *r)
{
/* Only direct stat requests are handled. */
assert(direct);
enum conflate_mgmt_cb_result rv = RV_ERROR;
char *key = get_simple_kvpair_val(form, "key");
if (key) {
if (conflate_delete_private(handle, key,
handle->conf->save_path)) {
rv = RV_OK;
}
} else {
rv = RV_BADARG;
}
return rv;
}
void conflate_init_commands(void)
{
if (commands_initialized) {
return;
}
conflate_register_mgmt_cb("set_private",
"Set a private value on the agent.",
process_set_private);
conflate_register_mgmt_cb("get_private",
"Get a private value from the agent.",
process_get_private);
conflate_register_mgmt_cb("rm_private",
"Delete a private value from the agent.",
process_delete_private);
conflate_register_mgmt_cb("serverlist", "Configure a server list.",
process_serverlist);
commands_initialized = true;
}