From 0f0c9a48dc098a692bd5ada9d01f800ceca163ff Mon Sep 17 00:00:00 2001 From: MAO Dongyang Date: Wed, 29 Mar 2023 17:13:13 +0200 Subject: [PATCH] daemonize --- System2/daemonize/Makefile | 12 ++++++ System2/daemonize/coucou.txt | 1 + System2/daemonize/main.c | 75 ++++++++++++++++++++++++++++++++++++ System2/daemonize/test.txt | 0 System2/sendToFile/Makefile | 12 ++++++ System2/sendToFile/main.c | 39 ++++++++++++------- System2/sendToFile/test.txt | 1 + 7 files changed, 126 insertions(+), 14 deletions(-) create mode 100644 System2/daemonize/Makefile create mode 100644 System2/daemonize/coucou.txt create mode 100644 System2/daemonize/main.c create mode 100644 System2/daemonize/test.txt create mode 100644 System2/sendToFile/Makefile diff --git a/System2/daemonize/Makefile b/System2/daemonize/Makefile new file mode 100644 index 0000000..016db65 --- /dev/null +++ b/System2/daemonize/Makefile @@ -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 \ No newline at end of file diff --git a/System2/daemonize/coucou.txt b/System2/daemonize/coucou.txt new file mode 100644 index 0000000..940faab --- /dev/null +++ b/System2/daemonize/coucou.txt @@ -0,0 +1 @@ +coucou, je suis bien en vie! diff --git a/System2/daemonize/main.c b/System2/daemonize/main.c new file mode 100644 index 0000000..ff11d96 --- /dev/null +++ b/System2/daemonize/main.c @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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(); +} diff --git a/System2/daemonize/test.txt b/System2/daemonize/test.txt new file mode 100644 index 0000000..e69de29 diff --git a/System2/sendToFile/Makefile b/System2/sendToFile/Makefile new file mode 100644 index 0000000..016db65 --- /dev/null +++ b/System2/sendToFile/Makefile @@ -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 \ No newline at end of file diff --git a/System2/sendToFile/main.c b/System2/sendToFile/main.c index 7b4502f..54ed887 100644 --- a/System2/sendToFile/main.c +++ b/System2/sendToFile/main.c @@ -1,23 +1,34 @@ #include #include #include +#include +#include void handler(int signum) { - FILE *fp; - 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++) + if (signum == SIGUSR1) { - - printf("i = %d, In main\n", i); - sleep(1); + FILE *fp; + fp = fopen("test.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); } - 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(); +} \ No newline at end of file diff --git a/System2/sendToFile/test.txt b/System2/sendToFile/test.txt index e69de29..940faab 100644 --- a/System2/sendToFile/test.txt +++ b/System2/sendToFile/test.txt @@ -0,0 +1 @@ +coucou, je suis bien en vie!