|
Packit |
6bd9ab |
/*
|
|
Packit |
6bd9ab |
daemonize.h - definition of functions for daemonising an application
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
Copyright (C) 2014 Arthur de Jong
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
This library is free software; you can redistribute it and/or
|
|
Packit |
6bd9ab |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6bd9ab |
License as published by the Free Software Foundation; either
|
|
Packit |
6bd9ab |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
This library is distributed in the hope that it will be useful,
|
|
Packit |
6bd9ab |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6bd9ab |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6bd9ab |
Lesser General Public License for more details.
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6bd9ab |
License along with this library; if not, write to the Free Software
|
|
Packit |
6bd9ab |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
Packit |
6bd9ab |
02110-1301 USA
|
|
Packit |
6bd9ab |
*/
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
#ifndef NSLCD__DAEMONINZE_H
|
|
Packit |
6bd9ab |
#define NSLCD__DAEMONINZE_H 1
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
/*
|
|
Packit |
6bd9ab |
To properly run as a daemon an application should:
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
- close all open file descriptors (see daemonize_closefds() for that)
|
|
Packit |
6bd9ab |
- (re)set proper signal handlers and signal mask
|
|
Packit |
6bd9ab |
- sanitise the environment
|
|
Packit |
6bd9ab |
- fork() / setsid() / fork() to detach from terminal, become process
|
|
Packit |
6bd9ab |
leader and run in the background (see daemonize_demon() for that)
|
|
Packit |
6bd9ab |
- reconnect stdin/stdout/stderr to /dev/null (see
|
|
Packit |
6bd9ab |
daemonize_redirect_stdio() for that)
|
|
Packit |
6bd9ab |
- set the umask to a reasonable value
|
|
Packit |
6bd9ab |
- chdir(/) to avoid locking any mounts
|
|
Packit |
6bd9ab |
- drop privileges as appropriate
|
|
Packit |
6bd9ab |
- chroot() if appropriate
|
|
Packit |
6bd9ab |
- create and lock a pidfile
|
|
Packit |
6bd9ab |
- exit the starting process if initialisation is complete (see
|
|
Packit |
6bd9ab |
daemonize_ready() for that)
|
|
Packit |
6bd9ab |
*/
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
/* This closes all open file descriptors, except stdin, stdout and stderr. */
|
|
Packit |
6bd9ab |
void daemonize_closefds(void);
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
/* Redirect stdio, stdin and stderr to /dev/null. */
|
|
Packit |
6bd9ab |
void daemonize_redirect_stdio(void);
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
/* Detach from the controlling terminal and run in the background. This
|
|
Packit |
6bd9ab |
function does:
|
|
Packit |
6bd9ab |
- double fork and exit first child
|
|
Packit |
6bd9ab |
- in the first child call setsid() to detach from any terminal and
|
|
Packit |
6bd9ab |
create an independent session
|
|
Packit |
6bd9ab |
- keep the parent process waiting until a call to daemonize_ready() is
|
|
Packit |
6bd9ab |
done by the deamon process
|
|
Packit |
6bd9ab |
This function returns either an error which indicates that the
|
|
Packit |
6bd9ab |
daemonizing failed for some reason (usually sets errno), or returns
|
|
Packit |
6bd9ab |
without error indicating that the process has been daemonized. */
|
|
Packit |
6bd9ab |
int daemonize_daemon(void);
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
/* Signal that the original parent may exit because the service has been
|
|
Packit |
6bd9ab |
initialised. The status indicates the exit code of the original process and
|
|
Packit |
6bd9ab |
message, if not NULL or an empty string, is printed to stderr. */
|
|
Packit |
6bd9ab |
void daemonize_ready(int status, const char *message);
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
#endif /* not NSLCD__DAEMONINZE_H */
|