-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdaemonize.c
More file actions
97 lines (89 loc) · 3.46 KB
/
daemonize.c
File metadata and controls
97 lines (89 loc) · 3.46 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
/***************************************************************************
* Copyright (C) 2015 by Stoian Ivanov *
* sdr@tera-com.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License version 2 *
* as published by the Free Software Foundation. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/** \file
\brief Daemonize declarations
\author Stoian Ivanov sdr@mail.bg
\author Graham Shaw via http://www.microhowto.info/howto/cause_a_process_to_become_a_daemon_in_c.html
*/
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include "daemonize.h"
void daemonize(const char *newstdin,const char *newstdout,const char *newsterr, const char *pidfile) {
// Fork, allowing the parent process to terminate.
pid_t pid = fork();
if (pid == -1) {
fprintf(stderr,"failed to fork while daemonizing (errno=%d)",errno); _exit(-1);
} else if (pid != 0) {
_exit(0);
}
// Start a new session for the daemon.
if (setsid()==-1) {
fprintf(stderr,"failed to become a session leader while daemonizing(errno=%d)",errno); _exit(-1);
}
// Fork again, allowing the parent process to terminate.
signal(SIGHUP,SIG_IGN);
pid=fork();
if (pid == -1) {
fprintf(stderr,"failed to fork while daemonizing (errno=%d)",errno); _exit(-1);
} else if (pid != 0) {
_exit(0);
}
// Close then reopen standard file descriptors.
if (newstdin) {
int fd=open(newstdin,O_RDONLY);
if ( fd== -1) {
fprintf(stderr,"failed to open %s as stdin while daemonising (errno=%d)",newstdin,errno);_exit(-1);
} else {
dup2(fd,STDIN_FILENO);
close(fd);
}
}
if (newstdout){
int fd=open(newstdout,O_WRONLY);
if ( fd== -1) {
fprintf(stderr,"failed to open %s as stdout while daemonising (errno=%d)",newstdout,errno);_exit(-1);
} else{
dup2(fd,STDOUT_FILENO);
close(fd);
}
}
if (newsterr){
int fd=open(newsterr,O_RDWR);
if (fd == -1) {
fprintf(stderr,"failed to open %s as stderr while daemonising (errno=%d)",newsterr,errno);_exit(-1);
} else {
dup2(fd,STDERR_FILENO);
close(fd);
}
}
if (pidfile){
int fd=open(pidfile,O_WRONLY|O_TRUNC|O_CREAT);
if ( fd== -1) {
fprintf(stderr,"failed to open %s as pidfile while daemonising (errno=%d)",pidfile,errno);_exit(-1);
} else{
char buf[20];
int len=snprintf(buf,20,"%d",getpid());
write(fd,buf,len);
close(fd);
}
}
}