-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkdaemon.c
55 lines (46 loc) · 888 Bytes
/
kdaemon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// kdaemon.c
// mylib
//
// Created by bikang on 17/10/29.
// Copyright (c) 2017年 bikang. All rights reserved.
//
#include "kdaemon.h"
/**
**/
int daemon_it(){
pid_t pid,sid;
//创建一个子进程
pid = fork();
if (pid < 0){
exit(EXIT_FAILURE);
}
if (pid > 0){
exit(EXIT_SUCCESS);
}
//设置文件掩码,最大权限
umask(0);
//创建一个新的会话id
sid = setsid();
if (sid < 0){
exit(EXIT_FAILURE);
}
//处理信号
//切换到根目录
if (chdir("/") < 0) {
exit(EXIT_FAILURE);
}
//关闭标准输出,输入,error
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
return 0;
}
void test_daemon(){
puts("test daemon");
daemon_it();
while (1) {
puts("waiting...\n");
sleep(1);
}
}