a running minimal c-daemon ready

This commit is contained in:
Aliberk Sandıkçı 2023-07-26 13:52:50 +03:00
parent 98ce8ec7f7
commit de2245fa39
Signed by: asandikci
GPG Key ID: 25C67A03B5666BC1
6 changed files with 77 additions and 29 deletions

3
.gitignore vendored
View File

@ -75,3 +75,6 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf
### USER MODIFIED ###
c-daemon/bin/

View File

@ -9,6 +9,25 @@ This repo will be include two separate daemons. One of them is written with C la
<br>
How to start c-daemon + go processes:
```bash
git clone https://git.aliberksandikci.com.tr/liderahenk/go-daemon
cd go-daemon/
go build -o c-daemon/bin/ info
gcc -o c-daemon/bin/test c-daemon/daemon/test.c c-daemon/daemon/daem
on.c
./c-daemon/bin/test
```
<br>
---
<br>
<details><summary>Notes</summary>
### Summary - [MakeUseOf](https://www.makeuseof.com/create-daemons-on-linux/)
@ -65,3 +84,5 @@ Now return to the terminal where you are running your application (./test) and p
First of all, you can say that the new subprocess is running in the **background** since you do not see the **+** character in the **STAT** field. You can now see that the parent process of your process is the systemd process (or other init in use)
I'm too lazy to continue this readme(about c-daemon). just continue in [original website](https://www.makeuseof.com/create-daemons-on-linux/) or just review last versions of c files
</detailsc>

View File

@ -1,27 +0,0 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/fs.h>
#include <linux/limits.h>
int _daemon(int nochdir, int noclose){
pid_t pid;
// pid_t defined in sys/types.h
// pid_t is a signed int data type
pid = fork(); // fork of the parent process
// fork() function defined in unistd.h
if (pid < 0){
exit(EXIT_FAILURE); // exit and EXIT_FAILURE defined in stdlib.h
}
else if (pid > 0){
exit(EXIT_SUCCESS);
}
if (setsid() == -1){
return -1;
}
return 0;
}

48
c-daemon/daemon/daemon.c Normal file
View File

@ -0,0 +1,48 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/fs.h>
#include <linux/limits.h>
#define NR_OPEN 1024
int _daemon(int nochdir, int noclose){
pid_t pid, sid;
// pid_t defined in sys/types.h
// pid_t is a signed int data type
pid = fork(); // fork of the parent process
// fork() function defined in unistd.h
if (pid < 0){
exit(EXIT_FAILURE); // exit and EXIT_FAILURE defined in stdlib.h
}
else if (pid > 0){
exit(EXIT_SUCCESS);
}
// sid = setsid();
// if (sid < 0) {
// exit(EXIT_FAILURE);
// }
// if ((chdir("/")) < 0) {
// exit(EXIT_FAILURE);
// }
// if (!noclose) {
// for (int i = 0; i < NR_OPEN; i++)
// close(i);
// open("/dev/null", O_RDWR);
// dup(0);
// dup(0);
// }
// close(STDOUT_FILENO);
// close(STDERR_FILENO);
while (1) {
int status = system("../go-files/info");
// TODO SECURITY ISSUE
// LINK https://stackoverflow.com/questions/5237482/how-do-i-execute-an-external-program-within-c-code-in-linux-with-arguments#
sleep(1);
}
exit(EXIT_SUCCESS);
}

View File

@ -3,7 +3,10 @@
int _daemon (int, int);
int main(){
printf("START");
_daemon(0, 0);
printf("END");
getchar();
printf("CLOSE");
return 0;
}