summaryrefslogtreecommitdiff
path: root/src/journal/journalctl.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-03-11 18:03:13 -0400
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-03-22 15:31:45 -0400
commit6fe391c56d3f4231576ccc9d62d2000f37640a92 (patch)
tree8e2785d70206a8de88975756f3df59a92ba6dc10 /src/journal/journalctl.c
parent478c82693c386e7a6e8e4b37cc99fb19b12e7186 (diff)
journalctl: be smarter about journal error checks
There are many ways in which we can get those checks wrong, so it is better to warn and then error out on a real access failure. The error messages are wrapped to <80 lines, because their primary use is to be displayed in the terminal, and it is easier to read them this way. Reading them in the journal can be a bit trickier, but this is a bug in logs-show.c.
Diffstat (limited to 'src/journal/journalctl.c')
-rw-r--r--src/journal/journalctl.c136
1 files changed, 92 insertions, 44 deletions
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 8543adfb8a..91dbde3ba1 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -883,63 +883,111 @@ static int verify(sd_journal *j) {
return r;
}
-static int access_check(void) {
-
#ifdef HAVE_ACL
- /* If /var/log/journal doesn't even exist, unprivileged users have no access at all */
- if (access("/var/log/journal", F_OK) < 0 && geteuid() != 0 && in_group("systemd-journal") <= 0) {
- log_error("Unprivileged users can't see messages unless persistent log storage is enabled. Users in the group 'systemd-journal' can always see messages.");
- return -EACCES;
+static int access_check_var_log_journal(sd_journal *j) {
+ _cleanup_strv_free_ char **g = NULL;
+ bool have_access;
+ int r;
+
+ assert(j);
+
+ have_access = in_group("systemd-journal") > 0;
+
+ if (!have_access) {
+ /* Let's enumerate all groups from the default ACL of
+ * the directory, which generally should allow access
+ * to most journal files too */
+ r = search_acl_groups(&g, "/var/log/journal/", &have_access);
+ if (r < 0)
+ return r;
}
- /* If /var/log/journal exists, try to pring a nice notice if the user lacks access to it */
- if (!arg_quiet && geteuid() != 0) {
- _cleanup_strv_free_ char **g = NULL;
- bool have_access;
- int r;
+ if (!have_access) {
- have_access = in_group("systemd-journal") > 0;
+ if (strv_isempty(g))
+ log_notice("Hint: You are currently not seeing messages from other users and\n"
+ "the system. Users in the group 'systemd-journal' can see all messages.\n"
+ "Pass -q to turn this notice off.");
+ else {
+ _cleanup_free_ char *s = NULL;
- if (!have_access) {
- /* Let's enumerate all groups from the default
- * ACL of the directory, which generally
- * should allow access to most journal
- * files too */
- r = search_acl_groups(&g, "/var/log/journal/", &have_access);
+ r = strv_extend(&g, "systemd-journal");
if (r < 0)
- return r;
+ return log_oom();
+
+ strv_sort(g);
+ strv_uniq(g);
+
+ s = strv_join(g, "', '");
+ if (!s)
+ return log_oom();
+
+ log_notice("Hint: You are currently not seeing messages from other users and the system.\n"
+ "Users in the groups '%s' can see all messages.\n"
+ "Pass -q to turn this notice off.", s);
}
+ }
- if (!have_access) {
+ return 0;
+}
+#endif
- if (strv_isempty(g))
- log_notice("Hint: You are currently not seeing messages from other users and the system. Users in the group 'systemd-journal' can see all messages. Pass -q to turn this notice off.");
- else {
- _cleanup_free_ char *s = NULL;
+static int access_check(sd_journal *j) {
+ uint64_t eacces = EACCES, *code;
+ Iterator it;
+ int r = 0;
- r = strv_extend(&g, "systemd-journal");
- if (r < 0)
- return log_oom();
+ assert(j);
+ assert(j->errors);
+ assert(j->files);
- strv_sort(g);
- strv_uniq(g);
+ if (set_isempty(j->errors)) {
+ if (hashmap_isempty(j->files))
+ log_info("No journal files were found.");
+ return 0;
+ }
- s = strv_join(g, "', '");
- if (!s)
- return log_oom();
+ if (!set_contains(j->errors, &eacces)) {
+#ifdef HAVE_ACL
+ /* If /var/log/journal doesn't even exist,
+ unprivileged users have no access at all */
+ if (access("/var/log/journal", F_OK) < 0 &&
+ geteuid() != 0 &&
+ in_group("systemd-journal") <= 0) {
+ log_error("Unprivileged users can't see messages unless persistent log storage\n"
+ "is enabled. Users in the group 'systemd-journal' can always see messages.");
+ return -EACCES;
+ }
- log_notice("Hint: You are currently not seeing messages from other users and the system. Users in the groups '%s' can see all messages. Pass -q to turn this notice off.", s);
- }
+ /* If /var/log/journal exists, try to pring a nice
+ notice if the user lacks access to it */
+ if (!arg_quiet && geteuid() != 0) {
+ r = access_check_var_log_journal(j);
+ if (r < 0)
+ return r;
}
- }
#else
- if (geteuid() != 0 && in_group("systemd-journal") <= 0) {
- log_error("No access to messages. Only users in the group 'systemd-journal' can see messages.");
- return -EACCES;
- }
+ if (geteuid() != 0 && in_group("systemd-journal") <= 0)
+ log_error("No access to messages.\n"
+ "Users in the group 'systemd-journal' can see messages.");
#endif
+ if (hashmap_isempty(j->files)) {
+ log_error("No journal files were opened, due to insufficient permissions.");
+ r = -EACCES;
+ }
+ }
- return 0;
+ SET_FOREACH(code, j->errors, it) {
+ int err = -PTR_TO_INT(code);
+ assert(err > 0);
+ if (err != EACCES)
+ log_warning("Error was encountered while opening journal files: %s",
+ strerror(err));
+ }
+
+ log_notice("Hint: run journalctl in debug mode: SYSTEMD_LOG_LEVEL=debug journalct ...");
+
+ return r;
}
int main(int argc, char *argv[]) {
@@ -987,10 +1035,6 @@ int main(int argc, char *argv[]) {
goto finish;
}
- r = access_check();
- if (r < 0)
- return EXIT_FAILURE;
-
if (arg_directory)
r = sd_journal_open_directory(&j, arg_directory, 0);
else
@@ -1000,6 +1044,10 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
+ r = access_check(j);
+ if (r < 0)
+ return EXIT_FAILURE;
+
if (arg_action == ACTION_VERIFY) {
r = verify(j);
goto finish;