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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
/***************************************************Project Mutator****************************************************/
/*first line intentionally left blank.*/
/*Copyright (C) 2017 Farzad Sadeghi
This source file contains mutator's daemon.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
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 General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/
/**********************************************************************************************************************/
/*macros*/
#define __DBG
#if 1
#undef __DBG
#endif
/**********************************************************************************************************************/
/*inclusion directives*/
#include "mutatord.h"
#include "daemon_aux.h"
/*library headers*/
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
/**********************************************************************************************************************/
void time_n_date(FILE* infile)
{
time_t tnd = time(NULL);
fprintf(infile, "%s", ctime(&tnd));
}
void sigint_callback_handler(int signum)
{
exit(signum);
}
void sigterm_callback_handler(int signum)
{
//fclose(infile);
exit(signum);
}
int main(void)
{
signal(SIGINT, sigint_callback_handler);
signal(SIGTERM, sigterm_callback_handler);
/*getting a process ID*/
pid_t pid;
/*getting a session ID*/
pid_t sid;
int server_exit_code;
FILE *mut_log;
mut_log = fopen("mutatord-log", "w");
/*printing out time n date*/
fprintf(mut_log, "%s", "daemon started on ");
time_n_date(mut_log);
/*fork off the parent process*/
pid = fork();
fprintf(mut_log, "%s", "child forked on ");
time_n_date(mut_log);
if (pid < 0)
{
//fprintf(mut_log, "%s", "failed to get a pid.\n");
exit(EXIT_FAILURE);
}
if (pid > 0)
{
fprintf(mut_log, "%s%d%s", "successfully got a pid:", pid, "\n");
exit(EXIT_SUCCESS);
}
/*i don't have a bellybutton so we're fine.*/
umask(0);
fprintf(mut_log, "%s", "set umask to 0.\n");
/*create a new session ID for the child process*/
sid = setsid();
if (sid < 0)
{
fprintf(mut_log, "%s", "failed to get an sid.\n");
exit(EXIT_FAILURE);
}
else
{
fprintf(mut_log, "%s%d%s", "got an sid:", sid, "\n");
}
/*change the current working directory*/
if ((chdir("/")) < 0)
{
fprintf(mut_log, "%s", "failed to change to root dir.\n");
exit(EXIT_FAILURE);
}
else
{
fprintf(mut_log, "%s", "changed to root dir.\n");
}
/*close the standard file descriptors*/
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
fprintf(mut_log, "%s", "closed standard file descriptors..\n");
/*deamon loop*/
do
{
fprintf(mut_log, "%s", "running server...\n");
//sleep(1);
server_exit_code = mutator_server(mut_log);
fprintf(mut_log, "%s%d%s", "server terminated with exit code ", server_exit_code, "\n");
fprintf (mut_log, "%s", "closing down server\n");
fclose(mut_log);
}while(0);
return server_exit_code;
/*@DEVI-these obviously will never run. theyre just a reminder that i need to handle the gracefull shutdown*/
#if 0
fclose(mut_log);
exit(EXIT_SUCCESS);
#endif
}
/*last line intentionally left blank*/
|