aboutsummaryrefslogtreecommitdiffstats
path: root/daemon/mutatord.c
blob: 980207be5dad52d250e28ad56112cd463e4fc86c (plain) (blame)
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
/***************************************************Project Mutator****************************************************/
/*first line intentionally left blank.*/
/*the source code for the static checks(Misra-C,...)*/
/*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.*/
/**********************************************************************************************************************/
/*inclusion directives*/
#include "mutatord.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 signal_callback_handler(int signum)
{
  exit(signum);
}

int main(void)
{
  //signal(SIGINT, signal_callback_handler);

  /*getting a process ID*/
  pid_t pid;
  /*getting a session ID*/
  pid_t sid;

  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();

  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);
  }

  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*/
  while(1)
  {
    fprintf(mut_log, "%s", "mutatord is running fine.\n");
    sleep(1);
  }

  fclose(mut_log);
  exit(EXIT_SUCCESS);

}
/*last line intentionally left blank*/