about summary refs log tree commit diff
path: root/users/Profpatsch/shortcuttable/default.nix
blob: 13ba22040056013d667fb1cfbf0a07a9fe1c21ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
{ depot, lib, pkgs, ... }:

let
  # run prog... and restart whenever SIGHUP is received
  #
  # this is useful for binding to a shortcut.
  #
  # Unfortunately, this requires a bunch of workarounds around the semantics of `trap`,
  # but the general idea of bundling subprocesses with `setsid` is somewhat sound.
  runShortcuttable =
    depot.nix.writeExecline "run-shortcuttable" { } [
      "importas"
      "-i"
      "run"
      "XDG_RUNTIME_DIR"
      "if"
      [ "mkdir" "-p" "\${run}/shortcuttable/test" ]
      "getpid"
      "-E"
      "controlpid"
      savePid
      "\${run}/shortcuttable/test/control"
      "$controlpid"

      # start the program
      "background"
      [
        startSaveSID
        "\${run}/shortcuttable/test/running-sid"
        "$@"
      ]

      "trap"
      [
        "SIGHUP"
        [
          "if"
          [ "echo" "got hup" ]
          "if"
          [
            "if"
            [ "echo" "killing our child processes" ]
            "envfile"
            "\${run}/shortcuttable/test/running-sid"
            "importas"
            "-ui"
            "child_sid"
            "pid"
            "foreground"
            [ "ps" "-f" "--sid" "$child_sid" ]
            ctrlCCtrlDSid
            "$child_sid"
          ]
          "if"
          [ "echo" "restarting into" "$@" ]
          "background"
          [
            startSaveSID
            "\${run}/shortcuttable/test/running-sid"
            "$@"
          ]
        ]
        "SIGTERM"
        [
          (killShortcuttable { signal = "TERM"; })
          "\${run}/shortcuttable/test/running-sid"
          "\${run}/shortcuttable/test/exit"
        ]
        "SIGINT"
        [
          (killShortcuttable { signal = "INT"; })
          "\${run}/shortcuttable/test/running-sid"
          "\${run}/shortcuttable/test/exit"
        ]
      ]
      depot.users.Profpatsch.execline.setsid
      "child_sid"
      "getpid"
      "-E"
      "exitpid"
      savePid
      "\${run}/shortcuttable/test/exit"
      "$exitpid"
      "sleep"
      "infinity"
    ];

  killShortcuttable = { signal }: depot.nix.writeExecline "kill-shortcuttable" { readNArgs = 2; } [
    "if"
    [ "echo" "got SIG${signal}, quitting" ]
    "if"
    [
      "envfile"
      "$1"
      "importas"
      "-ui"
      "child_sid"
      "pid"
      "foreground"
      [ "ps" "-f" "--sid" "$child_sid" ]
      ctrlCCtrlDSid
      "$child_sid"
    ]
    "if"
    [ "echo" "killing shortcuttable loop" ]
    "envfile"
    "$2"
    "importas"
    "-ui"
    "trap_pid"
    "pid"
    "foreground"
    [ "ps" "-fp" "$trap_pid" ]
    "kill"
    "--signal"
    signal
    "$trap_pid"
  ];

  savePid = depot.nix.writeExecline "save-pid" { readNArgs = 2; } [
    "if"
    [ "echo" "saving process:" ]
    "if"
    [ "ps" "-fp" "$2" ]
    "if"
    [
      "redirfd"
      "-w"
      "1"
      "$1"
      "printf"
      "pid = %s\n"
      "$2"
    ]
    "$@"
  ];

  # try to kill process, first with SIGTERM then SIGQUIT (in case it’s a repl)
  ctrlCCtrlDSid = depot.nix.writeExecline "ctrl-c-ctrl-d" { readNArgs = 1; } [
    "ifelse"
    "-n"
    [ "kill" "--signal" "TERM" "--" "-\${1}" ]
    [
      "if"
      [ "echo" "could not kill via SIGTERM, trying SIGQUIT …" ]
      "ifelse"
      "-n"
      [ "kill" "--signal" "QUIT" "--" "-\${1}" ]
      [ "echo" "SIGQUIT failed as well, keeping it running" ]
      "$@"
    ]
    "$@"
  ];

  startSaveSID = depot.nix.writeExecline "start-save-sid" { readNArgs = 1; } [
    depot.users.Profpatsch.execline.setsid
    "child_sid"
    "importas"
    "-ui"
    "child_sid"
    "child_sid"
    "if"
    [ "echo" "children sid:" "$child_sid" ]
    savePid
    "$1"
    "$child_sid"
    "$@"
  ];


in
runShortcuttable