2 * "$Id: listen.c 7673 2008-06-18 22:31:26Z mike $"
4 * Server listening routines for the Common UNIX Printing System (CUPS)
7 * Copyright 2007 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 * Get the server's IP address...
150 httpAddrFreeList(ServerAddrs
);
152 if ((ServerAddrs
= httpAddrGetList(ServerName
, AF_UNSPEC
, NULL
)) == NULL
)
153 cupsdLogMessage(CUPSD_LOG_ERROR
,
154 "Unable to find IP address for server name \"%s\"!",
158 * Setup socket listeners...
161 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
), LocalPort
= 0,
164 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
166 httpAddrString(&(lis
->address
), s
, sizeof(s
));
169 if (lis
->address
.addr
.sa_family
== AF_INET6
)
170 p
= ntohs(lis
->address
.ipv6
.sin6_port
);
172 #endif /* AF_INET6 */
174 if (lis
->address
.addr
.sa_family
== AF_LOCAL
)
177 #endif /* AF_LOCAL */
178 p
= ntohs(lis
->address
.ipv4
.sin_port
);
181 * If needed, create a socket for listening...
187 * Create a socket for listening...
190 lis
->fd
= socket(lis
->address
.addr
.sa_family
, SOCK_STREAM
, 0);
194 cupsdLogMessage(CUPSD_LOG_ERROR
,
195 "Unable to open listen socket for address %s:%d - %s.",
196 s
, p
, strerror(errno
));
201 * Set things up to reuse the local address for this port.
206 setsockopt(lis
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
208 setsockopt(lis
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
212 * Bind to the port we found...
216 if (lis
->address
.addr
.sa_family
== AF_INET6
)
220 * Accept only IPv6 connections on this socket, to avoid
221 * potential security issues and to make all platforms behave
227 setsockopt(lis
->fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, (char *)&val
, sizeof(val
));
229 setsockopt(lis
->fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &val
, sizeof(val
));
231 # endif /* IPV6_V6ONLY */
233 status
= bind(lis
->fd
, (struct sockaddr
*)&(lis
->address
),
234 httpAddrLength(&(lis
->address
)));
237 #endif /* AF_INET6 */
239 if (lis
->address
.addr
.sa_family
== AF_LOCAL
)
241 mode_t mask
; /* Umask setting */
245 * Remove any existing domain socket file...
248 unlink(lis
->address
.un
.sun_path
);
251 * Save the curent umask and set it to 0...
257 * Bind the domain socket...
260 status
= bind(lis
->fd
, (struct sockaddr
*)&(lis
->address
),
261 httpAddrLength(&(lis
->address
)));
264 * Restore the umask...
270 #endif /* AF_LOCAL */
271 status
= bind(lis
->fd
, (struct sockaddr
*)&(lis
->address
),
272 sizeof(lis
->address
.ipv4
));
276 cupsdLogMessage(CUPSD_LOG_ERROR
,
277 "Unable to bind socket for address %s:%d - %s.",
278 s
, p
, strerror(errno
));
285 * Listen for new clients.
288 if (listen(lis
->fd
, ListenBackLog
) < 0)
290 cupsdLogMessage(CUPSD_LOG_ERROR
,
291 "Unable to listen for clients on address %s:%d - %s.",
292 s
, p
, strerror(errno
));
297 fcntl(lis
->fd
, F_SETFD
, fcntl(lis
->fd
, F_GETFD
) | FD_CLOEXEC
);
300 cupsdLogMessage(CUPSD_LOG_INFO
, "Listening to %s:%d on fd %d...",
304 cupsdLogMessage(CUPSD_LOG_INFO
, "Listening to %s on fd %d...",
307 if (chmod(s
, 0140777))
308 cupsdLogMessage(CUPSD_LOG_ERROR
,
309 "Unable to change permisssions on domain socket "
310 "\"%s\" - %s", s
, strerror(errno
));
314 * Save the first port that is bound to the local loopback or
318 if ((!LocalPort
|| LocalEncryption
== HTTP_ENCRYPT_ALWAYS
) && p
> 0 &&
319 (httpAddrLocalhost(&(lis
->address
)) ||
320 httpAddrAny(&(lis
->address
))))
323 LocalEncryption
= lis
->encryption
;
327 if (lis
->address
.addr
.sa_family
== AF_LOCAL
&& !have_domain
)
328 have_domain
= lis
->address
.un
.sun_path
;
329 #endif /* AF_LOCAL */
333 * Make sure that we are listening on localhost!
336 if (!LocalPort
&& !have_domain
)
338 cupsdLogMessage(CUPSD_LOG_EMERG
,
339 "No Listen or Port lines were found to allow access via "
346 cupsdEndProcess(getpid(), 0);
350 * Set the CUPS_SERVER, IPP_PORT, and CUPS_ENCRYPTION variables based on
357 * Use domain sockets for the local connection...
360 cupsdSetEnv("CUPS_SERVER", have_domain
);
362 LocalEncryption
= HTTP_ENCRYPT_IF_REQUESTED
;
367 * Use the default local loopback address for the server...
370 cupsdSetEnv("CUPS_SERVER", "localhost");
373 cupsdSetEnv("CUPS_ENCRYPTION", encryptions
[LocalEncryption
]);
376 cupsdSetEnvf("IPP_PORT", "%d", LocalPort
);
379 * Resume listening for connections...
382 cupsdResumeListening();
387 * 'cupsdStopListening()' - Close all listening sockets...
391 cupsdStopListening(void)
393 cupsd_listener_t
*lis
; /* Current listening socket */
396 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
397 "cupsdStopListening: closing all listen sockets.");
399 cupsdPauseListening();
401 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
403 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
408 closesocket(lis
->fd
);
415 * Remove domain sockets...
418 # ifdef HAVE_LAUNCH_H
419 if (lis
->address
.addr
.sa_family
== AF_LOCAL
&& !Launchd
)
421 if (lis
->address
.addr
.sa_family
== AF_LOCAL
)
422 # endif /* HAVE_LAUNCH_H */
423 unlink(lis
->address
.un
.sun_path
);
424 #endif /* AF_LOCAL */
431 * End of "$Id: listen.c 7673 2008-06-18 22:31:26Z mike $".