Did some stuff
[matches/swarm.git] / src / main.c
1 /**
2  * @file main.c
3  * @purpose Contains main function, definition of options global variable, and the general signal handler
4  * @author Sam Moore ([email protected])
5  */
6
7 // --- external includes --- //
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <signal.h>
11
12
13 // --- local includes --- //
14 #include "options.h"
15 #include "master.h"
16 #include "slave.h"
17 #include "daemon.h"
18
19
20 // --- variable definitions --- //
21 Options options; // basically all global variables are contained in this structure
22
23
24 // --- function declarations  --- //
25 void signal_handler(int signal);
26
27
28
29 // --- function definitions --- //
30
31 /**
32  * @funct main
33  * @purpose The main function; starts argument parsing function, 
34  *      starts appropriate main function based on the type of the instance (interactive, remote, daemon wrapper)
35  * @param argc - You should know what this is. But it's the size of the argv array. In case you didn't know.
36  * @param argv - You should also know what this is. It contains arguments passed to the program.
37  */
38
39 int main(int argc, char ** argv)
40 {
41         options_setup(argc, argv, &options); // fill in the options
42
43         // Setup signal handlers
44         if (signal(SIGTERM, signal_handler) == SIG_ERR)
45                 error("main", "Setting signal handler");
46         if (signal(SIGINT, signal_handler) == SIG_ERR)
47                 error("main", "Setting signal handler");
48         if (signal(SIGHUP, signal_handler) == SIG_ERR)
49                 error("main", "Setting signal handler");
50         if (signal(SIGPIPE, signal_handler) == SIG_ERR)
51                 error("main", "Setting signal handler");
52         //if (signal(SIGSEGV, signal_handler) == SIG_ERR)
53         //      error("main", "Setting signal handler");
54
55
56         // Start the appropriate actual main function
57         if (options.master_addr == NULL) // This is not a "remote" instance
58         {
59                 if (options.daemon_wrapper)
60                 {
61                         // There is a daemon running; send commands to the daemon
62                         Daemon_wrapper(&options);
63                 }
64                 else
65                 {
66                         // Daemons and interactive instances run this function
67                         Master_main(&options);
68                 }
69         }
70         else
71         {
72                 // Remote instance
73                 Slave_main(&options);
74         }
75
76         exit(EXIT_SUCCESS);
77         return 0;       
78 }
79
80 /**
81  * @funct signal_handler
82  * @purpose Handle any signals that would cause the program to terminate
83  * @param sig - The signal number
84  */
85 void signal_handler(int sig)
86 {
87         // There is a bit of a race condition with the SIGCHLD handler
88         // We can't be sure whether this handler gets called first, or SIGCHLD (due to a child getting the signal)
89         // We need to ignore SIGCHLD if this handler gets called first  
90         signal(SIGCHLD, SIG_IGN);
91         signal(sig, SIG_IGN); // I'm not sure why we need this? //TODO: Remove?
92
93         log_print(2, "Signal_handler", "Got signal %d; exiting", sig);
94         exit(EXIT_SUCCESS);
95 }
96

UCC git Repository :: git.ucc.asn.au