-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgitftp.c
68 lines (56 loc) · 1.08 KB
/
gitftp.c
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
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include "socket.h"
#include "ftp.h"
#define DEFAULT_PORT "8021"
/* to prevent zombies */
void wait_for_kids(void)
{
while (wait(NULL) >= 0)
;
}
int main(int argc, char **argv)
{
int sock, accepted;
pid_t pid;
int ip[4];
if (argc != 2)
{
fprintf(stderr, "Usage: %s repo-path\n", *argv);
return EXIT_FAILURE;
}
sock = negotiate_listen(DEFAULT_PORT);
if (sock < 0)
exit(EXIT_FAILURE);
atexit(wait_for_kids);
while (1)
{
if ((accepted = accept(sock, NULL, NULL)) < 0)
{
perror("accept()");
exit(EXIT_FAILURE);
}
get_ip_port(accepted, ip, NULL);
if ((pid = fork()) < 0)
{
perror("fork()");
write(accepted, "452 unable to fork\n", 19);
}
else if (pid == 0)
{
close(sock); /* belongs to parent */
ftp_session(accepted, ip, argv[1]);
exit(EXIT_SUCCESS);
}
close(accepted); /* child has its own dup */
}
return EXIT_SUCCESS;
}