File: daemon.c

package info (click to toggle)
ident2 1.05-1.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 220 kB
  • ctags: 75
  • sloc: ansic: 833; sh: 203; makefile: 80
file content (236 lines) | stat: -rw-r--r-- 5,302 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Ident-2 - an Identity server for UNIX
 * Copyright (C) 1998-2001 Michael Bacarella
 * Copyright (C) 2003 Netgraft Corporation
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Please view the file README for program information.
 *
 * ---------------------
 *	This file provides the 'daemon' implementation of
 *	ident2.
 *
 *	Daemon waits for connections
 *	forks, handles connections, cleans up,
 *	dies, etc.
 *
 *	Module entry point is ``daemon_service''
 * ---------------------
 */

#include "ident2.h"


static int
_go_daemon (void)
{
	switch (fork()) {
	case 0:
		setsid();
		m_register_pid ();
		m_reduce_rights ();
		return 0;
	case -1:
		syslog (LOG_ERR, "d_core: error fork(): %s\n", strerror(errno));
		return -1;
	default:
		exit (0);
	}
}
	
	/*
	 *	remove dead children from the process vector
	 */
static void
_reap_proc (pid_t *pv, size_t Max_Connections)
{
	pid_t *p;
	int r, s;

	for (p = pv; p < pv+Max_Connections; p++) {
		if (*p == 0)
			continue;

		while ((r = waitpid (*p, &s, WNOHANG)) == -1)
			if (errno != EINTR) {
				syslog (LOG_NOTICE,
					"warning: waitpid() error: %s",
						strerror (errno));
				break;
			}
		if (r == 0)
			continue;
		if (r > 0)
			*p = 0;
	}
}


static void
declient (int s)
{
	struct sockaddr sin;
	int ss = sizeof (sin);
	close (accept (s, (struct sockaddr *)&sin, &ss));
}

                                                 	
static void _sig_ign (int s) { return; }


static int
_accept_connect (int sv, struct sockaddr_in *sin)
{
	size_t sl = sizeof (struct sockaddr_in);
	int cl;

	while ((cl = accept(sv, (struct sockaddr *)sin, &sl)) == -1) {
		if (errno == EINTR)
			continue;
		return -1;
	}
	return cl;
}
	
	/*
	 *	will wait for a connection on SV,
	 *	find a space in the proc table and fork() 
	 *	child for it. the child will accept() it and
	 *	process the waiting client.
	 *	we rely on the delivery of EINTR to interrupt
	 *	select(), which gives us a chance to test for
	 *	deceased children.
	 */
static void
d_core (int sv)
{
	struct sigaction sa;
 	struct sockaddr_in sin;
 	pid_t	*pv;
				/* become daemon */
	if (_go_daemon () == -1) {
		syslog (LOG_ERR, "d_core: error fork(): %s\n", strerror(errno));
		return;
	}
			
	pv = xmalloc (Max_Connections * sizeof (pid_t)); 
	memset (pv, 0, Max_Connections * sizeof(pid_t));
	 
	sa.sa_handler = _sig_ign;
	sigemptyset (&sa.sa_mask);
	sa.sa_flags = 0;
	
	if (sigaction (SIGCHLD, &sa, NULL) == -1) {
		syslog (LOG_ERR, "error: registering SIGCHLD handler: %s",
			strerror (errno));
		return;
	}
	
	while (1) {
		pid_t *p;		
		int cl;
		fd_set rfd;
		
		FD_ZERO(&rfd);
		FD_SET(sv, &rfd);
		
		if (select (sv+1, &rfd, NULL, NULL, NULL) == -1) {
			if (errno != EINTR)
				break;
			_reap_proc (pv, Max_Connections);
			continue;
		}
		if (!FD_ISSET (sv, &rfd))
			continue;

		for (p = pv; p < pv+Max_Connections; p++)
			if (*p == 0)
				break;
			
		if (p == pv+Max_Connections) {
			syslog (LOG_INFO, "refusing %s: too many open "
				"connections", inet_ntoa (sin.sin_addr));
			declient (sv);
		}
	
		if ((cl = _accept_connect (sv, &sin)) == -1) {
			syslog (LOG_NOTICE, "warning: inconsistency error: "
				"select() and accept() disagree: %s",
					strerror (errno));
			continue;
		}	
		
		if ((*p = fork()) == -1) {
			syslog (LOG_ERR, "warning: couldn't fork(): %s",
				strerror (errno));
			*p = 0;
		} else if (*p == 0) {
			child_service (cl, cl);
			close (cl);
			exit (0);
		}
		close (cl); /* close now that it's mapped into the child */
	}
}


void daemon_service (void)
{
	int s;
	struct sockaddr_in sin;

	openlog ("ident2", LOG_PID, LOG_DAEMON);
		
	if ((s = socket (PF_INET, SOCK_STREAM, 0)) == -1) {
		syslog (LOG_ERR, "error: socket(): %s", strerror (errno));
		return;
	}
	
	sin.sin_family = AF_INET;
	sin.sin_port = htons (Ident_Port);
	sin.sin_addr.s_addr = INADDR_ANY;

	if (bind (s, (struct sockaddr *)&sin, sizeof (sin)) == -1) {
		syslog (LOG_ERR, "error: binding to port %d: bind(): %s",
			Ident_Port, strerror (errno));
		fprintf (stderr, "error: binding to port %d: bind(): %s\n\n",
			Ident_Port, strerror (errno));
		return;
	}
	
	if (listen (s, 15) == -1) {
		syslog (LOG_ERR, "error: listening to port %d: listen(): %s",
				Ident_Port, strerror (errno));
		fprintf (stderr, "error: listening to port %d: listen(): %s\n",
				Ident_Port, strerror (errno));
		return;
	}

	fclose (stdin);
	fclose (stderr);
	fclose (stdout);

	syslog (LOG_NOTICE, "identity services started [%s]",
			ID_VERSION);
	d_core (s);

	syslog (LOG_ERR, "error: identity services terminated on "
		"internal error: See last (few) message(s)");

	closelog ();
	close (s);
	return;	
}