-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemonize.c
80 lines (70 loc) · 1.7 KB
/
daemonize.c
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
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <stdlib.h>
#include <fcntl.h>
#include <dirent.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>
#include <pthread.h>
#include <netinet/in.h>
#include <libgen.h> // basename
#include "sysmond.h"
void daemonize()
{
int pid;
struct stat fileStat;
FILE* file;
// Change to root directory
if ( chdir( "/" ) < 0 )
{
perror( "chdir()" );
exit( EXIT_FAILURE );
}
// See if pid file exists
if ( ! (stat( sysmond_args.pidFile, &fileStat ) ) &&
( (! S_ISREG( fileStat.st_mode ) ) ||
(fileStat.st_size > 11 )
)
)
{
fprintf( stderr,
"Error: PID file `%s' already exists and looks suspicious.\n",
sysmond_args.pidFile );
exit( EXIT_FAILURE );
}
// Set up signal handler
install_sighandler();
// Open pidFile
if ( ! ( file = fopen( sysmond_args.pidFile, "w" ) ) )
{
fprintf( stderr, "fopen(\"%s\"): %s\n", sysmond_args.pidFile,
strerror( errno )
);
exit( EXIT_FAILURE );
}
// fork and write pidFile
if ( (pid = fork() ) == -1 )
{
perror( "fork()" );
exit( EXIT_FAILURE );
}
else if ( pid != 0 )
{
// Original process
fprintf( file, "%d\n", pid );
fclose( file );
exit( EXIT_SUCCESS );
}
// Now continue running the forked process
fclose( file );
close( STDIN_FILENO );
close( STDOUT_FILENO );
close( STDERR_FILENO );
nice( -1 ); // Bump priority
set_affinity( 1 ); // Just run on CPU 1
}