]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/listen.c
Y2k copyright changes.
[thirdparty/cups.git] / scheduler / listen.c
1 /*
2 * "$Id: listen.c,v 1.3 2000/01/04 13:46:10 mike Exp $"
3 *
4 * Server listening routines for the Common UNIX Printing System (CUPS)
5 * scheduler.
6 *
7 * Copyright 1997-2000 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Easy Software Products and are protected by Federal
11 * copyright law. Distribution and use rights are outlined in the file
12 * "LICENSE.txt" which should have been included with this file. If this
13 * file is missing or damaged please contact Easy Software Products
14 * at:
15 *
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
19 * Hollywood, Maryland 20636-3111 USA
20 *
21 * Voice: (301) 373-9603
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
24 *
25 * Contents:
26 *
27 * StartListening() - Create all listening sockets...
28 * StopListening() - Close all listening sockets...
29 */
30
31 /*
32 * Include necessary headers...
33 */
34
35 #include "cupsd.h"
36
37
38 /*
39 * 'StartListening()' - Create all listening sockets...
40 */
41
42 void
43 StartListening(void)
44 {
45 int i, /* Looping var */
46 val; /* Parameter value */
47 listener_t *lis; /* Current listening socket */
48
49
50 /*
51 * Setup socket listeners...
52 */
53
54 for (i = NumListeners, lis = Listeners; i > 0; i --, lis ++)
55 {
56 LogMessage(LOG_DEBUG, "StartListening() address=%08x port=%d",
57 ntohl(lis->address.sin_addr.s_addr),
58 ntohs(lis->address.sin_port));
59
60 if ((lis->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
61 {
62 LogMessage(LOG_ERROR, "StartListening() Unable to open listen socket - %s.",
63 strerror(errno));
64 exit(errno);
65 }
66
67 fcntl(lis->fd, F_SETFD, fcntl(lis->fd, F_GETFD) | FD_CLOEXEC);
68
69 /*
70 * Set things up to reuse the local address for this port.
71 */
72
73 val = 1;
74 #ifdef __sun
75 setsockopt(lis->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
76 #else
77 setsockopt(lis->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
78 #endif /* __sun */
79
80 /*
81 * Bind to the port we found...
82 */
83
84 if (bind(lis->fd, (struct sockaddr *)&(lis->address), sizeof(lis->address)) < 0)
85 {
86 LogMessage(LOG_ERROR, "StartListening() Unable to bind socket - %s.", strerror(errno));
87 exit(errno);
88 }
89
90 /*
91 * Listen for new clients.
92 */
93
94 if (listen(lis->fd, SOMAXCONN) < 0)
95 {
96 LogMessage(LOG_ERROR, "StartListening() Unable to listen for clients - %s.",
97 strerror(errno));
98 exit(errno);
99 }
100
101 /*
102 * Setup the select() input mask to contain the listening socket we have.
103 */
104
105 DEBUG_printf(("StartListening: Adding fd %d to InputSet...\n", lis->fd));
106 FD_SET(lis->fd, &InputSet);
107 }
108
109 LogMessage(LOG_DEBUG, "StartListening() NumListeners=%d", NumListeners);
110 }
111
112
113 /*
114 * 'StopListening()' - Close all listening sockets...
115 */
116
117 void
118 StopListening(void)
119 {
120 int i; /* Looping var */
121 listener_t *lis; /* Current listening socket */
122
123
124 for (i = NumListeners, lis = Listeners; i > 0; i --, lis ++)
125 {
126 #if defined(WIN32) || defined(__EMX__)
127 closesocket(lis->fd);
128 #else
129 close(lis->fd);
130 #endif /* WIN32 || __EMX__ */
131
132 DEBUG_printf(("StopListening: Removing fd %d from InputSet...\n", lis->fd));
133 FD_CLR(lis->fd, &InputSet);
134 }
135
136 LogMessage(LOG_DEBUG, "StopListening()");
137 }
138
139
140 /*
141 * End of "$Id: listen.c,v 1.3 2000/01/04 13:46:10 mike Exp $".
142 */