-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdaemonize.h
85 lines (70 loc) · 2.72 KB
/
daemonize.h
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
MIT/Expat License
Copyright (c) 2016-2020 Artem Boldariev <[email protected]>
See the LICENSE.txt for details about the terms of use.
*/
#ifndef _DAEMONIZE_H
#define _DAEMONIZE_H
#ifndef _WIN32
/* Daemon creation flags. */
enum {
DMN_DEFAULT = 0,
DMN_NO_CLOSE = 1, /* Do not close existing file descriptors and do not redirect standard file descriptors to '/dev/null'. */
DMN_KEEP_SIGNAL_HANDLERS = 2, /* Do not reset signal handlers to their defaults. */
DMN_NO_CHDIR = 4, /* Do not change the current directory of the daemon to '/'. */
DMN_NO_UMASK = 8 /* Do not set umask to 0. */
};
#ifdef __cplusplus
extern "C" {
#endif
extern pid_t daemonize(int flags);
/*
* Description
daemonize() - low level function to daemonize process.
Roughly corresponds to the BSDs' daemon() function but uses
double fork technique (which is important on System V
flavoured Unix systems) and allows specifying more
daemonization parameters.
* Arguments:
flags - a bit mask of the daemon creation flags, see above.
* Return value
daemonize() follows fork() semantics.
By default, the function returns PID of the process
to the parent process (which starts daemon) or 0
to the daemon process. On error it returns -1 - this value
might be returned to both parent and daemon process. In this
case, errno value will be set accordingly.
*/
extern pid_t rundaemon(int flags,
int (*daemon_func)(void *udata),
void *udata,
int *exit_code,
const char *pid_file_path);
/*
* Description
rundaemon() - thin wrapper on top of daemonize which
might check and create PID-file, if desired. It is recommended
to use this function instead of daemonize() whenever applicable.
* Arguments:
flags - daemon creation flags, see above;
daemon_func - function pointer to the function to be called after
successful - daemonization (actual daemon body);
udata - pointer to be passed as the value in a call to daemon_func;
exit_code - pointer to variable to receive value returned by daemon_func;
pid_file_path - full pathname to the PID file. This file will be
used for checking if the daemon is not already running (by checking if
the file exists and locked) and will be created if not exists. On daemon exit
this file will be removed under normal conditions.
This value might be NULL, in this case no any checks performed and
no file will be created.
* Return value
This functions shares return values with daemonize() function mentioned
above with one notable addition - if daemon already running it will
return -2 to the process which starts the daemon. No daemonization
will be performed in this case.
*/
#ifdef __cplusplus
}
#endif
#endif /* _WIN32 */
#endif /* _DAEMONIZE_H */