hostnamectl.c revision bc2708414babc5c99bb8000e63c84e87606cc15d
/*-*- 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 <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <getopt.h>
#include <locale.h>
#include <string.h>
#include "dbus-common.h"
#include "util.h"
#include "spawn-polkit-agent.h"
#include "build.h"
#include "hwclock.h"
#include "strv.h"
#include "sd-id128.h"
#include "virt.h"
static enum transport {
static bool arg_ask_password = true;
static bool arg_set_transient = false;
static bool arg_set_pretty = false;
static bool arg_set_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;
}
typedef struct StatusInfo {
const char *hostname;
const char *static_hostname;
const char *pretty_hostname;
const char *icon_name;
const char *chassis;
} StatusInfo;
static void print_status_info(StatusInfo *i) {
int r;
struct utsname u;
assert(i);
printf(" Static hostname: %s\n",
strna(i->static_hostname));
printf("Transient hostname: %s\n",
printf(" Pretty hostname: %s\n"
" Icon name: %s\n"
" Chassis: %s\n",
strna(i->pretty_hostname),
r = sd_id128_get_machine(&mid);
if (r >= 0)
r = sd_id128_get_boot(&bid);
if (r >= 0)
if (detect_virtualization(&id) > 0)
"PRETTY_NAME", &pretty_name,
"CPE_NAME", &cpe_name,
NULL);
if (!isempty(pretty_name))
printf(" Kernel: %s %s\n"
}
switch (dbus_message_iter_get_arg_type(iter)) {
case DBUS_TYPE_STRING: {
const char *s;
if (!isempty(s)) {
i->hostname = s;
i->static_hostname = s;
i->pretty_hostname = s;
i->icon_name = s;
i->chassis = s;
}
break;
}
}
return 0;
}
const char *interface = "";
int r;
bus,
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
"org.freedesktop.DBus.Properties",
"GetAll",
&reply,
NULL,
if (r < 0)
return r;
log_error("Failed to parse reply.");
return -EIO;
}
const char *name;
log_error("Failed to parse reply.");
return -EIO;
}
log_error("Failed to parse reply.");
return -EIO;
}
log_error("Failed to parse reply.");
return -EIO;
}
if (r < 0) {
log_error("Failed to parse reply.");
return r;
}
}
return 0;
}
static char* hostname_simplify(char *s) {
char *p, *d;
for (p = s, d = s; *p; p++) {
if ((*p >= 'a' && *p <= 'z') ||
(*p >= '0' && *p <= '9') ||
*p == '-' || *p == '_')
*(d++) = *p;
else if (*p >= 'A' && *p <= 'Z')
*(d++) = *p - 'A' + 'a';
else if (*p == ' ')
*(d++) = '-';
}
*d = 0;
strshorten(s, HOST_NAME_MAX);
return s;
}
dbus_bool_t interactive = true;
_cleanup_free_ char *h = NULL;
int r;
assert(n == 2);
if (arg_set_pretty) {
bus,
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
"SetPrettyHostname",
&reply,
NULL,
if (r < 0)
return r;
if (!h)
return log_oom();
hostname = hostname_simplify(h);
}
if (arg_set_static) {
bus,
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
"SetStaticHostname",
&reply,
NULL,
if (r < 0)
return r;
}
if (arg_set_transient) {
bus,
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
"SetHostname",
&reply,
NULL,
if (r < 0)
return r;
}
return 0;
}
dbus_bool_t interactive = true;
assert(n == 2);
return bus_method_call_with_reply(
bus,
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
"SetIconName",
&reply,
NULL,
}
dbus_bool_t interactive = true;
assert(n == 2);
return bus_method_call_with_reply(
bus,
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
"SetChassis",
&reply,
NULL,
}
static int 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"
" --transient Only set transient hostname\n"
" --static Only set static hostname\n"
" --pretty Only set pretty hostname\n"
" --no-ask-password Do not prompt for password\n"
" -H --host=[USER@]HOST Operate on remote host\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",
return 0;
}
enum {
ARG_VERSION = 0x100,
};
};
int c;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return 0;
case 'P':
break;
case 'H':
break;
case ARG_SET_TRANSIENT:
arg_set_transient = true;
break;
case ARG_SET_PRETTY:
arg_set_pretty = true;
break;
case ARG_SET_STATIC:
arg_set_static = true;
break;
case ARG_NO_ASK_PASSWORD:
arg_ask_password = false;
break;
case '?':
return -EINVAL;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
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.");
}
if (!bus) {
return -EIO;
}
}
int r, retval = EXIT_FAILURE;
log_open();
if (r < 0)
goto finish;
else if (r == 0) {
goto finish;
}
if (arg_transport == TRANSPORT_NORMAL)
else if (arg_transport == TRANSPORT_POLKIT)
else if (arg_transport == TRANSPORT_SSH)
else
assert_not_reached("Uh, invalid transport...");
retval = r < 0 ? EXIT_FAILURE : r;
if (bus) {
}
return retval;
}