-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
81 lines (73 loc) · 2.19 KB
/
server.c
File metadata and controls
81 lines (73 loc) · 2.19 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: andcardo <andcardo@student.42lisboa.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 15:56:43 by andcardo #+# #+# */
/* Updated: 2025/12/03 18:10:56 by andcardo ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
t_s_data g_s_data;
static void signal_handler(int signal, siginfo_t *info, void *context)
{
(void)context;
if (signal == SIGUSR1)
g_s_data.c |= (1 << (7 - g_s_data.bit_index));
g_s_data.bit_index++;
if (g_s_data.bit_index == 8)
{
g_s_data.buffer[g_s_data.buffer_index] = g_s_data.c;
g_s_data.buffer_index++;
if (g_s_data.buffer_index == BUF_SIZE)
{
write(1, g_s_data.buffer, BUF_SIZE);
g_s_data.buffer_index = 0;
}
else if (g_s_data.c == '\0')
{
write(1, g_s_data.buffer, g_s_data.buffer_index - 1);
g_s_data.buffer_index = 0;
}
g_s_data.bit_index = 0;
g_s_data.c = 0;
}
kill(info->si_pid, SIGUSR1);
}
static void print_s_data_pid(void)
{
char *pid_str;
pid_str = ft_itoa(getpid());
if (!pid_str)
{
write(2, "Error: Malloc failed\n", 21);
exit(1);
}
write(1, "Server PID: ", 12);
write(1, pid_str, ft_strlen(pid_str));
write(1, "\n", 1);
free(pid_str);
}
static void setup_action(void)
{
struct sigaction sa;
sa.sa_sigaction = signal_handler;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
}
int main(void)
{
g_s_data.bit_index = 0;
g_s_data.c = 0;
g_s_data.buffer_index = 0;
ft_bzero(g_s_data.buffer, BUF_SIZE);
print_s_data_pid();
setup_action();
while (1)
pause();
return (0);
}