-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_bonus.c
88 lines (78 loc) · 2.29 KB
/
server_bonus.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rapcampo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/23 13:38:23 by rapcampo #+# #+# */
/* Updated: 2024/01/20 20:24:35 by rapcampo ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk_bonus.h"
unsigned char *g_message = NULL;
void flood_string(unsigned char bits, siginfo_t *info)
{
if (bits == 0x00)
{
write(1, g_message, ft_unstrlen(g_message));
write(1, "\n", 1);
free(g_message);
kill(info->si_pid, SIGUSR1);
g_message = NULL;
}
else
g_message = ft_unstrjoin(g_message, bits);
}
void handle_signals(int sig, siginfo_t *info, void *s)
{
static int byte;
static unsigned char bits;
(void)s;
if (sig == SIGUSR1)
bits |= (0x01 << byte);
byte++;
if (byte == 0x08)
{
flood_string(bits, info);
byte = 0;
bits = 0;
}
}
void handle_sigint(int sig)
{
if (sig == SIGINT)
{
if (g_message)
free(g_message);
write(1, "\n\n\e[92mServer has been shutdown successfuly!\e[0m\n", 50);
exit(EXIT_SUCCESS);
}
}
static void print_pid(void)
{
char *pid;
pid = ft_itoa(getpid());
write(1, "\e[31mServer PID: ", 18);
write(1, pid, ft_strlen(pid));
write(1, "\e[0;5;95m\nWaiting for message...\e[0m\n", 38);
free(pid);
}
int main(int argc, char **argv)
{
struct sigaction sa;
sa.sa_sigaction = &handle_signals;
sigemptyset(&sa.sa_mask);
(void) argv;
sa.sa_flags = SA_SIGINFO;
print_pid();
while (argc == 1)
{
signal(SIGINT, handle_sigint);
if (sigaction(SIGUSR1, &sa, NULL) == -1)
write(1, "\e[91mhandler could not be resolved\e[0m", 39);
if (sigaction(SIGUSR2, &sa, NULL) == -1)
write(1, "\e[91mhandler could not be resolved\e[0m", 39);
pause();
}
}