-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
77 lines (71 loc) · 1.91 KB
/
server.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: xle-boul <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 10:46:38 by xle-boul #+# #+# */
/* Updated: 2022/02/21 21:21:37 by xle-boul ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
void ft_error_handler(int i)
{
if (i == 0)
{
write(1, "Error KILL\n", 12);
exit(1);
}
if (i == 1)
{
write(1, "Error SIGACTION\n", 17);
exit(1);
}
}
void ft_signal_handler(int sig, siginfo_t *info, void *context)
{
static unsigned char val = 0;
static int bit = 1;
static int id = 0;
if (info->si_pid != 0)
id = info->si_pid;
(void)context ;
if (sig == SIGUSR1)
val += 0;
if (sig == SIGUSR2)
val += bit;
bit <<= 1;
if (bit == 256)
{
bit = 1;
if (val == 0)
if (kill(id, SIGUSR2) == -1)
ft_error_handler(0);
if (val != 0)
write(1, &val, 1);
val = 0;
}
if (kill(id, SIGUSR1) == -1)
ft_error_handler(0);
}
int main(void)
{
pid_t pid;
struct sigaction action;
action.sa_flags = SA_SIGINFO;
action.sa_sigaction = ft_signal_handler;
pid = getpid();
if (sigaction(SIGUSR1, &action, NULL) == -1
|| sigaction(SIGUSR2, &action, NULL) == -1)
{
ft_error_handler(1);
return (1);
}
write(1, "PID = ", 6);
ft_putnbr(pid);
write(1, "\n", 1);
while (1)
pause();
return (0);
}