Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e88a2fc

Browse files
committed
Add function to become daemon
Signed-off-by: Seungha Son <seungha.son@samsung.com>
1 parent d071e70 commit e88a2fc

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

‎daemon/daemon.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
5+
#include <sys/stat.h>
6+
#include <fcntl.h>
7+
8+
static void make_child(void)
9+
{
10+
switch (fork()) {
11+
case -1:
12+
exit(EXIT_FAILURE);
13+
case 0:
14+
break;
15+
default:
16+
exit(EXIT_SUCCESS);
17+
}
18+
}
19+
20+
void s_daemon_become(void)
21+
{
22+
int ret;
23+
int fd;
24+
int max_fd;
25+
26+
make_child();
27+
28+
ret = setsid();
29+
if (ret == -1)
30+
exit(EXIT_FAILURE);
31+
32+
make_child();
33+
34+
umask(0);
35+
36+
ret = chdir("/");
37+
if (ret != 0)
38+
exit(EXIT_FAILURE);
39+
40+
max_fd = sysconf(_SC_OPEN_MAX);
41+
if (max_fd == -1)
42+
max_fd = 8192;
43+
44+
for (fd = 0; fd < max_fd; fd++)
45+
close(fd);
46+
47+
close(STDIN_FILENO);
48+
49+
fd = open("/dev/null", O_RDWR);
50+
if (fd != STDIN_FILENO)
51+
exit(EXIT_FAILURE);
52+
if (dup2(STDIN_FILENO, STDOUT_FILENO) != STDOUT_FILENO)
53+
exit(EXIT_FAILURE);
54+
if (dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO)
55+
exit(EXIT_FAILURE);
56+
}

‎daemon/daemon.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef __DAEMON_H__
2+
#define __DAEMON_H__
3+
4+
void s_daemon_become(void);
5+
6+
#endif

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /