2 * "$Id: listen.c 7918 2008-09-08 22:03:01Z mike $"
4 * Server listening routines for the Common UNIX Printing System (CUPS)
7 * Copyright 2007-2009 by Apple Inc.
8 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
18 * cupsdDeleteAllListeners() - Delete all listeners.
19 * cupsdPauseListening() - Clear input polling on all listening sockets...
20 * cupsdResumeListening() - Set input polling on all listening sockets...
21 * cupsdStartListening() - Create all listening sockets...
22 * cupsdStopListening() - Close all listening sockets...
26 * Include necessary headers...
33 * Make sure the IPV6_V6ONLY is defined on Linux - older versions of
34 * glibc don't define it even if the kernel supports it...
37 #if defined(__linux) && !defined(IPV6_V6ONLY)
38 # define IPV6_V6ONLY 26
39 #endif /* __linux && !IPV6_V6ONLY */
43 * 'cupsdDeleteAllListeners()' - Delete all listeners.
47 cupsdDeleteAllListeners(void)
49 cupsd_listener_t
*lis
; /* Current listening socket */
52 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
54 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
57 cupsArrayDelete(Listeners
);
63 * 'cupsdPauseListening()' - Clear input polling on all listening sockets...
67 cupsdPauseListening(void)
69 cupsd_listener_t
*lis
; /* Current listening socket */
72 if (cupsArrayCount(Listeners
) < 1)
75 if (cupsArrayCount(Clients
) == MaxClients
)
76 cupsdLogMessage(CUPSD_LOG_WARN
,
77 "Max clients reached, holding new connections...");
78 else if (errno
== ENFILE
|| errno
== EMFILE
)
79 cupsdLogMessage(CUPSD_LOG_WARN
,
80 "Too many open files, holding new connections for "
83 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdPauseListening: Clearing input bits...");
85 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
87 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
88 cupsdRemoveSelect(lis
->fd
);
90 ListeningPaused
= time(NULL
) + 30;
95 * 'cupsdResumeListening()' - Set input polling on all listening sockets...
99 cupsdResumeListening(void)
101 cupsd_listener_t
*lis
; /* Current listening socket */
104 if (cupsArrayCount(Listeners
) < 1)
107 cupsdLogMessage(CUPSD_LOG_INFO
, "Resuming new connection processing...");
108 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
109 "cupsdResumeListening: Setting input bits...");
111 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
113 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
114 cupsdAddSelect(lis
->fd
, (cupsd_selfunc_t
)cupsdAcceptClient
, NULL
, lis
);
121 * 'cupsdStartListening()' - Create all listening sockets...
125 cupsdStartListening(void)
127 int status
; /* Bind result */
128 int p
, /* Port number */
129 val
; /* Parameter value */
130 cupsd_listener_t
*lis
; /* Current listening socket */
131 char s
[256]; /* String addresss */
132 const char *have_domain
; /* Have a domain socket? */
133 static const char * const encryptions
[] =
134 { /* Encryption values */
142 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartListening: %d Listeners",
143 cupsArrayCount(Listeners
));
146 * Setup socket listeners...
149 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
), LocalPort
= 0,
152 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
154 httpAddrString(&(lis
->address
), s
, sizeof(s
));
157 if (lis
->address
.addr
.sa_family
== AF_INET6
)
158 p
= ntohs(lis
->address
.ipv6
.sin6_port
);
160 #endif /* AF_INET6 */
162 if (lis
->address
.addr
.sa_family
== AF_LOCAL
)
165 #endif /* AF_LOCAL */
166 p
= ntohs(lis
->address
.ipv4
.sin_port
);
169 * If needed, create a socket for listening...
175 * Create a socket for listening...
178 lis
->fd
= socket(lis
->address
.addr
.sa_family
, SOCK_STREAM
, 0);
182 cupsdLogMessage(CUPSD_LOG_ERROR
,
183 "Unable to open listen socket for address %s:%d - %s.",
184 s
, p
, strerror(errno
));
188 * IPv6 is often disabled while DNS returns IPv6 addresses...
191 if (lis
->address
.addr
.sa_family
!= AF_INET6
&&
192 (FatalErrors
& CUPSD_FATAL_LISTEN
))
193 cupsdEndProcess(getpid(), 0);
195 if (FatalErrors
& CUPSD_FATAL_LISTEN
)
196 cupsdEndProcess(getpid(), 0);
197 #endif /* AF_INET6 */
203 * Set things up to reuse the local address for this port.
208 setsockopt(lis
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
210 setsockopt(lis
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
214 * Bind to the port we found...
218 if (lis
->address
.addr
.sa_family
== AF_INET6
)
222 * Accept only IPv6 connections on this socket, to avoid
223 * potential security issues and to make all platforms behave
229 setsockopt(lis
->fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, (char *)&val
, sizeof(val
));
231 setsockopt(lis
->fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &val
, sizeof(val
));
233 # endif /* IPV6_V6ONLY */
235 status
= bind(lis
->fd
, (struct sockaddr
*)&(lis
->address
),
236 httpAddrLength(&(lis
->address
)));
239 #endif /* AF_INET6 */
241 if (lis
->address
.addr
.sa_family
== AF_LOCAL
)
243 mode_t mask
; /* Umask setting */
247 * Remove any existing domain socket file...
250 unlink(lis
->address
.un
.sun_path
);
253 * Save the curent umask and set it to 0...
259 * Bind the domain socket...
262 status
= bind(lis
->fd
, (struct sockaddr
*)&(lis
->address
),
263 httpAddrLength(&(lis
->address
)));
266 * Restore the umask...
272 #endif /* AF_LOCAL */
273 status
= bind(lis
->fd
, (struct sockaddr
*)&(lis
->address
),
274 sizeof(lis
->address
.ipv4
));
278 cupsdLogMessage(CUPSD_LOG_ERROR
,
279 "Unable to bind socket for address %s:%d - %s.",
280 s
, p
, strerror(errno
));
284 if (FatalErrors
& CUPSD_FATAL_LISTEN
)
285 cupsdEndProcess(getpid(), 0);
291 * Listen for new clients.
294 if (listen(lis
->fd
, ListenBackLog
) < 0)
296 cupsdLogMessage(CUPSD_LOG_ERROR
,
297 "Unable to listen for clients on address %s:%d - %s.",
298 s
, p
, strerror(errno
));
303 if (FatalErrors
& CUPSD_FATAL_LISTEN
)
304 cupsdEndProcess(getpid(), 0);
310 fcntl(lis
->fd
, F_SETFD
, fcntl(lis
->fd
, F_GETFD
) | FD_CLOEXEC
);
313 cupsdLogMessage(CUPSD_LOG_INFO
, "Listening to %s:%d on fd %d...",
317 cupsdLogMessage(CUPSD_LOG_INFO
, "Listening to %s on fd %d...",
320 if (chmod(s
, 0140777))
321 cupsdLogMessage(CUPSD_LOG_ERROR
,
322 "Unable to change permisssions on domain socket "
323 "\"%s\" - %s", s
, strerror(errno
));
327 * Save the first port that is bound to the local loopback or
331 if ((!LocalPort
|| LocalEncryption
== HTTP_ENCRYPT_ALWAYS
) && p
> 0 &&
332 (httpAddrLocalhost(&(lis
->address
)) ||
333 httpAddrAny(&(lis
->address
))))
336 LocalEncryption
= lis
->encryption
;
340 if (lis
->address
.addr
.sa_family
== AF_LOCAL
&& !have_domain
)
341 have_domain
= lis
->address
.un
.sun_path
;
342 #endif /* AF_LOCAL */
346 * Make sure that we are listening on localhost!
349 if (!LocalPort
&& !have_domain
)
351 cupsdLogMessage(CUPSD_LOG_EMERG
,
352 "No Listen or Port lines were found to allow access via "
355 if (FatalErrors
& (CUPSD_FATAL_CONFIG
| CUPSD_FATAL_LISTEN
))
356 cupsdEndProcess(getpid(), 0);
360 * Set the CUPS_SERVER, IPP_PORT, and CUPS_ENCRYPTION variables based on
367 * Use domain sockets for the local connection...
370 cupsdSetEnv("CUPS_SERVER", have_domain
);
372 LocalEncryption
= HTTP_ENCRYPT_IF_REQUESTED
;
377 * Use the default local loopback address for the server...
380 cupsdSetEnv("CUPS_SERVER", "localhost");
383 cupsdSetEnv("CUPS_ENCRYPTION", encryptions
[LocalEncryption
]);
386 cupsdSetEnvf("IPP_PORT", "%d", LocalPort
);
389 * Resume listening for connections...
392 cupsdResumeListening();
397 * 'cupsdStopListening()' - Close all listening sockets...
401 cupsdStopListening(void)
403 cupsd_listener_t
*lis
; /* Current listening socket */
406 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
407 "cupsdStopListening: closing all listen sockets.");
409 cupsdPauseListening();
411 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
413 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
418 closesocket(lis
->fd
);
425 * Remove domain sockets...
428 # ifdef HAVE_LAUNCH_H
429 if (lis
->address
.addr
.sa_family
== AF_LOCAL
&& !Launchd
)
431 if (lis
->address
.addr
.sa_family
== AF_LOCAL
)
432 # endif /* HAVE_LAUNCH_H */
433 unlink(lis
->address
.un
.sun_path
);
434 #endif /* AF_LOCAL */
441 * End of "$Id: listen.c 7918 2008-09-08 22:03:01Z mike $".