hostnamectl.c revision d9d93745cd2efcdfca8f82d798e61e0ee70cef5c
/*-*- 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 "sd-bus.h"
#include "bus-util.h"
#include "bus-error.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"
#include "architecture.h"
#include "fileio.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;
} StatusInfo;
static void print_status_info(StatusInfo *i) {
int r;
struct utsname u;
assert(i);
printf(" Static hostname: %s\n",
strna(i->static_hostname));
if (!isempty(i->pretty_hostname) &&
printf(" Pretty hostname: %s\n",
strna(i->pretty_hostname));
printf("Transient hostname: %s\n",
printf(" Icon name: %s\n"
" Chassis: %s\n",
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 (r < 0)
if (!isempty(pretty_name))
printf(" Kernel: %s %s\n"
" Architecture: %s\n",
}
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 map[] = {
{}
};
int r;
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
map,
&info);
if (r < 0)
goto fail;
fail:
return 0;
}
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. */
if (!h)
return log_oom();
hostname_cleanup(h, true);
p = "";
else {
p = hostname;
hostname = h;
}
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);
}
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"
" --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",
return 0;
}
enum {
ARG_VERSION = 0x100,
};
{}
};
int c;
switch (c) {
case 'h':
return help();
case ARG_VERSION:
return 0;
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) {
goto finish;
}
return r < 0 ? EXIT_FAILURE : r;
}