Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
ccan
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mirror
ccan
Commits
ca4485a1
Commit
ca4485a1
authored
Jan 06, 2011
by
Rusty Russell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
daemonize: set stderr to /dev/null.
parent
6835b78d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
19 additions
and
5 deletions
+19
-5
ccan/daemonize/daemonize.c
ccan/daemonize/daemonize.c
+9
-0
ccan/daemonize/daemonize.h
ccan/daemonize/daemonize.h
+3
-2
ccan/daemonize/test/run.c
ccan/daemonize/test/run.c
+7
-3
No files found.
ccan/daemonize/daemonize.c
View file @
ca4485a1
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <fcntl.h>
/* This code is based on Stevens Advanced Programming in the UNIX
/* This code is based on Stevens Advanced Programming in the UNIX
* Environment. */
* Environment. */
...
@@ -21,6 +22,14 @@ bool daemonize(void)
...
@@ -21,6 +22,14 @@ bool daemonize(void)
close
(
STDOUT_FILENO
);
close
(
STDOUT_FILENO
);
close
(
STDERR_FILENO
);
close
(
STDERR_FILENO
);
/* Many routines write to stderr; that can cause chaos if used
* for something else, so set it here. */
if
(
open
(
"/dev/null"
,
O_WRONLY
)
!=
0
)
return
false
;
if
(
dup2
(
0
,
STDERR_FILENO
)
!=
STDERR_FILENO
)
return
false
;
close
(
0
);
/* Session leader so ^C doesn't whack us. */
/* Session leader so ^C doesn't whack us. */
setsid
();
setsid
();
/* Move off any mount points we might be in. */
/* Move off any mount points we might be in. */
...
...
ccan/daemonize/daemonize.h
View file @
ca4485a1
...
@@ -6,11 +6,12 @@
...
@@ -6,11 +6,12 @@
* daemonize - turn this process into a daemon.
* daemonize - turn this process into a daemon.
*
*
* This routine forks us off to become a daemon. It returns false on failure
* This routine forks us off to become a daemon. It returns false on failure
* (
ie. fork()
failed) and sets errno.
* (
eg. fork(), chdir or open
failed) and sets errno.
*
*
* Side effects for programmers to be aware of:
* Side effects for programmers to be aware of:
* - PID changes (our parent exits, we become child of init)
* - PID changes (our parent exits, we become child of init)
* - stdin, stdout and stderr file descriptors are closed
* - stdin and stdout file descriptors are closed
* - stderr is reopened to /dev/null so you don't reuse it
* - Current working directory changes to /
* - Current working directory changes to /
* - Umask is set to 0.
* - Umask is set to 0.
*/
*/
...
...
ccan/daemonize/test/run.c
View file @
ca4485a1
...
@@ -43,8 +43,12 @@ int main(int argc, char *argv[])
...
@@ -43,8 +43,12 @@ int main(int argc, char *argv[])
=
read
(
STDIN_FILENO
,
buffer
,
1
)
==
-
1
?
errno
:
0
;
=
read
(
STDIN_FILENO
,
buffer
,
1
)
==
-
1
?
errno
:
0
;
daemonized
.
write_to_stdout
daemonized
.
write_to_stdout
=
write
(
STDOUT_FILENO
,
buffer
,
1
)
==
-
1
?
errno
:
0
;
=
write
(
STDOUT_FILENO
,
buffer
,
1
)
==
-
1
?
errno
:
0
;
daemonized
.
write_to_stderr
if
(
write
(
STDERR_FILENO
,
buffer
,
1
)
!=
1
)
{
=
write
(
STDERR_FILENO
,
buffer
,
1
)
==
-
1
?
errno
:
0
;
daemonized
.
write_to_stderr
=
errno
;
if
(
daemonized
.
write_to_stderr
==
0
)
daemonized
.
write_to_stderr
=
-
1
;
}
else
daemonized
.
write_to_stderr
=
0
;
/* Make sure parent exits. */
/* Make sure parent exits. */
while
(
getppid
()
==
pid
)
while
(
getppid
()
==
pid
)
...
@@ -64,7 +68,7 @@ int main(int argc, char *argv[])
...
@@ -64,7 +68,7 @@ int main(int argc, char *argv[])
ok1
(
daemonized
.
in_root_dir
);
ok1
(
daemonized
.
in_root_dir
);
ok1
(
daemonized
.
read_from_stdin
==
EBADF
);
ok1
(
daemonized
.
read_from_stdin
==
EBADF
);
ok1
(
daemonized
.
write_to_stdout
==
EBADF
);
ok1
(
daemonized
.
write_to_stdout
==
EBADF
);
ok1
(
daemonized
.
write_to_stderr
==
EBADF
);
ok1
(
daemonized
.
write_to_stderr
==
0
);
return
exit_status
();
return
exit_status
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment