From ffe10fa0391ea0f692854791ec0a91097ded58a4 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 24 Mar 2016 19:30:15 +0000 Subject: [PATCH] cgi-fcgi-interp: wip garbage collection, before new pidfile-based approach --- cprogs/cgi-fcgi-interp.c | 52 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/cprogs/cgi-fcgi-interp.c b/cprogs/cgi-fcgi-interp.c index 2732774..752db40 100644 --- a/cprogs/cgi-fcgi-interp.c +++ b/cprogs/cgi-fcgi-interp.c @@ -346,12 +346,62 @@ static bool check_garbage(void) { return 0; } +static void tidy_1_garbage(const char *leaf) { + int r; + struct sockaddr_un sun; + + int fd = -1; + + memset(&sun,0,sizeof(sun)); + sun.sun_family = AF_UNIX; + r = snprintf(sun.sun_path, sizeof(sun.sun_path), "%s/%s", run_base, leaf); + if (r >= sizeof(sun_path)) + goto cannot; + + fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd<0) err("create socket for tidying garbage"); + + r = fcntl(fd, F_SETFL, O_NONBLOCK); + if (r<0) err("set garbage socket nonblocking"); + + r = connect(fd, &sun, sizeof(sun)); + if (r) { + if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) + goto cannot; + if (errno != EINPROGRESS) + err("connect to garbage socket (%s)", sun.sun_path); + /* well, EINPROGRESS, let's just carry on and hope write works */ + } + + r = write( + + static void tidy_garbage(void) { const char *this_garbage = - m_asprintf("%s/%lu.%lu", run_base, + m_asprintf("%s/g%lu.%lu", run_base, (unsigned long)sock_stab.st_ino, (unsigned long)getpid()); + r = rename(socket_path, this_garbage); + if (r) { + if (!(errno == ENOENT)) + err("rename socket from old runner (from %s to %s)", + socket_path, this_garbage); + } + + DIR *d = opendir(run_base); + if (!d) err("open run directory (%d) to clean up garbage", run_base); + struct dirent *de; + while ((errno = 0, de = readdir(d))) { + if (de->d_name[0] != 'g') + continue; + tidy_1_garbage(de->d_name); + } + if (errno) err("read run directory (%d) to clean up garbage", run_base); + /* + (void)r; + if (r) { + printf("this_garb: %s\n", this_garbage); } -- 2.30.2