summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/log.c19
-rw-r--r--src/shared/path-lookup.c9
-rw-r--r--src/shared/socket-util.c39
-rw-r--r--src/shared/socket-util.h2
-rw-r--r--src/shared/strv.c25
-rw-r--r--src/shared/strv.h1
-rw-r--r--src/shared/util.c57
-rw-r--r--src/shared/util.h7
8 files changed, 127 insertions, 32 deletions
diff --git a/src/shared/log.c b/src/shared/log.c
index ff2dd45350..293c261f9e 100644
--- a/src/shared/log.c
+++ b/src/shared/log.c
@@ -541,11 +541,11 @@ static int log_dispatch(
k = write_to_journal(level, file, line, func,
object_name, object, buffer);
- if (k <= 0) {
- if (k < 0 && k != -EAGAIN)
+ if (k < 0) {
+ if (k != -EAGAIN)
log_close_journal();
log_open_kmsg();
- } else
+ } else if (k > 0)
r++;
}
@@ -554,11 +554,11 @@ static int log_dispatch(
k = write_to_syslog(level, file, line, func,
object_name, object, buffer);
- if (k <= 0) {
- if (k < 0 && k != -EAGAIN)
+ if (k < 0) {
+ if (k != -EAGAIN)
log_close_syslog();
log_open_kmsg();
- } else
+ } else if (k > 0)
r++;
}
@@ -571,11 +571,10 @@ static int log_dispatch(
k = write_to_kmsg(level, file, line, func,
object_name, object, buffer);
- if (k <= 0) {
- if (k < 0 && k != -EAGAIN)
- log_close_kmsg();
+ if (k < 0) {
+ log_close_kmsg();
log_open_console();
- } else
+ } else if (k > 0)
r++;
}
diff --git a/src/shared/path-lookup.c b/src/shared/path-lookup.c
index fa4995ceea..ffdc5367cb 100644
--- a/src/shared/path-lookup.c
+++ b/src/shared/path-lookup.c
@@ -41,21 +41,26 @@ DEFINE_STRING_TABLE_LOOKUP(systemd_running_as, SystemdRunningAs);
int user_config_home(char **config_home) {
const char *e;
+ char *r;
e = getenv("XDG_CONFIG_HOME");
if (e) {
- if (asprintf(config_home, "%s/systemd/user", e) < 0)
+ r = strappend(e, "/systemd/user");
+ if (!r)
return -ENOMEM;
+ *config_home = r;
return 1;
} else {
const char *home;
home = getenv("HOME");
if (home) {
- if (asprintf(config_home, "%s/.config/systemd/user", home) < 0)
+ r = strappend(home, "/.config/systemd/user");
+ if (!r)
return -ENOMEM;
+ *config_home = r;
return 1;
}
}
diff --git a/src/shared/socket-util.c b/src/shared/socket-util.c
index 6c94d69486..f6ddea3183 100644
--- a/src/shared/socket-util.c
+++ b/src/shared/socket-util.c
@@ -565,6 +565,45 @@ bool socket_address_matches_fd(const SocketAddress *a, int fd) {
return false;
}
+int make_socket_fd(const char* address, int flags) {
+ SocketAddress a;
+ int fd, r;
+ char _cleanup_free_ *p = NULL;
+
+ r = socket_address_parse(&a, address);
+ if (r < 0) {
+ log_error("failed to parse socket: %s", strerror(-r));
+ return r;
+ }
+
+ fd = socket(socket_address_family(&a), flags, 0);
+ if (fd < 0) {
+ log_error("socket(): %m");
+ return -errno;
+ }
+
+ r = socket_address_print(&a, &p);
+ if (r < 0) {
+ log_error("socket_address_print(): %s", strerror(-r));
+ return r;
+ }
+ log_info("Listening on %s", p);
+
+ r = bind(fd, &a.sockaddr.sa, a.size);
+ if (r < 0) {
+ log_error("bind to %s: %m", address);
+ return -errno;
+ }
+
+ r = listen(fd, SOMAXCONN);
+ if (r < 0) {
+ log_error("listen on %s: %m", address);
+ return -errno;
+ }
+
+ return fd;
+}
+
static const char* const netlink_family_table[] = {
[NETLINK_ROUTE] = "route",
[NETLINK_FIREWALL] = "firewall",
diff --git a/src/shared/socket-util.h b/src/shared/socket-util.h
index 771765d323..33838345ed 100644
--- a/src/shared/socket-util.h
+++ b/src/shared/socket-util.h
@@ -88,6 +88,8 @@ bool socket_address_is_netlink(const SocketAddress *a, const char *s);
bool socket_address_matches_fd(const SocketAddress *a, int fd);
+int make_socket_fd(const char* address, int flags);
+
bool socket_address_equal(const SocketAddress *a, const SocketAddress *b);
bool socket_address_needs_mount(const SocketAddress *a, const char *prefix);
diff --git a/src/shared/strv.c b/src/shared/strv.c
index ec25755289..60c4762572 100644
--- a/src/shared/strv.c
+++ b/src/shared/strv.c
@@ -305,6 +305,31 @@ char **strv_split_quoted(const char *s) {
return r;
}
+char **strv_split_newlines(const char *s) {
+ char **l;
+ unsigned n;
+
+ assert(s);
+
+ /* Special version of strv_split() that splits on newlines and
+ * suppresses an empty string at the end */
+
+ l = strv_split(s, NEWLINE);
+ if (!l)
+ return NULL;
+
+ n = strv_length(l);
+ if (n <= 0)
+ return l;
+
+ if (isempty(l[n-1])) {
+ free(l[n-1]);
+ l[n-1] = NULL;
+ }
+
+ return l;
+}
+
char *strv_join(char **l, const char *separator) {
char *r, *e;
char **s;
diff --git a/src/shared/strv.h b/src/shared/strv.h
index b3802a7a3f..623f10216d 100644
--- a/src/shared/strv.h
+++ b/src/shared/strv.h
@@ -58,6 +58,7 @@ static inline bool strv_isempty(char **l) {
char **strv_split(const char *s, const char *separator) _malloc_;
char **strv_split_quoted(const char *s) _malloc_;
+char **strv_split_newlines(const char *s) _malloc_;
char *strv_join(char **l, const char *separator) _malloc_;
diff --git a/src/shared/util.c b/src/shared/util.c
index f5adedc531..e643cd367c 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -2859,12 +2859,13 @@ cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
}
}
-int status_vprintf(const char *status, bool ellipse, const char *format, va_list ap) {
+int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) {
static const char status_indent[] = " "; /* "[" STATUS "] " */
_cleanup_free_ char *s = NULL;
_cleanup_close_ int fd = -1;
- struct iovec iovec[5];
+ struct iovec iovec[6];
int n = 0;
+ static bool prev_ephemeral;
assert(format);
@@ -2902,6 +2903,10 @@ int status_vprintf(const char *status, bool ellipse, const char *format, va_list
zero(iovec);
+ if (prev_ephemeral)
+ IOVEC_SET_STRING(iovec[n++], "\r" ANSI_ERASE_TO_END_OF_LINE);
+ prev_ephemeral = ephemeral;
+
if (status) {
if (!isempty(status)) {
IOVEC_SET_STRING(iovec[n++], "[");
@@ -2912,7 +2917,8 @@ int status_vprintf(const char *status, bool ellipse, const char *format, va_list
}
IOVEC_SET_STRING(iovec[n++], s);
- IOVEC_SET_STRING(iovec[n++], "\n");
+ if (!ephemeral)
+ IOVEC_SET_STRING(iovec[n++], "\n");
if (writev(fd, iovec, n) < 0)
return -errno;
@@ -2920,14 +2926,14 @@ int status_vprintf(const char *status, bool ellipse, const char *format, va_list
return 0;
}
-int status_printf(const char *status, bool ellipse, const char *format, ...) {
+int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) {
va_list ap;
int r;
assert(format);
va_start(ap, format);
- r = status_vprintf(status, ellipse, format, ap);
+ r = status_vprintf(status, ellipse, ephemeral, format, ap);
va_end(ap);
return r;
@@ -3503,6 +3509,29 @@ int vtnr_from_tty(const char *tty) {
return i;
}
+char *resolve_dev_console(char **active) {
+ char *tty;
+
+ /* Resolve where /dev/console is pointing to, if /sys is actually ours
+ * (i.e. not read-only-mounted which is a sign for container setups) */
+
+ if (path_is_read_only_fs("/sys") > 0)
+ return NULL;
+
+ if (read_one_line_file("/sys/class/tty/console/active", active) < 0)
+ return NULL;
+
+ /* If multiple log outputs are configured the last one is what
+ * /dev/console points to */
+ tty = strrchr(*active, ' ');
+ if (tty)
+ tty++;
+ else
+ tty = *active;
+
+ return tty;
+}
+
bool tty_is_vc_resolve(const char *tty) {
char *active = NULL;
bool b;
@@ -3512,19 +3541,11 @@ bool tty_is_vc_resolve(const char *tty) {
if (startswith(tty, "/dev/"))
tty += 5;
- /* Resolve where /dev/console is pointing to, if /sys is
- * actually ours (i.e. not read-only-mounted which is a sign
- * for container setups) */
- if (streq(tty, "console") && path_is_read_only_fs("/sys") <= 0)
- if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
- /* If multiple log outputs are configured the
- * last one is what /dev/console points to */
- tty = strrchr(active, ' ');
- if (tty)
- tty++;
- else
- tty = active;
- }
+ if (streq(tty, "console")) {
+ tty = resolve_dev_console(&active);
+ if (!tty)
+ return false;
+ }
b = tty_is_vc(tty);
free(active);
diff --git a/src/shared/util.h b/src/shared/util.h
index 19cc36af84..b5ad1ff3b5 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -55,10 +55,12 @@ union dirent_storage {
#define FORMAT_BYTES_MAX 8
#define ANSI_HIGHLIGHT_ON "\x1B[1;39m"
+#define ANSI_RED_ON "\x1B[31m"
#define ANSI_HIGHLIGHT_RED_ON "\x1B[1;31m"
#define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m"
#define ANSI_HIGHLIGHT_YELLOW_ON "\x1B[1;33m"
#define ANSI_HIGHLIGHT_OFF "\x1B[0m"
+#define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
size_t page_size(void);
#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
@@ -354,8 +356,8 @@ int pipe_eof(int fd);
cpu_set_t* cpu_set_malloc(unsigned *ncpus);
-int status_vprintf(const char *status, bool ellipse, const char *format, va_list ap);
-int status_printf(const char *status, bool ellipse, const char *format, ...);
+int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap);
+int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...);
int status_welcome(void);
int fd_columns(int fd);
@@ -388,6 +390,7 @@ DIR *xopendirat(int dirfd, const char *name, int flags);
char *fstab_node_to_udev_node(const char *p);
+char *resolve_dev_console(char **active);
bool tty_is_vc(const char *tty);
bool tty_is_vc_resolve(const char *tty);
bool tty_is_console(const char *tty);