From 5c3d2e3f43ebc640d7968cbb2f1949d0fd24fc1b Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 22 Jan 2021 11:18:42 +0900 Subject: [PATCH 1/9] hostnamectl: improve log message on failure --- src/hostname/hostnamectl.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index 62da90b121..99c9883efa 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -222,9 +222,9 @@ static int show_status(int argc, char **argv, void *userdata) { } } -static int set_simple_string(sd_bus *bus, const char *method, const char *value) { +static int set_simple_string(sd_bus *bus, const char *target, const char *method, const char *value) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; - int r = 0; + int r; polkit_agent_open_if_enabled(arg_transport, arg_ask_password); @@ -237,7 +237,7 @@ static int set_simple_string(sd_bus *bus, const char *method, const char *value) &error, NULL, "sb", value, arg_ask_password); if (r < 0) - return log_error_errno(r, "Could not set property: %s", bus_error_message(&error, r)); + return log_error_errno(r, "Could not set %s: %s", target, bus_error_message(&error, r)); return 0; } @@ -262,7 +262,7 @@ static int set_hostname(int argc, char **argv, void *userdata) { else p = hostname; /* Use the passed name as pretty hostname */ - r = set_simple_string(bus, "SetPrettyHostname", p); + r = set_simple_string(bus, "pretty hostname", "SetPrettyHostname", p); if (r < 0) return r; @@ -280,13 +280,13 @@ static int set_hostname(int argc, char **argv, void *userdata) { } if (arg_static) { - r = set_simple_string(bus, "SetStaticHostname", hostname); + r = set_simple_string(bus, "static hostname", "SetStaticHostname", hostname); if (r < 0) return r; } if (arg_transient) { - r = set_simple_string(bus, "SetHostname", hostname); + r = set_simple_string(bus, "transient hostname", "SetHostname", hostname); if (r < 0) return r; } @@ -295,19 +295,19 @@ static int set_hostname(int argc, char **argv, void *userdata) { } static int set_icon_name(int argc, char **argv, void *userdata) { - return set_simple_string(userdata, "SetIconName", argv[1]); + return set_simple_string(userdata, "icon", "SetIconName", argv[1]); } static int set_chassis(int argc, char **argv, void *userdata) { - return set_simple_string(userdata, "SetChassis", argv[1]); + return set_simple_string(userdata, "chassis", "SetChassis", argv[1]); } static int set_deployment(int argc, char **argv, void *userdata) { - return set_simple_string(userdata, "SetDeployment", argv[1]); + return set_simple_string(userdata, "deployment", "SetDeployment", argv[1]); } static int set_location(int argc, char **argv, void *userdata) { - return set_simple_string(userdata, "SetLocation", argv[1]); + return set_simple_string(userdata, "location", "SetLocation", argv[1]); } static int help(void) { From 0ce2a086da8cfc89f4ed49ed965e4e8f17b5ccac Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 29 Jan 2021 15:28:41 +0900 Subject: [PATCH 2/9] bus-error: align error definitions --- src/libsystemd/sd-bus/bus-common-errors.h | 209 +++++++++++----------- 1 file changed, 105 insertions(+), 104 deletions(-) diff --git a/src/libsystemd/sd-bus/bus-common-errors.h b/src/libsystemd/sd-bus/bus-common-errors.h index 7e5be17d53..1e61dd62d3 100644 --- a/src/libsystemd/sd-bus/bus-common-errors.h +++ b/src/libsystemd/sd-bus/bus-common-errors.h @@ -3,121 +3,122 @@ #include "bus-error.h" -#define BUS_ERROR_NO_SUCH_UNIT "org.freedesktop.systemd1.NoSuchUnit" -#define BUS_ERROR_NO_UNIT_FOR_PID "org.freedesktop.systemd1.NoUnitForPID" -#define BUS_ERROR_NO_UNIT_FOR_INVOCATION_ID "org.freedesktop.systemd1.NoUnitForInvocationID" -#define BUS_ERROR_UNIT_EXISTS "org.freedesktop.systemd1.UnitExists" -#define BUS_ERROR_LOAD_FAILED "org.freedesktop.systemd1.LoadFailed" -#define BUS_ERROR_BAD_UNIT_SETTING "org.freedesktop.systemd1.BadUnitSetting" -#define BUS_ERROR_JOB_FAILED "org.freedesktop.systemd1.JobFailed" -#define BUS_ERROR_NO_SUCH_JOB "org.freedesktop.systemd1.NoSuchJob" -#define BUS_ERROR_NOT_SUBSCRIBED "org.freedesktop.systemd1.NotSubscribed" -#define BUS_ERROR_ALREADY_SUBSCRIBED "org.freedesktop.systemd1.AlreadySubscribed" -#define BUS_ERROR_ONLY_BY_DEPENDENCY "org.freedesktop.systemd1.OnlyByDependency" +#define BUS_ERROR_NO_SUCH_UNIT "org.freedesktop.systemd1.NoSuchUnit" +#define BUS_ERROR_NO_UNIT_FOR_PID "org.freedesktop.systemd1.NoUnitForPID" +#define BUS_ERROR_NO_UNIT_FOR_INVOCATION_ID "org.freedesktop.systemd1.NoUnitForInvocationID" +#define BUS_ERROR_UNIT_EXISTS "org.freedesktop.systemd1.UnitExists" +#define BUS_ERROR_LOAD_FAILED "org.freedesktop.systemd1.LoadFailed" +#define BUS_ERROR_BAD_UNIT_SETTING "org.freedesktop.systemd1.BadUnitSetting" +#define BUS_ERROR_JOB_FAILED "org.freedesktop.systemd1.JobFailed" +#define BUS_ERROR_NO_SUCH_JOB "org.freedesktop.systemd1.NoSuchJob" +#define BUS_ERROR_NOT_SUBSCRIBED "org.freedesktop.systemd1.NotSubscribed" +#define BUS_ERROR_ALREADY_SUBSCRIBED "org.freedesktop.systemd1.AlreadySubscribed" +#define BUS_ERROR_ONLY_BY_DEPENDENCY "org.freedesktop.systemd1.OnlyByDependency" #define BUS_ERROR_TRANSACTION_JOBS_CONFLICTING "org.freedesktop.systemd1.TransactionJobsConflicting" -#define BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC "org.freedesktop.systemd1.TransactionOrderIsCyclic" -#define BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE "org.freedesktop.systemd1.TransactionIsDestructive" -#define BUS_ERROR_UNIT_MASKED "org.freedesktop.systemd1.UnitMasked" -#define BUS_ERROR_UNIT_GENERATED "org.freedesktop.systemd1.UnitGenerated" -#define BUS_ERROR_UNIT_LINKED "org.freedesktop.systemd1.UnitLinked" -#define BUS_ERROR_JOB_TYPE_NOT_APPLICABLE "org.freedesktop.systemd1.JobTypeNotApplicable" -#define BUS_ERROR_NO_ISOLATION "org.freedesktop.systemd1.NoIsolation" -#define BUS_ERROR_SHUTTING_DOWN "org.freedesktop.systemd1.ShuttingDown" -#define BUS_ERROR_SCOPE_NOT_RUNNING "org.freedesktop.systemd1.ScopeNotRunning" -#define BUS_ERROR_NO_SUCH_DYNAMIC_USER "org.freedesktop.systemd1.NoSuchDynamicUser" -#define BUS_ERROR_NOT_REFERENCED "org.freedesktop.systemd1.NotReferenced" -#define BUS_ERROR_DISK_FULL "org.freedesktop.systemd1.DiskFull" -#define BUS_ERROR_NOTHING_TO_CLEAN "org.freedesktop.systemd1.NothingToClean" -#define BUS_ERROR_UNIT_BUSY "org.freedesktop.systemd1.UnitBusy" -#define BUS_ERROR_UNIT_INACTIVE "org.freedesktop.systemd1.UnitInactive" +#define BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC "org.freedesktop.systemd1.TransactionOrderIsCyclic" +#define BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE "org.freedesktop.systemd1.TransactionIsDestructive" +#define BUS_ERROR_UNIT_MASKED "org.freedesktop.systemd1.UnitMasked" +#define BUS_ERROR_UNIT_GENERATED "org.freedesktop.systemd1.UnitGenerated" +#define BUS_ERROR_UNIT_LINKED "org.freedesktop.systemd1.UnitLinked" +#define BUS_ERROR_JOB_TYPE_NOT_APPLICABLE "org.freedesktop.systemd1.JobTypeNotApplicable" +#define BUS_ERROR_NO_ISOLATION "org.freedesktop.systemd1.NoIsolation" +#define BUS_ERROR_SHUTTING_DOWN "org.freedesktop.systemd1.ShuttingDown" +#define BUS_ERROR_SCOPE_NOT_RUNNING "org.freedesktop.systemd1.ScopeNotRunning" +#define BUS_ERROR_NO_SUCH_DYNAMIC_USER "org.freedesktop.systemd1.NoSuchDynamicUser" +#define BUS_ERROR_NOT_REFERENCED "org.freedesktop.systemd1.NotReferenced" +#define BUS_ERROR_DISK_FULL "org.freedesktop.systemd1.DiskFull" +#define BUS_ERROR_NOTHING_TO_CLEAN "org.freedesktop.systemd1.NothingToClean" +#define BUS_ERROR_UNIT_BUSY "org.freedesktop.systemd1.UnitBusy" +#define BUS_ERROR_UNIT_INACTIVE "org.freedesktop.systemd1.UnitInactive" -#define BUS_ERROR_NO_SUCH_MACHINE "org.freedesktop.machine1.NoSuchMachine" -#define BUS_ERROR_NO_SUCH_IMAGE "org.freedesktop.machine1.NoSuchImage" -#define BUS_ERROR_NO_MACHINE_FOR_PID "org.freedesktop.machine1.NoMachineForPID" -#define BUS_ERROR_MACHINE_EXISTS "org.freedesktop.machine1.MachineExists" -#define BUS_ERROR_NO_PRIVATE_NETWORKING "org.freedesktop.machine1.NoPrivateNetworking" -#define BUS_ERROR_NO_SUCH_USER_MAPPING "org.freedesktop.machine1.NoSuchUserMapping" -#define BUS_ERROR_NO_SUCH_GROUP_MAPPING "org.freedesktop.machine1.NoSuchGroupMapping" +#define BUS_ERROR_NO_SUCH_MACHINE "org.freedesktop.machine1.NoSuchMachine" +#define BUS_ERROR_NO_SUCH_IMAGE "org.freedesktop.machine1.NoSuchImage" +#define BUS_ERROR_NO_MACHINE_FOR_PID "org.freedesktop.machine1.NoMachineForPID" +#define BUS_ERROR_MACHINE_EXISTS "org.freedesktop.machine1.MachineExists" +#define BUS_ERROR_NO_PRIVATE_NETWORKING "org.freedesktop.machine1.NoPrivateNetworking" +#define BUS_ERROR_NO_SUCH_USER_MAPPING "org.freedesktop.machine1.NoSuchUserMapping" +#define BUS_ERROR_NO_SUCH_GROUP_MAPPING "org.freedesktop.machine1.NoSuchGroupMapping" -#define BUS_ERROR_NO_SUCH_PORTABLE_IMAGE "org.freedesktop.portable1.NoSuchImage" -#define BUS_ERROR_BAD_PORTABLE_IMAGE_TYPE "org.freedesktop.portable1.BadImageType" +#define BUS_ERROR_NO_SUCH_PORTABLE_IMAGE "org.freedesktop.portable1.NoSuchImage" +#define BUS_ERROR_BAD_PORTABLE_IMAGE_TYPE "org.freedesktop.portable1.BadImageType" -#define BUS_ERROR_NO_SUCH_SESSION "org.freedesktop.login1.NoSuchSession" -#define BUS_ERROR_NO_SESSION_FOR_PID "org.freedesktop.login1.NoSessionForPID" -#define BUS_ERROR_NO_SUCH_USER "org.freedesktop.login1.NoSuchUser" -#define BUS_ERROR_NO_USER_FOR_PID "org.freedesktop.login1.NoUserForPID" -#define BUS_ERROR_NO_SUCH_SEAT "org.freedesktop.login1.NoSuchSeat" -#define BUS_ERROR_SESSION_NOT_ON_SEAT "org.freedesktop.login1.SessionNotOnSeat" -#define BUS_ERROR_NOT_IN_CONTROL "org.freedesktop.login1.NotInControl" -#define BUS_ERROR_DEVICE_IS_TAKEN "org.freedesktop.login1.DeviceIsTaken" -#define BUS_ERROR_DEVICE_NOT_TAKEN "org.freedesktop.login1.DeviceNotTaken" -#define BUS_ERROR_OPERATION_IN_PROGRESS "org.freedesktop.login1.OperationInProgress" -#define BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED "org.freedesktop.login1.SleepVerbNotSupported" -#define BUS_ERROR_SESSION_BUSY "org.freedesktop.login1.SessionBusy" -#define BUS_ERROR_NOT_YOUR_DEVICE "org.freedesktop.login1.NotYourDevice" +#define BUS_ERROR_NO_SUCH_SESSION "org.freedesktop.login1.NoSuchSession" +#define BUS_ERROR_NO_SESSION_FOR_PID "org.freedesktop.login1.NoSessionForPID" +#define BUS_ERROR_NO_SUCH_USER "org.freedesktop.login1.NoSuchUser" +#define BUS_ERROR_NO_USER_FOR_PID "org.freedesktop.login1.NoUserForPID" +#define BUS_ERROR_NO_SUCH_SEAT "org.freedesktop.login1.NoSuchSeat" +#define BUS_ERROR_SESSION_NOT_ON_SEAT "org.freedesktop.login1.SessionNotOnSeat" +#define BUS_ERROR_NOT_IN_CONTROL "org.freedesktop.login1.NotInControl" +#define BUS_ERROR_DEVICE_IS_TAKEN "org.freedesktop.login1.DeviceIsTaken" +#define BUS_ERROR_DEVICE_NOT_TAKEN "org.freedesktop.login1.DeviceNotTaken" +#define BUS_ERROR_OPERATION_IN_PROGRESS "org.freedesktop.login1.OperationInProgress" +#define BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED "org.freedesktop.login1.SleepVerbNotSupported" +#define BUS_ERROR_SESSION_BUSY "org.freedesktop.login1.SessionBusy" +#define BUS_ERROR_NOT_YOUR_DEVICE "org.freedesktop.login1.NotYourDevice" -#define BUS_ERROR_AUTOMATIC_TIME_SYNC_ENABLED "org.freedesktop.timedate1.AutomaticTimeSyncEnabled" -#define BUS_ERROR_NO_NTP_SUPPORT "org.freedesktop.timedate1.NoNTPSupport" +#define BUS_ERROR_AUTOMATIC_TIME_SYNC_ENABLED "org.freedesktop.timedate1.AutomaticTimeSyncEnabled" +#define BUS_ERROR_NO_NTP_SUPPORT "org.freedesktop.timedate1.NoNTPSupport" -#define BUS_ERROR_NO_SUCH_PROCESS "org.freedesktop.systemd1.NoSuchProcess" +#define BUS_ERROR_NO_SUCH_PROCESS "org.freedesktop.systemd1.NoSuchProcess" -#define BUS_ERROR_NO_NAME_SERVERS "org.freedesktop.resolve1.NoNameServers" -#define BUS_ERROR_INVALID_REPLY "org.freedesktop.resolve1.InvalidReply" -#define BUS_ERROR_NO_SUCH_RR "org.freedesktop.resolve1.NoSuchRR" -#define BUS_ERROR_CNAME_LOOP "org.freedesktop.resolve1.CNameLoop" -#define BUS_ERROR_ABORTED "org.freedesktop.resolve1.Aborted" -#define BUS_ERROR_NO_SUCH_SERVICE "org.freedesktop.resolve1.NoSuchService" -#define BUS_ERROR_DNSSEC_FAILED "org.freedesktop.resolve1.DnssecFailed" -#define BUS_ERROR_NO_TRUST_ANCHOR "org.freedesktop.resolve1.NoTrustAnchor" -#define BUS_ERROR_RR_TYPE_UNSUPPORTED "org.freedesktop.resolve1.ResourceRecordTypeUnsupported" -#define BUS_ERROR_NO_SUCH_LINK "org.freedesktop.resolve1.NoSuchLink" -#define BUS_ERROR_LINK_BUSY "org.freedesktop.resolve1.LinkBusy" -#define BUS_ERROR_NETWORK_DOWN "org.freedesktop.resolve1.NetworkDown" -#define BUS_ERROR_NO_SUCH_DNSSD_SERVICE "org.freedesktop.resolve1.NoSuchDnssdService" -#define BUS_ERROR_DNSSD_SERVICE_EXISTS "org.freedesktop.resolve1.DnssdServiceExists" -#define _BUS_ERROR_DNS "org.freedesktop.resolve1.DnsError." +#define BUS_ERROR_NO_NAME_SERVERS "org.freedesktop.resolve1.NoNameServers" +#define BUS_ERROR_INVALID_REPLY "org.freedesktop.resolve1.InvalidReply" +#define BUS_ERROR_NO_SUCH_RR "org.freedesktop.resolve1.NoSuchRR" +#define BUS_ERROR_CNAME_LOOP "org.freedesktop.resolve1.CNameLoop" +#define BUS_ERROR_ABORTED "org.freedesktop.resolve1.Aborted" +#define BUS_ERROR_NO_SUCH_SERVICE "org.freedesktop.resolve1.NoSuchService" +#define BUS_ERROR_DNSSEC_FAILED "org.freedesktop.resolve1.DnssecFailed" +#define BUS_ERROR_NO_TRUST_ANCHOR "org.freedesktop.resolve1.NoTrustAnchor" +#define BUS_ERROR_RR_TYPE_UNSUPPORTED "org.freedesktop.resolve1.ResourceRecordTypeUnsupported" +#define BUS_ERROR_NO_SUCH_LINK "org.freedesktop.resolve1.NoSuchLink" +#define BUS_ERROR_LINK_BUSY "org.freedesktop.resolve1.LinkBusy" +#define BUS_ERROR_NETWORK_DOWN "org.freedesktop.resolve1.NetworkDown" +#define BUS_ERROR_NO_SUCH_DNSSD_SERVICE "org.freedesktop.resolve1.NoSuchDnssdService" +#define BUS_ERROR_DNSSD_SERVICE_EXISTS "org.freedesktop.resolve1.DnssdServiceExists" +#define _BUS_ERROR_DNS "org.freedesktop.resolve1.DnsError." -#define BUS_ERROR_NO_SUCH_TRANSFER "org.freedesktop.import1.NoSuchTransfer" -#define BUS_ERROR_TRANSFER_IN_PROGRESS "org.freedesktop.import1.TransferInProgress" +#define BUS_ERROR_NO_SUCH_TRANSFER "org.freedesktop.import1.NoSuchTransfer" +#define BUS_ERROR_TRANSFER_IN_PROGRESS "org.freedesktop.import1.TransferInProgress" -#define BUS_ERROR_NO_PRODUCT_UUID "org.freedesktop.hostname1.NoProductUUID" +#define BUS_ERROR_NO_PRODUCT_UUID "org.freedesktop.hostname1.NoProductUUID" -#define BUS_ERROR_SPEED_METER_INACTIVE "org.freedesktop.network1.SpeedMeterInactive" -#define BUS_ERROR_UNMANAGED_INTERFACE "org.freedesktop.network1.UnmanagedInterface" +#define BUS_ERROR_SPEED_METER_INACTIVE "org.freedesktop.network1.SpeedMeterInactive" +#define BUS_ERROR_UNMANAGED_INTERFACE "org.freedesktop.network1.UnmanagedInterface" -#define BUS_ERROR_NO_SUCH_HOME "org.freedesktop.home1.NoSuchHome" -#define BUS_ERROR_UID_IN_USE "org.freedesktop.home1.UIDInUse" -#define BUS_ERROR_USER_NAME_EXISTS "org.freedesktop.home1.UserNameExists" -#define BUS_ERROR_HOME_EXISTS "org.freedesktop.home1.HomeExists" -#define BUS_ERROR_HOME_ALREADY_ACTIVE "org.freedesktop.home1.HomeAlreadyActive" -#define BUS_ERROR_HOME_ALREADY_FIXATED "org.freedesktop.home1.HomeAlreadyFixated" -#define BUS_ERROR_HOME_UNFIXATED "org.freedesktop.home1.HomeUnfixated" -#define BUS_ERROR_HOME_NOT_ACTIVE "org.freedesktop.home1.HomeNotActive" -#define BUS_ERROR_HOME_ABSENT "org.freedesktop.home1.HomeAbsent" -#define BUS_ERROR_HOME_BUSY "org.freedesktop.home1.HomeBusy" -#define BUS_ERROR_BAD_PASSWORD "org.freedesktop.home1.BadPassword" -#define BUS_ERROR_BAD_RECOVERY_KEY "org.freedesktop.home1.BadRecoveryKey" -#define BUS_ERROR_LOW_PASSWORD_QUALITY "org.freedesktop.home1.LowPasswordQuality" -#define BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN "org.freedesktop.home1.BadPasswordAndNoToken" -#define BUS_ERROR_TOKEN_PIN_NEEDED "org.freedesktop.home1.TokenPinNeeded" -#define BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED "org.freedesktop.home1.TokenProtectedAuthenticationPathNeeded" -#define BUS_ERROR_TOKEN_USER_PRESENCE_NEEDED "org.freedesktop.home1.TokenUserPresenceNeeded" -#define BUS_ERROR_TOKEN_ACTION_TIMEOUT "org.freedesktop.home1.TokenActionTimeout" -#define BUS_ERROR_TOKEN_PIN_LOCKED "org.freedesktop.home1.TokenPinLocked" -#define BUS_ERROR_TOKEN_BAD_PIN "org.freedesktop.home1.BadPin" +#define BUS_ERROR_NO_SUCH_HOME "org.freedesktop.home1.NoSuchHome" +#define BUS_ERROR_UID_IN_USE "org.freedesktop.home1.UIDInUse" +#define BUS_ERROR_USER_NAME_EXISTS "org.freedesktop.home1.UserNameExists" +#define BUS_ERROR_HOME_EXISTS "org.freedesktop.home1.HomeExists" +#define BUS_ERROR_HOME_ALREADY_ACTIVE "org.freedesktop.home1.HomeAlreadyActive" +#define BUS_ERROR_HOME_ALREADY_FIXATED "org.freedesktop.home1.HomeAlreadyFixated" +#define BUS_ERROR_HOME_UNFIXATED "org.freedesktop.home1.HomeUnfixated" +#define BUS_ERROR_HOME_NOT_ACTIVE "org.freedesktop.home1.HomeNotActive" +#define BUS_ERROR_HOME_ABSENT "org.freedesktop.home1.HomeAbsent" +#define BUS_ERROR_HOME_BUSY "org.freedesktop.home1.HomeBusy" +#define BUS_ERROR_BAD_PASSWORD "org.freedesktop.home1.BadPassword" +#define BUS_ERROR_BAD_RECOVERY_KEY "org.freedesktop.home1.BadRecoveryKey" +#define BUS_ERROR_LOW_PASSWORD_QUALITY "org.freedesktop.home1.LowPasswordQuality" +#define BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN "org.freedesktop.home1.BadPasswordAndNoToken" +#define BUS_ERROR_TOKEN_PIN_NEEDED "org.freedesktop.home1.TokenPinNeeded" +#define BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED \ + "org.freedesktop.home1.TokenProtectedAuthenticationPathNeeded" +#define BUS_ERROR_TOKEN_USER_PRESENCE_NEEDED "org.freedesktop.home1.TokenUserPresenceNeeded" +#define BUS_ERROR_TOKEN_ACTION_TIMEOUT "org.freedesktop.home1.TokenActionTimeout" +#define BUS_ERROR_TOKEN_PIN_LOCKED "org.freedesktop.home1.TokenPinLocked" +#define BUS_ERROR_TOKEN_BAD_PIN "org.freedesktop.home1.BadPin" #define BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT "org.freedesktop.home1.BadPinFewTriesLeft" -#define BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT "org.freedesktop.home1.BadPinOneTryLeft" -#define BUS_ERROR_BAD_SIGNATURE "org.freedesktop.home1.BadSignature" -#define BUS_ERROR_HOME_RECORD_MISMATCH "org.freedesktop.home1.RecordMismatch" -#define BUS_ERROR_HOME_RECORD_DOWNGRADE "org.freedesktop.home1.RecordDowngrade" -#define BUS_ERROR_HOME_RECORD_SIGNED "org.freedesktop.home1.RecordSigned" -#define BUS_ERROR_BAD_HOME_SIZE "org.freedesktop.home1.BadHomeSize" -#define BUS_ERROR_NO_PRIVATE_KEY "org.freedesktop.home1.NoPrivateKey" -#define BUS_ERROR_HOME_LOCKED "org.freedesktop.home1.HomeLocked" -#define BUS_ERROR_HOME_NOT_LOCKED "org.freedesktop.home1.HomeNotLocked" -#define BUS_ERROR_NO_DISK_SPACE "org.freedesktop.home1.NoDiskSpace" -#define BUS_ERROR_TOO_MANY_OPERATIONS "org.freedesktop.home1.TooManyOperations" -#define BUS_ERROR_AUTHENTICATION_LIMIT_HIT "org.freedesktop.home1.AuthenticationLimitHit" -#define BUS_ERROR_HOME_CANT_AUTHENTICATE "org.freedesktop.home1.HomeCantAuthenticate" +#define BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT "org.freedesktop.home1.BadPinOneTryLeft" +#define BUS_ERROR_BAD_SIGNATURE "org.freedesktop.home1.BadSignature" +#define BUS_ERROR_HOME_RECORD_MISMATCH "org.freedesktop.home1.RecordMismatch" +#define BUS_ERROR_HOME_RECORD_DOWNGRADE "org.freedesktop.home1.RecordDowngrade" +#define BUS_ERROR_HOME_RECORD_SIGNED "org.freedesktop.home1.RecordSigned" +#define BUS_ERROR_BAD_HOME_SIZE "org.freedesktop.home1.BadHomeSize" +#define BUS_ERROR_NO_PRIVATE_KEY "org.freedesktop.home1.NoPrivateKey" +#define BUS_ERROR_HOME_LOCKED "org.freedesktop.home1.HomeLocked" +#define BUS_ERROR_HOME_NOT_LOCKED "org.freedesktop.home1.HomeNotLocked" +#define BUS_ERROR_NO_DISK_SPACE "org.freedesktop.home1.NoDiskSpace" +#define BUS_ERROR_TOO_MANY_OPERATIONS "org.freedesktop.home1.TooManyOperations" +#define BUS_ERROR_AUTHENTICATION_LIMIT_HIT "org.freedesktop.home1.AuthenticationLimitHit" +#define BUS_ERROR_HOME_CANT_AUTHENTICATE "org.freedesktop.home1.HomeCantAuthenticate" BUS_ERROR_MAP_ELF_USE(bus_common_errors); From 957991b7acec20ccf2c21f0162e9da54ea22d2dd Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 25 Jan 2021 13:16:20 +0900 Subject: [PATCH 3/9] hostname: introduce two bus errors for updating file --- src/hostname/hostnamed.c | 8 ++++++++ src/libsystemd/sd-bus/bus-common-errors.c | 2 ++ src/libsystemd/sd-bus/bus-common-errors.h | 2 ++ 3 files changed, 12 insertions(+) diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index 0e4dd02b49..cc5c21cd1f 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -767,6 +767,10 @@ static int method_set_static_hostname(sd_bus_message *m, void *userdata, sd_bus_ r = context_write_data_static_hostname(c); if (r < 0) { log_error_errno(r, "Failed to write static hostname: %m"); + if (ERRNO_IS_PRIVILEGE(r)) + return sd_bus_error_set(error, BUS_ERROR_FILE_IS_PROTECTED, "Not allowed to update /etc/hostname."); + if (r == -EROFS) + return sd_bus_error_set(error, BUS_ERROR_READ_ONLY_FILESYSTEM, "/etc/hostname is in a read-only filesystem."); return sd_bus_error_set_errnof(error, r, "Failed to set static hostname: %m"); } @@ -843,6 +847,10 @@ static int set_machine_info(Context *c, sd_bus_message *m, int prop, sd_bus_mess r = context_write_data_machine_info(c); if (r < 0) { log_error_errno(r, "Failed to write machine info: %m"); + if (ERRNO_IS_PRIVILEGE(r)) + return sd_bus_error_set(error, BUS_ERROR_FILE_IS_PROTECTED, "Not allowed to update /etc/machine-info."); + if (r == -EROFS) + return sd_bus_error_set(error, BUS_ERROR_READ_ONLY_FILESYSTEM, "/etc/machine-info is in a read-only filesystem."); return sd_bus_error_set_errnof(error, r, "Failed to write machine info: %m"); } diff --git a/src/libsystemd/sd-bus/bus-common-errors.c b/src/libsystemd/sd-bus/bus-common-errors.c index ef1fa3711b..4966db07b3 100644 --- a/src/libsystemd/sd-bus/bus-common-errors.c +++ b/src/libsystemd/sd-bus/bus-common-errors.c @@ -101,6 +101,8 @@ BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map bus_common_errors[] = { SD_BUS_ERROR_MAP(BUS_ERROR_TRANSFER_IN_PROGRESS, EBUSY), SD_BUS_ERROR_MAP(BUS_ERROR_NO_PRODUCT_UUID, EOPNOTSUPP), + SD_BUS_ERROR_MAP(BUS_ERROR_FILE_IS_PROTECTED, EACCES), + SD_BUS_ERROR_MAP(BUS_ERROR_READ_ONLY_FILESYSTEM, EROFS), SD_BUS_ERROR_MAP(BUS_ERROR_SPEED_METER_INACTIVE, EOPNOTSUPP), SD_BUS_ERROR_MAP(BUS_ERROR_UNMANAGED_INTERFACE, EOPNOTSUPP), diff --git a/src/libsystemd/sd-bus/bus-common-errors.h b/src/libsystemd/sd-bus/bus-common-errors.h index 1e61dd62d3..56eb5551ec 100644 --- a/src/libsystemd/sd-bus/bus-common-errors.h +++ b/src/libsystemd/sd-bus/bus-common-errors.h @@ -81,6 +81,8 @@ #define BUS_ERROR_TRANSFER_IN_PROGRESS "org.freedesktop.import1.TransferInProgress" #define BUS_ERROR_NO_PRODUCT_UUID "org.freedesktop.hostname1.NoProductUUID" +#define BUS_ERROR_FILE_IS_PROTECTED "org.freedesktop.hostname1.FileIsProtected" +#define BUS_ERROR_READ_ONLY_FILESYSTEM "org.freedesktop.hostname1.ReadOnlyFilesystem" #define BUS_ERROR_SPEED_METER_INACTIVE "org.freedesktop.network1.SpeedMeterInactive" #define BUS_ERROR_UNMANAGED_INTERFACE "org.freedesktop.network1.UnmanagedInterface" From 2d79e90a1fb5cff8a94a936196533e4450e155a6 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 22 Jan 2021 11:20:50 +0900 Subject: [PATCH 4/9] hostnamectl: try to set transient hostname even if updating static or pretty hostname failed If no target (--pretty, --static, or --transient) is specified, then let's try to set transient hostname even if setting static or pretty hostname failed. This may be useful for read-only filesystem. --- src/hostname/hostnamectl.c | 57 +++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index 99c9883efa..aaba1deacd 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -11,6 +11,7 @@ #include "alloc-util.h" #include "architecture.h" +#include "bus-common-errors.h" #include "bus-error.h" #include "bus-map-properties.h" #include "hostname-util.h" @@ -222,36 +223,45 @@ static int show_status(int argc, char **argv, void *userdata) { } } -static int set_simple_string(sd_bus *bus, const char *target, const char *method, const char *value) { - _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; +static int set_simple_string_internal(sd_bus *bus, sd_bus_error *error, const char *target, const char *method, const char *value) { + _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; int r; polkit_agent_open_if_enabled(arg_transport, arg_ask_password); + if (!error) + error = &e; + r = sd_bus_call_method( bus, "org.freedesktop.hostname1", "/org/freedesktop/hostname1", "org.freedesktop.hostname1", method, - &error, NULL, + error, NULL, "sb", value, arg_ask_password); if (r < 0) - return log_error_errno(r, "Could not set %s: %s", target, bus_error_message(&error, r)); + return log_error_errno(r, "Could not set %s: %s", target, bus_error_message(error, r)); return 0; } +static int set_simple_string(sd_bus *bus, const char *target, const char *method, const char *value) { + return set_simple_string_internal(bus, NULL, target, method, value); +} + static int set_hostname(int argc, char **argv, void *userdata) { _cleanup_free_ char *h = NULL; const char *hostname = argv[1]; sd_bus *bus = userdata; - int r; + bool implicit = false, show_hint = false; + int r, ret = 0; if (!arg_pretty && !arg_static && !arg_transient) - arg_pretty = arg_static = arg_transient = true; + arg_pretty = arg_static = arg_transient = implicit = true; if (arg_pretty) { + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; const char *p; /* If the passed hostname is already valid, then assume the user doesn't know anything about pretty @@ -262,9 +272,17 @@ static int set_hostname(int argc, char **argv, void *userdata) { else p = hostname; /* Use the passed name as pretty hostname */ - r = set_simple_string(bus, "pretty hostname", "SetPrettyHostname", p); - if (r < 0) - return r; + r = set_simple_string_internal(bus, &error, "pretty hostname", "SetPrettyHostname", p); + if (r < 0) { + if (implicit && + sd_bus_error_has_names(&error, + BUS_ERROR_FILE_IS_PROTECTED, + BUS_ERROR_READ_ONLY_FILESYSTEM)) { + show_hint = true; + ret = r; + } else + return r; + } /* Now that we set the pretty hostname, let's clean up the parameter and use that as static * hostname. If the hostname was already valid as static hostname, this will only chop off the trailing @@ -280,9 +298,19 @@ static int set_hostname(int argc, char **argv, void *userdata) { } if (arg_static) { - r = set_simple_string(bus, "static hostname", "SetStaticHostname", hostname); - if (r < 0) - return r; + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + + r = set_simple_string_internal(bus, &error, "static hostname", "SetStaticHostname", hostname); + if (r < 0) { + if (implicit && + sd_bus_error_has_names(&error, + BUS_ERROR_FILE_IS_PROTECTED, + BUS_ERROR_READ_ONLY_FILESYSTEM)) { + show_hint = true; + ret = r; + } else + return r; + } } if (arg_transient) { @@ -291,7 +319,10 @@ static int set_hostname(int argc, char **argv, void *userdata) { return r; } - return 0; + if (show_hint) + log_info("Hint: use --transient option when /etc/machine-info or /etc/hostname cannot be modified (e.g. located in read-only filesystem)."); + + return ret; } static int set_icon_name(int argc, char **argv, void *userdata) { From c388618ec03aceb3c8d4dca75b639f876f28dd99 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 22 Jan 2021 11:28:06 +0900 Subject: [PATCH 5/9] hostnamectl: unset pretty hostname only when no target is specified --- src/hostname/hostnamectl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index aaba1deacd..b97411c42b 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -267,7 +267,7 @@ static int set_hostname(int argc, char **argv, void *userdata) { /* If the passed hostname is already valid, then assume the user doesn't know anything about pretty * hostnames, so let's unset the pretty hostname, and just set the passed hostname as static/dynamic * hostname. */ - if (arg_static && hostname_is_valid(hostname, VALID_HOSTNAME_TRAILING_DOT)) + if (implicit && hostname_is_valid(hostname, VALID_HOSTNAME_TRAILING_DOT)) p = ""; /* No pretty hostname (as it is redundant), just a static one */ else p = hostname; /* Use the passed name as pretty hostname */ From ba12e41d059349039993fd240046c858cae4256b Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 22 Jan 2021 16:38:52 +0900 Subject: [PATCH 6/9] hostname: re-read file later when failed to update file Previously, even when writing e.g. /etc/hostname fails, the static hostname in Context is not restored. So, the subsequent call of the same method succeeds: ``` $ sudo chattr +i /etc/hostname $ sudo hostnamectl --static set-hostname aaa Could not set static hostname: Access denied $ echo $? 1 $ sudo hostnamectl --static set-hostname aaa $ echo $? 0 ``` This makes when updating file is failed, the saved stat is cleared. So, the static hostname or machine information in the context are always consistent to the corresponding files. --- src/hostname/hostnamed.c | 46 +++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index cc5c21cd1f..73949fbfb8 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -367,20 +367,41 @@ static int context_update_kernel_hostname( return r; /* 0 if no change, 1 if something was done */ } +static void unset_statp(struct stat **p) { + if (!*p) + return; + + **p = (struct stat) {}; +} + static int context_write_data_static_hostname(Context *c) { + _cleanup_(unset_statp) struct stat *s = NULL; + int r; + assert(c); + /* Make sure that if we fail here, we invalidate the cached information, since it was updated + * already, even if we can't make it hit the disk. */ + s = &c->etc_hostname_stat; + if (isempty(c->data[PROP_STATIC_HOSTNAME])) { - if (unlink("/etc/hostname") < 0) - return errno == ENOENT ? 0 : -errno; + if (unlink("/etc/hostname") < 0 && errno != ENOENT) + return -errno; + + TAKE_PTR(s); return 0; } - return write_string_file_atomic_label("/etc/hostname", c->data[PROP_STATIC_HOSTNAME]); + r = write_string_file_atomic_label("/etc/hostname", c->data[PROP_STATIC_HOSTNAME]); + if (r < 0) + return r; + + TAKE_PTR(s); + return 0; } static int context_write_data_machine_info(Context *c) { - + _cleanup_(unset_statp) struct stat *s = NULL; static const char * const name[_PROP_MAX] = { [PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME", [PROP_ICON_NAME] = "ICON_NAME", @@ -388,12 +409,15 @@ static int context_write_data_machine_info(Context *c) { [PROP_DEPLOYMENT] = "DEPLOYMENT", [PROP_LOCATION] = "LOCATION", }; - _cleanup_strv_free_ char **l = NULL; int r; assert(c); + /* Make sure that if we fail here, we invalidate the cached information, since it was updated + * already, even if we can't make it hit the disk. */ + s = &c->etc_machine_info_stat; + r = load_env_file(NULL, "/etc/machine-info", &l); if (r < 0 && r != -ENOENT) return r; @@ -421,13 +445,19 @@ static int context_write_data_machine_info(Context *c) { } if (strv_isempty(l)) { - if (unlink("/etc/machine-info") < 0) - return errno == ENOENT ? 0 : -errno; + if (unlink("/etc/machine-info") < 0 && errno != ENOENT) + return -errno; + TAKE_PTR(s); return 0; } - return write_env_file_label("/etc/machine-info", l); + r = write_env_file_label("/etc/machine-info", l); + if (r < 0) + return r; + + TAKE_PTR(s); + return 0; } static int property_get_hardware_vendor( From e8acf0918654082bf24de9e73bf11577d2899c36 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 22 Jan 2021 16:46:18 +0900 Subject: [PATCH 7/9] hostname: use free_and_strdup_and_warn() --- src/hostname/hostnamed.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index 73949fbfb8..840e657141 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -790,7 +790,7 @@ static int method_set_static_hostname(sd_bus_message *m, void *userdata, sd_bus_ if (r == 0) return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ - r = free_and_strdup(&c->data[PROP_STATIC_HOSTNAME], name); + r = free_and_strdup_warn(&c->data[PROP_STATIC_HOSTNAME], name); if (r < 0) return r; @@ -870,7 +870,7 @@ static int set_machine_info(Context *c, sd_bus_message *m, int prop, sd_bus_mess if (r == 0) return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ - r = free_and_strdup(&c->data[prop], name); + r = free_and_strdup_warn(&c->data[prop], name); if (r < 0) return r; From f2d3ec7abf0743a6225ea04bb45bfe467ff20d99 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 25 Jan 2021 13:44:00 +0900 Subject: [PATCH 8/9] hostnamectl: show hint when user try to set transient hostname but static hostname is already used --- src/hostname/hostnamectl.c | 32 +++++++++++++++++++++++++++++--- src/shared/hostname-setup.c | 2 +- src/shared/hostname-setup.h | 4 +++- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index b97411c42b..50c96d9594 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -14,6 +14,7 @@ #include "bus-common-errors.h" #include "bus-error.h" #include "bus-map-properties.h" +#include "hostname-setup.h" #include "hostname-util.h" #include "main-func.h" #include "pretty-print.h" @@ -117,12 +118,17 @@ static void print_status_info(StatusInfo *i) { printf(" Hardware Model: %s\n", i->hardware_model); } -static int show_one_name(sd_bus *bus, const char* attr) { +static int get_one_name(sd_bus *bus, const char* attr, char **ret) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; const char *s; int r; + assert(bus); + assert(attr); + + /* This obtains one string property, and copy it if 'ret' is set, or print it otherwise. */ + r = sd_bus_get_property( bus, "org.freedesktop.hostname1", @@ -137,7 +143,16 @@ static int show_one_name(sd_bus *bus, const char* attr) { if (r < 0) return bus_log_parse_error(r); - printf("%s\n", s); + if (ret) { + char *str; + + str = strdup(s); + if (!str) + return log_oom(); + + *ret = str; + } else + printf("%s\n", s); return 0; } @@ -211,7 +226,7 @@ static int show_status(int argc, char **argv, void *userdata) { attr = arg_pretty ? "PrettyHostname" : arg_static ? "StaticHostname" : "Hostname"; - return show_one_name(bus, attr); + return get_one_name(bus, attr, NULL); } else { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; @@ -260,6 +275,17 @@ static int set_hostname(int argc, char **argv, void *userdata) { if (!arg_pretty && !arg_static && !arg_transient) arg_pretty = arg_static = arg_transient = implicit = true; + if (!implicit && !arg_static && arg_transient) { + _cleanup_free_ char *source = NULL; + + r = get_one_name(bus, "HostnameSource", &source); + if (r < 0) + return r; + + if (hostname_source_from_string(source) == HOSTNAME_STATIC) + log_info("Hint: static hostname is already set, so the specified transient hostname will not be used."); + } + if (arg_pretty) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; const char *p; diff --git a/src/shared/hostname-setup.c b/src/shared/hostname-setup.c index c0465d3dcd..5cdcdbd93c 100644 --- a/src/shared/hostname-setup.c +++ b/src/shared/hostname-setup.c @@ -233,4 +233,4 @@ static const char* const hostname_source_table[] = { [HOSTNAME_FALLBACK] = "fallback", }; -DEFINE_STRING_TABLE_LOOKUP_TO_STRING(hostname_source, HostnameSource); +DEFINE_STRING_TABLE_LOOKUP(hostname_source, HostnameSource); diff --git a/src/shared/hostname-setup.h b/src/shared/hostname-setup.h index 022f0eb835..1c9d08a5df 100644 --- a/src/shared/hostname-setup.h +++ b/src/shared/hostname-setup.h @@ -11,7 +11,9 @@ typedef enum HostnameSource { _HOSTNAME_INVALID = -1, } HostnameSource; -const char* hostname_source_to_string(HostnameSource source); +const char* hostname_source_to_string(HostnameSource source) _const_; +HostnameSource hostname_source_from_string(const char *str) _pure_; + int sethostname_idempotent(const char *s); int shorten_overlong(const char *s, char **ret); From 822be62fb23ed0ec1062ffd18057e53f6c2f8c01 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 29 Jan 2021 16:48:03 +0900 Subject: [PATCH 9/9] hostnamectl: use Table --- src/hostname/hostnamectl.c | 194 +++++++++++++++++++++++++++---------- 1 file changed, 145 insertions(+), 49 deletions(-) diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index 50c96d9594..e7cb015ef2 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -14,6 +14,7 @@ #include "bus-common-errors.h" #include "bus-error.h" #include "bus-map-properties.h" +#include "format-table.h" #include "hostname-setup.h" #include "hostname-util.h" #include "main-func.h" @@ -49,46 +50,109 @@ typedef struct StatusInfo { const char *hardware_model; } StatusInfo; -static void print_status_info(StatusInfo *i) { +static int print_status_info(StatusInfo *i) { + _cleanup_(table_unrefp) Table *table = NULL; sd_id128_t mid = {}, bid = {}; + TableCell *cell; int r; assert(i); - printf(" Static hostname: %s\n", strna(i->static_hostname)); + table = table_new("key", "value"); + if (!table) + return log_oom(); + + assert_se(cell = table_get_cell(table, 0, 0)); + (void) table_set_ellipsize_percent(table, cell, 100); + (void) table_set_align_percent(table, cell, 100); + + table_set_header(table, false); + + r = table_set_empty_string(table, "n/a"); + if (r < 0) + return log_oom(); + + r = table_add_many(table, + TABLE_STRING, "Static hostname:", + TABLE_STRING, i->static_hostname); + if (r < 0) + return table_log_add_error(r); if (!isempty(i->pretty_hostname) && - !streq_ptr(i->pretty_hostname, i->static_hostname)) - printf(" Pretty hostname: %s\n", i->pretty_hostname); + !streq_ptr(i->pretty_hostname, i->static_hostname)) { + r = table_add_many(table, + TABLE_STRING, "Pretty hostname:", + TABLE_STRING, i->pretty_hostname); + if (r < 0) + return table_log_add_error(r); + } if (!isempty(i->hostname) && - !streq_ptr(i->hostname, i->static_hostname)) - printf("Transient hostname: %s\n", i->hostname); + !streq_ptr(i->hostname, i->static_hostname)) { + r = table_add_many(table, + TABLE_STRING, "Transient hostname:", + TABLE_STRING, i->hostname); + if (r < 0) + return table_log_add_error(r); + } - if (!isempty(i->icon_name)) - printf(" Icon name: %s\n", - strna(i->icon_name)); + if (!isempty(i->icon_name)) { + r = table_add_many(table, + TABLE_STRING, "Icon name:", + TABLE_STRING, i->icon_name); + if (r < 0) + return table_log_add_error(r); + } - if (!isempty(i->chassis)) - printf(" Chassis: %s\n", - strna(i->chassis)); + if (!isempty(i->chassis)) { + r = table_add_many(table, + TABLE_STRING, "Chassis:", + TABLE_STRING, i->chassis); + if (r < 0) + return table_log_add_error(r); + } - if (!isempty(i->deployment)) - printf(" Deployment: %s\n", i->deployment); + if (!isempty(i->deployment)) { + r = table_add_many(table, + TABLE_STRING, "Deployment:", + TABLE_STRING, i->deployment); + if (r < 0) + return table_log_add_error(r); + } - if (!isempty(i->location)) - printf(" Location: %s\n", i->location); + if (!isempty(i->location)) { + r = table_add_many(table, + TABLE_STRING, "Location:", + TABLE_STRING, i->location); + if (r < 0) + return table_log_add_error(r); + } r = sd_id128_get_machine(&mid); - if (r >= 0) - printf(" Machine ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(mid)); + if (r >= 0) { + r = table_add_many(table, + TABLE_STRING, "Machine ID:", + TABLE_ID128, mid); + if (r < 0) + return table_log_add_error(r); + } r = sd_id128_get_boot(&bid); - if (r >= 0) - printf(" Boot ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(bid)); + if (r >= 0) { + r = table_add_many(table, + TABLE_STRING, "Boot ID:", + TABLE_ID128, bid); + if (r < 0) + return table_log_add_error(r); + } - if (!isempty(i->virtualization)) - printf(" Virtualization: %s\n", i->virtualization); + if (!isempty(i->virtualization)) { + r = table_add_many(table, + TABLE_STRING, "Virtualization:", + TABLE_STRING, i->virtualization); + if (r < 0) + return table_log_add_error(r); + } if (!isempty(i->os_pretty_name)) { _cleanup_free_ char *formatted = NULL; @@ -99,23 +163,61 @@ static void print_status_info(StatusInfo *i) { t = formatted; } - printf(" Operating System: %s\n", t); + r = table_add_many(table, + TABLE_STRING, "Operating System:", + TABLE_STRING, t); + if (r < 0) + return table_log_add_error(r); } - if (!isempty(i->os_cpe_name)) - printf(" CPE OS Name: %s\n", i->os_cpe_name); + if (!isempty(i->os_cpe_name)) { + r = table_add_many(table, + TABLE_STRING, "CPE OS Name:", + TABLE_STRING, i->os_cpe_name); + if (r < 0) + return table_log_add_error(r); + } - if (!isempty(i->kernel_name) && !isempty(i->kernel_release)) - printf(" Kernel: %s %s\n", i->kernel_name, i->kernel_release); + if (!isempty(i->kernel_name) && !isempty(i->kernel_release)) { + const char *v; - if (!isempty(i->architecture)) - printf(" Architecture: %s\n", i->architecture); + v = strjoina(i->kernel_name, " ", i->kernel_release); + r = table_add_many(table, + TABLE_STRING, "Kernel:", + TABLE_STRING, v); + if (r < 0) + return table_log_add_error(r); + } - if (!isempty(i->hardware_vendor)) - printf(" Hardware Vendor: %s\n", i->hardware_vendor); + if (!isempty(i->architecture)) { + r = table_add_many(table, + TABLE_STRING, "Architecture:", + TABLE_STRING, i->architecture); + if (r < 0) + return table_log_add_error(r); + } - if (!isempty(i->hardware_model)) - printf(" Hardware Model: %s\n", i->hardware_model); + if (!isempty(i->hardware_vendor)) { + r = table_add_many(table, + TABLE_STRING, "Hardware Vendor:", + TABLE_STRING, i->hardware_vendor); + if (r < 0) + return table_log_add_error(r); + } + + if (!isempty(i->hardware_model)) { + r = table_add_many(table, + TABLE_STRING, "Hardware Model:", + TABLE_STRING, i->hardware_model); + if (r < 0) + return table_log_add_error(r); + } + + r = table_print(table, NULL); + if (r < 0) + return table_log_print_error(r); + + return 0; } static int get_one_name(sd_bus *bus, const char* attr, char **ret) { @@ -157,7 +259,7 @@ static int get_one_name(sd_bus *bus, const char* attr, char **ret) { return 0; } -static int show_all_names(sd_bus *bus, sd_bus_error *error) { +static int show_all_names(sd_bus *bus) { StatusInfo info = {}; static const struct bus_properties_map hostname_map[] = { @@ -185,6 +287,7 @@ static int show_all_names(sd_bus *bus, sd_bus_error *error) { }; _cleanup_(sd_bus_message_unrefp) sd_bus_message *host_message = NULL, *manager_message = NULL; + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; int r; r = bus_map_all_properties(bus, @@ -192,29 +295,28 @@ static int show_all_names(sd_bus *bus, sd_bus_error *error) { "/org/freedesktop/hostname1", hostname_map, 0, - error, + &error, &host_message, &info); if (r < 0) - return r; + return log_error_errno(r, "Failed to query system properties: %s", bus_error_message(&error, r)); r = bus_map_all_properties(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", manager_map, 0, - error, + &error, &manager_message, &info); + if (r < 0) + return log_error_errno(r, "Failed to query system properties: %s", bus_error_message(&error, r)); - print_status_info(&info); - - return r; + return print_status_info(&info); } static int show_status(int argc, char **argv, void *userdata) { sd_bus *bus = userdata; - int r; if (arg_pretty || arg_static || arg_transient) { const char *attr; @@ -227,15 +329,9 @@ static int show_status(int argc, char **argv, void *userdata) { arg_static ? "StaticHostname" : "Hostname"; return get_one_name(bus, attr, NULL); - } else { - _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; - - r = show_all_names(bus, &error); - if (r < 0) - return log_error_errno(r, "Failed to query system properties: %s", bus_error_message(&error, r)); - - return 0; } + + return show_all_names(bus); } static int set_simple_string_internal(sd_bus *bus, sd_bus_error *error, const char *target, const char *method, const char *value) {