File: daemon.c

package info (click to toggle)
xtell 1.91.2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 164 kB
  • ctags: 65
  • sloc: ansic: 962; makefile: 81; sh: 39
file content (165 lines) | stat: -rw-r--r-- 3,912 bytes parent folder | download | duplicates (7)
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
/* ==================================================================
 *	DAEMON.C
 *
 *	This file provides the 'daemon' implementation of
 *	xtell.
 *
 *
 *	Daemon-
 *		Regular daemon which waits for connections
 *		forks, handles connections, cleans up,
 *		dies, etc.
 *
 * =============================================================== */

#include "xtelld.h"

/* the entry point is daemon_service (last function, at the bottom)	*/

unsigned long	open_connections;


/* ------------------------------------
 *	child_reaper:
 *		free resources of child service
 *		as well as close the descriptor
 *		on the parent end
 * ------------------------------------
 */
static void child_reaper (int s)
{
	int	status;
	
	wait (&status);
	if (WIFEXITED(status))	/* exit value is a socket descriptor */
		killsock (WEXITSTATUS(status));
	
	signal (SIGCHLD, child_reaper);
	
	if (open_connections > 0)
		open_connections--;
	
}




/* ------------------------------------------------------------------
 * daemon_core:
 *	this maintains a standalone -forking- daemon
 *	it basically waits for a connection, forks off
 *	a handler for it, and cleans up when it dies.
 *	results in much smaller code, but also a wee bit more
 *	overhead.
 * ------------------------------------------------------------------
 */
static void daemon_core (int sd)
{
	struct	sockaddr_in	sin;
	int	sinsize, sd_peal;
	
	sinsize = sizeof (struct sockaddr);
	
	signal (SIGCHLD, child_reaper);
	
	while (1) {

		sd_peal = accept (sd, (struct sockaddr *)&sin, &sinsize);
		if (sd_peal == -1) {
			if (errno == EINTR)
				continue;
			else
				break;
		}
		else if (open_connections < max_connections) {
			switch (fork ()) {
				case 0:
					alarm (client_timeout);
					signal (SIGALRM, xtell_killtic);
					
						/* we trick the inetd service handler into
						 * thinking it was spawned by inetd, rather
						 * than this process. harmless prank. */	
					
					inetd_service (sd_peal, sd_peal);

					killsock (sd_peal);
					exit (sd_peal);
				
				case -1:
                                        fprintf(stderr, "daemon_service: could not fork!!\n");
                                        syslog(LOG_ERR, "daemon_service: could not fork!!");
					break;
				
				default:
					open_connections++;
					break;
			}
		}
		else
			killsock (sd_peal);
	
	}
	fprintf(stderr, "accept() returned error: %s\n", strerror (errno));
	syslog(LOG_ERR, "accept() returned error: %s", strerror (errno));
}





/* ------------------------------------------------------------------
 * daemon_service	:
 * ------------------------------------------------------------------
 */
void daemon_service (void)
{
	int	sd, err;
	struct sockaddr_in	sin;
	
	open_connections = 0;
	
	openlog ("xtell", LOG_PID, LOG_AUTH);
	
	setsid ();
        
	if (fork ())		/* daemonize */
		exit (0);
	chdir ("/"); 

	sd = socket (PF_INET, SOCK_STREAM, 0);
	if (sd == -1) {
		syslog(LOG_ERR, "Cannot allocate socket: %s", strerror (errno));
		fprintf(stderr, "Cannot allocate socket: %s\n", strerror (errno));
		exit (-1);
	}
	
	sin.sin_family = AF_INET;
	sin.sin_port = htons (xtell_port);
	sin.sin_addr.s_addr = INADDR_ANY;
	err = bind (sd, (struct sockaddr *)&sin, sizeof (sin));
	if (err == -1) {
		syslog(LOG_ERR, "Cannot bind to port %d: %s", 
			xtell_port, strerror (errno));
		fprintf(stderr, "Cannot bind to port %d: %s\n", 
			xtell_port, strerror (errno));
		exit (-1);
	}
	
	if (-1 == listen (sd, 5)) {
		syslog(LOG_ERR, "Cannot listen on port %d: %s", 
			xtell_port, strerror (errno));
		fprintf(stderr, "Cannot listen on port %d: %s\n", 
			xtell_port, strerror (errno));
		exit (-1);
	}

	syslog (LOG_NOTICE, "xtell services started [ %s ]", 
		VERSION);
	daemon_core (sd);
	syslog(LOG_ERR, "xtell services terminated (exiting on internal error)");
	fprintf(stderr, "xtell services terminated (exiting on internal error)\n");
	closelog ();
	killsock (sd);

}