diff --git a/DESIGN.md b/DESIGN.md
index 9f7191c..c92799a 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -499,7 +499,24 @@ nothing):
- Then the normal run goes on, so a stream of `/status` never stops the
fetches. After a `/pause`, the run ends as paused.
-To go on after `/pause`, root runs `pullgate status NAME --resume`.
+**`/resume NAME` and `/ask-again NAME`** (code6x/pullgate#33) go the same
+way, with request files `resume` and `ask-again`. They are limited, so
+that they cannot undo a safety stop:
+
+- `resume`: root lifts the pause only if the pause file is one that
+ `/pause` wrote: a regular file of root, with one link, of exactly one
+ line `paused from Telegram at YYYY-MM-DD HH:MM UTC`. A pause by pullgate
+ itself (a cut-off apply), by root (`pullgate status --pause`) or with
+ any other text stays, and the reply says to run `pullgate status NAME
+ --resume` as root. `/resume` never ends the broken state (section 10).
+- `ask-again`: root clears only an open question for the last tip (its
+ message may be lost; the next question has a new code) or an `expired`
+ mark, and saves the state before the listener closes the message. It
+ refuses every other case: a tip that is skipped, failed, held, refused,
+ failed its check, the failed commit of the broken state, or an instance
+ that is paused or off. When `resume` and `pause` come in one run, the
+ pause wins. Root's `pullgate status
+ NAME --ask-again` still clears every mark.
**`/check`.** The listener writes a check request (section 8) only when all
of these hold, else it writes nothing:
diff --git a/README.md b/README.md
index 74a577d..f563b31 100644
--- a/README.md
+++ b/README.md
@@ -191,16 +191,25 @@ usual Apply question, or "No new commit (the applied commit is …)", or
- Without `pullgate-check@NAME.path`, the request waits for the next timer
run.
-## Status and pause: `/status`, `/pause`
+## Status and pause: `/status`, `/pause`, `/resume`, `/ask-again`
- `/status` (all instances) or `/status NAME`: the mode, the applied
commit, the last tip, the open question, paused or not and why, broken
or not. It changes nothing. At most one answer per instance a minute.
- `/pause NAME`: pauses the instance. Nothing is fetched, asked or applied
- until root runs `pullgate status NAME --resume` on the host. If it is
+ until you send `/resume NAME` (or root runs `pullgate status NAME --resume`). If it is
paused already, the old pause stays as it is. A reason after NAME is not
sent to the host.
+- `/resume NAME`: lifts a pause that `/pause` set. Whoever holds the bot
+ token can do that too: if you suspect the token, pause as root. Any other pause (one
+ that pullgate set itself after a cut-off apply, or one that root set)
+ stays: run `pullgate status NAME --resume` as root.
+- `/ask-again NAME`: asks again about the newest commit, but only when its
+ question expired or was lost. A commit that was skipped, failed, held or
+ refused is not asked again from Telegram; root can, with
+ `pullgate status NAME --ask-again`.
+
As with `/check`, only your user ID in your chat counts, and the listener
only writes a small request file; root does the work.
diff --git a/THREAT-MODEL.md b/THREAT-MODEL.md
index bdd766b..9b11e1d 100644
--- a/THREAT-MODEL.md
+++ b/THREAT-MODEL.md
@@ -479,7 +479,7 @@ until the owner resets it (DESIGN.md section 8). The run unit has no start
limit, so the timer goes on. This denies; it never approves. A clock jump back of more than
a minute allows one more run.
-### T18b. `/status` and `/pause` from a stolen token or the listener
+### T18b. `/status`, `/pause`, `/resume`, `/ask-again` from a stolen token or the listener
*Attack.* Someone with the bot token, or a compromised listener, sends
`/status` or `/pause`, or writes the request files directly.
@@ -500,7 +500,22 @@ reads its own state and acts. So the most they can do:
`/pause` cannot replace the reason of a safety pause, and a later
limited `/resume` (code6x/pullgate#33) cannot use it to lift one.
-They cannot resume, approve, or change the state: the state directory is
+*`/resume` and `/ask-again`* (code6x/pullgate#33) can undo a stop, so
+they are limited. `/resume` lifts only a pause whose file is exactly what
+`/pause` writes (root's regular file, one link, one line in that form):
+the only pause a thief could set anyway. So if the owner suspects the
+token or the listener, the owner pauses as root (`pullgate status NAME
+--pause`): a token holder can lift a `/pause`. `/resume` moves the file
+aside and checks that it is the file it checked, so a pause that root
+writes at the same moment is put back and stays. A pause that pullgate set after a
+cut-off apply, or that root set, stays, and so does the broken state
+(T6). `/ask-again` clears only an expired mark or an open question; it
+never re-asks a commit that the owner skipped or that failed, was held or
+was refused, so a thief cannot re-ask a skipped commit and hope for a
+wrong tap. A new question still needs the owner's tap, and counts in
+`questions_per_day`.
+
+Apart from that, they cannot approve or change the state: the state directory is
root's (0700), and any other file in the check directory is removed and
does nothing (a test).
diff --git a/internal/gate/apply.go b/internal/gate/apply.go
index 3cdaa9c..789b37c 100644
--- a/internal/gate/apply.go
+++ b/internal/gate/apply.go
@@ -124,7 +124,7 @@ func (e *Env) ignoreAll(st *state.State, paused bool) (Result, error) {
why := "the instance is off (mode = \"off\")."
res := Off
if paused {
- why = "the instance is paused. Run as root: pullgate status " + e.Cfg.Name + " --resume"
+ why = "the instance is paused. " + e.resumeHint()
res = Paused
}
for {
diff --git a/internal/gate/check.go b/internal/gate/check.go
index 1e84e08..167c345 100644
--- a/internal/gate/check.go
+++ b/internal/gate/check.go
@@ -4,6 +4,7 @@ import (
"strings"
"time"
+ "github.com/code6x/pullgate/internal/config"
"github.com/code6x/pullgate/internal/message"
"github.com/code6x/pullgate/internal/spool"
@@ -27,11 +28,20 @@ func (e *Env) takeChecks() spool.Requests {
e.Log.Error("read the check requests", "err", err)
}
if r.Any() {
- e.Log.Info("requests taken", "check", r.Check, "status", r.Status, "pause", r.Pause)
+ e.Log.Info("requests taken", "check", r.Check, "status", r.Status, "pause", r.Pause, "resume", r.Resume, "ask-again", r.AskAgain)
}
return r
}
+// resumeHint says how to go on after a pause: /resume for a /pause, else
+// root.
+func (e *Env) resumeHint() string {
+ if e.Dir.TelegramPaused(e.UID) {
+ return "Send /resume " + e.Cfg.Name + ""
+ }
+ return "Run as root: pullgate status " + e.Cfg.Name + " --resume"
+}
+
// pauseRequest acts on /pause: it writes the pause file, unless there is
// one. A pause that is there already is never changed, so that a later
// /resume (code6x/pullgate#33) cannot lift it. The pause itself is never
@@ -48,7 +58,7 @@ func (e *Env) pauseRequest(st *state.State) {
text = "⏸ " + e.Cfg.Name + " was paused already: " + message.Code(e.Dir.PauseReason())
default:
e.Log.Info("paused from Telegram")
- text = "⏸ " + e.Cfg.Name + " is paused. Nothing is fetched, asked or applied. To go on, run as root: pullgate status " + e.Cfg.Name + " --resume"
+ text = "⏸ " + e.Cfg.Name + " is paused. Nothing is fetched, asked or applied. To go on, send /resume " + e.Cfg.Name + "."
}
if d := now.Sub(st.PauseNote); d >= CheckInterval || d <= -CheckInterval {
st.PauseNote = now
@@ -56,6 +66,74 @@ func (e *Env) pauseRequest(st *state.State) {
}
}
+// resumeRequest acts on /resume: it lifts only a pause that /pause wrote
+// (code6x/pullgate#33). A pause by pullgate itself (a cut-off apply) or by
+// root stays: a compromised listener must not undo a safety stop.
+func (e *Env) resumeRequest(st *state.State) {
+ var text string
+ switch {
+ case !e.Dir.Paused():
+ text = e.Cfg.Name + " is not paused."
+ default:
+ lifted, err := e.Dir.ResumeTelegram(e.UID)
+ if err != nil {
+ e.Log.Error("remove the pause file", "err", err)
+ text = "Error: " + e.Cfg.Name + " could not be resumed. See the journal of the host."
+ break
+ }
+ if !lifted {
+ text = "⏸ " + e.Cfg.Name + " stays paused: this pause was not set from Telegram (" + message.Code(e.Dir.PauseReason()) + "), so /resume does not lift it. Check the host, then run as root: pullgate status " + e.Cfg.Name + " --resume"
+ break
+ }
+ e.Log.Info("resumed from Telegram")
+ text = "▶ " + e.Cfg.Name + " goes on."
+ if st.Broken != nil {
+ text += " It is still broken: only a fix forward is asked."
+ }
+ }
+ e.notice(text)
+}
+
+// askAgainRequest acts on /ask-again: it asks again only about a question
+// that expired or was lost (an open one), never about a commit that was
+// skipped, failed, held, refused or failed its check (code6x/pullgate#33).
+func (e *Env) askAgainRequest(st *state.State) {
+ tip := st.LastTip
+ m := st.Marks[tip]
+ switch {
+ case e.Dir.Paused() || e.Cfg.Mode == config.ModeOff:
+ e.notice("Not asked again: " + e.Cfg.Name + " is paused or off. Resume it first.")
+ case tip == "" || tip == st.Applied:
+ e.notice("Nothing to ask again: there is no new commit.")
+ case st.Broken != nil && (tip == st.Broken.SHA || tip == st.Broken.OldSHA):
+ e.notice("Not asked again: " + code(tip) + " is the failed commit of the broken state. Push a fix forward, or check the host and run as root: pullgate status " + e.Cfg.Name + " --resume")
+ case tip == st.Held:
+ e.notice("Not asked again: the apply of " + code(tip) + " failed and it is held. To try it again, run as root: pullgate status " + e.Cfg.Name + " --ask-again")
+ case m != nil && m.Mark == state.MarkAsked:
+ q := forgetOpen(st, tip)
+ delete(st.Marks, tip)
+ // Save first: the listener forgets the code only when root has.
+ if err := e.Dir.Save(st); err != nil {
+ e.Log.Error("save the state", "err", err)
+ e.notice("Error: the question could not be closed. See the journal of the host.")
+ return
+ }
+ if q != nil && !q.Auto {
+ e.edit(q.ID, "This question for "+code(q.SHA)+" is closed: the owner sent /ask-again. The next question has a new code.")
+ }
+ e.Log.Info("ask again from Telegram: open question", "sha", tip)
+ e.notice("Asking again about " + code(tip) + ".")
+ case m != nil && m.Mark == state.MarkExpired:
+ delete(st.Marks, tip)
+ e.Log.Info("ask again from Telegram: expired question", "sha", tip)
+ e.notice("Asking again about " + code(tip) + ".")
+ case m == nil:
+ e.notice("Nothing to ask again: " + code(tip) + " has no question that expired or was lost.")
+ default:
+ e.notice("Not asked again: " + code(tip) + " was " + m.Mark + ". /ask-again asks only about a question that expired or was lost. To ask anyway, run as root: pullgate status " + e.Cfg.Name + " --ask-again")
+ }
+}
+
// statusRequest answers /status with the facts of "pullgate status",
// at most once a minute.
func (e *Env) statusRequest(st *state.State) {
diff --git a/internal/gate/request_test.go b/internal/gate/request_test.go
index c852328..5d6cf2a 100644
--- a/internal/gate/request_test.go
+++ b/internal/gate/request_test.go
@@ -113,3 +113,180 @@ func TestListenerCannotPauseByOtherFiles(t *testing.T) {
})
}
}
+
+func TestResumeLiftsOnlyATelegramPause(t *testing.T) {
+ cases := map[string]func(f *fixture){
+ "root --pause": func(f *fixture) { _ = f.env.Status(new(strings.Builder), StatusFlags{Pause: true}) },
+ "cut off": func(f *fixture) { _ = f.env.Dir.Pause("the apply of x was cut off") },
+ "planted prefix": func(f *fixture) { _ = f.env.Dir.Pause(state.TelegramPause + "2026-11-11 12:00 UTC and more") },
+ "two lines": func(f *fixture) { _ = f.env.Dir.Pause(state.TelegramPause + "2026-11-11 12:00 UTC\nx") },
+ "telegram": nil,
+ }
+ for name, pause := range cases {
+ t.Run(name, func(t *testing.T) {
+ f := newFixture(t, ``, "")
+ f.applyNew("one")
+ if pause == nil {
+ f.requestKind(spool.RequestPause)
+ f.run()
+ } else {
+ pause(f)
+ }
+ f.outbox()
+ f.requestKind(spool.RequestResume)
+ f.run()
+ out := f.outbox()
+ if pause == nil {
+ if f.env.Dir.Paused() || !contains(out, "goes on") {
+ t.Fatal("/resume must lift a /pause")
+ }
+ return
+ }
+ if !f.env.Dir.Paused() || !contains(out, "this pause was not set from Telegram") {
+ t.Fatalf("/resume must not lift this pause: %+v", out)
+ }
+ })
+ }
+}
+
+func TestResumeDoesNotEndBroken(t *testing.T) {
+ f := newFixture(t, `on_failure = "rollback"`, "")
+ breakIt(t, f)
+ f.requestKind(spool.RequestPause)
+ f.run()
+ f.requestKind(spool.RequestResume)
+ f.run()
+ if f.env.Dir.Paused() || f.state().Broken == nil || !contains(f.outbox(), "still broken") {
+ t.Fatal("/resume lifts the pause, but the broken state stays")
+ }
+}
+
+func TestAskAgainOnlyExpiredOrLost(t *testing.T) {
+ t.Run("expired", func(t *testing.T) {
+ f := newFixture(t, ``, "")
+ f.applyNew("one")
+ f.commit("second", "two", nil)
+ f.run()
+ f.ask()
+ f.now = f.now.Add(13 * time.Hour) // approval_ttl is 12h
+ if r := f.run(); r != Nothing {
+ t.Fatalf("after expiry: %s", r)
+ }
+ f.outbox()
+ f.requestKind(spool.RequestAskAgain)
+ if r := f.run(); r != Asked {
+ t.Fatalf("/ask-again on an expired question: %s", r)
+ }
+ })
+ t.Run("lost", func(t *testing.T) {
+ f := newFixture(t, ``, "")
+ f.applyNew("one")
+ f.commit("second", "two", nil)
+ f.run()
+ old := f.ask() // as if it never reached the chat
+ f.requestKind(spool.RequestAskAgain)
+ if r := f.run(); r != Asked {
+ t.Fatalf("/ask-again on an open question: %s", r)
+ }
+ if f.ask().Code == old.Code {
+ t.Fatal("the new question needs a new code")
+ }
+ })
+ for _, mark := range []string{"skipped", "failed", "held", "refused", "check-failed"} {
+ t.Run(mark, func(t *testing.T) {
+ f := newFixture(t, ``, "")
+ f.applyNew("one")
+ sha := f.commit("second", "two", nil)
+ f.run()
+ f.outbox()
+ st := f.state()
+ st.Questions = nil
+ if mark == "held" {
+ st.Held = sha
+ st.SetMark(sha, state.MarkFailed, f.now)
+ } else {
+ st.SetMark(sha, mark, f.now)
+ }
+ if err := f.env.Dir.Save(st); err != nil {
+ t.Fatal(err)
+ }
+ f.requestKind(spool.RequestAskAgain)
+ if r := f.run(); r != Nothing {
+ t.Fatalf("/ask-again on %s: %s", mark, r)
+ }
+ if !contains(f.outbox(), "Not asked again") {
+ t.Fatal("no reply")
+ }
+ })
+ }
+}
+
+func TestTelegramPauseFileForm(t *testing.T) {
+ good := state.TelegramPause + "2026-11-11 12:00 UTC"
+ for name, setup := range map[string]func(f *fixture){
+ "hard link": func(f *fixture) {
+ _ = f.env.Dir.Pause(good)
+ _ = os.Link(f.env.Dir.PauseFile(), f.env.Dir.PauseFile()+"2")
+ },
+ "symlink": func(f *fixture) {
+ other := filepath.Join(f.base, "other")
+ _ = os.WriteFile(other, []byte(good+"\n"), 0o644)
+ _ = os.Symlink(other, f.env.Dir.PauseFile())
+ },
+ "trailing space": func(f *fixture) { _ = f.env.Dir.Pause(good + " ") },
+ "not UTC": func(f *fixture) { _ = f.env.Dir.Pause(state.TelegramPause + "2026-11-11 12:00 CET") },
+ } {
+ t.Run(name, func(t *testing.T) {
+ f := newFixture(t, ``, "")
+ if err := f.env.Dir.Ensure(); err != nil {
+ t.Fatal(err)
+ }
+ setup(f)
+ if f.env.Dir.TelegramPaused(f.env.UID) {
+ t.Fatal("must not count as a /pause")
+ }
+ if ok, _ := f.env.Dir.ResumeTelegram(f.env.UID); ok || !f.env.Dir.Paused() {
+ t.Fatal("must not be lifted")
+ }
+ })
+ }
+}
+
+func TestPauseWinsOverResumeInOneRun(t *testing.T) {
+ f := newFixture(t, ``, "")
+ f.applyNew("one")
+ f.requestKind(spool.RequestPause)
+ f.run()
+ f.now = f.now.Add(2 * time.Minute)
+ f.requestKind(spool.RequestResume)
+ f.requestKind(spool.RequestPause)
+ f.run()
+ if !f.env.Dir.Paused() {
+ t.Fatal("with /resume and /pause in one run, the pause wins")
+ }
+}
+
+func TestAskAgainRefusedWhilePausedOrBroken(t *testing.T) {
+ f := newFixture(t, `on_failure = "rollback"`, "")
+ breakIt(t, f)
+ f.outbox()
+ f.requestKind(spool.RequestAskAgain)
+ f.run()
+ if !contains(f.outbox(), "failed commit of the broken state") {
+ t.Fatal("/ask-again must refuse the broken failed commit")
+ }
+ g := newFixture(t, ``, "")
+ g.applyNew("one")
+ g.commit("second", "two", nil)
+ g.run()
+ q := g.ask()
+ _ = g.env.Dir.Pause("paused by the owner")
+ g.requestKind(spool.RequestAskAgain)
+ g.run()
+ if !contains(g.outbox(), "is paused or off") {
+ t.Fatal("/ask-again while paused must be refused")
+ }
+ if g.state().FindCode(q.Code) < 0 {
+ t.Fatal("the question must stay while paused")
+ }
+}
diff --git a/internal/gate/run.go b/internal/gate/run.go
index c616840..4a62e45 100644
--- a/internal/gate/run.go
+++ b/internal/gate/run.go
@@ -157,10 +157,18 @@ func (e *Env) Run(ctx context.Context) (Result, error) {
if err != nil {
return "", err
}
+ // Resume before pause: when both came, the pause wins (fail closed).
+ if requests.Resume > 0 {
+ e.resumeRequest(st)
+ paused = e.Dir.Paused()
+ }
if requests.Pause > 0 {
e.pauseRequest(st)
paused = e.Dir.Paused()
}
+ if requests.AskAgain > 0 {
+ e.askAgainRequest(st)
+ }
if requests.Status > 0 {
e.statusRequest(st)
}
@@ -179,7 +187,7 @@ func (e *Env) Run(ctx context.Context) (Result, error) {
why := e.Cfg.Name + " is off (mode = \"off\"), so nothing was fetched."
res := Off
if paused {
- why = e.Cfg.Name + " is paused, so nothing was fetched. Run as root: pullgate status " + e.Cfg.Name + " --resume"
+ why = e.Cfg.Name + " is paused, so nothing was fetched. " + e.resumeHint()
res = Paused
}
e.Log.Info(string(res))
diff --git a/internal/listener/listener.go b/internal/listener/listener.go
index 1f29422..a187d52 100644
--- a/internal/listener/listener.go
+++ b/internal/listener/listener.go
@@ -431,8 +431,10 @@ const updateCommand = "/update"
// statusCommand and pauseCommand ask root for the status, or to pause an
// instance (code6x/pullgate#32).
const (
- statusCommand = "/status"
- pauseCommand = "/pause"
+ statusCommand = "/status"
+ pauseCommand = "/pause"
+ resumeCommand = "/resume" // code6x/pullgate#33
+ askAgainCommand = "/ask-again" // code6x/pullgate#33
)
// RequestGap is the least time between two /status or two /pause for the
@@ -459,8 +461,8 @@ func (l *Listener) request(ctx context.Context, m *telegram.Message, kind string
default:
l.Log.Info("message ignored", "reason", "bad "+kind+" arguments", "from", fromID(m))
use := "/status or /status NAME"
- if kind == spool.RequestPause {
- use = "/pause NAME"
+ if kind != spool.RequestStatus {
+ use = "/" + kind + " NAME"
}
l.sendReply(ctx, m, "Use "+use+". NAME is one of: "+html.EscapeString(strings.Join(l.Cfg.Instances, ", "))+".")
return
@@ -488,10 +490,8 @@ func (l *Listener) request(ctx context.Context, m *telegram.Message, kind string
l.Log.Info(kind+" request taken", "instances", ok)
var text []string
if len(ok) > 0 {
- verb := "Asking for the status of"
- if kind == spool.RequestPause {
- verb = "Pausing"
- }
+ verb := map[string]string{spool.RequestStatus: "Asking for the status of", spool.RequestPause: "Pausing",
+ spool.RequestResume: "Asking to resume", spool.RequestAskAgain: "Asking to ask again on"}[kind]
line := verb + " " + html.EscapeString(strings.Join(ok, ", ")) + "…"
if kind == spool.RequestPause && len(f) > 2 {
line += " (the reason is not sent to the host: a request holds no text)"
@@ -523,7 +523,9 @@ func (l *Listener) helpText() string {
"/check: look for a new commit now, on every instance",
"/check NAME: the same, on one instance",
"/status or /status NAME: the mode, the applied commit, the open question, paused or not",
- "/pause NAME: stop the instance; to go on, root runs pullgate status NAME --resume",
+ "/pause NAME: stop the instance",
+ "/resume NAME: go on after a /pause; any other pause needs root",
+ "/ask-again NAME: ask again about a question that expired or was lost",
"/update: look for a new release of pullgate; a question asks before it is installed",
"/help: this list",
"",
@@ -557,7 +559,7 @@ func (l *Listener) command(ctx context.Context, m *telegram.Message) {
l.update(ctx, m)
return
}
- if why == spool.RequestStatus || why == spool.RequestPause {
+ if why == spool.RequestStatus || why == spool.RequestPause || why == spool.RequestResume || why == spool.RequestAskAgain {
l.request(ctx, m, why)
return
}
@@ -629,6 +631,10 @@ func (l *Listener) checkNames(m *telegram.Message) ([]string, string, bool) {
return nil, spool.RequestStatus, false
case firstWord(m.Text) == pauseCommand:
return nil, spool.RequestPause, false
+ case firstWord(m.Text) == resumeCommand:
+ return nil, spool.RequestResume, false
+ case firstWord(m.Text) == askAgainCommand:
+ return nil, spool.RequestAskAgain, false
case !strings.HasPrefix(m.Text, checkCommand):
return nil, "not a command", false
case m.Text == checkCommand:
diff --git a/internal/listener/listener_test.go b/internal/listener/listener_test.go
index cf75516..bf000d5 100644
--- a/internal/listener/listener_test.go
+++ b/internal/listener/listener_test.go
@@ -393,7 +393,7 @@ func TestHelp(t *testing.T) {
if len(ms) != 1 || ms[0].Chat != chat || ms[0].ReplyTo == 0 {
t.Fatalf("want one reply: %+v", ms)
}
- for _, want := range []string{"/check", "/check NAME", "/help", "" + inst + "", "" + inst2 + "", "Apply", "Skip", "--pause", "--resume", "--ask-again", "/status", "/pause NAME"} {
+ for _, want := range []string{"/check", "/check NAME", "/help", "" + inst + "", "" + inst2 + "", "Apply", "Skip", "--pause", "--resume", "--ask-again", "/status", "/pause NAME", "/resume NAME", "/ask-again NAME"} {
if !strings.Contains(ms[0].Text, want) {
t.Errorf("the help must hold %q:\n%s", want, ms[0].Text)
}
@@ -664,3 +664,19 @@ func TestStatusAndPauseNotForOthers(t *testing.T) {
t.Fatal("others get no reply and write no request")
}
}
+
+func TestResumeAndAskAgainCommands(t *testing.T) {
+ f := newL(t)
+ f.say(owner, chat, "/resume "+inst, false)
+ f.say(owner, chat, "/ask-again "+inst, false)
+ f.say(owner, chat, "/resume", false) // needs a name
+ f.say(owner, chat, "/ask-again "+inst+" x", false) // no more words
+ f.say(999, chat, "/resume "+inst, false) // not the owner
+ r, bad, err := f.d.TakeRequests(os.Geteuid())
+ if r != (spool.Requests{Resume: 1, AskAgain: 1}) || len(bad) != 0 || err != nil {
+ t.Fatalf("requests: %+v %v %v", r, bad, err)
+ }
+ if ms := f.tg.Messages(); len(ms) != 4 {
+ t.Fatalf("want 2 answers and 2 usage replies, got %+v", ms)
+ }
+}
diff --git a/internal/spool/spool.go b/internal/spool/spool.go
index 5a244e4..c7b9413 100644
--- a/internal/spool/spool.go
+++ b/internal/spool/spool.go
@@ -261,23 +261,25 @@ func (d Dirs) HasCheck() bool {
// one file name, and its content is the name and a newline: no data from
// the message.
const (
- RequestStatus = "status"
- RequestPause = "pause"
+ RequestStatus = "status"
+ RequestPause = "pause"
+ RequestResume = "resume" // code6x/pullgate#33
+ RequestAskAgain = "ask-again" // code6x/pullgate#33
)
// WriteRequest writes a status or pause request (the listener).
func (d Dirs) WriteRequest(kind string) error {
- if kind != RequestStatus && kind != RequestPause {
+ if kind != RequestStatus && kind != RequestPause && kind != RequestResume && kind != RequestAskAgain {
return fmt.Errorf("unknown request kind %q", kind)
}
return writeIn(d.CheckDir(), []byte(kind+"\n"), 0o600, -1, kind)
}
// Requests counts the valid requests that TakeRequests took.
-type Requests struct{ Check, Status, Pause int }
+type Requests struct{ Check, Status, Pause, Resume, AskAgain int }
// Any reports whether there was a valid request.
-func (r Requests) Any() bool { return r.Check+r.Status+r.Pause > 0 }
+func (r Requests) Any() bool { return r.Check+r.Status+r.Pause+r.Resume+r.AskAgain > 0 }
// TakeChecks is TakeRequests, for callers that know only check requests.
func (d Dirs) TakeChecks(listenerUID int) (int, []error, error) {
@@ -319,6 +321,10 @@ func (d Dirs) TakeRequests(listenerUID int) (Requests, []error, error) {
count, want = &r.Status, RequestStatus+"\n"
case RequestPause:
count, want = &r.Pause, RequestPause+"\n"
+ case RequestResume:
+ count, want = &r.Resume, RequestResume+"\n"
+ case RequestAskAgain:
+ count, want = &r.AskAgain, RequestAskAgain+"\n"
}
switch {
case err != nil:
diff --git a/internal/state/state.go b/internal/state/state.go
index a9e2420..b15792a 100644
--- a/internal/state/state.go
+++ b/internal/state/state.go
@@ -15,6 +15,7 @@ import (
"io/fs"
"os"
"path/filepath"
+ "regexp"
"sort"
"strings"
"syscall"
@@ -271,6 +272,69 @@ func (d Dir) PauseReason() string {
return line
}
+// telegramPauseRE is the whole first line of a pause from /pause.
+var telegramPauseRE = regexp.MustCompile(`^` + regexp.QuoteMeta(TelegramPause) + `[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2} UTC$`)
+
+// ResumeTelegram removes the pause file only if it is a /pause one
+// (TelegramPaused). It moves the file aside first and checks that the file
+// it moved is the one it checked; if not (root wrote a new pause in
+// between), it puts that one back without replacing anything, and keeps
+// the pause. It reports whether the pause was lifted.
+func (d Dir) ResumeTelegram(uid uint32) (bool, error) {
+ f, err := os.OpenFile(d.PauseFile(), os.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0)
+ if err != nil {
+ return false, nil
+ }
+ defer func() { _ = f.Close() }()
+ checked, err := f.Stat()
+ if err != nil || !telegramPauseFile(f, checked, uid) {
+ return false, nil
+ }
+ tmp := d.file(".resume-tmp")
+ _ = os.Remove(tmp)
+ if err := os.Rename(d.PauseFile(), tmp); err != nil {
+ return false, err
+ }
+ moved, err := os.Lstat(tmp)
+ if err != nil || !os.SameFile(checked, moved) {
+ // Not the file that was checked: put it back, never over a new one.
+ if lerr := os.Link(tmp, d.PauseFile()); lerr != nil && !errors.Is(lerr, fs.ErrExist) {
+ return false, lerr
+ }
+ _ = os.Remove(tmp)
+ return false, nil
+ }
+ return true, os.Remove(tmp)
+}
+
+// TelegramPaused reports whether the pause file is one that /pause wrote:
+// a regular file of uid, not linked twice, of one line that is exactly the
+// reason of /pause. Any other pause (by pullgate itself or by root) is not.
+func (d Dir) TelegramPaused(uid uint32) bool {
+ f, err := os.OpenFile(d.PauseFile(), os.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0)
+ if err != nil {
+ return false
+ }
+ defer func() { _ = f.Close() }()
+ fi, err := f.Stat()
+ if err != nil {
+ return false
+ }
+ return telegramPauseFile(f, fi, uid)
+}
+
+func telegramPauseFile(f *os.File, fi os.FileInfo, uid uint32) bool {
+ st, ok := fi.Sys().(*syscall.Stat_t)
+ if !ok || !fi.Mode().IsRegular() || st.Uid != uid || st.Nlink != 1 || fi.Size() > 100 {
+ return false
+ }
+ b, err := io.ReadAll(io.LimitReader(f, 101))
+ if err != nil {
+ return false
+ }
+ return telegramPauseRE.MatchString(strings.TrimSuffix(string(b), "\n")) && strings.Count(string(b), "\n") == 1
+}
+
// Resume removes the pause file.
func (d Dir) Resume() error {
err := os.Remove(d.PauseFile())