Commit a2c29fe2 authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner

Dump the state file

To let nuci and others know about the current activity.
parent 3ef3641c
......@@ -40,6 +40,7 @@ local journal = require "journal"
local locks = require "locks"
local DBG = DBG
local WARN = WARN
local state_dump = state_dump
module "transaction"
......@@ -140,6 +141,7 @@ local function pkg_move(status, plan, errors_collected)
-- Go through the list once more and perform the prepared operations
for _, op in ipairs(plan) do
if op.op == "install" then
state_dump("install")
utils.table_merge(all_configs, op.old_configs)
-- Unfortunately, we need to merge the control files first, otherwise the maintainer scripts won't run. They expect to live in the info dir when they are run. And we need to run the preinst script before merging the files.
backend.pkg_merge_control(op.dir .. "/control", op.control.Package, op.control.files)
......@@ -180,6 +182,7 @@ local function pkg_scripts(status, plan, removes, to_install, errors_collected,
end
end
-- Clean up the files from removed or upgraded packages
state_dump("remove")
backend.pkg_cleanup_files(removes, all_configs)
for _, op in ipairs(plan) do
if op.op == "remove" and not to_install[op.name] then
......
......@@ -23,6 +23,7 @@ local INFO = INFO
local md5 = md5
local sha256 = sha256
local reexec = reexec
local state_dump = state_dump
local utils = require "utils"
local sandbox = require "sandbox"
local uri = require "uri"
......@@ -42,12 +43,14 @@ function prepare(entrypoint)
local ep_uri = uri(tlc, entrypoint)
local ok, tls = ep_uri:get()
if not ok then error(tls) end
state_dump("get list")
--[[
Run the top level script with full privileges.
The script shall be part of updater anyway.
]]
local err = sandbox.run_sandboxed(tls, "[Top level download]", 'Full')
if err then error(err) end
state_dump("examine")
-- Go through all the requirements and decide what we need
postprocess.run()
local required = planner.required_pkgs(postprocess.available_packages, requests.content_requests)
......
......@@ -122,6 +122,12 @@ static int lua_log(lua_State *L) {
return 0;
}
static int lua_state_dump(lua_State *L) {
const char *state = luaL_checkstring(L, 1);
state_dump(state);
return 0;
}
/*
* Put a value from the stack (at index) into our own table in the registry.
* Return the index under which it is stored in there. The returned value allocated
......@@ -634,6 +640,7 @@ struct injected_func {
static const struct injected_func injected_funcs[] = {
{ lua_log, "log" },
{ lua_state_dump, "state_dump" },
{ lua_run_command, "run_command" },
{ lua_download, "download" },
{ lua_events_wait, "events_wait" },
......
......@@ -24,6 +24,8 @@ DBG(msg, msg, ...)::
DIE(msg, msg, ...)::
In addition to outputting the message on the ``DIE`` level, it also
terminates the application with error exit code.
state_dump(msg)::
Dump the given message into `/tmp/update-state/state`.
Asynchronous events
-------------------
......
......@@ -22,6 +22,7 @@
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
struct level_info {
......@@ -44,6 +45,16 @@ static enum log_level syslog_level = LL_DISABLE;
static enum log_level stderr_level = LL_WARN;
static bool syslog_opened = false;
void state_dump(const char *msg) {
FILE *f = fopen("/tmp/update-state/state", "w");
if (f) {
fprintf(f, "%s\n", msg);
fclose(f);
} else {
WARN("Could not dump state: %s", strerror(errno));
}
}
void log_internal(enum log_level level, const char *file, size_t line, const char *func, const char *format, ...) {
bool do_syslog = (level <= syslog_level);
bool do_stderr = (level <= stderr_level);
......@@ -64,6 +75,14 @@ void log_internal(enum log_level level, const char *file, size_t line, const cha
}
if (do_stderr)
fprintf(stderr, "%s:%s:%zu (%s):%s\n", levels[level].prefix, file, line, func, msg);
if (level == LL_DIE) {
state_dump("error");
FILE *f = fopen("/tmp/update-state/last_error", "w");
if (f) {
fprintf(f, "%s\n", msg);
fclose(f);
}
}
}
void log_syslog_level(enum log_level level) {
......
......@@ -47,6 +47,8 @@ void log_internal(enum log_level level, const char *file, size_t line, const cha
enum log_level log_level_get(const char *str) __attribute__((nonnull));
void state_dump(const char *msg) __attribute__((nonnull));
void log_syslog_level(enum log_level level);
void log_syslog_name(const char *name);
void log_stderr_level(enum log_level level);
......
......@@ -46,6 +46,7 @@ int main(int argc, char *argv[]) {
// Some setup of the machinery
log_stderr_level(LL_DBG);
log_syslog_level(LL_DBG);
state_dump("startup");
args_backup(argc, (const char **)argv);
struct events *events = events_new();
struct interpreter *interpreter = interpreter_create(events);
......@@ -78,5 +79,11 @@ int main(int argc, char *argv[]) {
interpreter_destroy(interpreter);
events_destroy(events);
arg_backup_clear();
return trans_ok ? 0 : 1;
if (trans_ok) {
state_dump("done");
return 0;
} else {
state_dump("error");
return 1;
}
}
......@@ -2,4 +2,10 @@
get-api-crl
mkdir -p /tmp/update-state
pkgupdate file:///etc/updater/entry.lua --batch
# This manipulation is not safe against concurrent runs of updater.sh. But the damage in such case is small (just a wrong status visible in nuci for a while) and it should happen only rarely, so we ignore it.
STATE=$(cat /tmp/update-state/state)
if [ "$STATE" != "done" ] && [ "$STATE" != "error" ] ; then
echo lost >/tmp/update-state/state
fi
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment