This source file includes following definitions.
- cib_rename
- retrieveCib
- cib_archive_filter
- cib_archive_sort
- readCibXmlFile
- uninitializeCib
- activateCibXml
- cib_diskwrite_complete
- write_cib_contents
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <dirent.h>
19
20 #include <sys/param.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <sys/stat.h>
24
25 #include <glib.h>
26 #include <libxml/tree.h>
27
28 #include <crm/crm.h>
29
30 #include <crm/cib.h>
31 #include <crm/common/util.h>
32 #include <crm/common/xml.h>
33 #include <crm/cib/internal.h>
34 #include <crm/cluster.h>
35
36 #include <pacemaker-based.h>
37
38 crm_trigger_t *cib_writer = NULL;
39
40 int write_cib_contents(gpointer p);
41
42 static void
43 cib_rename(const char *old)
44 {
45 int new_fd;
46 char *new = crm_strdup_printf("%s/cib.auto.XXXXXX", cib_root);
47
48 umask(S_IWGRP | S_IWOTH | S_IROTH);
49 new_fd = mkstemp(new);
50
51 if ((new_fd < 0) || (rename(old, new) < 0)) {
52 crm_err("Couldn't archive unusable file %s (disabling disk writes and continuing)",
53 old);
54 cib_writes_enabled = FALSE;
55 } else {
56 crm_err("Archived unusable file %s as %s", old, new);
57 }
58
59 if (new_fd > 0) {
60 close(new_fd);
61 }
62 free(new);
63 }
64
65
66
67
68
69 static xmlNode *
70 retrieveCib(const char *filename, const char *sigfile)
71 {
72 xmlNode *root = NULL;
73
74 crm_info("Reading cluster configuration file %s (digest: %s)",
75 filename, sigfile);
76 switch (cib_file_read_and_verify(filename, sigfile, &root)) {
77 case -pcmk_err_cib_corrupt:
78 crm_warn("Continuing but %s will NOT be used.", filename);
79 break;
80
81 case -pcmk_err_cib_modified:
82
83 crm_warn("Continuing but %s will NOT be used.", filename);
84 cib_rename(filename);
85 cib_rename(sigfile);
86 break;
87 }
88 return root;
89 }
90
91
92
93
94 #ifndef DT_UNKNOWN
95 # define DT_UNKNOWN 0
96 # define DT_FIFO 1
97 # define DT_CHR 2
98 # define DT_DIR 4
99 # define DT_BLK 6
100 # define DT_REG 8
101 # define DT_LNK 10
102 # define DT_SOCK 12
103 # define DT_WHT 14
104 #endif
105
106 static int cib_archive_filter(const struct dirent * a)
107 {
108 int rc = 0;
109
110 struct stat s;
111 char *a_path = crm_strdup_printf("%s/%s", cib_root, a->d_name);
112
113 if(stat(a_path, &s) != 0) {
114 rc = errno;
115 crm_trace("%s - stat failed: %s (%d)", a->d_name, pcmk_rc_str(rc), rc);
116 rc = 0;
117
118 } else if ((s.st_mode & S_IFREG) != S_IFREG) {
119 unsigned char dtype;
120 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
121 dtype = a->d_type;
122 #else
123 switch (s.st_mode & S_IFMT) {
124 case S_IFREG: dtype = DT_REG; break;
125 case S_IFDIR: dtype = DT_DIR; break;
126 case S_IFCHR: dtype = DT_CHR; break;
127 case S_IFBLK: dtype = DT_BLK; break;
128 case S_IFLNK: dtype = DT_LNK; break;
129 case S_IFIFO: dtype = DT_FIFO; break;
130 case S_IFSOCK: dtype = DT_SOCK; break;
131 default: dtype = DT_UNKNOWN; break;
132 }
133 #endif
134 crm_trace("%s - wrong type (%d)", a->d_name, dtype);
135
136 } else if(strstr(a->d_name, "cib-") != a->d_name) {
137 crm_trace("%s - wrong prefix", a->d_name);
138
139 } else if (pcmk__ends_with_ext(a->d_name, ".sig")) {
140 crm_trace("%s - wrong suffix", a->d_name);
141
142 } else {
143 crm_debug("%s - candidate", a->d_name);
144 rc = 1;
145 }
146
147 free(a_path);
148 return rc;
149 }
150
151 static int cib_archive_sort(const struct dirent ** a, const struct dirent **b)
152 {
153
154 int rc = 0;
155 struct stat buf;
156
157 time_t a_age = 0;
158 time_t b_age = 0;
159
160 char *a_path = crm_strdup_printf("%s/%s", cib_root, a[0]->d_name);
161 char *b_path = crm_strdup_printf("%s/%s", cib_root, b[0]->d_name);
162
163 if(stat(a_path, &buf) == 0) {
164 a_age = buf.st_ctime;
165 }
166 if(stat(b_path, &buf) == 0) {
167 b_age = buf.st_ctime;
168 }
169
170 free(a_path);
171 free(b_path);
172
173 if(a_age > b_age) {
174 rc = 1;
175 } else if(a_age < b_age) {
176 rc = -1;
177 }
178
179 crm_trace("%s (%lu) vs. %s (%lu) : %d",
180 a[0]->d_name, (unsigned long)a_age,
181 b[0]->d_name, (unsigned long)b_age, rc);
182 return rc;
183 }
184
185 xmlNode *
186 readCibXmlFile(const char *dir, const char *file, gboolean discard_status)
187 {
188 struct dirent **namelist = NULL;
189
190 int lpc = 0;
191 char *sigfile = NULL;
192 char *sigfilepath = NULL;
193 char *filename = NULL;
194 const char *name = NULL;
195 const char *value = NULL;
196 const char *use_valgrind = pcmk__env_option(PCMK__ENV_VALGRIND_ENABLED);
197
198 xmlNode *root = NULL;
199 xmlNode *status = NULL;
200
201 sigfile = crm_strdup_printf("%s.sig", file);
202 if (pcmk__daemon_can_write(dir, file) == FALSE
203 || pcmk__daemon_can_write(dir, sigfile) == FALSE) {
204 cib_status = -EACCES;
205 return NULL;
206 }
207
208 filename = crm_strdup_printf("%s/%s", dir, file);
209 sigfilepath = crm_strdup_printf("%s/%s", dir, sigfile);
210 free(sigfile);
211
212 cib_status = pcmk_ok;
213 root = retrieveCib(filename, sigfilepath);
214 free(filename);
215 free(sigfilepath);
216
217 if (root == NULL) {
218 crm_warn("Primary configuration corrupt or unusable, trying backups in %s", cib_root);
219 lpc = scandir(cib_root, &namelist, cib_archive_filter, cib_archive_sort);
220 if (lpc < 0) {
221 crm_err("scandir(%s) failed: %s", cib_root, pcmk_rc_str(errno));
222 }
223 }
224
225 while (root == NULL && lpc > 1) {
226 crm_debug("Testing %d candidates", lpc);
227
228 lpc--;
229
230 filename = crm_strdup_printf("%s/%s", cib_root, namelist[lpc]->d_name);
231 sigfile = crm_strdup_printf("%s.sig", filename);
232
233 crm_info("Reading cluster configuration file %s (digest: %s)",
234 filename, sigfile);
235 if (cib_file_read_and_verify(filename, sigfile, &root) < 0) {
236 crm_warn("Continuing but %s will NOT be used.", filename);
237 } else {
238 crm_notice("Continuing with last valid configuration archive: %s", filename);
239 }
240
241 free(namelist[lpc]);
242 free(filename);
243 free(sigfile);
244 }
245 free(namelist);
246
247 if (root == NULL) {
248 root = createEmptyCib(0);
249 crm_warn("Continuing with an empty configuration.");
250 }
251
252 if (cib_writes_enabled && (use_valgrind != NULL)
253 && (crm_is_true(use_valgrind)
254 || (strstr(use_valgrind, PCMK__SERVER_BASED) != NULL))) {
255
256 cib_writes_enabled = FALSE;
257 crm_err("*** Disabling disk writes to avoid confusing Valgrind ***");
258 }
259
260 status = pcmk__xe_first_child(root, PCMK_XE_STATUS, NULL, NULL);
261 if (discard_status && status != NULL) {
262
263 pcmk__xml_free(status);
264 status = NULL;
265 }
266 if (status == NULL) {
267 pcmk__xe_create(root, PCMK_XE_STATUS);
268 }
269
270
271
272
273 name = PCMK_XA_ADMIN_EPOCH;
274 value = crm_element_value(root, name);
275 if (value == NULL) {
276 crm_warn("No value for %s was specified in the configuration.", name);
277 crm_warn("The recommended course of action is to shutdown,"
278 " run crm_verify and fix any errors it reports.");
279 crm_warn("We will default to zero and continue but may get"
280 " confused about which configuration to use if"
281 " multiple nodes are powered up at the same time.");
282 crm_xml_add_int(root, name, 0);
283 }
284
285 name = PCMK_XA_EPOCH;
286 value = crm_element_value(root, name);
287 if (value == NULL) {
288 crm_xml_add_int(root, name, 0);
289 }
290
291 name = PCMK_XA_NUM_UPDATES;
292 value = crm_element_value(root, name);
293 if (value == NULL) {
294 crm_xml_add_int(root, name, 0);
295 }
296
297
298 pcmk__xe_remove_attr(root, PCMK_XA_DC_UUID);
299
300 if (discard_status) {
301 crm_log_xml_trace(root, "[on-disk]");
302 }
303
304 if (!pcmk__configured_schema_validates(root)) {
305 cib_status = -pcmk_err_schema_validation;
306 }
307 return root;
308 }
309
310 gboolean
311 uninitializeCib(void)
312 {
313 xmlNode *tmp_cib = the_cib;
314
315 if (tmp_cib == NULL) {
316 crm_debug("The CIB has already been deallocated.");
317 return FALSE;
318 }
319
320 the_cib = NULL;
321
322 crm_debug("Deallocating the CIB.");
323
324 pcmk__xml_free(tmp_cib);
325
326 crm_debug("The CIB has been deallocated.");
327
328 return TRUE;
329 }
330
331
332
333
334
335 int
336 activateCibXml(xmlNode * new_cib, gboolean to_disk, const char *op)
337 {
338 if (new_cib) {
339 xmlNode *saved_cib = the_cib;
340
341 pcmk__assert(new_cib != saved_cib);
342 the_cib = new_cib;
343 pcmk__xml_free(saved_cib);
344 if (cib_writes_enabled && cib_status == pcmk_ok && to_disk) {
345 crm_debug("Triggering CIB write for %s op", op);
346 mainloop_set_trigger(cib_writer);
347 }
348 return pcmk_ok;
349 }
350
351 crm_err("Ignoring invalid CIB");
352 if (the_cib) {
353 crm_warn("Reverting to last known CIB");
354 } else {
355 crm_crit("Could not write out new CIB and no saved version to revert to");
356 }
357 return -ENODATA;
358 }
359
360 static void
361 cib_diskwrite_complete(mainloop_child_t * p, pid_t pid, int core, int signo, int exitcode)
362 {
363 const char *errmsg = "Could not write CIB to disk";
364
365 if ((exitcode != 0) && cib_writes_enabled) {
366 cib_writes_enabled = FALSE;
367 errmsg = "Disabling CIB disk writes after failure";
368 }
369
370 if ((signo == 0) && (exitcode == 0)) {
371 crm_trace("Disk write [%d] succeeded", (int) pid);
372
373 } else if (signo == 0) {
374 crm_err("%s: process %d exited %d", errmsg, (int) pid, exitcode);
375
376 } else {
377 crm_err("%s: process %d terminated with signal %d (%s)%s",
378 errmsg, (int) pid, signo, strsignal(signo),
379 (core? " and dumped core" : ""));
380 }
381
382 mainloop_trigger_complete(cib_writer);
383 }
384
385 int
386 write_cib_contents(gpointer p)
387 {
388 int exit_rc = pcmk_ok;
389 xmlNode *cib_local = NULL;
390
391
392 if (p) {
393
394 cib_local = pcmk__xml_copy(NULL, p);
395
396 } else {
397 int pid = 0;
398 int bb_state = qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_STATE_GET, 0);
399
400
401
402
403
404
405
406 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE);
407
408 pid = fork();
409 if (pid < 0) {
410 crm_err("Disabling disk writes after fork failure: %s", pcmk_rc_str(errno));
411 cib_writes_enabled = FALSE;
412 return FALSE;
413 }
414
415 if (pid) {
416
417 mainloop_child_add(pid, 0, "disk-writer", NULL, cib_diskwrite_complete);
418 if (bb_state == QB_LOG_STATE_ENABLED) {
419
420 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_TRUE);
421 }
422
423 return -1;
424 }
425
426
427
428
429
430
431 cib_local = pcmk__xml_copy(NULL, the_cib);
432 }
433
434
435 exit_rc = cib_file_write_with_digest(cib_local, cib_root, "cib.xml");
436
437
438 pcmk__xml_free(cib_local);
439 if (p == NULL) {
440 crm_exit_t exit_code = CRM_EX_OK;
441
442 switch (exit_rc) {
443 case pcmk_ok:
444 exit_code = CRM_EX_OK;
445 break;
446 case pcmk_err_cib_modified:
447 exit_code = CRM_EX_DIGEST;
448 break;
449 case pcmk_err_cib_backup:
450 case pcmk_err_cib_save:
451 exit_code = CRM_EX_CANTCREAT;
452 break;
453 default:
454 exit_code = CRM_EX_ERROR;
455 break;
456 }
457
458
459 pcmk_common_cleanup();
460 _exit(exit_code);
461 }
462 return exit_rc;
463 }