Difference between revisions of "Daemon linux"
From Teknologisk videncenter
m (→Checking the daemon) |
m |
||
Line 2: | Line 2: | ||
*'''setsid()''' - damon will be the session leader. Sets Session ID (SID) and Group ID (GID) to Process ID (PID) | *'''setsid()''' - damon will be the session leader. Sets Session ID (SID) and Group ID (GID) to Process ID (PID) | ||
*Reopening stdin, stdout and stderr to /dev/null detaches the process from the controlling terminal and Parent PID is set to 1 (init or systemd - depending on Linux version/distribution) | *Reopening stdin, stdout and stderr to /dev/null detaches the process from the controlling terminal and Parent PID is set to 1 (init or systemd - depending on Linux version/distribution) | ||
− | |||
<source lang=c line> | <source lang=c line> | ||
+ | #include <stdio.h> | ||
+ | #include <unistd.h> | ||
+ | #include <stdlib.h> | ||
+ | #include <syslog.h> | ||
+ | |||
+ | |||
+ | void daemon_create(void); | ||
+ | |||
+ | int main(void) { | ||
+ | |||
+ | printf("Starting my_daemon\n"); | ||
+ | openlog("my_daemon", LOG_INFO, LOG_DAEMON); | ||
+ | daemon_create(); | ||
+ | |||
+ | for (int i=0; i < 100; i++) { | ||
+ | syslog(LOG_INFO,"Countet to %d", i); | ||
+ | sleep(2); | ||
+ | } | ||
+ | return(0); | ||
+ | } | ||
+ | |||
void daemon_create(void) { | void daemon_create(void) { | ||
pid_t pid; | pid_t pid; | ||
Line 10: | Line 30: | ||
pid = fork(); | pid = fork(); | ||
if(pid < 0){ | if(pid < 0){ | ||
− | + | syslog(LOG_ERR, "Error in fork: %m"); | |
exit(1); | exit(1); | ||
} | } | ||
if(pid > 0){ | if(pid > 0){ | ||
+ | syslog(LOG_INFO, "Parent dying"); | ||
exit(0); // Parent die | exit(0); // Parent die | ||
} | } | ||
if(setsid() < 0){ // Make child session leader | if(setsid() < 0){ // Make child session leader | ||
− | + | syslog(LOG_ERR, "Error in setsid: %m"); | |
− | exit( | + | exit(2); |
} | } | ||
− | if ( | + | if (freopen("/dev/null", "r", stdin) == NULL |
|| freopen("/dev/null", "w", stdout) == NULL | || freopen("/dev/null", "w", stdout) == NULL | ||
|| freopen("/dev/null", "w", stderr) == NULL ) { | || freopen("/dev/null", "w", stderr) == NULL ) { | ||
− | + | syslog(LOG_ERR, "When creating daemon freopen from STDIN/OUT/ERR failed: %m"); | |
− | + | exit(3); | |
− | + | ||
+ | } | ||
} | } | ||
</source> | </source> |
Revision as of 07:27, 17 December 2022
Example of creating a daemon in Linux:
- setsid() - damon will be the session leader. Sets Session ID (SID) and Group ID (GID) to Process ID (PID)
- Reopening stdin, stdout and stderr to /dev/null detaches the process from the controlling terminal and Parent PID is set to 1 (init or systemd - depending on Linux version/distribution)
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <syslog.h>
5
6
7 void daemon_create(void);
8
9 int main(void) {
10
11 printf("Starting my_daemon\n");
12 openlog("my_daemon", LOG_INFO, LOG_DAEMON);
13 daemon_create();
14
15 for (int i=0; i < 100; i++) {
16 syslog(LOG_INFO,"Countet to %d", i);
17 sleep(2);
18 }
19 return(0);
20 }
21
22 void daemon_create(void) {
23 pid_t pid;
24
25 pid = fork();
26 if(pid < 0){
27 syslog(LOG_ERR, "Error in fork: %m");
28 exit(1);
29 }
30 if(pid > 0){
31 syslog(LOG_INFO, "Parent dying");
32 exit(0); // Parent die
33 }
34 if(setsid() < 0){ // Make child session leader
35 syslog(LOG_ERR, "Error in setsid: %m");
36 exit(2);
37 }
38 if (freopen("/dev/null", "r", stdin) == NULL
39 || freopen("/dev/null", "w", stdout) == NULL
40 || freopen("/dev/null", "w", stderr) == NULL ) {
41 syslog(LOG_ERR, "When creating daemon freopen from STDIN/OUT/ERR failed: %m");
42 exit(3);
43
44 }
45 }
Checking the daemon
$ ps xo uname,pid,ppid,pgid,sid,tty,comm | grep -P '(my_daemon|COMMAND)'
USER PID PPID PGID SID TTY COMMAND
heth 778254 1 778254 778254 ? my_daemon