3 * Copyright (C) 2003 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /****************************************************************
22 * Elementary TCP server for echo service (port 7) using poll
24 * Author: Simone Piccardi
27 * Usage: echod -h give all info
29 * $Id: poll_echod.c,v 1.3 2004/02/17 23:48:46 piccardi Exp $
31 ****************************************************************/
33 * Include needed headers
36 #include <limits.h> /* system limits */
37 #include <sys/types.h> /* predefined types */
38 #include <unistd.h> /* include unix standard library */
39 #include <arpa/inet.h> /* IP addresses conversion utiliites */
40 #include <sys/socket.h> /* socket library */
41 #include <stdio.h> /* include standard I/O library */
43 #include <syslog.h> /* syslog system functions */
44 #include <signal.h> /* signal functions */
45 #include <errno.h> /* error code */
46 #include <string.h> /* error strings */
48 #include <sys/poll.h> /* poll function definition */
55 int demonize = 1; /* daemon use option: default is daemon */
56 int debugging = 0; /* debug info printing option: default is no debug */
57 /* Subroutines declaration */
59 void PrintErr(char * error);
60 /* Program beginning */
61 int main(int argc, char *argv[])
64 * Variables definition
68 struct sockaddr_in s_addr, c_addr;
71 struct pollfd *poll_set;
73 int max_fd, nread, nwrite;
76 * Input section: decode parameters passed in the calling
79 opterr = 0; /* don't want writing to stderr */
80 while ( (i = getopt(argc, argv, "hdicw:n:")) != -1) {
86 printf("Wrong -h option use\n");
100 waiting = strtol(optarg, NULL, 10);
103 n = strtol(optarg, NULL, 10);
105 case '?': /* unrecognized options */
106 printf("Unrecognized options -%c\n",optopt);
108 default: /* should not reached */
112 /* ***********************************************************
114 * Options processing completed
116 * Main code beginning
118 * ***********************************************************/
119 /* Main code begin here */
120 if (compat) { /* install signal handler */
121 Signal(SIGCHLD, HandSigCHLD); /* non restarting handler */
123 SignalRestart(SIGCHLD, HandSigCHLD); /* restarting handler */
126 if ( (list_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
127 perror("Socket creation error");
130 /* initialize address */
131 memset((void *)&s_addr, 0, sizeof(s_addr)); /* clear server address */
132 s_addr.sin_family = AF_INET; /* address type is INET */
133 s_addr.sin_port = htons(7); /* echo port is 7 */
134 s_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* connect from anywhere */
136 if (bind(list_fd, (struct sockaddr *)&s_addr, sizeof(s_addr)) < 0) {
137 perror("bind error");
140 /* release privileges and go daemon */
141 if (setgid(65534) !=0) { /* first give away group privileges */
142 perror("cannot give away group privileges");
145 if (setuid(65534) !=0) { /* and only after user ... */
146 perror("cannot give away user privileges");
149 if (demonize) { /* go daemon */
150 openlog(argv[0], 0, LOG_DAEMON); /* open logging */
151 if (daemon(0, 0) != 0) {
152 perror("cannot start as daemon");
157 if (listen(list_fd, BACKLOG) < 0 ) {
158 PrintErr("listen error");
161 if (waiting) sleep(waiting);
162 /* initialize all needed variables */
163 poll_set = (struct pollfd *) malloc(n * sizeof(struct pollfd));
164 max_fd = list_fd; /* maximum now is listening socket */
165 for (i=0; i<n; i++) {
167 poll_set[i].events = POLLRDNORM;
169 poll_set[max_fd].fd = list_fd;
170 /* main loop, wait for connection and data inside a select */
172 while ( ((n = poll(poll_set, max_fd + 1, -1)) < 0)
173 && (errno == EINTR)); /* wait for data or connection */
174 if (n < 0) { /* on real error exit */
175 PrintErr("poll error");
179 debug("Trovati %d socket attivi\n", n);
180 if (poll_set[list_fd].revents & POLLRDNORM) { /* if new connection */
181 n--; /* decrement active */
182 len = sizeof(c_addr); /* and call accept */
183 if ((fd = accept(list_fd, (struct sockaddr *)&c_addr, &len)) < 0) {
184 PrintErr("accept error");
187 debug("Connessione su fd %d restano %d socket attivi\n", fd, n);
188 poll_set[fd].fd = fd; /* set new connection socket */
189 if (max_fd < fd) max_fd = fd; /* if needed set new maximum */
190 debug("max_fd=%d\n", max_fd);
192 /* loop on open connections */
193 i = list_fd; /* first socket to look */
194 while (n != 0) { /* loop until active */
195 i++; /* start after listening socket */
196 debug("restano %d socket, fd %d\n", n, fd);
197 if (poll_set[i].fd == -1) continue; /* closed, go next */
198 if (poll_set[i].revents & (POLLRDNORM|POLLERR)) { /* if active process it*/
199 n--; /* decrease active */
200 debug("dati su fd %d\n", i);
201 nread = read(i, buffer, MAXLINE); /* read operations */
203 PrintErr("Errore in lettura");
206 if (nread == 0) { /* if closed connection */
207 debug("fd %d chiuso\n", i);
208 close(i); /* close file */
209 poll_set[i].fd = -1; /* mark as closed in table */
210 if (max_fd == i) { /* if was the maximum */
211 while (poll_set[--i].fd == -1); /* loop down */
212 max_fd = i; /* set new maximum */
213 debug("nuovo max_fd %d\n", max_fd);
214 break; /* and go back to select */
216 continue; /* continue loop on open */
218 nwrite = FullWrite(i, buffer, nread); /* write data */
220 PrintErr("Errore in scrittura");
226 /* normal exit, never reached */
230 * routine to print usage info and exit
233 printf("Elementary echo server\n");
235 printf(" echod [-h] \n");
236 printf(" -h print this help\n");
237 printf(" -d write debug info\n");
238 printf(" -i use interactively\n");
239 printf(" -c disable BSD signal semantics\n");
240 printf(" -n N set max contemporary connection\n");
241 printf(" -w N wait N sec. before calling poll\n");
245 * routine to print error on stout or syslog
247 void PrintErr(char * error) {
248 if (demonize) { /* daemon mode */
249 syslog(LOG_ERR, "%s: %m", error); /* log string and error message */