-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathin_echo.c
More file actions
126 lines (112 loc) · 2.89 KB
/
in_echo.c
File metadata and controls
126 lines (112 loc) · 2.89 KB
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
/**
* An internet domain stream socket echo server with a thread limit and
* inetd lanuch option -i
*
* Note that non privilaged programs cannot bind to the echo port.
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <syslog.h>
#include <sys/socket.h>
#include <netdb.h>
#define BUFSZ 100
#define THREADLIM 1
#define BKLOG 20
static pthread_mutex_t active_threads_mtx = PTHREAD_MUTEX_INITIALIZER;
static int active_threads;
static void *echo(void *arg) {
char buf[BUFSZ];
int fd, nread;
fd = (int)arg;
while ((nread = read(fd, buf, BUFSZ)) > 0 || errno == EINTR) {
if (write(fd, buf, nread) != nread) {
syslog(LOG_ERR, "Failed to complete write to client");
errno = 0;
break;
}
}
if (errno != 0)
syslog(LOG_ERR, "Error servicing client: %s", strerror(errno));
pthread_mutex_lock(&active_threads_mtx);
active_threads--;
pthread_mutex_unlock(&active_threads_mtx);
}
static int mksock() {
struct addrinfo hints, *res, *cur;
int sock;
memset(&hints, 0, sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
hints.ai_protocol = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
if (getaddrinfo(NULL, "echo", &hints, &res) != 0)
return -1;
sock = -1;
for (cur = res; cur != NULL; cur = cur->ai_next) {
if ((sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol)) == -1)
continue;
if (bind(sock, cur->ai_addr, cur->ai_addrlen) == -1) {
close(sock);
sock = -1;
continue;
}
if (listen(sock, BKLOG) == -1) {
close(sock);
sock = -1;
}
break;
}
return sock;
}
static void run_server() {
pthread_t thread;
int sock, fd;
syslog(LOG_INFO, "running");
if (daemon(0, 0) == -1) {
syslog(LOG_ERR, "daemon() failed");
exit(EXIT_FAILURE);
}
syslog(LOG_INFO,"detached");
if ((sock = mksock()) == -1) {
syslog(LOG_ERR, "Failed to create listeneing socket");
exit(EXIT_FAILURE);
}
syslog(LOG_INFO, "listening socket created");
while ((fd = accept(sock, NULL, NULL)) > -1) {
syslog(LOG_INFO, "accepted connection...");
pthread_mutex_lock(&active_threads_mtx);
if (active_threads >= THREADLIM) {
syslog(LOG_ERR, "Too many clients, skipping...");
close(fd);
}
if (pthread_create(&thread, NULL, echo, (void*)fd) != 0) {
syslog(LOG_ERR, "Failed to create thread");
pthread_mutex_unlock(&active_threads_mtx);
continue;
}
active_threads++;
pthread_mutex_unlock(&active_threads_mtx);
syslog(LOG_INFO, "detatched thread to handle child");
if(pthread_detach(thread) == -1) {
syslog(LOG_ERR, "Failed to detach thread, exiting");
exit(EXIT_FAILURE);
}
}
}
int main(int argc, char **argv) {
openlog("in_echo", 0, LOG_DAEMON);
if (argc > 1 && strcmp(argv[1], "-i") == 0) {
echo((void*)STDOUT_FILENO);
} else {
run_server();
}
closelog();
exit(EXIT_SUCCESS);
}