]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/listen.c
Merge changes from CUPS 1.4svn-r7874.
[thirdparty/cups.git] / scheduler / listen.c
1 /*
2 * "$Id: listen.c 7673 2008-06-18 22:31:26Z mike $"
3 *
4 * Server listening routines for the Common UNIX Printing System (CUPS)
5 * scheduler.
6 *
7 * Copyright 2007 by Apple Inc.
8 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
9 *
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/".
15 *
16 * Contents:
17 *
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...
23 */
24
25 /*
26 * Include necessary headers...
27 */
28
29 #include "cupsd.h"
30
31
32 /*
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...
35 */
36
37 #if defined(__linux) && !defined(IPV6_V6ONLY)
38 # define IPV6_V6ONLY 26
39 #endif /* __linux && !IPV6_V6ONLY */
40
41
42 /*
43 * 'cupsdDeleteAllListeners()' - Delete all listeners.
44 */
45
46 void
47 cupsdDeleteAllListeners(void)
48 {
49 cupsd_listener_t *lis; /* Current listening socket */
50
51
52 for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
53 lis;
54 lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
55 free(lis);
56
57 cupsArrayDelete(Listeners);
58 Listeners = NULL;
59 }
60
61
62 /*
63 * 'cupsdPauseListening()' - Clear input polling on all listening sockets...
64 */
65
66 void
67 cupsdPauseListening(void)
68 {
69 cupsd_listener_t *lis; /* Current listening socket */
70
71
72 if (cupsArrayCount(Listeners) < 1)
73 return;
74
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 "
81 "30 seconds...");
82
83 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdPauseListening: Clearing input bits...");
84
85 for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
86 lis;
87 lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
88 cupsdRemoveSelect(lis->fd);
89
90 ListeningPaused = time(NULL) + 30;
91 }
92
93
94 /*
95 * 'cupsdResumeListening()' - Set input polling on all listening sockets...
96 */
97
98 void
99 cupsdResumeListening(void)
100 {
101 cupsd_listener_t *lis; /* Current listening socket */
102
103
104 if (cupsArrayCount(Listeners) < 1)
105 return;
106
107 cupsdLogMessage(CUPSD_LOG_INFO, "Resuming new connection processing...");
108 cupsdLogMessage(CUPSD_LOG_DEBUG2,
109 "cupsdResumeListening: Setting input bits...");
110
111 for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
112 lis;
113 lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
114 cupsdAddSelect(lis->fd, (cupsd_selfunc_t)cupsdAcceptClient, NULL, lis);
115
116 ListeningPaused = 0;
117 }
118
119
120 /*
121 * 'cupsdStartListening()' - Create all listening sockets...
122 */
123
124 void
125 cupsdStartListening(void)
126 {
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 */
135 "IfRequested",
136 "Never",
137 "Required",
138 "Always"
139 };
140
141
142 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdStartListening: %d Listeners",
143 cupsArrayCount(Listeners));
144
145 /*
146 * Get the server's IP address...
147 */
148
149 if (ServerAddrs)
150 httpAddrFreeList(ServerAddrs);
151
152 if ((ServerAddrs = httpAddrGetList(ServerName, AF_UNSPEC, NULL)) == NULL)
153 cupsdLogMessage(CUPSD_LOG_ERROR,
154 "Unable to find IP address for server name \"%s\"!",
155 ServerName);
156
157 /*
158 * Setup socket listeners...
159 */
160
161 for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners), LocalPort = 0,
162 have_domain = NULL;
163 lis;
164 lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
165 {
166 httpAddrString(&(lis->address), s, sizeof(s));
167
168 #ifdef AF_INET6
169 if (lis->address.addr.sa_family == AF_INET6)
170 p = ntohs(lis->address.ipv6.sin6_port);
171 else
172 #endif /* AF_INET6 */
173 #ifdef AF_LOCAL
174 if (lis->address.addr.sa_family == AF_LOCAL)
175 p = 0;
176 else
177 #endif /* AF_LOCAL */
178 p = ntohs(lis->address.ipv4.sin_port);
179
180 /*
181 * If needed, create a socket for listening...
182 */
183
184 if (lis->fd == -1)
185 {
186 /*
187 * Create a socket for listening...
188 */
189
190 lis->fd = socket(lis->address.addr.sa_family, SOCK_STREAM, 0);
191
192 if (lis->fd == -1)
193 {
194 cupsdLogMessage(CUPSD_LOG_ERROR,
195 "Unable to open listen socket for address %s:%d - %s.",
196 s, p, strerror(errno));
197 continue;
198 }
199
200 /*
201 * Set things up to reuse the local address for this port.
202 */
203
204 val = 1;
205 #ifdef __sun
206 setsockopt(lis->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
207 #else
208 setsockopt(lis->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
209 #endif /* __sun */
210
211 /*
212 * Bind to the port we found...
213 */
214
215 #ifdef AF_INET6
216 if (lis->address.addr.sa_family == AF_INET6)
217 {
218 # ifdef IPV6_V6ONLY
219 /*
220 * Accept only IPv6 connections on this socket, to avoid
221 * potential security issues and to make all platforms behave
222 * the same.
223 */
224
225 val = 1;
226 # ifdef __sun
227 setsockopt(lis->fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&val, sizeof(val));
228 # else
229 setsockopt(lis->fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
230 # endif /* __sun */
231 # endif /* IPV6_V6ONLY */
232
233 status = bind(lis->fd, (struct sockaddr *)&(lis->address),
234 httpAddrLength(&(lis->address)));
235 }
236 else
237 #endif /* AF_INET6 */
238 #ifdef AF_LOCAL
239 if (lis->address.addr.sa_family == AF_LOCAL)
240 {
241 mode_t mask; /* Umask setting */
242
243
244 /*
245 * Remove any existing domain socket file...
246 */
247
248 unlink(lis->address.un.sun_path);
249
250 /*
251 * Save the curent umask and set it to 0...
252 */
253
254 mask = umask(0);
255
256 /*
257 * Bind the domain socket...
258 */
259
260 status = bind(lis->fd, (struct sockaddr *)&(lis->address),
261 httpAddrLength(&(lis->address)));
262
263 /*
264 * Restore the umask...
265 */
266
267 umask(mask);
268 }
269 else
270 #endif /* AF_LOCAL */
271 status = bind(lis->fd, (struct sockaddr *)&(lis->address),
272 sizeof(lis->address.ipv4));
273
274 if (status < 0)
275 {
276 cupsdLogMessage(CUPSD_LOG_ERROR,
277 "Unable to bind socket for address %s:%d - %s.",
278 s, p, strerror(errno));
279 close(lis->fd);
280 lis->fd = -1;
281 continue;
282 }
283
284 /*
285 * Listen for new clients.
286 */
287
288 if (listen(lis->fd, ListenBackLog) < 0)
289 {
290 cupsdLogMessage(CUPSD_LOG_ERROR,
291 "Unable to listen for clients on address %s:%d - %s.",
292 s, p, strerror(errno));
293 exit(errno);
294 }
295 }
296
297 fcntl(lis->fd, F_SETFD, fcntl(lis->fd, F_GETFD) | FD_CLOEXEC);
298
299 if (p)
300 cupsdLogMessage(CUPSD_LOG_INFO, "Listening to %s:%d on fd %d...",
301 s, p, lis->fd);
302 else
303 {
304 cupsdLogMessage(CUPSD_LOG_INFO, "Listening to %s on fd %d...",
305 s, lis->fd);
306
307 if (chmod(s, 0140777))
308 cupsdLogMessage(CUPSD_LOG_ERROR,
309 "Unable to change permisssions on domain socket "
310 "\"%s\" - %s", s, strerror(errno));
311 }
312
313 /*
314 * Save the first port that is bound to the local loopback or
315 * "any" address...
316 */
317
318 if ((!LocalPort || LocalEncryption == HTTP_ENCRYPT_ALWAYS) && p > 0 &&
319 (httpAddrLocalhost(&(lis->address)) ||
320 httpAddrAny(&(lis->address))))
321 {
322 LocalPort = p;
323 LocalEncryption = lis->encryption;
324 }
325
326 #ifdef AF_LOCAL
327 if (lis->address.addr.sa_family == AF_LOCAL && !have_domain)
328 have_domain = lis->address.un.sun_path;
329 #endif /* AF_LOCAL */
330 }
331
332 /*
333 * Make sure that we are listening on localhost!
334 */
335
336 if (!LocalPort && !have_domain)
337 {
338 cupsdLogMessage(CUPSD_LOG_EMERG,
339 "No Listen or Port lines were found to allow access via "
340 "localhost!");
341
342 /*
343 * Commit suicide...
344 */
345
346 cupsdEndProcess(getpid(), 0);
347 }
348
349 /*
350 * Set the CUPS_SERVER, IPP_PORT, and CUPS_ENCRYPTION variables based on
351 * the listeners...
352 */
353
354 if (have_domain)
355 {
356 /*
357 * Use domain sockets for the local connection...
358 */
359
360 cupsdSetEnv("CUPS_SERVER", have_domain);
361
362 LocalEncryption = HTTP_ENCRYPT_IF_REQUESTED;
363 }
364 else
365 {
366 /*
367 * Use the default local loopback address for the server...
368 */
369
370 cupsdSetEnv("CUPS_SERVER", "localhost");
371 }
372
373 cupsdSetEnv("CUPS_ENCRYPTION", encryptions[LocalEncryption]);
374
375 if (LocalPort)
376 cupsdSetEnvf("IPP_PORT", "%d", LocalPort);
377
378 /*
379 * Resume listening for connections...
380 */
381
382 cupsdResumeListening();
383 }
384
385
386 /*
387 * 'cupsdStopListening()' - Close all listening sockets...
388 */
389
390 void
391 cupsdStopListening(void)
392 {
393 cupsd_listener_t *lis; /* Current listening socket */
394
395
396 cupsdLogMessage(CUPSD_LOG_DEBUG2,
397 "cupsdStopListening: closing all listen sockets.");
398
399 cupsdPauseListening();
400
401 for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
402 lis;
403 lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
404 {
405 if (lis->fd != -1)
406 {
407 #ifdef WIN32
408 closesocket(lis->fd);
409 #else
410 close(lis->fd);
411 #endif /* WIN32 */
412
413 #ifdef AF_LOCAL
414 /*
415 * Remove domain sockets...
416 */
417
418 # ifdef HAVE_LAUNCH_H
419 if (lis->address.addr.sa_family == AF_LOCAL && !Launchd)
420 # else
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 */
425 }
426 }
427 }
428
429
430 /*
431 * End of "$Id: listen.c 7673 2008-06-18 22:31:26Z mike $".
432 */