61 lines
1.7 KiB
C
61 lines
1.7 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: o_ :::::: ::: */
|
|
/* server.c :+: / :+::+: :+: */
|
|
/* +:+ > +:++:+ +:+ */
|
|
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
|
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
|
/* Created: 2024/07/18 17:24:54 by whaffman #+#+# #+#+# #+# #+# | */
|
|
/* Updated: 2024/07/27 15:04:43 by whaffman ### ### ### ### / \ */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include <signal.h>
|
|
#include <unistd.h>
|
|
#include <stdio.h>
|
|
#include "libft.h"
|
|
|
|
void signal_handler(int signum, siginfo_t *info, void *context)
|
|
{
|
|
static int count = 0;
|
|
static char c = '\0';
|
|
static pid_t client_pid;
|
|
|
|
(void) context;
|
|
if (client_pid != info->si_pid)
|
|
{
|
|
count = 0;
|
|
c = '\0';
|
|
client_pid = info->si_pid;
|
|
}
|
|
c = c << 1;
|
|
if (signum == SIGUSR1)
|
|
c |= 1;
|
|
count++;
|
|
if (count == 8)
|
|
{
|
|
if (c)
|
|
write(1, &c, 1);
|
|
else
|
|
write(1, "\n", 1);
|
|
c = '\0';
|
|
count = 0;
|
|
}
|
|
kill(info->si_pid, SIGUSR1);
|
|
return ;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
struct sigaction sa;
|
|
|
|
printf("MiniTalk Server started on pid: %d\n", getpid());
|
|
sa.sa_sigaction = signal_handler;
|
|
sigemptyset(&sa.sa_mask);
|
|
sa.sa_flags = SA_SIGINFO;
|
|
sigaction(SIGUSR1, &sa, NULL);
|
|
sigaction(SIGUSR2, &sa, NULL);
|
|
while (1)
|
|
pause();
|
|
}
|