ref: a8558cb1e89245ecb38501b0fe4b64462ba69889
parent: 1260c5c1735d5cf5a60890df7471bcd808b6231d
author: sirjofri <sirjofri@sirjofri.de>
date: Tue Jul 16 10:11:31 EDT 2024
gracefully shut down other processes
--- a/engine.c
+++ b/engine.c
@@ -7,6 +7,7 @@
char Einitengine[] = "initengine: %r";
int p[3];
+int pid[3];
char *preamble;
Width *firstwidth = nil;
@@ -103,13 +104,15 @@
{
Biobuf *bin;
char *s;
+ int apid;
- switch (rfork(RFPROC|RFMEM|RFFDG)) {
+ switch (apid = rfork(RFPROC|RFMEM|RFFDG)) {
case -1:
sysfatal(Einitengine);
case 0: /* child */
break;
default: /* parent */
+ pid[2] = apid;
return 1;
}
@@ -133,6 +136,7 @@
int in[2];
int out[2];
int err[2];
+ int apid;
Biobuf *bin;
char *s;
@@ -143,7 +147,7 @@
if (pipe(err) < 0)
sysfatal(Einitengine);
- switch (fork()) {
+ switch (apid = fork()) {
case -1: /* error */
sysfatal(Einitengine);
break;
@@ -167,17 +171,19 @@
p[0] = in[1];
p[1] = out[0];
p[2] = err[0];
+ pid[0] = apid;
break;
}
outchan = mkchan();
- switch (rfork(RFPROC|RFMEM|RFFDG)) {
+ switch (apid = rfork(RFPROC|RFMEM|RFFDG)) {
case -1: /* error */
sysfatal(Einitengine);
case 0: /* child reader */
break;
default: /* parent */
+ pid[1] = apid;
return spawnerrrdr();
}
@@ -207,6 +213,17 @@
if (o) {
*o = recv(outchan);
}
+}
+
+void
+teardown(void)
+{
+ if (postnote(PNPROC, pid[0], "hangup") < 0)
+ sysfatal("hoc could not be exited: %r");
+ if (postnote(PNPROC, pid[1], "hangup") < 0)
+ sysfatal("reader could not be exited: %r");
+ if (postnote(PNPROC, pid[2], "hangup") < 0)
+ sysfatal("err reader could not be exited: %r");
}
static int
--- a/spread.c
+++ b/spread.c
@@ -559,6 +559,12 @@
}
void
+onexit(void)
+{
+ teardown();
+}
+
+void
main(int argc, char **argv)
{
int e;
@@ -576,6 +582,8 @@
}ARGEND;
file = (argc == 1) ? *argv : nil;
+
+ atexit(onexit);
if (!inithoc())
sysfatal("%r");
--- a/spread.h
+++ b/spread.h
@@ -15,6 +15,7 @@
int loadfile(char *file);
int writefile(char *file);
Response getvalue(P);
+void teardown(void);
void freeresponse(Response*);
int getwidth(int);