+exim_exit(EXIT_SUCCESS);
+}
+
+
+/*************************************************
+* Listener socket for local work prompts *
+*************************************************/
+
+ssize_t
+daemon_client_sockname(struct sockaddr_un * sup, uschar ** sname)
+{
+#ifdef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
+sup->sun_path[0] = 0; /* Abstract local socket addr - Linux-specific? */
+return offsetof(struct sockaddr_un, sun_path) + 1
+ + snprintf(sup->sun_path+1, sizeof(sup->sun_path)-1, "exim_%d", getpid());
+#else
+*sname = string_sprintf("%s/p_%d", spool_directory, getpid());
+return offsetof(struct sockaddr_un, sun_path)
+ + snprintf(sup->sun_path, sizeof(sup->sun_path), "%s", CS *sname);
+#endif
+}
+
+ssize_t
+daemon_notifier_sockname(struct sockaddr_un * sup)
+{
+#ifdef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
+sup->sun_path[0] = 0; /* Abstract local socket addr - Linux-specific? */
+return offsetof(struct sockaddr_un, sun_path) + 1
+ + snprintf(sup->sun_path+1, sizeof(sup->sun_path)-1, "%s",
+ CS expand_string(notifier_socket));
+#else
+notifier_socket_name = expand_string(notifier_socket);
+return offsetof(struct sockaddr_un, sun_path)
+ + snprintf(sup->sun_path, sizeof(sup->sun_path), "%s",
+ CS notifier_socket_name);
+#endif
+}
+
+
+static void
+daemon_notifier_socket(void)
+{
+int fd;
+const uschar * where;
+struct sockaddr_un sa_un = {.sun_family = AF_UNIX};
+ssize_t len;
+
+if (!f.notifier_socket_en)
+ {
+ DEBUG(D_any) debug_printf("-oY used so not creating notifier socket\n");
+ return;
+ }
+if (override_local_interfaces && !override_pid_file_path)
+ {
+ DEBUG(D_any)
+ debug_printf("-oX used without -oP so not creating notifier socket\n");
+ return;
+ }
+if (!notifier_socket || !*notifier_socket)
+ {
+ DEBUG(D_any) debug_printf("no name for notifier socket\n");
+ return;
+ }
+
+DEBUG(D_any) debug_printf("creating notifier socket\n");
+
+#ifdef SOCK_CLOEXEC
+if ((fd = socket(PF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
+ { where = US"socket"; goto bad; }
+#else
+if ((fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0)
+ { where = US"socket"; goto bad; }
+(void)fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+#endif
+
+len = daemon_notifier_sockname(&sa_un);
+
+#ifdef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
+DEBUG(D_any) debug_printf(" @%s\n", sa_un.sun_path+1);
+#else /* filesystem-visible and persistent; will neeed removal */
+DEBUG(D_any) debug_printf(" %s\n", sa_un.sun_path);
+#endif
+
+if (bind(fd, (const struct sockaddr *)&sa_un, (socklen_t)len) < 0)
+ { where = US"bind"; goto bad; }
+
+#ifdef SO_PASSCRED /* Linux */
+if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0)
+ { where = US"SO_PASSCRED"; goto bad2; }
+#elif defined(LOCAL_CREDS) /* FreeBSD-ish */
+if (setsockopt(fd, 0, LOCAL_CREDS, &on, sizeof(on)) < 0)
+ { where = US"LOCAL_CREDS"; goto bad2; }
+#endif
+
+/* debug_printf("%s: fd %d\n", __FUNCTION__, fd); */
+daemon_notifier_fd = fd;
+return;
+
+bad2:
+#ifndef EXIM_HAVE_ABSTRACT_UNIX_SOCKETS
+ Uunlink(sa_un.sun_path);
+#endif
+bad:
+ log_write(0, LOG_MAIN|LOG_PANIC, "%s %s: %s",
+ __FUNCTION__, where, strerror(errno));
+ close(fd);
+ return;
+}
+
+
+/* Data for notifier-triggered queue runs */
+
+static uschar queuerun_msgid[MESSAGE_ID_LENGTH+1];
+static const uschar * queuerun_msg_qname;
+
+
+/* The notifier socket has something to read. Pull the message from it, decode
+and do the action.
+*/
+
+static void
+daemon_notification(void)
+{
+uschar buf[256], cbuf[256];
+struct sockaddr_un sa_un;
+struct iovec iov = {.iov_base = buf, .iov_len = sizeof(buf)-1};
+struct msghdr msg = { .msg_name = &sa_un,
+ .msg_namelen = sizeof(sa_un),
+ .msg_iov = &iov,
+ .msg_iovlen = 1,
+ .msg_control = cbuf,
+ .msg_controllen = sizeof(cbuf)
+ };
+ssize_t sz;
+
+buf[sizeof(buf)-1] = 0;
+if ((sz = recvmsg(daemon_notifier_fd, &msg, 0)) <= 0) return;
+if (sz >= sizeof(buf)) return;
+
+#ifdef notdef
+debug_printf("addrlen %d\n", msg.msg_namelen);
+#endif
+DEBUG(D_queue_run)
+ if (msg.msg_namelen > 0)
+ {
+ BOOL abstract = !*sa_un.sun_path;
+ char * name = sa_un.sun_path + (abstract ? 1 : 0);
+ int namelen = (int)msg.msg_namelen - abstract ? 1 : 0;
+ if (*name)
+ debug_printf("%s from addr '%s%.*s'\n", __FUNCTION__,
+ abstract ? "@" : "",
+ namelen, name);
+ else
+ debug_printf("%s (from unknown addr)\n", __FUNCTION__);
+ }
+ else
+ debug_printf("%s (from unknown addr)\n", __FUNCTION__);
+
+/* Refuse to handle the item unless the peer has good credentials */
+#ifdef SCM_CREDENTIALS
+# define EXIM_SCM_CR_TYPE SCM_CREDENTIALS
+#elif defined(LOCAL_CREDS) && defined(SCM_CREDS)
+# define EXIM_SCM_CR_TYPE SCM_CREDS
+#else
+ /* The OS has no way to get the creds of the caller (for a unix/datagram socket.
+ Punt; don't try to check. */
+#endif
+
+#ifdef EXIM_SCM_CR_TYPE
+for (struct cmsghdr * cp = CMSG_FIRSTHDR(&msg);
+ cp;
+ cp = CMSG_NXTHDR(&msg, cp))
+ if (cp->cmsg_level == SOL_SOCKET && cp->cmsg_type == EXIM_SCM_CR_TYPE)
+ {
+# ifdef SCM_CREDENTIALS /* Linux */
+ struct ucred * cr = (struct ucred *) CMSG_DATA(cp);
+ if (cr->uid && cr->uid != exim_uid)
+ {
+ DEBUG(D_queue_run) debug_printf("%s: sender creds pid %d uid %d gid %d\n",
+ __FUNCTION__, (int)cr->pid, (int)cr->uid, (int)cr->gid);
+ }
+# elif defined(LOCAL_CREDS) /* BSD-ish */
+ struct sockcred * cr = (struct sockcred *) CMSG_DATA(cp);
+ if (cr->sc_uid && cr->sc_uid != exim_uid)
+ {
+ DEBUG(D_queue_run) debug_printf("%s: sender creds pid ??? uid %d gid %d\n",
+ __FUNCTION__, (int)cr->sc_uid, (int)cr->sc_gid);
+ }
+# endif
+ break;
+ }
+#endif
+
+buf[sz] = 0;
+switch (buf[0])
+ {
+#ifndef DISABLE_QUEUE_RAMP
+ case NOTIFY_MSG_QRUN:
+ /* this should be a message_id */
+ DEBUG(D_queue_run)
+ debug_printf("%s: qrunner trigger: %s\n", __FUNCTION__, buf+1);
+
+ memcpy(queuerun_msgid, buf+1, MESSAGE_ID_LENGTH+1);
+
+ for (qrunner * q = qrunners; q; q = q->next)
+ if (q->name
+ ? Ustrcmp(q->name, buf+1+MESSAGE_ID_LENGTH+1) == 0
+ : !buf[1+MESSAGE_ID_LENGTH+1]
+ )
+ { queuerun_msg_qname = q->name; break; }
+ return;
+#endif
+
+ case NOTIFY_QUEUE_SIZE_REQ:
+ {
+ uschar buf[16];
+ int len = snprintf(CS buf, sizeof(buf), "%u", queue_count_cached());
+
+ DEBUG(D_queue_run)
+ debug_printf("%s: queue size request: %s\n", __FUNCTION__, buf);
+
+ if (sendto(daemon_notifier_fd, buf, len, 0,
+ (const struct sockaddr *)&sa_un, msg.msg_namelen) < 0)
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "%s: sendto: %s\n", __FUNCTION__, strerror(errno));
+ break;
+ }
+
+ case NOTIFY_REGEX:
+ regex_at_daemon(buf);
+ break;
+ }
+return;
+}
+
+
+
+static void
+daemon_inetd_wtimeout(time_t last_connection_time)
+{
+time_t resignal_interval = inetd_wait_timeout;
+
+if (last_connection_time == (time_t)0)
+ {
+ DEBUG(D_any)
+ debug_printf("inetd wait timeout expired, but still not seen first message, ignoring\n");
+ }
+else
+ {
+ time_t now = time(NULL);
+ if (now == (time_t)-1)
+ {
+ DEBUG(D_any) debug_printf("failed to get time: %s\n", strerror(errno));
+ }
+ else if ((now - last_connection_time) >= inetd_wait_timeout)
+ {
+ DEBUG(D_any)
+ debug_printf("inetd wait timeout %d expired, ending daemon\n",
+ inetd_wait_timeout);
+ log_write(0, LOG_MAIN, "exim %s daemon terminating, inetd wait timeout reached.\n",
+ version_string);
+ daemon_die(); /* Does not return */
+ }
+ else
+ resignal_interval -= (now - last_connection_time);
+ }
+
+sigalrm_seen = FALSE;
+ALARM(resignal_interval);
+}
+
+
+/* Re-sort the qrunners list, and return the shortest interval.
+That could be negatime.
+The next-tick times should have been updated by any runs initiated,
+though will not be when the global limit on runners was reached.
+
+Unlikely to have many queues, so insertion-sort.
+*/
+
+static int
+next_qrunner_interval(void)
+{
+qrunner * sorted = NULL;
+for (qrunner * q = qrunners, * next; q; q = next)
+ {
+ next = q->next;
+ q->next = NULL;
+ if (sorted)
+ {
+ qrunner ** p = &sorted;
+ for (qrunner * qq; qq = *p; p = &qq->next)
+ if ( q->next_tick < qq->next_tick
+ || q->next_tick == qq->next_tick && q->interval < qq->interval
+ )
+ {
+ *p = q;
+ q->next = qq;
+ goto INSERTED;
+ }
+ *p = q;
+ INSERTED: ;
+ }
+ else
+ sorted = q;
+ }
+qrunners = sorted;
+return qrunners ? qrunners->next_tick - time(NULL) : 0;
+}
+
+/* See if we can do a queue run. If policy limit permit, kick one off.
+If both notification and timer events are present, handle the former
+and leave the timer outstanding.
+
+Return the number of seconds until the next due runner.
+*/
+
+static int
+daemon_qrun(int local_queue_run_max, struct pollfd * fd_polls, int listen_socket_count)
+{
+DEBUG(D_any) debug_printf("%s received\n",
+#ifndef DISABLE_QUEUE_RAMP
+ *queuerun_msgid ? "qrun notification" :
+#endif
+ "SIGALRM");
+
+/* Do a full queue run in a child process, if required, unless we already have
+enough queue runners on the go. If we are not running as root, a re-exec is
+required. In the calling process, restart the alamr timer for the next run. */
+
+if (is_multiple_qrun()) /* we are managing periodic runs */
+ if (local_queue_run_max <= 0 || queue_run_count < local_queue_run_max)
+ {
+ qrunner * q = NULL;
+
+#ifndef DISABLE_QUEUE_RAMP
+ /* If this is a triggered run for a specific message, see if we can start
+ another runner for this queue. */
+
+ if (*queuerun_msgid)
+ {
+ for (qrunner * qq = qrunners; qq; qq = qq->next)
+ if (qq->name == queuerun_msg_qname)
+ {
+ q = qq->run_count < qq->run_max ? qq : NULL;
+ break;
+ }
+ }
+ else
+#endif
+ /* Normal periodic runL in order of run priority, find the first queue
+ for which we can start a runner */
+
+ for (q = qrunners; q; q = q->next)
+ if (q->run_count < q->run_max) break;
+
+ if (q) /* found a queue to run */
+ {
+ pid_t pid;
+
+ /* Bump this queue's next-tick by it's interval */
+
+ if (q->interval)
+ {
+ time_t now = time(NULL);
+ do ; while ((q->next_tick += q->interval) <= now);
+ }
+
+ if ((pid = exim_fork(US"queue-runner")) == 0)
+ {
+ /* Disable debugging if it's required only for the daemon process. We
+ leave the above message, because it ties up with the "child ended"
+ debugging messages. */
+
+ if (f.debug_daemon) debug_selector = 0;
+
+ /* Close any open listening sockets in the child */
+
+ close_daemon_sockets(daemon_notifier_fd,
+ fd_polls, listen_socket_count);
+
+ /* Reset SIGHUP and SIGCHLD in the child in both cases. */
+
+ signal(SIGHUP, SIG_DFL);
+ signal(SIGCHLD, SIG_DFL);
+ signal(SIGTERM, SIG_DFL);
+ signal(SIGINT, SIG_DFL);
+
+ /* Re-exec if privilege has been given up, unless deliver_drop_
+ privilege is set. Reset SIGALRM before exec(). */
+
+ if (geteuid() != root_uid && !deliver_drop_privilege)
+ {
+ uschar opt[8];
+ uschar *p = opt;
+ uschar *extra[7];
+ int extracount = 1;
+
+ signal(SIGALRM, SIG_DFL);
+ queue_name = US"";
+
+ *p++ = '-';
+ *p++ = 'q';
+ if ( q->queue_2stage
+#ifndef DISABLE_QUEUE_RAMP
+ && !*queuerun_msgid
+#endif
+ ) *p++ = 'q';
+ if (q->queue_run_first_delivery) *p++ = 'i';
+ if (q->queue_run_force) *p++ = 'f';
+ if (q->deliver_force_thaw) *p++ = 'f';
+ if (q->queue_run_local) *p++ = 'l';
+ *p = 0;
+
+ extra[0] = q->name
+ ? string_sprintf("%sG%s", opt, q->name) : opt;
+
+#ifndef DISABLE_QUEUE_RAMP
+ if (*queuerun_msgid)
+ {
+ log_write(0, LOG_MAIN, "notify triggered queue run");
+ extra[extracount++] = queuerun_msgid; /* Trigger only the */
+ extra[extracount++] = queuerun_msgid; /* one message */
+ }
+#endif
+
+ /* If -R or -S were on the original command line, ensure they get
+ passed on. */
+
+ if (deliver_selectstring)
+ {
+ extra[extracount++] = f.deliver_selectstring_regex ? US"-Rr" : US"-R";
+ extra[extracount++] = deliver_selectstring;
+ }
+
+ if (deliver_selectstring_sender)
+ {
+ extra[extracount++] = f.deliver_selectstring_sender_regex
+ ? US"-Sr" : US"-S";
+ extra[extracount++] = deliver_selectstring_sender;
+ }
+
+ /* Overlay this process with a new execution. */
+
+ (void)child_exec_exim(CEE_EXEC_PANIC, FALSE, NULL, FALSE, extracount,
+ extra[0], extra[1], extra[2], extra[3], extra[4], extra[5], extra[6]);
+
+ /* Control never returns here. */
+ }
+
+ /* No need to re-exec; SIGALRM remains set to the default handler */
+
+#ifndef DISABLE_QUEUE_RAMP
+ if (*queuerun_msgid)
+ {
+ log_write(0, LOG_MAIN, "notify triggered queue run");
+ f.queue_2stage = FALSE;
+ queue_run(q, queuerun_msgid, queuerun_msgid, FALSE);
+ }
+ else
+#endif
+ queue_run(q, NULL, NULL, FALSE);
+ exim_underbar_exit(EXIT_SUCCESS);
+ }
+
+ if (pid < 0)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "daemon: fork of queue-runner "
+ "process failed: %s", strerror(errno));
+ log_close_all();
+ }
+ else
+ {
+ for (int i = 0; i < local_queue_run_max; ++i)
+ if (queue_runner_slots[i].pid <= 0)
+ {
+ queue_runner_slots[i].pid = pid;
+ queue_runner_slots[i].queue_name = q->name;
+ q->run_count++;
+ queue_run_count++;
+ break;
+ }
+ DEBUG(D_any) debug_printf("%d queue-runner process%s running\n",
+ queue_run_count, queue_run_count == 1 ? "" : "es");
+ }
+ }
+ }
+
+/* The queue run has been initiated (unless we were already running enough) */
+
+#ifndef DISABLE_QUEUE_RAMP
+if (*queuerun_msgid) /* it was a fast-ramp kick; dealt with */
+ *queuerun_msgid = 0;
+else /* periodic or one-time queue run */
+#endif
+ /* Set up next timer callback. Impose a minimum 1s tick,
+ even when a run was outstanding */
+ {
+ int interval = next_qrunner_interval();
+ if (interval <= 0) interval = 1;
+
+ sigalrm_seen = FALSE;
+ if (qrunners) /* there are still periodic qrunners */
+ {
+ ALARM(interval); /* set up next qrun tick */
+ return interval;
+ }
+ }
+return 0;
+}
+
+
+
+
+static const uschar *
+describe_queue_runners(void)
+{
+gstring * g = NULL;
+
+if (!is_multiple_qrun()) return US"no queue runs";
+
+for (qrunner * q = qrunners; q; q = q->next)
+ {
+ g = string_catn(g, US"-q", 2);
+ if (q->queue_2stage) g = string_catn(g, US"q", 1);
+ if (q->name) g = string_append(g, 3, US"G", q->name, US"/");
+ g = string_cat(g, readconf_printtime(q->interval));
+ g = string_catn(g, US" ", 1);
+ }
+gstring_trim(g, 1);
+gstring_release_unused(g);
+return string_from_gstring(g);