当前位置:网站首页>Create a daemon
Create a daemon
2022-07-27 01:38:00 【Boring ah le】
Reference resources 《 The background and development : Core technology and application 》 The first 10.4 Section
If you want a process not to be affected by user or terminal changes or other changes , Then we must turn this process into a daemon
One 、 Basic steps of creating a daemon :
1、 Create child process , The parent process exits
2、 call setid():
(1) Let the process get rid of the original session control ;
(2) Let the process out of the control of the original process group ;
(3) Let the process get rid of the control of the original control terminal
3、 Change the current directory to root
4、 Reset file permission mask
5、 Close the file descriptor
All the above steps are to make the child process get rid of the control of the original parent process .
Two 、 Implementation code
/* Implement an instance of a daemon ( every other 10s stay /tmp/dameon.log Write a sentence )*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<sys/stat.h>
#define MAXFILE 65535
int main()
{
pid_t pc;
int i,fd,len;
char *buf="this is a Dameon\n";
len = strlen(buf);
pc = fork(); /* First step : Create child process , The parent process exits */
if(pc<0)
{
printf("error fork\n");
exit(1);
}
else if(pc>0)// Child process number =0, Parent process number is greater than 0
{
exit(0);// The parent process exits , Child process becomes orphan process
}
setsid(); /* The second step :setsid() Function to create a new session , And serve as the leader of the conversation group call setid effect :1、 Let the process get rid of the original session control ; 2、 Let the process out of the control of the original process group ; 3、 Let the process get rid of the control of the original control terminal */
chdir("/"); /* The third step : Change the current directory to root */
umask(0); /* Step four : Reset file permission mask */
for(i=0;i<MAXFILE;i++) /* Step five : Close the file descriptor */
close(i);
while(1)
{
if((fd=open("/tmp/dameon.log",O_CREAT|O_WRONLY|O_APPEND,0600))<0)
{
perror("open");
exit(1);
}
write(fd,buf,len+1);
close(fd);
sleep(10);
}
return 0;
}
边栏推荐
- Linked list general OJ
- Problem feedback: the synchronization of mobile app failed: the external changes of the data warehouse were damaged. The iPad app also downloaded the warehouse as soon as it was opened, and then flash
- 十二、正则表达式
- 十五、expect
- [vulnerability practice] logic vulnerability mining
- ESP8266 STA_ TCP_ Server
- Navicat operation database
- hdc_ std
- 16、 Awk
- Unity twitter login access
猜你喜欢
![[ctf attack and defense world] questions about cookies in the web area](/img/96/6e91ee19343a1ddc49dc2bc94cba62.png)
[ctf attack and defense world] questions about cookies in the web area

Web services (07) - LNMP one click deployment

Shell(7)case语句

Web服务(02)——Web服务器中间件

ESP8266---JSON数据交换格式

iptables防火墙(二)

Software Foundation of software test interview questions

【Oracle】获取最近工作日及前N个工作日

Jenkins -- Basic -- 03 -- post installation setup wizard

Finding the greatest common divisor
随机推荐
markdown
ESP8266-----SNTP获取网络时间
ESP8266连接乐鑫云平台IOT_Demo
Basic introduction to Matlab [1]
Shell(7)case语句
Problem feedback: the synchronization of mobile app failed: the external changes of the data warehouse were damaged. The iPad app also downloaded the warehouse as soon as it was opened, and then flash
[by pass] bypass method of WAF
Summary of heap sorting related knowledge
十五、expect
Shell脚本——文件的备份,更新和回滚
iptables防火墙(二)
32三剑客sed
ESP8266 STA_ TCP_ Server
Shell(13)三剑客
域名分析和DNS的配置安装
Unity a user-friendly UI grayscale shader
[SQL injection] error injection
Esp8266 connects to the IOT of Lexin cloud platform_ Demo
ESP8266 AP_ MODE
Linked list general OJ