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
75a2f5a4
Commit
75a2f5a4
authored
Apr 15, 2009
by
Rusty Russell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
daemonize module
parent
2a1c08c3
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
104 additions
and
0 deletions
+104
-0
ccan/daemonize/_info.c
ccan/daemonize/_info.c
+54
-0
ccan/daemonize/daemonize.c
ccan/daemonize/daemonize.c
+31
-0
ccan/daemonize/daemonize.h
ccan/daemonize/daemonize.h
+19
-0
No files found.
ccan/daemonize/_info.c
0 → 100644
View file @
75a2f5a4
#include <stdio.h>
#include <string.h>
#include "config.h"
/**
* daemonize - routine to turn a process into a well-behaved daemon.
*
* Daemons should detach themselves thoroughly from the process which launched
* them, and not prevent any filesystems from being unmounted. daemonize()
* helps with the process.
*
* Example:
* #include <ccan/daemonize/daemonize.h>
* #include <ccan/str/str.h>
* #include <err.h>
* #include <unistd.h>
* #include <stdlib.h>
*
* static void usage(const char *name)
* {
* errx(1, "Usage: %s [--daemonize]\n", name);
* }
*
* // Wait for a minute, possibly as a daemon.
* int main(int argc, char *argv[])
* {
* if (argc != 1) {
* if (argc == 2 && streq(argv[1], "--daemonize")) {
* if (!daemonize())
* err(1, "Failed to become daemon");
* } else
* usage(argv[1]);
* }
* sleep(60);
* exit(0);
* }
*
* Licence: BSD (2 clause, ie. MIT)
*/
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
!=
2
)
return
1
;
if
(
strcmp
(
argv
[
1
],
"depends"
)
==
0
)
{
return
0
;
}
if
(
strcmp
(
argv
[
1
],
"libs"
)
==
0
)
{
return
0
;
}
return
1
;
}
ccan/daemonize/daemonize.c
0 → 100644
View file @
75a2f5a4
#include <ccan/daemonize/daemonize.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
/* This code is based on Stevens Advanced Programming in the UNIX
* Environment. */
bool
daemonize
(
void
)
{
pid_t
pid
;
/* Separate from our parent via fork, so init inherits us. */
if
((
pid
=
fork
())
<
0
)
return
false
;
if
(
pid
!=
0
)
exit
(
0
);
/* Don't hold files open. */
close
(
STDIN_FILENO
);
close
(
STDOUT_FILENO
);
close
(
STDERR_FILENO
);
/* Session leader so ^C doesn't whack us. */
setsid
();
/* Move off any mount points we might be in. */
chdir
(
"/"
);
/* Discard our parent's old-fashioned umask prejudices. */
umask
(
0
);
return
true
;
}
ccan/daemonize/daemonize.h
0 → 100644
View file @
75a2f5a4
#ifndef CCAN_DAEMONIZE_H
#define CCAN_DAEMONIZE_H
#include <stdbool.h>
/**
* daemonize - turn this process into a daemon.
*
* This routine forks us off to become a daemon. It returns false on failure
* (ie. fork() failed) and sets errno.
*
* Side effects for programmers to be aware of:
* - PID changes (our parent exits, we become child of init)
* - stdin, stdout and stderr file descriptors are closed
* - Current working directory changes to /
* - Umask is set to 0.
*/
bool
daemonize
(
void
);
#endif
/* CCAN_DAEMONIZE_H */
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