static int journal_previous_skip (lua_State *L) { sd_journal *j = check_journal(L, 1); uint64_t skip = luaL_checkuint64(L, 2); int err = sd_journal_previous_skip(j, skip); if (err < 0) return handle_error(L, -err); lua_pushinteger(L, err); return 1; }
static void test_skip(void (*setup)(void)) { char t[] = "/tmp/journal-skip-XXXXXX"; sd_journal *j; int r; assert_se(mkdtemp(t)); assert_se(chdir(t) >= 0); setup(); /* Seek to head, iterate down. */ assert_ret(sd_journal_open_directory(&j, t, 0)); assert_ret(sd_journal_seek_head(j)); assert_ret(sd_journal_next(j)); test_check_numbers_down(j, 4); sd_journal_close(j); /* Seek to tail, iterate up. */ assert_ret(sd_journal_open_directory(&j, t, 0)); assert_ret(sd_journal_seek_tail(j)); assert_ret(sd_journal_previous(j)); test_check_numbers_up(j, 4); sd_journal_close(j); /* Seek to tail, skip to head, iterate down. */ assert_ret(sd_journal_open_directory(&j, t, 0)); assert_ret(sd_journal_seek_tail(j)); assert_ret(r = sd_journal_previous_skip(j, 4)); assert_se(r == 4); test_check_numbers_down(j, 4); sd_journal_close(j); /* Seek to head, skip to tail, iterate up. */ assert_ret(sd_journal_open_directory(&j, t, 0)); assert_ret(sd_journal_seek_head(j)); assert_ret(r = sd_journal_next_skip(j, 4)); assert_se(r == 4); test_check_numbers_up(j, 4); sd_journal_close(j); log_info("Done..."); if (arg_keep) log_info("Not removing %s", t); else { journal_directory_vacuum(".", 3000000, 0, 0, NULL); assert_se(rm_rf_dangerous(t, false, true, false) >= 0); } puts("------------------------------------------------------------"); }
void systemd_init(pmdaInterface *dp) { int sts; int journal_fd; dp->version.six.desc = systemd_desc; dp->version.six.fetch = systemd_fetch; dp->version.six.text = systemd_text; dp->version.six.attribute = systemd_contextAttributeCallBack; pmdaSetFetchCallBack(dp, systemd_fetchCallBack); pmdaSetEndContextCallBack(dp, systemd_end_contextCallBack); pmdaInit(dp, NULL, 0, metrictab, sizeof(metrictab)/sizeof(metrictab[0])); /* Initialize the systemd side. This is failure-tolerant. */ /* XXX: SD_JOURNAL_{LOCAL|RUNTIME|SYSTEM}_ONLY */ sts = sd_journal_open(& journald_context, 0); if (sts < 0) { __pmNotifyErr(LOG_ERR, "sd_journal_open failure: %s", strerror(-sts)); dp->status = sts; return; } sts = sd_journal_open(& journald_context_seeky, 0); if (sts < 0) { __pmNotifyErr(LOG_ERR, "sd_journal_open #2 failure: %s", strerror(-sts)); dp->status = sts; return; } sts = sd_journal_seek_tail(journald_context); if (sts < 0) { __pmNotifyErr(LOG_ERR, "sd_journal_seek_tail failure: %s", strerror(-sts)); } /* Work around RHBZ979487. */ sts = sd_journal_previous_skip(journald_context, 1); if (sts < 0) { __pmNotifyErr(LOG_ERR, "sd_journal_previous_skip failure: %s", strerror(-sts)); } /* Arrange to wake up for journal events. */ journal_fd = sd_journal_get_fd(journald_context); if (journal_fd < 0) { __pmNotifyErr(LOG_ERR, "sd_journal_get_fd failure: %s", strerror(-journal_fd)); /* NB: not a fatal error; the select() loop will still time out and periodically poll. This makes it ok for sd_journal_reliable_fd() to be 0. */ } else { FD_SET(journal_fd, &fds); if (journal_fd > maxfd) maxfd = journal_fd; } /* NB: One queue is used for both .records and .records_raw; they just use different decoder callbacks. */ queue_entries = pmdaEventNewQueue("systemd", maxmem); if (queue_entries < 0) __pmNotifyErr(LOG_ERR, "pmdaEventNewQueue failure: %s", pmErrStr(queue_entries)); }
int main(int argc, char *argv[]) { int r; sd_journal *j = NULL; unsigned line = 0; bool need_seek = false; sd_id128_t previous_boot_id; bool previous_boot_id_valid = false; bool have_pager; log_parse_environment(); log_open(); r = parse_argv(argc, argv); if (r <= 0) goto finish; if (arg_new_id128) { r = generate_new_id128(); goto finish; } #ifdef HAVE_ACL if (!arg_quiet && geteuid() != 0 && in_group("adm") <= 0) log_warning("Showing user generated messages only. Users in the group 'adm' can see all messages. Pass -q to turn this message off."); #endif if (arg_directory) r = sd_journal_open_directory(&j, arg_directory, 0); else r = sd_journal_open(&j, arg_local ? SD_JOURNAL_LOCAL_ONLY : 0); if (r < 0) { log_error("Failed to open journal: %s", strerror(-r)); goto finish; } if (arg_print_header) { journal_print_header(j); r = 0; goto finish; } r = add_this_boot(j); if (r < 0) goto finish; r = add_matches(j, argv + optind); if (r < 0) goto finish; if (!arg_quiet) { usec_t start, end; char start_buf[FORMAT_TIMESTAMP_MAX], end_buf[FORMAT_TIMESTAMP_MAX]; r = sd_journal_get_cutoff_realtime_usec(j, &start, &end); if (r < 0) { log_error("Failed to get cutoff: %s", strerror(-r)); goto finish; } if (r > 0) { if (arg_follow) printf("Logs begin at %s.\n", format_timestamp(start_buf, sizeof(start_buf), start)); else printf("Logs begin at %s, end at %s.\n", format_timestamp(start_buf, sizeof(start_buf), start), format_timestamp(end_buf, sizeof(end_buf), end)); } } if (arg_lines >= 0) { r = sd_journal_seek_tail(j); if (r < 0) { log_error("Failed to seek to tail: %s", strerror(-r)); goto finish; } r = sd_journal_previous_skip(j, arg_lines); } else { r = sd_journal_seek_head(j); if (r < 0) { log_error("Failed to seek to head: %s", strerror(-r)); goto finish; } r = sd_journal_next(j); } if (r < 0) { log_error("Failed to iterate through journal: %s", strerror(-r)); goto finish; } have_pager = !arg_no_pager && !arg_follow; if (have_pager) { columns(); pager_open(); } if (arg_output == OUTPUT_JSON) { fputc('[', stdout); fflush(stdout); } for (;;) { for (;;) { sd_id128_t boot_id; int flags = (arg_show_all*OUTPUT_SHOW_ALL | have_pager*OUTPUT_FULL_WIDTH); if (need_seek) { r = sd_journal_next(j); if (r < 0) { log_error("Failed to iterate through journal: %s", strerror(-r)); goto finish; } } if (r == 0) break; r = sd_journal_get_monotonic_usec(j, NULL, &boot_id); if (r >= 0) { if (previous_boot_id_valid && !sd_id128_equal(boot_id, previous_boot_id)) printf(ANSI_HIGHLIGHT_ON "----- Reboot -----" ANSI_HIGHLIGHT_OFF "\n"); previous_boot_id = boot_id; previous_boot_id_valid = true; } line ++; r = output_journal(j, arg_output, line, 0, flags); if (r < 0) goto finish; need_seek = true; } if (!arg_follow) break; r = sd_journal_wait(j, (uint64_t) -1); if (r < 0) { log_error("Couldn't wait for log event: %s", strerror(-r)); goto finish; } } if (arg_output == OUTPUT_JSON) fputs("\n]\n", stdout); finish: if (j) sd_journal_close(j); pager_close(); return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; }