Skip to content

Commit 7b289c4

Browse files
committed
Refactor: libcrmservice: Simplify systemd override path getters
Separate the path string creation logic from the directory creation logic. This reduces some duplication. Signed-off-by: Reid Wahl <[email protected]>
1 parent 2c94e05 commit 7b289c4

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

lib/services/systemd.c

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,6 @@ unit_method_complete(DBusPendingCall *pending, void *user_data)
798798
}
799799
}
800800

801-
#define SYSTEMD_OVERRIDE_ROOT "/run/systemd/system/"
802-
803801
/* When the cluster manages a systemd resource, we create a unit file override
804802
* to order the service "before" pacemaker. The "before" relationship won't
805803
* actually be used, since systemd won't ever start the resource -- we're
@@ -819,37 +817,31 @@ unit_method_complete(DBusPendingCall *pending, void *user_data)
819817

820818
/*!
821819
* \internal
822-
* \brief Create a runtime drop-in directory for a systemd unit
823-
*
824-
* This directory does not survive a reboot.
820+
* \brief Get runtime drop-in directory path for a systemd unit
825821
*
826822
* \param[in] agent Systemd resource agent
827823
*
828-
* \return Standard Pacemaker return code
829-
*
830-
* \note Any configuration in \c /etc takes precedence over our drop-in.
831-
* \todo Document this in Pacemaker Explained or Administration?
824+
* \return Drop-in directory path
832825
*/
833-
static int
834-
create_override_dir(const char *agent)
826+
static GString *
827+
get_override_dir(const char *agent)
835828
{
836-
char *override_dir = crm_strdup_printf(SYSTEMD_OVERRIDE_ROOT
837-
"/%s.service.d", agent);
838-
int rc = pcmk__build_path(override_dir, 0755);
829+
GString *buf = g_string_sized_new(128);
839830

840-
if (rc != pcmk_rc_ok) {
841-
crm_err("Could not create systemd override directory %s: %s",
842-
override_dir, pcmk_rc_str(rc));
843-
}
844-
free(override_dir);
845-
return rc;
831+
pcmk__g_strcat(buf, "/run/systemd/system/", agent, ".service.d", NULL);
832+
return buf;
846833
}
847834

848-
static char *
849-
get_override_filename(const char *agent)
835+
/*!
836+
* \internal
837+
* \brief Append systemd override filename to a directory path
838+
*
839+
* \param[in,out] buf Buffer containing directory path to append to
840+
*/
841+
static inline void
842+
append_override_basename(GString *buf)
850843
{
851-
return crm_strdup_printf(SYSTEMD_OVERRIDE_ROOT
852-
"/%s.service.d/50-pacemaker.conf", agent);
844+
g_string_append(buf, "/50-pacemaker.conf");
853845
}
854846

855847
/*!
@@ -869,22 +861,26 @@ get_override_filename(const char *agent)
869861
static int
870862
systemd_create_override(const char *agent, int timeout)
871863
{
872-
char *filename = NULL;
864+
GString *filename = NULL;
873865
FILE *fp = NULL;
874866
int fd = 0;
875867
char *override = NULL;
876-
int rc = create_override_dir(agent);
868+
int rc = pcmk_rc_ok;
877869

870+
filename = get_override_dir(agent);
871+
rc = pcmk__build_path(filename->str, 0755);
878872
if (rc != pcmk_rc_ok) {
873+
crm_err("Could not create systemd override directory %s: %s",
874+
filename->str, pcmk_rc_str(rc));
879875
goto done;
880876
}
881877

882-
filename = get_override_filename(agent);
883-
fp = fopen(filename, "w");
878+
append_override_basename(filename);
879+
fp = fopen(filename->str, "w");
884880
if (fp == NULL) {
885881
rc = errno;
886882
crm_err("Cannot open systemd override file %s for writing: %s",
887-
filename, pcmk_rc_str(rc));
883+
filename->str, pcmk_rc_str(rc));
888884
goto done;
889885
}
890886

@@ -895,14 +891,14 @@ systemd_create_override(const char *agent, int timeout)
895891
if ((fd < 0) || (fchmod(fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) < 0)) {
896892
rc = errno;
897893
crm_err("Failed to set permissions on systemd override file %s: %s",
898-
filename, pcmk_rc_str(rc));
894+
filename->str, pcmk_rc_str(rc));
899895
goto done;
900896
}
901897

902898
override = crm_strdup_printf(SYSTEMD_OVERRIDE_TEMPLATE, agent);
903899
if (fputs(override, fp) == EOF) {
904900
rc = EIO;
905-
crm_err("Cannot write to systemd override file %s", filename);
901+
crm_err("Cannot write to systemd override file %s", filename->str);
906902
}
907903

908904
done:
@@ -916,32 +912,36 @@ systemd_create_override(const char *agent, int timeout)
916912

917913
} else if (fp != NULL) {
918914
// File was created, so remove it
919-
unlink(filename);
915+
unlink(filename->str);
920916
}
921917

922-
free(filename);
918+
if (filename != NULL) {
919+
g_string_free(filename, TRUE);
920+
}
923921
free(override);
924922
return rc;
925923
}
926924

927925
static void
928926
systemd_remove_override(const char *agent, int timeout)
929927
{
930-
char *filename = get_override_filename(agent);
928+
GString *filename = get_override_dir(agent);
931929

932-
if (unlink(filename) < 0) {
930+
append_override_basename(filename);
931+
if (unlink(filename->str) < 0) {
933932
int rc = errno;
934933

935934
if (rc != ENOENT) {
936935
// Stop may be called when already stopped, which is fine
937936
crm_warn("Cannot remove systemd override file %s: %s",
938-
filename, pcmk_rc_str(rc));
937+
filename->str, pcmk_rc_str(rc));
939938
}
940939

941940
} else {
942941
systemd_daemon_reload(timeout);
943942
}
944-
free(filename);
943+
944+
g_string_free(filename, TRUE);
945945
}
946946

947947
/*!

0 commit comments

Comments
 (0)