about summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorC. McEnroe <june@causal.agency>2021-09-27 21:36:26 +0000
committerC. McEnroe <june@causal.agency>2021-09-27 21:36:26 +0000
commit9411b044ee37862b90b5da0a18ea4f5984095de4 (patch)
treec5edfe031d5892b715cf34ae7178d1caf744207e
parentCall msync(2) after modifying tiles (diff)
downloadtorus-9411b044ee37862b90b5da0a18ea4f5984095de4.tar.gz
torus-9411b044ee37862b90b5da0a18ea4f5984095de4.zip
Handle KCGI_EXIT gracefully
-rw-r--r--image.c24
1 files changed, 13 insertions, 11 deletions
diff --git a/image.c b/image.c
index 3abcc2e..573ded1 100644
--- a/image.c
+++ b/image.c
@@ -214,11 +214,11 @@ static void worker(void) {
 	if (error) err(EX_OSERR, "pledge");
 #endif
 
-	for (;;) {
+	for (
 		struct kreq req;
-		error = khttp_fcgi_parse(fcgi, &req);
-		if (error) errkcgi(EX_DATAERR, error, "khttp_fcgi_parse");
-
+		!(error = khttp_fcgi_parse(fcgi, &req));
+		khttp_free(&req)
+	) {
 		uint32_t tileX = TileInitX;
 		uint32_t tileY = TileInitY;
 		if (req.fieldmap[KeyX]) {
@@ -231,18 +231,18 @@ static void worker(void) {
 		error = khttp_head(
 			&req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]
 		);
-		if (error == KCGI_HUP) goto next;
+		if (error == KCGI_HUP) continue;
 		if (error) errkcgi(EX_IOERR, error, "khttp_head");
 
 		error = khttp_head(
 			&req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_IMAGE_PNG]
 		);
-		if (error == KCGI_HUP) goto next;
+		if (error == KCGI_HUP) continue;
 		if (error) errkcgi(EX_IOERR, error, "khttp_head");
 
 		// XXX: kcgi never enables compression for FastCGI.
 		error = khttp_body(&req);
-		if (error == KCGI_HUP) goto next;
+		if (error == KCGI_HUP) continue;
 		if (error) errkcgi(EX_IOERR, error, "khttp_body");
 
 		struct Stream cookie = { .req = &req };
@@ -251,10 +251,9 @@ static void worker(void) {
 
 		render(stream, tileX, tileY);
 		fclose(stream);
-
-next:
-		khttp_free(&req);
 	}
+	if (error != KCGI_EXIT) errkcgi(EX_PROTOCOL, error, "khttp_fcgi_parse");
+	khttp_fcgi_free(fcgi);
 }
 
 #endif /* HAVE_KCGI */
@@ -297,7 +296,10 @@ int main(int argc, char *argv[]) {
 #endif
 
 #ifdef HAVE_KCGI
-	if (kcgi) worker();
+	if (kcgi) {
+		worker();
+		return EX_OK;
+	}
 #endif
 
 	render(stdout, tileX, tileY);