+#if defined(HAVE_ONDEMAND)
+
+/*
+ * 'add_ondemand_listener()' - Bind an open fd as a Listener.
+ */
+
+static void
+add_ondemand_listener(int fd, /* I - Socket file descriptor */
+ int idx) /* I - Listener number, for logging */
+{
+ cupsd_listener_t *lis; /* Listeners array */
+ http_addr_t addr; /* Address variable */
+ socklen_t addrlen; /* Length of address */
+ char s[256]; /* String addresss */
+
+ addrlen = sizeof(addr);
+
+ if (getsockname(fd, (struct sockaddr *)&addr, &addrlen))
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "service_checkin: Unable to get local address for listener #%d: %s",
+ idx + 1, strerror(errno));
+ return;
+ }
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "service_checkin: Listener #%d at fd %d, \"%s\".",
+ idx + 1, fd, httpAddrString(&addr, s, sizeof(s)));
+
+ /*
+ * Try to match the on-demand socket address to one of the listeners...
+ */
+
+ for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
+ lis;
+ lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
+ if (httpAddrEqual(&lis->address, &addr))
+ break;
+
+ /*
+ * Add a new listener If there's no match...
+ */
+
+ if (lis)
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "service_checkin: Matched existing listener #%d to %s.",
+ idx + 1, httpAddrString(&(lis->address), s, sizeof(s)));
+ }
+ else
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "service_checkin: Adding new listener #%d for %s.",
+ idx + 1, httpAddrString(&addr, s, sizeof(s)));
+
+ if ((lis = calloc(1, sizeof(cupsd_listener_t))) == NULL)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to allocate listener: %s.", strerror(errno));
+ exit(EXIT_FAILURE);
+ return;
+ }
+
+ cupsArrayAdd(Listeners, lis);
+
+ memcpy(&lis->address, &addr, sizeof(lis->address));
+ }
+
+ lis->fd = fd;
+ lis->on_demand = 1;
+
+# ifdef HAVE_SSL
+ if (httpAddrPort(&(lis->address)) == 443)
+ lis->encryption = HTTP_ENCRYPT_ALWAYS;
+# endif /* HAVE_SSL */
+}
+
+/*
+ * 'service_checkin()' - Check-in with launchd and collect the listening fds.
+ */
+
+static void
+service_checkin(void)
+{
+# ifdef HAVE_LAUNCH_ACTIVATE_SOCKET
+ int error; /* Check-in error, if any */
+ size_t i, /* Looping var */
+ count; /* Number of listeners */
+ int *ld_sockets; /* Listener sockets */
+
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: pid=%d", (int)getpid());
+
+ /*
+ * Check-in with launchd...
+ */
+
+ if ((error = launch_activate_socket("Listeners", &ld_sockets, &count)) != 0)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to get listener sockets: %s", strerror(error));
+ exit(EXIT_FAILURE);
+ return; /* anti-compiler-warning */
+ }
+
+ /*
+ * Try to match the launchd sockets to the cupsd listeners...
+ */
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: %d listeners.", (int)count);
+
+ for (i = 0; i < count; i ++)
+ {
+ add_ondemand_listener(ld_sockets[i], i);
+ }
+
+ free(ld_sockets);
+
+# elif defined(HAVE_LAUNCHD)
+ size_t i, /* Looping var */
+ count; /* Number of listeners */
+ launch_data_t ld_msg, /* Launch data message */
+ ld_resp, /* Launch data response */
+ ld_array, /* Launch data array */
+ ld_sockets, /* Launch data sockets dictionary */
+ tmp; /* Launch data */
+ int fd; /* File descriptor */
+
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: pid=%d", (int)getpid());
+
+ /*
+ * Check-in with launchd...
+ */
+
+ ld_msg = launch_data_new_string(LAUNCH_KEY_CHECKIN);
+ if ((ld_resp = launch_msg(ld_msg)) == NULL)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "service_checkin: launch_msg(\"" LAUNCH_KEY_CHECKIN
+ "\") IPC failure");
+ exit(EXIT_FAILURE);
+ return; /* anti-compiler-warning */
+ }
+
+ if (launch_data_get_type(ld_resp) == LAUNCH_DATA_ERRNO)
+ {
+ errno = launch_data_get_errno(ld_resp);
+ cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Check-in failed: %s",
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ return; /* anti-compiler-warning */
+ }
+
+ /*
+ * Get the sockets dictionary...
+ */
+
+ if ((ld_sockets = launch_data_dict_lookup(ld_resp, LAUNCH_JOBKEY_SOCKETS))
+ == NULL)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "service_checkin: No sockets found to answer requests on.");
+ exit(EXIT_FAILURE);
+ return; /* anti-compiler-warning */
+ }
+
+ /*
+ * Get the array of listener sockets...
+ */
+
+ if ((ld_array = launch_data_dict_lookup(ld_sockets, "Listeners")) == NULL)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "service_checkin: No sockets found to answer requests on.");
+ exit(EXIT_FAILURE);
+ return; /* anti-compiler-warning */
+ }
+
+ /*
+ * Add listening fd(s) to the Listener array...
+ */
+
+ if (launch_data_get_type(ld_array) == LAUNCH_DATA_ARRAY)
+ {
+ count = launch_data_array_get_count(ld_array);
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: %d listeners.", (int)count);
+
+ for (i = 0; i < count; i ++)
+ {
+ /*
+ * Get the launchd file descriptor and address...
+ */
+
+ if ((tmp = launch_data_array_get_index(ld_array, i)) != NULL)
+ {
+ fd = launch_data_get_fd(tmp);
+ add_ondemand_listener(fd, i);
+ }
+ }
+ }
+
+ launch_data_free(ld_msg);
+ launch_data_free(ld_resp);
+
+# else /* HAVE_SYSTEMD */
+ int i, /* Looping var */
+ count; /* Number of listeners */
+
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: pid=%d", (int)getpid());
+
+ /*
+ * Check-in with systemd...
+ */
+
+ if ((count = sd_listen_fds(0)) < 0)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to get listener sockets: %s", strerror(-count));
+ exit(EXIT_FAILURE);
+ return; /* anti-compiler-warning */
+ }
+
+ /*
+ * Try to match the systemd sockets to the cupsd listeners...
+ */
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: %d listeners.", count);
+
+ for (i = 0; i < count; i ++)
+ {
+ add_ondemand_listener(SD_LISTEN_FDS_START + i, i);
+ }
+# endif /* HAVE_LAUNCH_ACTIVATE_SOCKET */
+}
+
+
+/*
+ * 'service_checkout()' - Update the CUPS_KEEPALIVE file as needed.
+ */
+
+static void
+service_checkout(void)
+{
+ int fd; /* File descriptor */
+
+
+ /*
+ * Create or remove the "keep-alive" file based on whether there are active
+ * jobs or shared printers to advertise...
+ */
+
+ if (cupsArrayCount(ActiveJobs) || /* Active jobs */
+ WebInterface || /* Web interface enabled */
+ (Browsing && BrowseLocalProtocols && cupsArrayCount(Printers)))
+ /* Printers being shared */
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Creating keep-alive file \"" CUPS_KEEPALIVE "\".");
+
+ if ((fd = open(CUPS_KEEPALIVE, O_RDONLY | O_CREAT | O_EXCL, S_IRUSR)) >= 0)
+ close(fd);
+ }
+ else
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Removing keep-alive file \"" CUPS_KEEPALIVE "\".");
+
+ unlink(CUPS_KEEPALIVE);
+ }
+}
+#endif /* HAVE_ONDEMAND */
+
+