Handle non-response from daemon for $queue_size
[exim.git] / src / src / expand.c
index 4af4a3652ce7f18cee9a34674bb096bfd1bbeaa4..9b85c1e0dd582dc2070b99deea67581d67d56219 100644 (file)
@@ -1750,7 +1750,7 @@ return g ? g->s : NULL;
 static uschar *
 fn_queue_size(void)
 {
-struct sockaddr_un sun = {.sun_family = AF_UNIX};
+struct sockaddr_un sa_un = {.sun_family = AF_UNIX};
 uschar buf[16];
 int fd;
 ssize_t len;
@@ -1758,6 +1758,8 @@ const uschar * where;
 #ifndef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
 uschar * sname;
 #endif
+fd_set fds;
+struct timeval tv;
 
 if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0)
   {
@@ -1766,41 +1768,49 @@ if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0)
   }
 
 #ifdef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
-sun.sun_path[0] = 0;   /* Abstract local socket addr - Linux-specific? */
+sa_un.sun_path[0] = 0; /* Abstract local socket addr - Linux-specific? */
 len = offsetof(struct sockaddr_un, sun_path) + 1
-  + snprintf(sun.sun_path+1, sizeof(sun.sun_path)-1, "exim_%d", getpid());
+  + snprintf(sa_un.sun_path+1, sizeof(sa_un.sun_path)-1, "exim_%d", getpid());
 #else
 sname = string_sprintf("%s/p_%d", spool_directory, getpid());
 len = offsetof(struct sockaddr_un, sun_path)
-  + snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", sname);
+  + snprintf(sa_un.sun_path, sizeof(sa_un.sun_path), "%s", sname);
 #endif
 
-if (bind(fd, (const struct sockaddr *)&sun, len) < 0)
+if (bind(fd, (const struct sockaddr *)&sa_un, len) < 0)
   { where = US"bind"; goto bad; }
 
 #ifdef notdef
 debug_printf("local addr '%s%s'\n",
-  *sun.sun_path ? "" : "@",
-  sun.sun_path + (*sun.sun_path ? 0 : 1));
+  *sa_un.sun_path ? "" : "@",
+  sa_un.sun_path + (*sa_un.sun_path ? 0 : 1));
 #endif
 
 #ifdef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
-sun.sun_path[0] = 0;   /* Abstract local socket addr - Linux-specific? */
+sa_un.sun_path[0] = 0; /* Abstract local socket addr - Linux-specific? */
 len = offsetof(struct sockaddr_un, sun_path) + 1
-  + snprintf(sun.sun_path+1, sizeof(sun.sun_path)-1, "%s", NOTIFIER_SOCKET_NAME);
+  + snprintf(sa_un.sun_path+1, sizeof(sa_un.sun_path)-1, "%s", NOTIFIER_SOCKET_NAME);
 #else
 len = offsetof(struct sockaddr_un, sun_path)
-  + snprintf(sun.sun_path, sizeof(sun.sun_path), "%s/%s",
+  + snprintf(sa_un.sun_path, sizeof(sa_un.sun_path), "%s/%s",
                spool_directory, NOTIFIER_SOCKET_NAME);
 #endif
 
-if (connect(fd, (const struct sockaddr *)&sun, len) < 0)
-  { where = US"connect"; goto bad; }
+if (connect(fd, (const struct sockaddr *)&sa_un, len) < 0)
+  { where = US"connect"; goto bad2; }
 
 buf[0] = NOTIFY_QUEUE_SIZE_REQ;
 if (send(fd, buf, 1, 0) < 0) { where = US"send"; goto bad; }
 
-if ((len = recv(fd, buf, sizeof(buf), 0)) < 0) { where = US"recv"; goto bad; }
+FD_ZERO(&fds); FD_SET(fd, &fds);
+tv.tv_sec = 2; tv.tv_usec = 0;
+if (select(fd + 1, (SELECT_ARG2_TYPE *)&fds, NULL, NULL, &tv) != 1)
+  {
+  DEBUG(D_expand) debug_printf("no daemon response; using local evaluation\n");
+  len = snprintf(CS buf, sizeof(buf), "%u", queue_count_cached());
+  }
+else if ((len = recv(fd, buf, sizeof(buf), 0)) < 0)
+  { where = US"recv"; goto bad2; }
 
 close(fd);
 #ifndef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
@@ -1808,6 +1818,10 @@ Uunlink(sname);
 #endif
 return string_copyn(buf, len);
 
+bad2:
+#ifndef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
+  Uunlink(sname);
+#endif
 bad:
   close(fd);
   DEBUG(D_expand) debug_printf(" %s: %s\n", where, strerror(errno));