Skip to content

Commit

Permalink
Merge pull request #3392 from poettering/assorted-stuff
Browse files Browse the repository at this point in the history
Assorted stuff
  • Loading branch information
keszybz committed Jun 4, 2016
2 parents 3fb1ac5 + ac83514 commit 20f8477
Show file tree
Hide file tree
Showing 19 changed files with 92 additions and 153 deletions.
4 changes: 0 additions & 4 deletions TODO
Expand Up @@ -33,8 +33,6 @@ Janitorial Clean-ups:

Features:

* make sure bash completion uses journalctl --fields to get fields list

* use phyical_memory() to allow MemoryLimit= configuration based on available system memory

* ProtectKernelLogs= (drops CAP_SYSLOG, add seccomp for syslog() syscall, and DeviceAllow to /dev/kmsg) in service files
Expand All @@ -49,8 +47,6 @@ Features:

* RestrictNamespaces= or so in services (taking away the ability to create namespaces, with setns, unshare, clone)

* IAID field must move from [Link] to [DHCP] section in .network files

* make sure the ratelimit object can deal with USEC_INFINITY as way to turn off things

* journalctl: make sure -f ends when the container indicated by -M terminates
Expand Down
16 changes: 3 additions & 13 deletions shell-completion/bash/journalctl
Expand Up @@ -30,17 +30,6 @@ __get_machines() {
{ while read a b; do echo " $a"; done; } | sort -u;
}

__journal_fields=(MESSAGE{,_ID} PRIORITY CODE_{FILE,LINE,FUNC}
ERRNO SYSLOG_{FACILITY,IDENTIFIER,PID} COREDUMP_EXE
_{P,U,G}ID _COMM _EXE _CMDLINE
_CAP_EFFECTIVE _AUDIT_{SESSION,LOGINUID}
_SYSTEMD_{CGROUP,SESSION,{,USER_}UNIT,OWNER_UID,SLICE}
_SELINUX_CONTEXT _SOURCE_REALTIME_TIMESTAMP
_{BOOT,MACHINE}_ID _HOSTNAME _TRANSPORT
_KERNEL_{DEVICE,SUBSYSTEM}
_UDEV_{SYSNAME,DEVNODE,DEVLINK}
__CURSOR __{REALTIME,MONOTONIC}_TIMESTAMP)

__syslog_priorities=(emerg alert crit err warning notice info debug)

_journalctl() {
Expand Down Expand Up @@ -79,7 +68,7 @@ _journalctl() {
comps='short short-iso short-precise short-monotonic verbose export json json-pretty json-sse cat'
;;
--field|-F)
comps=${__journal_fields[*]}
comps=$(journalctl --fields | sort 2>/dev/null)
;;
--machine|-M)
comps=$( __get_machines )
Expand Down Expand Up @@ -125,8 +114,9 @@ _journalctl() {
mapfile -t field_vals < <(journalctl -F "${COMP_WORDS[COMP_CWORD-2]}" 2>/dev/null)
COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "$cur") )
else
mapfile -t field_vals < <(journalctl --fields 2>/dev/null)
compopt -o nospace
COMPREPLY=( $(compgen -W '${__journal_fields[*]}' -S= -- "$cur") )
COMPREPLY=( $(compgen -W '${field_vals[*]}' -S= -- "$cur") )
fi
}

