1 /*
2 Description:
3 
4 To compile: Please run the accompanying makefile.
5 
6 License: This program is made available under the BSD licnese. Please
7 	 see the accompanying LICENSE file for details.
8 
9 Author: Jesse Smith <jessefrgsmith@yahoo.ca>
10 
11 */
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <signal.h>
17 #include <time.h>
18 #include "swapexd.h"
19 
20 int keep_running = TRUE;
21 
22 
handler_sigterm(int signum)23 void handler_sigterm(int signum)
24 {
25    signal(SIGTERM, handler_sigterm);
26    signal(SIGINT, handler_sigterm);
27    keep_running = FALSE;
28    #ifdef DEBUG
29    printf("Caught termination signal.\n");
30    #endif
31 }
32 
33 
main(int argc,char * argv[])34 int main(int argc, char *argv[])
35 {
36   SWAP_DATA *swap_data;
37   int status;
38   int total_swap_size, swap_usage, swap_available;
39   pid_t process_id;
40 
41   // create data structure
42   swap_data = Init_Swap_Data();
43   if (! swap_data)
44   {
45      printf("Unable to allocate memory. Exiting.\n");
46      return 1;
47   }
48 
49   // check command line arguments
50   Get_Command_Line_Arguments(swap_data, argc, argv);
51 
52   // see if we should display version
53   if (swap_data->show_version)
54      Print_Version();
55   // see if we should display help
56   if (swap_data->show_help)
57      Print_Usage();
58 
59   if ( (swap_data->show_version) || (swap_data->show_help) )
60   {
61       Clean_Up_Swap_Data(swap_data);
62       exit(0);
63   }
64 
65   // give us random numbers
66   srand( time(NULL) );
67 
68   // try to load config file
69   Read_Config_File(swap_data);
70 
71   // see if we should run as a daemon
72   if (swap_data->daemon_mode)
73   {
74       #ifdef DEBUG
75       printf("Forking\n");
76       #endif
77       process_id = fork();
78       // parent
79       if (! process_id)
80       {
81          Clean_Up_Swap_Data(swap_data);
82          exit(0);
83       }
84       else  // child process
85       {
86          setsid();
87          process_id = fork();
88          if (process_id)
89          {
90            Clean_Up_Swap_Data(swap_data);
91            exit(0);
92          }
93       }
94   }     // end of using daemon mode
95 
96   // check to see if we need to create a PID file
97   if (swap_data->create_pid_file)
98      Create_PID_File();
99 
100   Create_Swap_Directory(swap_data);
101 
102   // register signal handler
103   signal(SIGTERM, handler_sigterm);
104   signal(SIGINT, handler_sigterm);
105   while (keep_running)
106   {
107   // get current amount of swap space
108      total_swap_size = Get_Total_Swap_Size();
109   // get current swap usage
110      swap_usage = Get_Swap_Usage();
111   // cacluate available swap space remaining
112      swap_available = total_swap_size - swap_usage;
113      #ifdef DEBUG
114      printf("Total swap: %d\n", total_swap_size);
115      printf("Swap used:  %d\n", swap_usage);
116      printf("Swap free:  %d\n", swap_available);
117      #endif
118   // if we are close to needing more swap, create/enlarge the swap file
119      if (swap_data->grow_buffer > swap_available)
120      {
121         #ifdef DEBUG
122         printf("Trying to grow swap file.\n");
123         #endif
124         Resize_Swap_File(swap_data, ACTION_GROW);
125      }
126   // if we have plenty of swap space available, reduce the swap file
127      else if ( (swap_data->current_swap_file_size > 0) &&
128                (swap_data->reduce_buffer < swap_available) )
129      {
130          #ifdef DEBUG
131          printf("Tring to shrink swap file.\n");
132          #endif
133          Resize_Swap_File(swap_data, ACTION_SHRINK);
134      }
135   // wait before running another test
136     sleep(5);
137   }   // end of endless loop
138 
139   #ifdef DEBUG
140   printf("Left main loop, cleaning up.\n");
141   #endif
142 
143   // clean up
144   Clean_Up_Swap_File(swap_data);
145   Clean_Up_Swap_Data(swap_data);
146   return 0;
147 }
148