daemonize
This commit is contained in:
parent
962ee7180e
commit
0f0c9a48dc
12
System2/daemonize/Makefile
Normal file
12
System2/daemonize/Makefile
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
CC=gcc
|
||||||
|
|
||||||
|
all: main clean
|
||||||
|
|
||||||
|
main: main.o
|
||||||
|
$(CC) -o main main.o
|
||||||
|
|
||||||
|
main.o: main.c
|
||||||
|
$(CC) -c main.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f *.o
|
1
System2/daemonize/coucou.txt
Normal file
1
System2/daemonize/coucou.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
coucou, je suis bien en vie!
|
75
System2/daemonize/main.c
Normal file
75
System2/daemonize/main.c
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
bool daemonize()
|
||||||
|
{
|
||||||
|
pid_t pid = fork();
|
||||||
|
if (pid < 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (pid > 0)
|
||||||
|
{
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
if (setsid() < 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
pid = fork();
|
||||||
|
if (pid < 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (pid > 0)
|
||||||
|
{
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
umask(0);
|
||||||
|
chdir("/");
|
||||||
|
FILE *fp;
|
||||||
|
fp = fopen("/Users/mdy/Development/langage_C/System2/daemonize/test.txt", "w");
|
||||||
|
close(STDIN_FILENO);
|
||||||
|
close(STDOUT_FILENO);
|
||||||
|
dup(fileno(fp));
|
||||||
|
close(STDERR_FILENO);
|
||||||
|
dup(fileno(fp));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void handler(int signum)
|
||||||
|
{
|
||||||
|
if (signum == SIGUSR1)
|
||||||
|
{
|
||||||
|
FILE *fp;
|
||||||
|
fp = fopen("/Users/mdy/Development/langage_C/System2/daemonize/coucou.txt", "w");
|
||||||
|
fprintf(fp, "coucou, je suis bien en vie!\n");
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
else if (signum == SIGUSR2)
|
||||||
|
{
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("received signal %d\n", signum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
daemonize();
|
||||||
|
if (signal(SIGUSR1, handler) == SIG_ERR)
|
||||||
|
printf("can't catch SIGUSR1\n");
|
||||||
|
if (signal(SIGUSR2, handler) == SIG_ERR)
|
||||||
|
printf("can't catch SIGUSR2\n");
|
||||||
|
for (;;)
|
||||||
|
pause();
|
||||||
|
}
|
0
System2/daemonize/test.txt
Normal file
0
System2/daemonize/test.txt
Normal file
12
System2/sendToFile/Makefile
Normal file
12
System2/sendToFile/Makefile
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
CC=gcc
|
||||||
|
|
||||||
|
all: main clean
|
||||||
|
|
||||||
|
main: main.o
|
||||||
|
$(CC) -o main main.o
|
||||||
|
|
||||||
|
main.o: main.c
|
||||||
|
$(CC) -c main.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f *.o
|
@ -1,23 +1,34 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
void handler(int signum)
|
void handler(int signum)
|
||||||
{
|
{
|
||||||
FILE *fp;
|
if (signum == SIGUSR1)
|
||||||
fp = fopen("test.txt", "w");
|
|
||||||
fprintf(fp, "coucou, je suis bien en vie!\n");
|
|
||||||
fclose(fp);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
signal(SIGUSR1, handler);
|
|
||||||
for (size_t i = 0; i < 30; i++)
|
|
||||||
{
|
{
|
||||||
|
FILE *fp;
|
||||||
printf("i = %d, In main\n", i);
|
fp = fopen("test.txt", "w");
|
||||||
sleep(1);
|
fprintf(fp, "coucou, je suis bien en vie!\n");
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
else if (signum == SIGUSR2)
|
||||||
|
{
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("received signal %d\n", signum);
|
||||||
}
|
}
|
||||||
return 0;
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
if (signal(SIGUSR1, handler) == SIG_ERR)
|
||||||
|
printf("can't catch SIGUSR1\n");
|
||||||
|
if (signal(SIGUSR2, handler) == SIG_ERR)
|
||||||
|
printf("can't catch SIGUSR2\n");
|
||||||
|
for (;;)
|
||||||
|
pause();
|
||||||
}
|
}
|
@ -0,0 +1 @@
|
|||||||
|
coucou, je suis bien en vie!
|
Loading…
x
Reference in New Issue
Block a user