Expand Down
8 changes: 4 additions & 4 deletions src/analyze/analyze.c
Expand Up @@ -758,9 +758,9 @@ static int list_dependencies_print(const char *name, unsigned int level, unsigne

if (times) {
if (times->time)
printf("%s%s @%s +%s%s", ANSI_HIGHLIGHT_RED, name,
printf("%s%s @%s +%s%s", ansi_highlight_red(), name,
format_timespan(ts, sizeof(ts), times->activating - boot->userspace_time, USEC_PER_MSEC),
format_timespan(ts2, sizeof(ts2), times->time, USEC_PER_MSEC), ANSI_NORMAL);
format_timespan(ts2, sizeof(ts2), times->time, USEC_PER_MSEC), ansi_normal());
else if (times->activated > boot->userspace_time)
printf("%s @%s", name, format_timespan(ts, sizeof(ts), times->activated - boot->userspace_time, USEC_PER_MSEC));
else
Expand Down Expand Up @@ -926,8 +926,8 @@ static int list_dependencies(sd_bus *bus, const char *name) {

if (times) {
if (times->time)
printf("%s%s +%s%s\n", ANSI_HIGHLIGHT_RED, id,
format_timespan(ts, sizeof(ts), times->time, USEC_PER_MSEC), ANSI_NORMAL);
printf("%s%s +%s%s\n", ansi_highlight_red(), id,
format_timespan(ts, sizeof(ts), times->time, USEC_PER_MSEC), ansi_normal());
else if (times->activated > boot->userspace_time)
printf("%s @%s\n", id, format_timespan(ts, sizeof(ts), times->activated - boot->userspace_time, USEC_PER_MSEC));
else
Expand Down
4 changes: 4 additions & 0 deletions src/basic/string-util.h
Expand Up @@ -66,6 +66,10 @@ static inline bool isempty(const char *p) {
return !p || !p[0];
}

static inline const char *empty_to_null(const char *p) {
return isempty(p) ? NULL : p;
}

static inline char *startswith(const char *s, const char *prefix) {
size_t l;

Expand Down
25 changes: 18 additions & 7 deletions src/basic/terminal-util.c
Expand Up @@ -155,14 +155,14 @@ int ask_char(char *ret, const char *replies, const char *text, ...) {
char c;
bool need_nl = true;

if (on_tty())
if (colors_enabled())
fputs(ANSI_HIGHLIGHT, stdout);

va_start(ap, text);
vprintf(text, ap);
va_end(ap);

if (on_tty())
if (colors_enabled())
fputs(ANSI_NORMAL, stdout);

fflush(stdout);
Expand Down Expand Up @@ -199,14 +199,14 @@ int ask_string(char **ret, const char *text, ...) {
char line[LINE_MAX];
va_list ap;

if (on_tty())
if (colors_enabled())
fputs(ANSI_HIGHLIGHT, stdout);

va_start(ap, text);
vprintf(text, ap);
va_end(ap);

if (on_tty())
if (colors_enabled())
fputs(ANSI_NORMAL, stdout);

fflush(stdout);
Expand Down Expand Up @@ -1193,6 +1193,19 @@ int open_terminal_in_namespace(pid_t pid, const char *name, int mode) {
return receive_one_fd(pair[0], 0);
}

bool terminal_is_dumb(void) {
const char *e;

if (!on_tty())
return true;

e = getenv("TERM");
if (!e)
return true;

return streq(e, "dumb");
}

bool colors_enabled(void) {
static int enabled = -1;

Expand All @@ -1202,10 +1215,8 @@ bool colors_enabled(void) {
colors = getenv("SYSTEMD_COLORS");
if (colors)
enabled = parse_boolean(colors) != 0;
else if (streq_ptr(getenv("TERM"), "dumb"))
enabled = false;
else
enabled = on_tty();
enabled = !terminal_is_dumb();
}

return enabled;
Expand Down
1 change: 1 addition & 0 deletions src/basic/terminal-util.h
Expand Up @@ -80,6 +80,7 @@ unsigned lines(void);
void columns_lines_cache_reset(int _unused_ signum);

bool on_tty(void);
bool terminal_is_dumb(void);
bool colors_enabled(void);

static inline const char *ansi_underline(void) {
Expand Down
2 changes: 1 addition & 1 deletion src/cgtop/cgtop.c
Expand Up @@ -558,7 +558,7 @@ static void display(Hashmap *a) {

assert(a);

if (on_tty())
if (!terminal_is_dumb())
fputs(ANSI_HOME_CLEAR, stdout);

array = alloca(sizeof(Group*) * hashmap_size(a));
Expand Down
14 changes: 6 additions & 8 deletions src/hostname/hostnamed.c
Expand Up @@ -479,8 +479,7 @@ static int method_set_static_hostname(sd_bus_message *m, void *userdata, sd_bus_
if (r < 0)
return r;

if (isempty(name))
name = NULL;
name = empty_to_null(name);

if (streq_ptr(name, c->data[PROP_STATIC_HOSTNAME]))
return sd_bus_reply_method_return(m, NULL);
Expand All @@ -499,9 +498,9 @@ static int method_set_static_hostname(sd_bus_message *m, void *userdata, sd_bus_
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */

if (isempty(name)) {
if (isempty(name))
c->data[PROP_STATIC_HOSTNAME] = mfree(c->data[PROP_STATIC_HOSTNAME]);
} else {
else {
char *h;

if (!hostname_is_valid(name, false))
Expand Down Expand Up @@ -546,8 +545,7 @@ static int set_machine_info(Context *c, sd_bus_message *m, int prop, sd_bus_mess
if (r < 0)
return r;

if (isempty(name))
name = NULL;
name = empty_to_null(name);

if (streq_ptr(name, c->data[prop]))
return sd_bus_reply_method_return(m, NULL);
Expand All @@ -570,9 +568,9 @@ static int set_machine_info(Context *c, sd_bus_message *m, int prop, sd_bus_mess
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */

if (isempty(name)) {
if (isempty(name))
c->data[prop] = mfree(c->data[prop]);
} else {
else {
char *h;

/* The icon name might ultimately be used as file
Expand Down
9 changes: 7 additions & 2 deletions src/journal/journal-verify.c
Expand Up @@ -54,7 +54,9 @@ static void draw_progress(uint64_t p, usec_t *last_usec) {
j = (n * (unsigned) p) / 65535ULL;
k = n - j;

fputs("\r\x1B[?25l" ANSI_HIGHLIGHT_GREEN, stdout);
fputs("\r", stdout);
if (colors_enabled())
fputs("\x1B[?25l" ANSI_HIGHLIGHT_GREEN, stdout);

for (i = 0; i < j; i++)
fputs("\xe2\x96\x88", stdout);
Expand All @@ -66,7 +68,10 @@ static void draw_progress(uint64_t p, usec_t *last_usec) {

printf(" %3"PRIu64"%%", 100U * p / 65535U);

fputs("\r\x1B[?25h", stdout);
fputs("\r", stdout);
if (colors_enabled())
fputs("\x1B[?25h", stdout);

fflush(stdout);
}

Expand Down
13 changes: 9 additions & 4 deletions src/journal/journalctl.c
Expand Up @@ -1664,15 +1664,19 @@ static int setup_keys(void) {
if (on_tty()) {
fprintf(stderr,
"\n"
"The new key pair has been generated. The " ANSI_HIGHLIGHT "secret sealing key" ANSI_NORMAL " has been written to\n"
"The new key pair has been generated. The %ssecret sealing key%s has been written to\n"
"the following local file. This key file is automatically updated when the\n"
"sealing key is advanced. It should not be used on multiple hosts.\n"
"\n"
"\t%s\n"
"\n"
"Please write down the following " ANSI_HIGHLIGHT "secret verification key" ANSI_NORMAL ". It should be stored\n"
"Please write down the following %ssecret verification key%s. It should be stored\n"
"at a safe location and should not be saved locally on disk.\n"
"\n\t" ANSI_HIGHLIGHT_RED, p);
"\n\t%s",
ansi_highlight(), ansi_normal(),
ansi_highlight(), ansi_normal(),
ansi_highlight_red(),
p);
fflush(stderr);
}
for (i = 0; i < seed_size; i++) {
Expand All @@ -1687,8 +1691,9 @@ static int setup_keys(void) {
char tsb[FORMAT_TIMESPAN_MAX], *hn;

fprintf(stderr,
ANSI_NORMAL "\n"
"%s\n"
"The sealing key is automatically changed every %s.\n",
ansi_normal(),
format_timespan(tsb, sizeof(tsb), arg_interval, 0));

hn = gethostname_malloc();
Expand Down
3 changes: 2 additions & 1 deletion src/libsystemd-network/sd-dhcp-server.c
Expand Up @@ -633,7 +633,8 @@ static int parse_request(uint8_t code, uint8_t len, const void *option, void *us

break;
case SD_DHCP_OPTION_MAXIMUM_MESSAGE_SIZE:
if (len == 2)

if (len == 2 && unaligned_read_be16(option) >= sizeof(DHCPPacket))
req->max_optlen = unaligned_read_be16(option) - sizeof(DHCPPacket);

break;
Expand Down
29 changes: 7 additions & 22 deletions src/locale/localed.c
Expand Up @@ -97,10 +97,6 @@ typedef struct Context {
Hashmap *polkit_registry;
} Context;

static const char* nonempty(const char *s) {
return isempty(s) ? NULL : s;
}

static bool startswith_comma(const char *s, const char *prefix) {
const char *t;

Expand Down Expand Up @@ -171,8 +167,7 @@ static int locale_read_data(Context *c) {
for (p = 0; p < _LOCALE_MAX; p++) {
assert(names[p]);

r = free_and_strdup(&c->locale[p],
nonempty(getenv(names[p])));
r = free_and_strdup(&c->locale[p], empty_to_null(getenv(names[p])));
if (r < 0)
return r;
}
Expand Down Expand Up @@ -1041,11 +1036,8 @@ static int method_set_vc_keyboard(sd_bus_message *m, void *userdata, sd_bus_erro
if (r < 0)
return r;

if (isempty(keymap))
keymap = NULL;

if (isempty(keymap_toggle))
keymap_toggle = NULL;
keymap = empty_to_null(keymap);
keymap_toggle = empty_to_null(keymap_toggle);

if (!streq_ptr(keymap, c->vc_keymap) ||
!streq_ptr(keymap_toggle, c->vc_keymap_toggle)) {
Expand Down Expand Up @@ -1214,17 +1206,10 @@ static int method_set_x11_keyboard(sd_bus_message *m, void *userdata, sd_bus_err
if (r < 0)
return r;

if (isempty(layout))
layout = NULL;

if (isempty(model))
model = NULL;

if (isempty(variant))
variant = NULL;

if (isempty(options))
options = NULL;
layout = empty_to_null(layout);
model = empty_to_null(model);
variant = empty_to_null(variant);
options = empty_to_null(options);

if (!streq_ptr(layout, c->x11_layout) ||
!streq_ptr(model, c->x11_model) ||
Expand Down
3 changes: 1 addition & 2 deletions src/machine/machine-dbus.c
Expand Up @@ -655,8 +655,7 @@ int bus_machine_method_open_shell(sd_bus_message *message, void *userdata, sd_bu
r = sd_bus_message_read(message, "ss", &user, &path);
if (r < 0)
return r;
if (isempty(user))
user = NULL;
user = empty_to_null(user);
if (isempty(path))
path = "/bin/sh";
if (!path_is_absolute(path))
Expand Down
12 changes: 4 additions & 8 deletions src/resolve/resolve-tool.c
Expand Up @@ -658,10 +658,8 @@ static int resolve_service(sd_bus *bus, const char *name, const char *type, cons
assert(bus);
assert(domain);

if (isempty(name))
name = NULL;
if (isempty(type))
type = NULL;
name = empty_to_null(name);
type = empty_to_null(type);

if (arg_ifindex > 0 && !if_indextoname(arg_ifindex, ifname))
return log_error_errno(errno, "Failed to resolve interface name for index %i: %m", arg_ifindex);
Expand Down Expand Up @@ -820,10 +818,8 @@ static int resolve_service(sd_bus *bus, const char *name, const char *type, cons
if (r < 0)
return bus_log_parse_error(r);

if (isempty(canonical_name))
canonical_name = NULL;
if (isempty(canonical_type))
canonical_type = NULL;
canonical_name = empty_to_null(canonical_name);
canonical_type = empty_to_null(canonical_type);

if (!streq_ptr(name, canonical_name) ||
!streq_ptr(type, canonical_type) ||
Expand Down
6 changes: 4 additions & 2 deletions src/shared/ask-password-api.c
Expand Up @@ -253,10 +253,12 @@ int ask_password_tty(
goto finish;
}

loop_write(ttyfd, ANSI_HIGHLIGHT, strlen(ANSI_HIGHLIGHT), false);
if (colors_enabled())
loop_write(ttyfd, ANSI_HIGHLIGHT, strlen(ANSI_HIGHLIGHT), false);
loop_write(ttyfd, message, strlen(message), false);
loop_write(ttyfd, " ", 1, false);
loop_write(ttyfd, ANSI_NORMAL, strlen(ANSI_NORMAL), false);
if (colors_enabled())
loop_write(ttyfd, ANSI_NORMAL, strlen(ANSI_NORMAL), false);

new_termios = old_termios;
new_termios.c_lflag &= ~(ICANON|ECHO);
Expand Down

0 comments on commit 20f8477

Please sign in to comment.