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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/*
* Copyright 2023-2024 the Pacemaker project contributors
*
* The version control history for this file may have further details.
*
* This source code is licensed under the GNU Lesser General Public License
* version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
*/
#include <crm_internal.h>
#include <ftw.h>
#include <unistd.h>
#include <sys/stat.h>
#include <crm/cib.h>
#include <crm/cib/cib_types.h>
#include <crm/cib/internal.h>
#include <crm/crm.h>
#include <crm/common/mainloop.h>
#include <crm/common/xml.h>
#include "pacemaker-execd.h"
static pid_t schema_fetch_pid = 0;
static int
rm_files(const char *pathname, const struct stat *sbuf, int type, struct FTW *ftwb)
{
/* Don't delete PCMK__REMOTE_SCHEMA_DIR . */
if (ftwb->level == 0) {
return 0;
}
if (remove(pathname) != 0) {
int rc = errno;
crm_err("Could not remove %s: %s", pathname, pcmk_rc_str(rc));
return -1;
}
return 0;
}
static void
clean_up_extra_schema_files(void)
{
const char *remote_schema_dir = pcmk__remote_schema_dir();
struct stat sb;
int rc;
rc = stat(remote_schema_dir, &sb);
if (rc == -1) {
if (errno == ENOENT) {
/* If the directory doesn't exist, try to make it first. */
if (mkdir(remote_schema_dir, 0755) != 0) {
rc = errno;
crm_err("Could not create directory for schemas: %s",
pcmk_rc_str(rc));
}
} else {
rc = errno;
crm_err("Could not create directory for schemas: %s",
pcmk_rc_str(rc));
}
} else if (!S_ISDIR(sb.st_mode)) {
/* If something exists with the same name that's not a directory, that's
* an error.
*/
crm_err("%s already exists but is not a directory", remote_schema_dir);
} else {
/* It's a directory - clear it out so we can download potentially new
* schema files.
*/
rc = nftw(remote_schema_dir, rm_files, 10, FTW_DEPTH|FTW_MOUNT|FTW_PHYS);
if (rc != 0) {
crm_err("Could not remove %s: %s", remote_schema_dir, pcmk_rc_str(rc));
}
}
}
static void
write_extra_schema_file(xmlNode *xml, void *user_data)
{
const char *remote_schema_dir = pcmk__remote_schema_dir();
const char *file = NULL;
char *path = NULL;
int rc;
file = crm_element_value(xml, PCMK_XA_PATH);
if (file == NULL) {
crm_warn("No destination path given in schema request");
return;
}
path = crm_strdup_printf("%s/%s", remote_schema_dir, file);
/* The schema is a CDATA node, which is a child of the <file> node. Traverse
* all children and look for the first CDATA child. There can't be more than
* one because we only have one file attribute on the parent.
*/
for (xmlNode *child = xml->children; child != NULL; child = child->next) {
FILE *stream = NULL;
if (child->type != XML_CDATA_SECTION_NODE) {
continue;
}
stream = fopen(path, "w+");
if (stream == NULL) {
crm_warn("Could not write schema file %s: %s", path, strerror(errno));
} else {
rc = fprintf(stream, "%s", child->content);
if (rc < 0) {
crm_warn("Could not write schema file %s: %s", path, strerror(errno));
}
fclose(stream);
}
break;
}
free(path);
}
static void
get_schema_files(void)
{
int rc = pcmk_rc_ok;
cib_t *cib = NULL;
xmlNode *reply;
cib = cib_new();
if (cib == NULL) {
_exit(ENOTCONN);
}
rc = cib->cmds->signon(cib, crm_system_name, cib_query);
if (rc != pcmk_ok) {
crm_err("Could not connect to the CIB manager: %s", pcmk_strerror(rc));
_exit(pcmk_rc2exitc(rc));
}
rc = cib->cmds->fetch_schemas(cib, &reply, pcmk__highest_schema_name(),
cib_sync_call);
if (rc != pcmk_ok) {
crm_err("Could not get schema files: %s", pcmk_strerror(rc));
rc = pcmk_legacy2rc(rc);
} else if (reply->children != NULL) {
/* The returned document looks something like this:
* <cib_command>
* <cib_calldata>
* <schemas>
* <schema version="pacemaker-1.1">
* <file path="foo-1.1">
* </file>
* <file path="bar-1.1">
* </file>
* ...
* </schema>
* <schema version="pacemaker-1.2">
* </schema>
* ...
* </schemas>
* </cib_calldata>
* </cib_command>
*
* All the <schemas> and <schema> tags are really just there for organizing
* the XML a little better. What we really care about are the <file> nodes,
* and specifically the path attributes and the CDATA children (not shown)
* of each. We can use an xpath query to reach down and get all the <file>
* nodes at once.
*
* If we already have the latest schema version, or we asked for one later
* than what the cluster supports, we'll get back an empty <schemas> node,
* so all this will continue to work. It just won't do anything.
*/
crm_foreach_xpath_result(reply, "//" PCMK_XA_FILE,
write_extra_schema_file, NULL);
}
free_xml(reply);
cib__clean_up_connection(&cib);
_exit(pcmk_rc2exitc(rc));
}
/* Load any additional schema files when the child is finished fetching and
* saving them to disk.
*/
static void
get_schema_files_complete(mainloop_child_t *p, pid_t pid, int core, int signo, int exitcode)
{
const char *errmsg = "Could not load additional schema files";
if ((signo == 0) && (exitcode == 0)) {
const char *remote_schema_dir = pcmk__remote_schema_dir();
/* Don't just crm_schema_init here because that will load the base
* schemas again too. Instead just load the things we fetched.
*/
pcmk__load_schemas_from_dir(remote_schema_dir);
pcmk__sort_schemas();
crm_info("Fetching extra schema files completed successfully");
} else {
if (signo == 0) {
crm_err("%s: process %d exited %d", errmsg, (int) pid, exitcode);
} else {
crm_err("%s: process %d terminated with signal %d (%s)%s",
errmsg, (int) pid, signo, strsignal(signo),
(core? " and dumped core" : ""));
}
/* Clean up any incomplete schema data we might have been downloading when
* the process timed out or crashed. We don't need to do any extra cleanup
* because we never loaded the extra schemas, and we don't need to call
* crm_schema_init because that was called in remoted_request_cib_schema_files
* before this function.
*/
clean_up_extra_schema_files();
}
}
void
remoted_request_cib_schema_files(void)
{
pid_t pid;
int rc;
/* If a previous schema-fetch process is still running when we're called
* again, it's hung. Attempt to kill it before cleaning up the extra
* directory.
*/
if (schema_fetch_pid != 0) {
if (mainloop_child_kill(schema_fetch_pid) == FALSE) {
crm_warn("Unable to kill pre-existing schema-fetch process");
return;
}
schema_fetch_pid = 0;
}
/* Clean up any extra schema files we downloaded from a previous cluster
* connection. After the files are gone, we need to wipe them from
* known_schemas, but there's no opposite operation for add_schema().
*
* Instead, unload all the schemas. This means we'll also forget about all
* installed schemas as well, which means that pcmk__highest_schema_name()
* would fail. So we need to load the base schemas right now.
*/
clean_up_extra_schema_files();
crm_schema_cleanup();
crm_schema_init();
crm_info("Fetching extra schema files from cluster");
pid = fork();
switch (pid) {
case -1: {
rc = errno;
crm_warn("Could not spawn process to get schema files: %s", pcmk_rc_str(rc));
break;
}
case 0:
/* child */
get_schema_files();
break;
default:
/* parent */
schema_fetch_pid = pid;
mainloop_child_add_with_flags(pid, 5 * 60 * 1000, "schema-fetch", NULL,
mainloop_leave_pid_group,
get_schema_files_complete);
break;
}
}
|