a running minimal c-daemon ready
This commit is contained in:
parent
98ce8ec7f7
commit
de2245fa39
6 changed files with 77 additions and 29 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -74,4 +74,7 @@ go.work
|
|||
modules.order
|
||||
Module.symvers
|
||||
Mkfile.old
|
||||
dkms.conf
|
||||
dkms.conf
|
||||
|
||||
### USER MODIFIED ###
|
||||
c-daemon/bin/
|
23
README.md
23
README.md
|
@ -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/)
|
||||
|
||||
|
@ -64,4 +83,6 @@ 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
|
||||
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>
|
|
@ -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
48
c-daemon/daemon/daemon.c
Normal 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);
|
||||
}
|
|
@ -3,7 +3,10 @@
|
|||
int _daemon (int, int);
|
||||
|
||||
int main(){
|
||||
printf("START");
|
||||
_daemon(0, 0);
|
||||
printf("END");
|
||||
getchar();
|
||||
printf("CLOSE");
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue