X-Git-Url: http://git.ipfire.org/?a=blobdiff_plain;f=scheduler%2Flisten.c;h=06e2ed6aac081af4565074ed99cee995e18392c3;hb=503b54c9302c8de6207e079a80a89a787eb612ea;hp=2b4f9e0fdea16c868d29185ad8dd6fdaf064398e;hpb=d1c13e168660dfc384ead2efe29eb20a7abc5950;p=thirdparty%2Fcups.git diff --git a/scheduler/listen.c b/scheduler/listen.c index 2b4f9e0fd..06e2ed6aa 100644 --- a/scheduler/listen.c +++ b/scheduler/listen.c @@ -1,25 +1,14 @@ /* - * "$Id: listen.c 7918 2008-09-08 22:03:01Z mike $" + * Server listening routines for the CUPS scheduler. * - * Server listening routines for the Common UNIX Printing System (CUPS) - * scheduler. + * Copyright 2007-2014 by Apple Inc. + * Copyright 1997-2006 by Easy Software Products, all rights reserved. * - * Copyright 2007-2009 by Apple Inc. - * Copyright 1997-2006 by Easy Software Products, all rights reserved. - * - * These coded instructions, statements, and computer programs are the - * property of Apple Inc. and are protected by Federal copyright - * law. Distribution and use rights are outlined in the file "LICENSE.txt" - * which should have been included with this file. If this file is - * file is missing or damaged, see the license at "http://www.cups.org/". - * - * Contents: - * - * cupsdDeleteAllListeners() - Delete all listeners. - * cupsdPauseListening() - Clear input polling on all listening sockets... - * cupsdResumeListening() - Set input polling on all listening sockets... - * cupsdStartListening() - Create all listening sockets... - * cupsdStopListening() - Close all listening sockets... + * These coded instructions, statements, and computer programs are the + * property of Apple Inc. and are protected by Federal copyright + * law. Distribution and use rights are outlined in the file "LICENSE.txt" + * which should have been included with this file. If this file is + * file is missing or damaged, see the license at "http://www.cups.org/". */ /* @@ -52,10 +41,19 @@ cupsdDeleteAllListeners(void) for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners); lis; lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) - free(lis); +#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) + if (!lis->on_demand) +#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ + { + cupsArrayRemove(Listeners, lis); + free(lis); + } - cupsArrayDelete(Listeners); - Listeners = NULL; + if (cupsArrayCount(Listeners) == 0) + { + cupsArrayDelete(Listeners); + Listeners = NULL; + } } @@ -124,9 +122,7 @@ cupsdResumeListening(void) void cupsdStartListening(void) { - int status; /* Bind result */ - int p, /* Port number */ - val; /* Parameter value */ + int p; /* Port number */ cupsd_listener_t *lis; /* Current listening socket */ char s[256]; /* String addresss */ const char *have_domain; /* Have a domain socket? */ @@ -152,18 +148,7 @@ cupsdStartListening(void) lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) { httpAddrString(&(lis->address), s, sizeof(s)); - -#ifdef AF_INET6 - if (lis->address.addr.sa_family == AF_INET6) - p = ntohs(lis->address.ipv6.sin6_port); - else -#endif /* AF_INET6 */ -#ifdef AF_LOCAL - if (lis->address.addr.sa_family == AF_LOCAL) - p = 0; - else -#endif /* AF_LOCAL */ - p = ntohs(lis->address.ipv4.sin_port); + p = httpAddrPort(&(lis->address)); /* * If needed, create a socket for listening... @@ -175,7 +160,7 @@ cupsdStartListening(void) * Create a socket for listening... */ - lis->fd = socket(lis->address.addr.sa_family, SOCK_STREAM, 0); + lis->fd = httpAddrListen(&(lis->address), p); if (lis->fd == -1) { @@ -198,131 +183,15 @@ cupsdStartListening(void) continue; } - - /* - * Set things up to reuse the local address for this port. - */ - - val = 1; -#ifdef __sun - setsockopt(lis->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val)); -#else - setsockopt(lis->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); -#endif /* __sun */ - - /* - * Bind to the port we found... - */ - -#ifdef AF_INET6 - if (lis->address.addr.sa_family == AF_INET6) - { -# ifdef IPV6_V6ONLY - /* - * Accept only IPv6 connections on this socket, to avoid - * potential security issues and to make all platforms behave - * the same. - */ - - val = 1; -# ifdef __sun - setsockopt(lis->fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&val, sizeof(val)); -# else - setsockopt(lis->fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val)); -# endif /* __sun */ -# endif /* IPV6_V6ONLY */ - - status = bind(lis->fd, (struct sockaddr *)&(lis->address), - httpAddrLength(&(lis->address))); - } - else -#endif /* AF_INET6 */ -#ifdef AF_LOCAL - if (lis->address.addr.sa_family == AF_LOCAL) - { - mode_t mask; /* Umask setting */ - - - /* - * Remove any existing domain socket file... - */ - - unlink(lis->address.un.sun_path); - - /* - * Save the curent umask and set it to 0... - */ - - mask = umask(0); - - /* - * Bind the domain socket... - */ - - status = bind(lis->fd, (struct sockaddr *)&(lis->address), - httpAddrLength(&(lis->address))); - - /* - * Restore the umask... - */ - - umask(mask); - } - else -#endif /* AF_LOCAL */ - status = bind(lis->fd, (struct sockaddr *)&(lis->address), - sizeof(lis->address.ipv4)); - - if (status < 0) - { - cupsdLogMessage(CUPSD_LOG_ERROR, - "Unable to bind socket for address %s:%d - %s.", - s, p, strerror(errno)); - close(lis->fd); - lis->fd = -1; - - if (FatalErrors & CUPSD_FATAL_LISTEN) - cupsdEndProcess(getpid(), 0); - - continue; - } - - /* - * Listen for new clients. - */ - - if (listen(lis->fd, ListenBackLog) < 0) - { - cupsdLogMessage(CUPSD_LOG_ERROR, - "Unable to listen for clients on address %s:%d - %s.", - s, p, strerror(errno)); - - close(lis->fd); - lis->fd = -1; - - if (FatalErrors & CUPSD_FATAL_LISTEN) - cupsdEndProcess(getpid(), 0); - - continue; - } } - fcntl(lis->fd, F_SETFD, fcntl(lis->fd, F_GETFD) | FD_CLOEXEC); - if (p) cupsdLogMessage(CUPSD_LOG_INFO, "Listening to %s:%d on fd %d...", s, p, lis->fd); else - { cupsdLogMessage(CUPSD_LOG_INFO, "Listening to %s on fd %d...", s, lis->fd); - if (chmod(s, 0140777)) - cupsdLogMessage(CUPSD_LOG_ERROR, - "Unable to change permisssions on domain socket " - "\"%s\" - %s", s, strerror(errno)); - } - /* * Save the first port that is bound to the local loopback or * "any" address... @@ -350,7 +219,7 @@ cupsdStartListening(void) { cupsdLogMessage(CUPSD_LOG_EMERG, "No Listen or Port lines were found to allow access via " - "localhost!"); + "localhost."); if (FatalErrors & (CUPSD_FATAL_CONFIG | CUPSD_FATAL_LISTEN)) cupsdEndProcess(getpid(), 0); @@ -412,31 +281,19 @@ cupsdStopListening(void) lis; lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) { - if (lis->fd != -1) +#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) + if (!lis->on_demand && lis->fd != -1) { -#ifdef WIN32 - closesocket(lis->fd); -#else - close(lis->fd); -#endif /* WIN32 */ - -#ifdef AF_LOCAL - /* - * Remove domain sockets... - */ + httpAddrClose(&(lis->address), lis->fd); + lis->fd = -1; + } -# ifdef HAVE_LAUNCH_H - if (lis->address.addr.sa_family == AF_LOCAL && !Launchd) -# else - if (lis->address.addr.sa_family == AF_LOCAL) -# endif /* HAVE_LAUNCH_H */ - unlink(lis->address.un.sun_path); -#endif /* AF_LOCAL */ +#else + if (lis->fd != -1) + { + httpAddrClose(&(lis->address), lis->fd); + lis->fd = -1; } +#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ } } - - -/* - * End of "$Id: listen.c 7918 2008-09-08 22:03:01Z mike $". - */