daemon: Record agent's systemd service

In https://github.com/coreos/rpm-ostree/pull/2395/, agent's id is
recorded in the journal. Similarly, record the systemd service that
called the client, as well.

Related to https://github.com/coreos/rpm-ostree/issues/1747.
This commit is contained in:
Kelvin Fan 2021-01-14 15:17:39 -05:00 committed by OpenShift Merge Robot
parent 8447fd7cfa
commit 3f9fe06d24
8 changed files with 55 additions and 7 deletions

View File

@ -68,6 +68,7 @@ struct RpmOstreeSysrootUpgrader {
RpmOstreeSysrootUpgraderFlags flags;
char *command_line;
char *agent;
char *sd_unit;
OstreeDeployment *cfg_merge_deployment;
OstreeDeployment *origin_merge_deployment;
@ -215,6 +216,7 @@ rpmostree_sysroot_upgrader_finalize (GObject *object)
g_free (self->osname);
g_free (self->command_line);
g_free (self->agent);
g_free (self->sd_unit);
g_clear_object (&self->cfg_merge_deployment);
g_clear_object (&self->origin_merge_deployment);
@ -343,12 +345,17 @@ rpmostree_sysroot_upgrader_new (OstreeSysroot *sysroot,
}
void
rpmostree_sysroot_upgrader_set_caller_info (RpmOstreeSysrootUpgrader *self, const char *initiating_command_line, const char *agent)
rpmostree_sysroot_upgrader_set_caller_info (RpmOstreeSysrootUpgrader *self,
const char *initiating_command_line,
const char *agent,
const char *sd_unit)
{
g_free (self->command_line);
self->command_line = g_strdup (initiating_command_line);
g_free (self->agent);
self->agent = g_strdup (agent);
g_free (self->sd_unit);
self->sd_unit = g_strdup (sd_unit);
}
RpmOstreeOrigin *
@ -1366,6 +1373,7 @@ write_history (RpmOstreeSysrootUpgrader *self,
"DEPLOYMENT_VERSION=%s", version ?: "",
"COMMAND_LINE=%s", self->command_line ?: "",
"AGENT=%s", self->agent ?: "",
"AGENT_SD_UNIT=%s", self->sd_unit ?: "",
NULL);
return TRUE;

View File

@ -78,7 +78,10 @@ RpmOstreeSysrootUpgrader *rpmostree_sysroot_upgrader_new (OstreeSysroot
GCancellable *cancellable,
GError **error);
void rpmostree_sysroot_upgrader_set_caller_info (RpmOstreeSysrootUpgrader *self, const char *initiating_command_line, const char *agent);
void rpmostree_sysroot_upgrader_set_caller_info (RpmOstreeSysrootUpgrader *self,
const char *initiating_command_line,
const char *agent,
const char *sd_unit);
OstreeDeployment* rpmostree_sysroot_upgrader_get_merge_deployment (RpmOstreeSysrootUpgrader *self);

View File

@ -732,6 +732,17 @@ rpmostreed_daemon_client_get_agent_id (RpmostreedDaemon *self, const char *clien
return g_strdup (clientdata->id);
}
/* Returns a string representing the systemd unit for @client, or %NULL if unknown */
char *
rpmostreed_daemon_client_get_sd_unit (RpmostreedDaemon *self, const char *client)
{
auto clientdata = static_cast<struct RpmOstreeClient *>(g_hash_table_lookup (self->bus_clients, client));
if (!clientdata || clientdata->sd_unit == NULL)
return NULL;
else
return g_strdup (clientdata->sd_unit);
}
void
rpmostreed_daemon_remove_client (RpmostreedDaemon *self,
const char *client)

View File

@ -44,6 +44,8 @@ char * rpmostreed_daemon_client_get_string (RpmostreedDaemon *self,
const char *client);
char * rpmostreed_daemon_client_get_agent_id (RpmostreedDaemon *self,
const char *client);
char * rpmostreed_daemon_client_get_sd_unit (RpmostreedDaemon *self,
const char *client);
void rpmostreed_daemon_exit_now (RpmostreedDaemon *self);
void rpmostreed_daemon_run_until_idle_exit (RpmostreedDaemon *self);
void rpmostreed_daemon_publish (RpmostreedDaemon *self,

View File

@ -1342,7 +1342,9 @@ deploy_transaction_execute (RpmostreedTransaction *transaction,
return FALSE;
}
rpmostree_sysroot_upgrader_set_caller_info (upgrader, command_line, rpmostreed_transaction_get_agent_id (RPMOSTREED_TRANSACTION(self)));
rpmostree_sysroot_upgrader_set_caller_info (upgrader, command_line,
rpmostreed_transaction_get_agent_id (RPMOSTREED_TRANSACTION(self)),
rpmostreed_transaction_get_sd_unit (RPMOSTREED_TRANSACTION(self)));
/* TODO - better logic for "changed" based on deployments */
if (changed || self->refspec)
@ -1751,7 +1753,9 @@ initramfs_etc_transaction_execute (RpmostreedTransaction *transaction,
rpmostree_sysroot_upgrader_new (sysroot, self->osname, static_cast<RpmOstreeSysrootUpgraderFlags>(upgrader_flags), cancellable, error);
if (upgrader == NULL)
return FALSE;
rpmostree_sysroot_upgrader_set_caller_info (upgrader, command_line, rpmostreed_transaction_get_agent_id (RPMOSTREED_TRANSACTION(self)));
rpmostree_sysroot_upgrader_set_caller_info (upgrader, command_line,
rpmostreed_transaction_get_agent_id (RPMOSTREED_TRANSACTION(self)),
rpmostreed_transaction_get_sd_unit (RPMOSTREED_TRANSACTION(self)));
g_autoptr(RpmOstreeOrigin) origin = rpmostree_sysroot_upgrader_dup_origin (upgrader);
@ -1925,7 +1929,9 @@ initramfs_state_transaction_execute (RpmostreedTransaction *transaction,
rpmostree_origin_set_regenerate_initramfs (origin, self->regenerate, self->args);
rpmostree_sysroot_upgrader_set_origin (upgrader, origin);
rpmostree_sysroot_upgrader_set_caller_info (upgrader, command_line, rpmostreed_transaction_get_agent_id (RPMOSTREED_TRANSACTION(self)));
rpmostree_sysroot_upgrader_set_caller_info (upgrader, command_line,
rpmostreed_transaction_get_agent_id (RPMOSTREED_TRANSACTION(self)),
rpmostreed_transaction_get_sd_unit (RPMOSTREED_TRANSACTION(self)));
if (!rpmostree_sysroot_upgrader_deploy (upgrader, NULL, cancellable, error))
return FALSE;
@ -2601,7 +2607,9 @@ kernel_arg_transaction_execute (RpmostreedTransaction *transaction,
g_autoptr(RpmOstreeSysrootUpgrader) upgrader =
rpmostree_sysroot_upgrader_new (sysroot, self->osname, static_cast<RpmOstreeSysrootUpgraderFlags>(upgrader_flags),
cancellable, error);
rpmostree_sysroot_upgrader_set_caller_info (upgrader, command_line, rpmostreed_transaction_get_agent_id (RPMOSTREED_TRANSACTION(self)));
rpmostree_sysroot_upgrader_set_caller_info (upgrader, command_line,
rpmostreed_transaction_get_agent_id (RPMOSTREED_TRANSACTION(self)),
rpmostreed_transaction_get_sd_unit (RPMOSTREED_TRANSACTION(self)));
/* We need the upgrader to perform the deployment */
if (upgrader == NULL)

View File

@ -41,9 +41,10 @@ struct _RpmostreedTransactionPrivate {
char *sysroot_path;
OstreeSysroot *sysroot;
gboolean sysroot_locked;
/* Capture of the client description and agent at txn creation time */
/* Capture of the client description, agent, and systemd unit at txn creation time */
char *client_description;
char *agent_id;
char *sd_unit;
gboolean redirect_output;
@ -507,6 +508,7 @@ transaction_finalize (GObject *object)
g_free (priv->client_description);
g_free (priv->agent_id);
g_free (priv->sd_unit);
G_OBJECT_CLASS (rpmostreed_transaction_parent_class)->finalize (object);
}
@ -541,6 +543,7 @@ transaction_constructed (GObject *object)
priv->client_description = rpmostreed_daemon_client_get_string (rpmostreed_daemon_get(), sender);
priv->agent_id = rpmostreed_daemon_client_get_agent_id (rpmostreed_daemon_get(), sender);
priv->sd_unit = rpmostreed_daemon_client_get_sd_unit (rpmostreed_daemon_get(), sender);
rpmostree_transaction_set_initiating_client_description ((RPMOSTreeTransaction*)self, priv->client_description);
}
}
@ -850,6 +853,15 @@ rpmostreed_transaction_get_agent_id (RpmostreedTransaction *transaction)
return priv->agent_id;
}
const char *
rpmostreed_transaction_get_sd_unit (RpmostreedTransaction *transaction)
{
g_return_val_if_fail (RPMOSTREED_IS_TRANSACTION (transaction), NULL);
RpmostreedTransactionPrivate *priv = rpmostreed_transaction_get_private (transaction);
return priv->sd_unit;
}
GDBusMethodInvocation *
rpmostreed_transaction_get_invocation (RpmostreedTransaction *transaction)
{

View File

@ -49,6 +49,7 @@ gboolean rpmostreed_transaction_get_active (RpmostreedTransactio
OstreeSysroot * rpmostreed_transaction_get_sysroot (RpmostreedTransaction *transaction);
const char * rpmostreed_transaction_get_client (RpmostreedTransaction *transaction);
const char * rpmostreed_transaction_get_agent_id (RpmostreedTransaction *transaction);
const char * rpmostreed_transaction_get_sd_unit (RpmostreedTransaction *transaction);
GDBusMethodInvocation *
rpmostreed_transaction_get_invocation (RpmostreedTransaction *transaction);
const char * rpmostreed_transaction_get_client_address (RpmostreedTransaction *transaction);

View File

@ -56,6 +56,9 @@ fi
vm_cmd journalctl --after-cursor "'$from_cursor'" -u rpm-ostreed -o json | jq -r '.AGENT//""' > agent.txt
assert_file_has_content agent.txt testing-agent-id
cursor=$(vm_get_journal_cursor)
vm_cmd journalctl --after-cursor "'$from_cursor'" -u rpm-ostreed -o json | jq -r '.AGENT_SD_UNIT//""' > agent_sd_unit.txt
assert_file_has_content agent_sd_unit.txt sshd.service
cursor=$(vm_get_journal_cursor)
vm_reboot_cmd rpm-ostree finalize-deployment "${commit}"
assert_streq "$(vm_get_booted_csum)" "${commit}"
vm_assert_journal_has_content $cursor "Finalized deployment; rebooting into ${commit}"