-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.cpp
221 lines (172 loc) · 4.94 KB
/
server.cpp
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
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <signal.h>
#include <fstream>
#include <ctime>
#include <iostream>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#define MAXLINE 4096
char *path = NULL;
char *cmd = NULL;
int listenfd = -1, connfd = -1;
void Handler(int sig) {
openlog("myServer", LOG_CONS | LOG_PID, LOG_USER);
syslog(LOG_INFO, "processing signal...");
char args[1024];
sprintf(args, "%d", listenfd);
pid_t fpid = fork();
if (fpid == 0) {
if (args == "-1") {
execle(path, cmd, NULL, environ);
} else {
execle(path, cmd, args, NULL, environ);
}
} else if (fpid > 0) {
syslog(LOG_INFO, "parent exit...");
} else {
syslog(LOG_ERR, "fork error: %s(errno: %d)\n", strerror(errno), errno);
exit(EXIT_FAILURE);
}
closelog();
}
void Child(int sockFd) {
char buff[4096];
int n;
if ((n = recv(sockFd, buff, MAXLINE, 0)) < 0) {
syslog(LOG_ERR, "recv error: %s(errno: %d)\n", strerror(errno), errno);
}
buff[n] = '\0';
std::ofstream out("/tmp/output", std::ios::app);
std::time_t result = std::time(0);
out<<boost::this_thread::get_id()<<" / "<<buff<<" "<<std::asctime(std::localtime(&result));
out.close();
}
void SetSig() {
openlog("myServer", LOG_CONS | LOG_PID, LOG_USER);
struct sigaction action;
action.sa_handler = Handler;
sigemptyset(&action.sa_mask);
action.sa_flags = SA_NOMASK;
// action.sa_flags |= SA_RESTART;
if (sigaction(SIGALRM, &action, NULL) < 0) {
syslog(LOG_ERR, "set signal error: %s(errno: %d)\n", strerror(errno), errno);
} else {
syslog(LOG_INFO, "set signal ok...");
}
closelog();
// alarm(3);
}
void NewConnect() {
openlog("myServer", LOG_CONS | LOG_PID, LOG_USER);
struct sockaddr_in servaddr;
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
syslog(LOG_ERR, "create socket error: %s(errno: %d)\n", strerror(errno), errno);
exit(EXIT_FAILURE);
}
// timeval timeout = {1, 0};
// if (setsockopt(listenfd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeval)) < 0)
// {
// exit(1);
// }
//
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(6666);
if(bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){
syslog(LOG_ERR, "bind socket error: %s(errno: %d)\n", strerror(errno), errno);
exit(EXIT_SUCCESS);
}
if(listen(listenfd, 10) == -1){
syslog(LOG_ERR, "listen socket error: %s(errno: %d)\n", strerror(errno), errno);
exit(EXIT_FAILURE);
}
while(1){
if((connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1){
if (errno == EINTR) {
syslog(LOG_ERR, "accept error: %s(errno: %d)\n", strerror(errno), errno);
break;
}
}
boost::thread thrd(boost::bind(&Child, connfd));
thrd.join();
close(connfd);
}
closelog();
}
void NewDaemon() {
pid_t pid, sid;
//fork the parent process
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
//we got a good pid, close the parent process
if (pid > 0) {
exit(EXIT_SUCCESS);
}
//change file mask
umask(0);
//create a new signature id for our child
sid = setsid();
if (sid < 0) {
exit(EXIT_FAILURE);
}
//change directory
//if we can not find the directory we exit with failure
// if ((chdir("/")) < 0) {
// exit(EXIT_FAILURE);
// }
//close standard file description
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
void NewListen() {
openlog("myServer", LOG_CONS | LOG_PID, LOG_USER);
while(1){
if((connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1){
if (errno == EINTR) {
syslog(LOG_ERR, "accept error: %s(errno: %d)\n", strerror(errno), errno);
break;
}
}
boost::thread thrd(boost::bind(&Child, connfd));
thrd.join();
close(connfd);
}
closelog();
}
int main(int argc, char const* argv[])
{
openlog("myServer", LOG_CONS | LOG_PID, LOG_USER);
path = const_cast<char *>(argv[0]);
cmd = const_cast<char *>(argv[0]);
if (argc == 1) {
NewDaemon();
SetSig();
NewConnect();
} else if (argc == 2) {
listenfd = atoi(argv[1]);
SetSig();
sleep(5);
NewListen();
} else {
syslog(LOG_ERR, "args error..");
exit(EXIT_FAILURE);
}
// close(listenfd);
// close the log
closelog();
return 0;
}