Skip to content

Commit f1f43dc

Browse files
author
Ken Gaillot
committed
Fix: tools: clean up before exiting crm_resource
Previously, crm_resource would close open connections and free memory before exiting only in certain circumstances. Now, it always does.
1 parent 062e4c8 commit f1f43dc

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

tools/crm_resource.c

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,43 +31,63 @@ int cib_options = cib_sync_call;
3131

3232
static GMainLoop *mainloop = NULL;
3333

34+
// Things that should be cleaned up on exit
35+
static cib_t *cib_conn = NULL;
36+
static pcmk_controld_api_t *controld_api = NULL;
37+
static pe_working_set_t *data_set = NULL;
38+
3439
#define MESSAGE_TIMEOUT_S 60
3540

41+
// Clean up and exit
42+
_Noreturn static void
43+
bye(crm_exit_t exit_code)
44+
{
45+
if (cib_conn != NULL) {
46+
cib_conn->cmds->signoff(cib_conn);
47+
cib_delete(cib_conn);
48+
}
49+
if (controld_api != NULL) {
50+
pcmk_free_controld_api(controld_api);
51+
}
52+
pe_free_working_set(data_set);
53+
crm_exit(exit_code);
54+
}
55+
3656
static gboolean
3757
resource_ipc_timeout(gpointer data)
3858
{
3959
fprintf(stderr, "Aborting because no messages received in %d seconds\n",
4060
MESSAGE_TIMEOUT_S);
4161
crm_err("No messages received in %d seconds", MESSAGE_TIMEOUT_S);
42-
crm_exit(CRM_EX_TIMEOUT);
62+
bye(CRM_EX_TIMEOUT);
4363
}
4464

4565
static void
46-
handle_controller_reply(pcmk_controld_api_t *controld_api, void *api_data,
66+
handle_controller_reply(pcmk_controld_api_t *capi, void *api_data,
4767
void *user_data)
4868
{
4969
fprintf(stderr, ".");
50-
if ((controld_api->replies_expected(controld_api) == 0)
70+
if ((capi->replies_expected(capi) == 0)
5171
&& mainloop && g_main_loop_is_running(mainloop)) {
5272
fprintf(stderr, " OK\n");
5373
crm_debug("Got all the replies we expected");
54-
crm_exit(CRM_EX_OK);
74+
bye(CRM_EX_OK);
5575
}
5676
}
5777

5878
static void
59-
handle_controller_drop(pcmk_controld_api_t *controld_api, void *api_data,
79+
handle_controller_drop(pcmk_controld_api_t *capi, void *api_data,
6080
void *user_data)
6181
{
6282
crm_info("Connection to controller was terminated");
63-
crm_exit(CRM_EX_DISCONNECT);
83+
bye(CRM_EX_DISCONNECT);
6484
}
6585

6686
static void
67-
start_mainloop(pcmk_controld_api_t *controld_api)
87+
start_mainloop(pcmk_controld_api_t *capi)
6888
{
69-
if (controld_api->replies_expected(controld_api) > 0) {
70-
unsigned int count = controld_api->replies_expected(controld_api);
89+
if (capi->replies_expected(capi) > 0) {
90+
unsigned int count = capi->replies_expected(capi);
7191

7292
fprintf(stderr, "Waiting for %d %s from the controller",
7393
count, pcmk__plural_alt(count, "reply", "replies"));
@@ -466,10 +486,7 @@ main(int argc, char **argv)
466486
GHashTable *override_params = NULL;
467487

468488
char *xml_file = NULL;
469-
pcmk_controld_api_t *controld_api = NULL;
470-
pe_working_set_t *data_set = NULL;
471489
xmlNode *cib_xml_copy = NULL;
472-
cib_t *cib_conn = NULL;
473490
resource_t *rsc = NULL;
474491
bool recursive = FALSE;
475492

@@ -560,7 +577,7 @@ main(int argc, char **argv)
560577
}
561578

562579
lrmd_api_delete(lrmd_conn);
563-
crm_exit(exit_code);
580+
bye(exit_code);
564581

565582
} else if (safe_str_eq("show-metadata", longname)) {
566583
char *standard = NULL;
@@ -589,7 +606,7 @@ main(int argc, char **argv)
589606
exit_code = crm_errno2exit(rc);
590607
}
591608
lrmd_api_delete(lrmd_conn);
592-
crm_exit(exit_code);
609+
bye(exit_code);
593610

594611
} else if (safe_str_eq("list-agents", longname)) {
595612
lrmd_list_t *list = NULL;
@@ -613,7 +630,7 @@ main(int argc, char **argv)
613630
exit_code = CRM_EX_NOSUCH;
614631
}
615632
lrmd_api_delete(lrmd_conn);
616-
crm_exit(exit_code);
633+
bye(exit_code);
617634

618635
} else if (safe_str_eq("class", longname)) {
619636
if (!(pcmk_get_ra_caps(optarg) & pcmk_ra_cap_params)) {
@@ -622,7 +639,7 @@ main(int argc, char **argv)
622639
optarg);
623640
}
624641

625-
crm_exit(exit_code);
642+
bye(exit_code);
626643
} else {
627644
v_class = optarg;
628645
}
@@ -896,13 +913,13 @@ main(int argc, char **argv)
896913
"validate-all", validate_options,
897914
override_params, timeout_ms);
898915
exit_code = crm_errno2exit(rc);
899-
crm_exit(exit_code);
916+
bye(exit_code);
900917
}
901918
}
902919

903920
if (argerr) {
904921
CMD_ERR("Invalid option(s) supplied, use --help for valid usage");
905-
crm_exit(CRM_EX_USAGE);
922+
bye(CRM_EX_USAGE);
906923
}
907924

908925
if (do_force) {
@@ -1368,15 +1385,6 @@ main(int argc, char **argv)
13681385

13691386
bail:
13701387

1371-
pe_free_working_set(data_set);
1372-
if (cib_conn != NULL) {
1373-
cib_conn->cmds->signoff(cib_conn);
1374-
cib_delete(cib_conn);
1375-
}
1376-
if (controld_api != NULL) {
1377-
pcmk_free_controld_api(controld_api);
1378-
}
1379-
13801388
if (is_ocf_rc) {
13811389
exit_code = rc;
13821390

@@ -1390,5 +1398,5 @@ main(int argc, char **argv)
13901398
}
13911399
}
13921400

1393-
crm_exit(exit_code);
1401+
bye(exit_code);
13941402
}

0 commit comments

Comments
 (0)