Repository navigation
Expand file tree
/
Copy pathserver.c
More file actions
234 lines (200 loc) · 6.79 KB
/
Copy pathserver.c
File metadata and controls
234 lines (200 loc) · 6.79 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
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
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "rxb.h"
#include "utils.h"
#ifdef USE_LIBUNISTRING
#include <unistr.h>
#endif
#define MAX_REQ_SIZE 4096
#define RESPONSE "----- END REQUEST -----\n"
void handler (int signo) {
int status;
(void) signo;
while(waitpid(-1, &status, WNOHANG) > 0) continue;
}
int main(int argc, char **argv) {
int err, sd, opt;
struct addrinfo hints, *res;
struct sigaction sa;
rxb_t rxb;
signal(SIGPIPE, SIG_IGN);
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sa.sa_handler = handler;
if(sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(EXIT_FAILURE);
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
char *port = argv[1];
if((err = getaddrinfo(NULL, port, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(err));
exit(EXIT_FAILURE);
}
if((sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
opt = 1;
if((err = setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) < 0) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
if((err = bind(sd, res->ai_addr, res->ai_addrlen)) < 0) {
perror("bind");
exit(EXIT_FAILURE);
}
freeaddrinfo(res);
if((err = listen(sd, SOMAXCONN)) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("Server in ascolto sulla porta %s...\n", argv[1]);
for(;;) {
int ns, pid;
if((ns = accept(sd, NULL, NULL)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
if((pid = fork()) < 0) {
perror("pid fork");
exit(EXIT_FAILURE);
} else if(pid == 0) {
close(sd);
rxb_init(&rxb, MAX_REQ_SIZE * 2);
signal(SIGCHLD, SIG_DFL);
for(;;) {
int pid1, pid2, p1p2[2], status;
char mese[1024], tipo[1024], n_res[32];
size_t mese_len, tipo_len, n_res_len;
char filename[1024 + 256];
memset(mese, 0, sizeof(mese));
mese_len = sizeof(mese) - 1;
if(rxb_readline(&rxb, ns, mese, &mese_len) < 0) {
perror("rxb_readline");
rxb_destroy(&rxb);
close(ns);
exit(EXIT_FAILURE);
}
#ifdef USE_LIBUNISTRING
if(u8_check((uint8_t *) mese, mese_len) != NULL) {
fprintf(stderr, "not a valid utf-8 string!\n");
rxb_destroy(&rxb);
close(ns);
exit(EXIT_FAILURE);
}
#endif
memset(tipo, 0, sizeof(tipo));
tipo_len = sizeof(tipo) - 1;
if(rxb_readline(&rxb, ns, tipo, &tipo_len) < 0) {
perror("rxb_readline");
rxb_destroy(&rxb);
close(ns);
exit(EXIT_FAILURE);
}
#ifdef USE_LIBUNISTRING
if(u8_check((uint8_t *) tipo, tipo_len) != NULL) {
fprintf(stderr, "not a valid utf-8 string!\n");
rxb_destroy(&rxb);
close(ns);
exit(EXIT_FAILURE);
}
#endif
memset(n_res, 0, sizeof(n_res));
n_res_len = sizeof(n_res) - 1;
if(rxb_readline(&rxb, ns, n_res, &n_res_len) < 0) {
perror("rxb_readline");
rxb_destroy(&rxb);
close(ns);
exit(EXIT_FAILURE);
}
#ifdef USE_LIBUNISTRING
if(u8_check((uint8_t *) n_res, n_res_len) != NULL) {
fprintf(stderr, "not a valid utf-8 string!\n");
rxb_destroy(&rxb);
close(ns);
exit(EXIT_FAILURE);
}
#endif
snprintf(filename, sizeof(filename), "./expenses/%s/%s.txt", mese, tipo);
if(open(filename, O_RDONLY) < 0) {
char *error = "file bloccato o inesistente.\n";
write_all(ns, error, strlen(error));
write_all(ns, RESPONSE, strlen(RESPONSE));
continue;
}
if(pipe(p1p2) < 0) {
perror("pipe");
exit(EXIT_FAILURE);
}
if((pid1 = fork()) < 0) {
perror("pid1 fork");
exit(EXIT_FAILURE);
} else if(pid1 == 0) {
close(p1p2[0]);
close(1);
if(dup(p1p2[1]) < 0) {
perror("dup");
exit(EXIT_FAILURE);
}
close(p1p2[1]);
execlp("sort", "sort", filename, (char *) NULL);
perror("sort execlp");
exit(EXIT_FAILURE);
}
if((pid2 = fork()) < 0) {
perror("pid2 fork");
exit(EXIT_FAILURE);
} else if(pid2 == 0) {
close(p1p2[1]);
close(0);
if(dup(p1p2[0]) < 0) {
perror("dup");
exit(EXIT_FAILURE);
}
close(1);
if(dup(ns) < 0) {
perror("dup");
exit(EXIT_FAILURE);
}
close(ns);
execlp("head", "head", "-n", n_res, (char *) NULL);
perror("head execlp");
exit(EXIT_FAILURE);
}
close(p1p2[0]);
close(p1p2[1]);
waitpid(pid1, &status, 0);
waitpid(pid2, &status, 0);
// Assicura che ci sia un newline prima del delimitatore
if(write_all(ns, "\n", 1) < 0) {
perror("write_all");
exit(EXIT_FAILURE);
}
if(write_all(ns, RESPONSE, strlen(RESPONSE)) < 0) {
perror("write_all");
exit(EXIT_FAILURE);
}
}
close(ns);
exit(EXIT_SUCCESS);
}
close(ns);
}
rxb_destroy(&rxb);
close(sd);
return 0;
}