]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/hostname/hostnamectl.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / hostname / hostnamectl.c
index cebc7742bf6f8a7a9a6ba27f1c506070d586b125..7f9bb49e0c900a2267794902c20903916acdcfd0 100644 (file)
@@ -1,9 +1,4 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2012 Lennart Poettering
-***/
 
 #include <getopt.h>
 #include <locale.h>
@@ -19,6 +14,8 @@
 #include "bus-error.h"
 #include "bus-util.h"
 #include "hostname-util.h"
+#include "main-func.h"
+#include "pretty-print.h"
 #include "spawn-polkit-agent.h"
 #include "util.h"
 #include "verbs.h"
@@ -44,6 +41,7 @@ typedef struct StatusInfo {
         const char *os_cpe_name;
         const char *virtualization;
         const char *architecture;
+        const char *home_url;
 } StatusInfo;
 
 static void print_status_info(StatusInfo *i) {
@@ -87,8 +85,17 @@ static void print_status_info(StatusInfo *i) {
         if (!isempty(i->virtualization))
                 printf("    Virtualization: %s\n", i->virtualization);
 
-        if (!isempty(i->os_pretty_name))
-                printf("  Operating System: %s\n", i->os_pretty_name);
+        if (!isempty(i->os_pretty_name)) {
+                _cleanup_free_ char *formatted = NULL;
+                const char *t = i->os_pretty_name;
+
+                if (i->home_url) {
+                        if (terminal_urlify(i->home_url, i->os_pretty_name, &formatted) >= 0)
+                                t = formatted;
+                }
+
+                printf("  Operating System: %s\n", t);
+        }
 
         if (!isempty(i->os_cpe_name))
                 printf("       CPE OS Name: %s\n", i->os_cpe_name);
@@ -141,6 +148,7 @@ static int show_all_names(sd_bus *bus, sd_bus_error *error) {
                 { "KernelRelease",             "s", NULL, offsetof(StatusInfo, kernel_release)  },
                 { "OperatingSystemPrettyName", "s", NULL, offsetof(StatusInfo, os_pretty_name)  },
                 { "OperatingSystemCPEName",    "s", NULL, offsetof(StatusInfo, os_cpe_name)     },
+                { "HomeURL",                   "s", NULL, offsetof(StatusInfo, home_url)        },
                 {}
         };
 
@@ -220,8 +228,9 @@ static int set_simple_string(sd_bus *bus, const char *method, const char *value)
                         &error, NULL,
                         "sb", value, arg_ask_password);
         if (r < 0)
-                log_error("Could not set property: %s", bus_error_message(&error, -r));
-        return r;
+                return log_error_errno(r, "Could not set property: %s", bus_error_message(&error, -r));
+
+        return 0;
 }
 
 static int set_hostname(int argc, char **argv, void *userdata) {
@@ -293,6 +302,13 @@ static int set_location(int argc, char **argv, void *userdata) {
 }
 
 static int help(void) {
+        _cleanup_free_ char *link = NULL;
+        int r;
+
+        r = terminal_urlify_man("hostnamectl", "1", &link);
+        if (r < 0)
+                return log_oom();
+
         printf("%s [OPTIONS...] COMMAND ...\n\n"
                "Query or change system hostname.\n\n"
                "  -h --help              Show this help\n"
@@ -310,7 +326,10 @@ static int help(void) {
                "  set-chassis NAME       Set chassis type for host\n"
                "  set-deployment NAME    Set deployment environment for host\n"
                "  set-location NAME      Set location for host\n"
-               , program_invocation_short_name);
+               "\nSee the %s for details.\n"
+               , program_invocation_short_name
+               , link
+        );
 
         return 0;
 }
@@ -408,7 +427,7 @@ static int hostnamectl_main(sd_bus *bus, int argc, char *argv[]) {
         return dispatch_verb(argc, argv, verbs, bus);
 }
 
-int main(int argc, char *argv[]) {
+static int run(int argc, char *argv[]) {
         _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
         int r;
 
@@ -418,16 +437,13 @@ int main(int argc, char *argv[]) {
 
         r = parse_argv(argc, argv);
         if (r <= 0)
-                goto finish;
+                return r;
 
         r = bus_connect_transport(arg_transport, arg_host, false, &bus);
-        if (r < 0) {
-                log_error_errno(r, "Failed to create bus connection: %m");
-                goto finish;
-        }
-
-        r = hostnamectl_main(bus, argc, argv);
+        if (r < 0)
+                return log_error_errno(r, "Failed to create bus connection: %m");
 
-finish:
-        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+        return hostnamectl_main(bus, argc, argv);
 }
+
+DEFINE_MAIN_FUNCTION(run);