Skip to content
1 change: 1 addition & 0 deletions man/hostnamectl.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@

<xi:include href="standard-options.xml" xpointer="help" />
<xi:include href="standard-options.xml" xpointer="version" />
<xi:include href="standard-options.xml" xpointer="json" />
</variablelist>
</refsect1>

Expand Down
28 changes: 16 additions & 12 deletions man/org.freedesktop.hostname1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ node /org/freedesktop/hostname1 {
in b interactive);
GetProductUUID(in b interactive,
out ay uuid);
Describe(out s UNNAMED);
properties:
readonly s Hostname = '...';
readonly s StaticHostname = '...';
Expand Down Expand Up @@ -118,6 +119,8 @@ node /org/freedesktop/hostname1 {

<variablelist class="dbus-method" generated="True" extra-ref="GetProductUUID()"/>

<variablelist class="dbus-method" generated="True" extra-ref="Describe()"/>

<variablelist class="dbus-property" generated="True" extra-ref="Hostname"/>

<variablelist class="dbus-property" generated="True" extra-ref="StaticHostname"/>
Expand Down Expand Up @@ -232,6 +235,17 @@ node /org/freedesktop/hostname1 {
<citerefentry><refentrytitle>machine-info</refentrytitle><manvolnum>3</manvolnum></citerefentry>
for that. For more information on these files and syscalls see the respective man pages.</para>

<para><varname>KernelName</varname>, <varname>KernelRelease</varname>, and
<varname>KernelVersion</varname> expose the kernel name (e.g. <literal>Linux</literal>), release
(e.g. <literal>5.0.0-11</literal>), and version (i.e. the build number, e.g. <literal>#11</literal>) as
reported by <citerefentry project="man-pages"><refentrytitle>uname</refentrytitle><manvolnum>2</manvolnum></citerefentry>.
<varname>OperatingSystemPrettyName</varname>, <varname>OperatingSystemCPEName</varname>, and
<varname>HomeURL</varname> expose the <varname>PRETTY_NAME=</varname>, <varname>CPE_NAME=</varname> and
<varname>HOME_URL=</varname> fields from
<citerefentry><refentrytitle>os-release</refentrytitle><manvolnum>5</manvolnum></citerefentry>. The
purpose of those properties is to allow remote clients to access this information over D-Bus. Local
clients can access the information directly.</para>

<refsect2>
<title>Methods</title>

Expand Down Expand Up @@ -261,22 +275,12 @@ node /org/freedesktop/hostname1 {
<citerefentry><refentrytitle>machine-info</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
the semantics of those settings.</para>

<para><function>GetProductUUID()</function> returns the "product uuid" as exposed by the kernel based
<para><function>GetProductUUID()</function> returns the "product UUID" as exposed by the kernel based
on DMI information in <filename>/sys/class/dmi/id/product_uuid</filename>. Reading the file directly
requires root privileges, and this method allows access to unprivileged clients through the polkit
framework.</para>

<para><varname>KernelName</varname>, <varname>KernelRelease</varname>, and
<varname>KernelVersion</varname> expose the kernel name (e.g. <literal>Linux</literal>), release
(e.g. <literal>5.0.0-11</literal>), and version (i.e. the build number, e.g. <literal>#11</literal>) as
reported by
<citerefentry project="man-pages"><refentrytitle>uname</refentrytitle><manvolnum>2</manvolnum></citerefentry>.
<varname>OperatingSystemPrettyName</varname>, <varname>OperatingSystemCPEName</varname>, and
<varname>HomeURL</varname> expose the <varname>PRETTY_NAME=</varname>, <varname>CPE_NAME=</varname> and
<varname>HOME_URL=</varname> fields from
<citerefentry><refentrytitle>os-release</refentrytitle><manvolnum>5</manvolnum></citerefentry>. The
purpose of those properties is to allow remote clients to access this information over D-Bus. Local
clients can access the information directly.</para>
<para><function>Describe()</function> returns a JSON representation of all properties in one.</para>
</refsect2>

<refsect2>
Expand Down
49 changes: 47 additions & 2 deletions src/hostname/hostnamectl.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#include "format-table.h"
#include "hostname-setup.h"
#include "hostname-util.h"
#include "json.h"
#include "main-func.h"
#include "parse-argument.h"
#include "pretty-print.h"
#include "spawn-polkit-agent.h"
#include "terminal-util.h"
Expand All @@ -30,6 +32,7 @@ static char *arg_host = NULL;
static bool arg_transient = false;
static bool arg_pretty = false;
static bool arg_static = false;
static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;

typedef struct StatusInfo {
const char *hostname;
Expand Down Expand Up @@ -317,6 +320,37 @@ static int show_all_names(sd_bus *bus) {

static int show_status(int argc, char **argv, void *userdata) {
sd_bus *bus = userdata;
int r;

if (arg_json_format_flags != JSON_FORMAT_OFF) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
const char *text = NULL;

r = sd_bus_call_method(
bus,
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
"Describe",
&error,
&reply,
NULL);
if (r < 0)
return log_error_errno(r, "Could not get description: %s", bus_error_message(&error, r));

r = sd_bus_message_read(reply, "s", &text);
if (r < 0)
return bus_log_parse_error(r);

r = json_parse(text, 0, &v, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse JSON: %m");

json_variant_dump(v, arg_json_format_flags, NULL, NULL);
return 0;
}

if (arg_pretty || arg_static || arg_transient) {
const char *attr;
Expand Down Expand Up @@ -489,6 +523,8 @@ static int help(void) {
" --transient Only set transient hostname\n"
" --static Only set static hostname\n"
" --pretty Only set pretty hostname\n"
" --json=pretty|short|off\n"
" Generate JSON output\n"
"\nSee the %s for details.\n",
program_invocation_short_name,
ansi_highlight(),
Expand All @@ -509,7 +545,8 @@ static int parse_argv(int argc, char *argv[]) {
ARG_NO_ASK_PASSWORD,
ARG_TRANSIENT,
ARG_STATIC,
ARG_PRETTY
ARG_PRETTY,
ARG_JSON,
};

static const struct option options[] = {
Expand All @@ -521,10 +558,11 @@ static int parse_argv(int argc, char *argv[]) {
{ "host", required_argument, NULL, 'H' },
{ "machine", required_argument, NULL, 'M' },
{ "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
{ "json", required_argument, NULL, ARG_JSON },
{}
};

int c;
int c, r;

assert(argc >= 0);
assert(argv);
Expand Down Expand Up @@ -565,6 +603,13 @@ static int parse_argv(int argc, char *argv[]) {
arg_ask_password = false;
break;

case ARG_JSON:
r = parse_json_argument(optarg, &arg_json_format_flags);
if (r <= 0)
return r;

break;

case '?':
return -EINVAL;

Expand Down
Loading