hostnamectl.c revision 3f6fd1ba65f962702753c4ad284b588e59689a23
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <getopt.h>
#include <locale.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "sd-bus.h"
#include "sd-id128.h"
#include "architecture.h"
#include "bus-error.h"
#include "bus-util.h"
#include "hostname-util.h"
#include "spawn-polkit-agent.h"
#include "util.h"
static bool arg_ask_password = true;
static bool arg_transient = false;
static bool arg_pretty = false;
static bool arg_static = false;
static void polkit_agent_open_if_enabled(void) {
/* Open the polkit agent as a child process if necessary */
if (!arg_ask_password)
return;
if (arg_transport != BUS_TRANSPORT_LOCAL)
return;
}
typedef struct StatusInfo {
char *hostname;
char *static_hostname;
char *pretty_hostname;
char *icon_name;
char *chassis;
char *deployment;
char *location;
char *kernel_name;
char *kernel_release;
char *os_pretty_name;
char *os_cpe_name;
char *virtualization;
char *architecture;
} StatusInfo;
static void print_status_info(StatusInfo *i) {
int r;
assert(i);
if (!isempty(i->pretty_hostname) &&
printf(" Icon name: %s\n",
printf(" Chassis: %s\n",
if (!isempty(i->deployment))
r = sd_id128_get_machine(&mid);
if (r >= 0)
r = sd_id128_get_boot(&bid);
if (r >= 0)
if (!isempty(i->virtualization))
if (!isempty(i->os_pretty_name))
if (!isempty(i->os_cpe_name))
if (!isempty(i->architecture))
}
const char *s;
int r;
r = sd_bus_get_property(
bus,
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
attr,
if (r < 0) {
return r;
}
if (r < 0)
return bus_log_parse_error(r);
printf("%s\n", s);
return 0;
}
StatusInfo info = {};
static const struct bus_properties_map hostname_map[] = {
{}
};
static const struct bus_properties_map manager_map[] = {
{}
};
int r;
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
&info);
if (r < 0)
goto fail;
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
&info);
fail:
return r;
}
const char *attr;
log_error("Cannot query more than one name type at a time");
return -EINVAL;
}
} else
return show_all_names(bus);
}
int r = 0;
r = sd_bus_call_method(
bus,
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
if (r < 0)
return r;
}
_cleanup_free_ char *h = NULL;
int r;
assert(n == 2);
if (arg_pretty) {
const char *p;
/* 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
* hostname. */
p = "";
/* maybe get rid of trailing dot */
} else {
if (!p)
return log_oom();
}
if (r < 0)
return r;
}
if (arg_static) {
if (r < 0)
return r;
}
if (arg_transient) {
if (r < 0)
return r;
}
return 0;
}
assert(n == 2);
}
assert(n == 2);
}
assert(n == 2);
}
assert(n == 2);
}
static void help(void) {
printf("%s [OPTIONS...] COMMAND ...\n\n"
"Query or change system hostname.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --no-ask-password Do not prompt for password\n"
" -H --host=[USER@]HOST Operate on remote host\n"
" -M --machine=CONTAINER Operate on local container\n"
" --transient Only set transient hostname\n"
" --static Only set static hostname\n"
" --pretty Only set pretty hostname\n\n"
"Commands:\n"
" status Show current hostname settings\n"
" set-hostname NAME Set system hostname\n"
" set-icon-name NAME Set icon name for host\n"
" 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"
}
enum {
ARG_VERSION = 0x100,
};
{}
};
int c;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return version();
case 'H':
break;
case 'M':
break;
case ARG_TRANSIENT:
arg_transient = true;
break;
case ARG_PRETTY:
arg_pretty = true;
break;
case ARG_STATIC:
arg_static = true;
break;
case ARG_NO_ASK_PASSWORD:
arg_ask_password = false;
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
return 1;
}
static const struct {
const char* verb;
const enum {
MORE,
LESS,
} argc_cmp;
const int argc;
} verbs[] = {
};
int left;
unsigned i;
if (left <= 0)
/* Special rule: no arguments means "status" */
i = 0;
else {
help();
return 0;
}
for (i = 0; i < ELEMENTSOF(verbs); i++)
break;
if (i >= ELEMENTSOF(verbs)) {
return -EINVAL;
}
}
case EQUAL:
log_error("Invalid number of arguments.");
return -EINVAL;
}
break;
case MORE:
log_error("Too few arguments.");
return -EINVAL;
}
break;
case LESS:
log_error("Too many arguments.");
return -EINVAL;
}
break;
default:
assert_not_reached("Unknown comparison operator.");
}
}
int r;
log_open();
if (r <= 0)
goto finish;
if (r < 0) {
log_error_errno(r, "Failed to create bus connection: %m");
goto finish;
}
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}