-#ifdef HAVE_LAUNCHD
-/*
- * 'launchd_checkin()' - Check-in with launchd and collect the listening fds.
- */
-
-static void
-launchd_checkin(void)
-{
- size_t i, /* Looping var */
- count; /* Numebr of listeners */
- int portnum; /* Port number */
- 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 */
- cupsd_listener_t *lis; /* Listeners array */
- http_addr_t addr; /* Address variable */
- socklen_t addrlen; /* Length of address */
- int fd; /* File descriptor */
- char s[256]; /* String addresss */
-
-
- cupsdLogMessage(CUPSD_LOG_DEBUG, "launchd_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,
- "launchd_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, "launchd_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,
- "launchd_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,
- "launchd_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);
-
- 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);
- addrlen = sizeof(addr);
-
- if (getsockname(fd, (struct sockaddr *)&addr, &addrlen))
- {
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "launchd_checkin: Unable to get local address - %s",
- strerror(errno));
- continue;
- }
-
- /*
- * Try to match the launchd 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,
- "launchd_checkin: Matched existing listener %s with fd %d...",
- httpAddrString(&(lis->address), s, sizeof(s)), fd);
- }
- else
- {
- cupsdLogMessage(CUPSD_LOG_DEBUG,
- "launchd_checkin: Adding new listener %s with fd %d...",
- httpAddrString(&addr, s, sizeof(s)), fd);
-
- if ((lis = calloc(1, sizeof(cupsd_listener_t))) == NULL)
- {
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "launchd_checkin: Unable to allocate listener - %s.",
- strerror(errno));
- exit(EXIT_FAILURE);
- }
-
- cupsArrayAdd(Listeners, lis);
-
- memcpy(&lis->address, &addr, sizeof(lis->address));
- }
-
- lis->fd = fd;
-
-# ifdef HAVE_SSL
- portnum = 0;
-
-# ifdef AF_INET6
- if (lis->address.addr.sa_family == AF_INET6)
- portnum = ntohs(lis->address.ipv6.sin6_port);
- else
-# endif /* AF_INET6 */
- if (lis->address.addr.sa_family == AF_INET)
- portnum = ntohs(lis->address.ipv4.sin_port);
-
- if (portnum == 443)
- lis->encryption = HTTP_ENCRYPT_ALWAYS;
-# endif /* HAVE_SSL */
- }
- }
- }
-
- launch_data_free(ld_msg);
- launch_data_free(ld_resp);
-}
-
-
-/*
- * 'launchd_checkout()' - Update the launchd KeepAlive file as needed.
- */
-
-static void
-launchd_checkout(void)
-{
- int fd; /* File descriptor */
-
-
- /*
- * Create or remove the launchd KeepAlive file based on whether
- * there are active jobs, polling, browsing for remote printers or
- * shared printers to advertise...
- */
-
- if ((cupsArrayCount(ActiveJobs) || NumPolled ||
- (Browsing &&
- (BrowseRemoteProtocols ||
- (BrowseLocalProtocols && NumBrowsers && cupsArrayCount(Printers))))))
- {
- cupsdLogMessage(CUPSD_LOG_DEBUG,
- "Creating launchd keepalive 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 launchd keepalive file \"" CUPS_KEEPALIVE "\"...");
-
- unlink(CUPS_KEEPALIVE);
- }
-}
-#endif /* HAVE_LAUNCHD */
-
-