int main(int argc, char *argv[]) { unsigned n = 0; _cleanup_journal_close_ sd_journal*j = NULL; log_set_max_level(LOG_DEBUG); assert_se(sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY) >= 0); assert_se(sd_journal_add_match(j, "_TRANSPORT=syslog", 0) >= 0); assert_se(sd_journal_add_match(j, "_UID=0", 0) >= 0); SD_JOURNAL_FOREACH_BACKWARDS(j) { const void *d; size_t l; assert_se(sd_journal_get_data(j, "MESSAGE", &d, &l) >= 0); printf("%.*s\n", (int) l, (char*) d); n ++; if (n >= 10) break; } return 0; }
static int add_matches(sd_journal *j, char **args) { char **i; int r; assert(j); STRV_FOREACH(i, args) { if (streq(*i, "+")) r = sd_journal_add_disjunction(j); else if (path_is_absolute(*i)) { char *p; const char *path; struct stat st; p = canonicalize_file_name(*i); path = p ? p : *i; if (stat(path, &st) < 0) { free(p); log_error("Couldn't stat file: %m"); return -errno; } if (S_ISREG(st.st_mode) && (0111 & st.st_mode)) { char *t; t = strappend("_EXE=", path); if (!t) { free(p); log_error("Out of memory"); return -ENOMEM; } r = sd_journal_add_match(j, t, 0); free(t); } else { free(p); log_error("File is not a regular file or is not executable: %s", *i); return -EINVAL; } free(p); } else r = sd_journal_add_match(j, *i, 0); if (r < 0) { log_error("Failed to add match '%s': %s", *i, strerror(-r)); return r; } } return 0; }
static int add_match(sd_journal *j, const char *match) { _cleanup_free_ char *p = NULL; const char* prefix, *pattern; pid_t pid; int r; if (strchr(match, '=')) prefix = ""; else if (strchr(match, '/')) { r = path_make_absolute_cwd(match, &p); if (r < 0) return log_error_errno(r, "path_make_absolute_cwd(\"%s\"): %m", match); match = p; prefix = "COREDUMP_EXE="; } else if (parse_pid(match, &pid) >= 0) prefix = "COREDUMP_PID="; else prefix = "COREDUMP_COMM="; pattern = strjoina(prefix, match); log_debug("Adding match: %s", pattern); r = sd_journal_add_match(j, pattern, 0); if (r < 0) return log_error_errno(r, "Failed to add match \"%s\": %m", match); return 0; }
static int add_matches(sd_journal *j, char **matches) { char **match; int r; r = sd_journal_add_match(j, "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR, 0); if (r < 0) return log_error_errno(r, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR); r = sd_journal_add_match(j, "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR, 0); if (r < 0) return log_error_errno(r, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR); STRV_FOREACH(match, matches) { r = add_match(j, *match); if (r < 0) return r; }
static int add_matches(sd_journal *j, char **args) { char **i; assert(j); STRV_FOREACH(i, args) { int r; if (streq(*i, "+")) r = sd_journal_add_disjunction(j); else if (path_is_absolute(*i)) { _cleanup_free_ char *p, *t = NULL; const char *path; struct stat st; p = canonicalize_file_name(*i); path = p ? p : *i; if (stat(path, &st) < 0) { log_error("Couldn't stat file: %m"); return -errno; } if (S_ISREG(st.st_mode) && (0111 & st.st_mode)) t = strappend("_EXE=", path); else if (S_ISCHR(st.st_mode)) asprintf(&t, "_KERNEL_DEVICE=c%u:%u", major(st.st_rdev), minor(st.st_rdev)); else if (S_ISBLK(st.st_mode)) asprintf(&t, "_KERNEL_DEVICE=b%u:%u", major(st.st_rdev), minor(st.st_rdev)); else { log_error("File is neither a device node, nor regular file, nor executable: %s", *i); return -EINVAL; } if (!t) return log_oom(); r = sd_journal_add_match(j, t, 0); } else r = sd_journal_add_match(j, *i, 0); if (r < 0) { log_error("Failed to add match '%s': %s", *i, strerror(-r)); return r; } }
static int journal_add_match (lua_State *L) { sd_journal *j = check_journal(L, 1); size_t size; const char *data = luaL_checklstring(L, 2, &size); int err = sd_journal_add_match(j, data, size); if (err != 0) return handle_error(L, -err); lua_pushboolean(L, 1); return 1; }
static int add_this_boot(sd_journal *j) { char match[9+32+1] = "_BOOT_ID="; sd_id128_t boot_id; int r; if (!arg_this_boot) return 0; r = sd_id128_get_boot(&boot_id); if (r < 0) { log_error("Failed to get boot id: %s", strerror(-r)); return r; } sd_id128_to_string(boot_id, match + 9); r = sd_journal_add_match(j, match, strlen(match)); if (r < 0) { log_error("Failed to add match: %s", strerror(-r)); return r; } return 0; }
int main(int argc, char *argv[]) { JournalFile *one, *two, *three; char t[] = "/tmp/journal-stream-XXXXXX"; unsigned i; _cleanup_journal_close_ sd_journal *j = NULL; char *z; const void *data; size_t l; log_set_max_level(LOG_DEBUG); assert_se(mkdtemp(t)); assert_se(chdir(t) >= 0); assert_se(journal_file_open("one.journal", O_RDWR|O_CREAT, 0666, true, NULL, NULL, NULL, &one) == 0); assert_se(journal_file_open("two.journal", O_RDWR|O_CREAT, 0666, true, NULL, NULL, NULL, &two) == 0); assert_se(journal_file_open("three.journal", O_RDWR|O_CREAT, 0666, true, NULL, NULL, NULL, &three) == 0); for (i = 0; i < N_ENTRIES; i++) { char *p, *q; dual_timestamp ts; struct iovec iovec[2]; dual_timestamp_get(&ts); assert_se(asprintf(&p, "NUMBER=%u", i) >= 0); iovec[0].iov_base = p; iovec[0].iov_len = strlen(p); assert_se(asprintf(&q, "MAGIC=%s", i % 5 == 0 ? "quux" : "waldo") >= 0); iovec[1].iov_base = q; iovec[1].iov_len = strlen(q); if (i % 10 == 0) assert_se(journal_file_append_entry(three, &ts, iovec, 2, NULL, NULL, NULL) == 0); else { if (i % 3 == 0) assert_se(journal_file_append_entry(two, &ts, iovec, 2, NULL, NULL, NULL) == 0); assert_se(journal_file_append_entry(one, &ts, iovec, 2, NULL, NULL, NULL) == 0); } free(p); free(q); } journal_file_close(one); journal_file_close(two); journal_file_close(three); assert_se(sd_journal_open_directory(&j, t, 0) >= 0); assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0); SD_JOURNAL_FOREACH_BACKWARDS(j) { _cleanup_free_ char *c = NULL; assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0); printf("\t%.*s\n", (int) l, (const char*) data); assert_se(sd_journal_get_cursor(j, &c) >= 0); assert_se(sd_journal_test_cursor(j, c) > 0); } SD_JOURNAL_FOREACH(j) { _cleanup_free_ char *c = NULL; assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0); printf("\t%.*s\n", (int) l, (const char*) data); assert_se(sd_journal_get_cursor(j, &c) >= 0); assert_se(sd_journal_test_cursor(j, c) > 0); } sd_journal_flush_matches(j); verify_contents(j, 1); printf("NEXT TEST\n"); assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0); assert_se(z = journal_make_match_string(j)); printf("resulting match expression is: %s\n", z); free(z); verify_contents(j, 5); printf("NEXT TEST\n"); sd_journal_flush_matches(j); assert_se(sd_journal_add_match(j, "MAGIC=waldo", 0) >= 0); assert_se(sd_journal_add_match(j, "NUMBER=10", 0) >= 0); assert_se(sd_journal_add_match(j, "NUMBER=11", 0) >= 0); assert_se(sd_journal_add_match(j, "NUMBER=12", 0) >= 0); assert_se(z = journal_make_match_string(j)); printf("resulting match expression is: %s\n", z); free(z); verify_contents(j, 0); assert_se(sd_journal_query_unique(j, "NUMBER") >= 0); SD_JOURNAL_FOREACH_UNIQUE(j, data, l) printf("%.*s\n", (int) l, (const char*) data); assert_se(rm_rf_dangerous(t, false, true, false) >= 0); return 0; }
int main(int argc, char *argv[]) { _cleanup_journal_close_ sd_journal*j; _cleanup_free_ char *t; log_set_max_level(LOG_DEBUG); assert_se(sd_journal_open(&j, 0) >= 0); assert_se(sd_journal_add_match(j, "foobar", 0) < 0); assert_se(sd_journal_add_match(j, "foobar=waldo", 0) < 0); assert_se(sd_journal_add_match(j, "", 0) < 0); assert_se(sd_journal_add_match(j, "=", 0) < 0); assert_se(sd_journal_add_match(j, "=xxxxx", 0) < 0); assert_se(sd_journal_add_match(j, "HALLO=WALDO", 0) >= 0); assert_se(sd_journal_add_match(j, "QUUX=mmmm", 0) >= 0); assert_se(sd_journal_add_match(j, "QUUX=xxxxx", 0) >= 0); assert_se(sd_journal_add_match(j, "HALLO=", 0) >= 0); assert_se(sd_journal_add_match(j, "QUUX=xxxxx", 0) >= 0); assert_se(sd_journal_add_match(j, "QUUX=yyyyy", 0) >= 0); assert_se(sd_journal_add_match(j, "PIFF=paff", 0) >= 0); assert_se(sd_journal_add_disjunction(j) >= 0); assert_se(sd_journal_add_match(j, "ONE=one", 0) >= 0); assert_se(sd_journal_add_match(j, "ONE=two", 0) >= 0); assert_se(sd_journal_add_match(j, "TWO=two", 0) >= 0); assert_se(sd_journal_add_conjunction(j) >= 0); assert_se(sd_journal_add_match(j, "L4_1=yes", 0) >= 0); assert_se(sd_journal_add_match(j, "L4_1=ok", 0) >= 0); assert_se(sd_journal_add_match(j, "L4_2=yes", 0) >= 0); assert_se(sd_journal_add_match(j, "L4_2=ok", 0) >= 0); assert_se(sd_journal_add_disjunction(j) >= 0); assert_se(sd_journal_add_match(j, "L3=yes", 0) >= 0); assert_se(sd_journal_add_match(j, "L3=ok", 0) >= 0); assert_se(t = journal_make_match_string(j)); printf("resulting match expression is: %s\n", t); assert_se(streq(t, "(((L3=ok OR L3=yes) OR ((L4_2=ok OR L4_2=yes) AND (L4_1=ok OR L4_1=yes))) AND ((TWO=two AND (ONE=two OR ONE=one)) OR (PIFF=paff AND (QUUX=yyyyy OR QUUX=xxxxx OR QUUX=mmmm) AND (HALLO= OR HALLO=WALDO))))")); return 0; }
QStringList UnitModel::getLastJrnlEntries(QString unit) const { QString match1, match2; int r, jflags; QStringList reply; const void *data; size_t length; uint64_t time; sd_journal *journal; if (!userBus.isEmpty()) { match1 = QString("USER_UNIT=" + unit); jflags = (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_CURRENT_USER); } else { match1 = QString("_SYSTEMD_UNIT=" + unit); match2 = QString("UNIT=" + unit); jflags = (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM); } r = sd_journal_open(&journal, jflags); if (r != 0) { qDebug() << "Failed to open journal"; return reply; } sd_journal_flush_matches(journal); r = sd_journal_add_match(journal, match1.toUtf8(), 0); if (r != 0) return reply; if (!match2.isEmpty()) { sd_journal_add_disjunction(journal); r = sd_journal_add_match(journal, match2.toUtf8(), 0); if (r != 0) return reply; } r = sd_journal_seek_tail(journal); if (r != 0) return reply; // Fetch the last 5 entries for (int i = 0; i < 5; ++i) { r = sd_journal_previous(journal); if (r == 1) { QString line; // Get the date and time r = sd_journal_get_realtime_usec(journal, &time); if (r == 0) { QDateTime date; date.setMSecsSinceEpoch(time/1000); line.append(date.toString("yyyy.MM.dd hh:mm")); } // Color messages according to priority r = sd_journal_get_data(journal, "PRIORITY", &data, &length); if (r == 0) { int prio = QString::fromUtf8((const char *)data, length).section('=',1).toInt(); if (prio <= 3) line.append("<span style='color:tomato;'>"); else if (prio == 4) line.append("<span style='color:khaki;'>"); else line.append("<span style='color:palegreen;'>"); } // Get the message itself r = sd_journal_get_data(journal, "MESSAGE", &data, &length); if (r == 0) { line.append(": " + QString::fromUtf8((const char *)data, length).section('=',1) + "</span>"); if (line.length() > 195) line = QString(line.left(195) + "..." + "</span>"); reply << line; } } else // previous failed, no more entries return reply; } sd_journal_close(journal); return reply; }
int main(int argc, char *argv[]) { _cleanup_(sd_journal_closep) sd_journal *j = NULL; _cleanup_free_ char *t; test_setup_logging(LOG_DEBUG); assert_se(sd_journal_open(&j, 0) >= 0); assert_se(sd_journal_add_match(j, "foobar", 0) < 0); assert_se(sd_journal_add_match(j, "foobar=waldo", 0) < 0); assert_se(sd_journal_add_match(j, "", 0) < 0); assert_se(sd_journal_add_match(j, "=", 0) < 0); assert_se(sd_journal_add_match(j, "=xxxxx", 0) < 0); assert_se(sd_journal_add_match(j, (uint8_t[4]){'A', '=', '\1', '\2'}, 4) >= 0); assert_se(sd_journal_add_match(j, (uint8_t[5]){'B', '=', 'C', '\0', 'D'}, 5) >= 0); assert_se(sd_journal_add_match(j, "HALLO=WALDO", 0) >= 0); assert_se(sd_journal_add_match(j, "QUUX=mmmm", 0) >= 0); assert_se(sd_journal_add_match(j, "QUUX=xxxxx", 0) >= 0); assert_se(sd_journal_add_match(j, "HALLO=", 0) >= 0); assert_se(sd_journal_add_match(j, "QUUX=xxxxx", 0) >= 0); assert_se(sd_journal_add_match(j, "QUUX=yyyyy", 0) >= 0); assert_se(sd_journal_add_match(j, "PIFF=paff", 0) >= 0); assert_se(sd_journal_add_disjunction(j) >= 0); assert_se(sd_journal_add_match(j, "ONE=one", 0) >= 0); assert_se(sd_journal_add_match(j, "ONE=two", 0) >= 0); assert_se(sd_journal_add_match(j, "TWO=two", 0) >= 0); assert_se(sd_journal_add_conjunction(j) >= 0); assert_se(sd_journal_add_match(j, "L4_1=yes", 0) >= 0); assert_se(sd_journal_add_match(j, "L4_1=ok", 0) >= 0); assert_se(sd_journal_add_match(j, "L4_2=yes", 0) >= 0); assert_se(sd_journal_add_match(j, "L4_2=ok", 0) >= 0); assert_se(sd_journal_add_disjunction(j) >= 0); assert_se(sd_journal_add_match(j, "L3=yes", 0) >= 0); assert_se(sd_journal_add_match(j, "L3=ok", 0) >= 0); assert_se(t = journal_make_match_string(j)); printf("resulting match expression is: %s\n", t); assert_se(streq(t, "(((L3=ok OR L3=yes) OR ((L4_2=ok OR L4_2=yes) AND (L4_1=ok OR L4_1=yes))) AND ((TWO=two AND (ONE=two OR ONE=one)) OR (PIFF=paff AND (QUUX=yyyyy OR QUUX=xxxxx OR QUUX=mmmm) AND (HALLO= OR HALLO=WALDO) AND B=C\\000D AND A=\\001\\002)))")); return 0; }