3 * @purpose Contains main function, definition of options global variable, and the general signal handler
7 // --- external includes --- //
13 // --- local includes --- //
20 // --- variable definitions --- //
21 Options options; // basically all global variables are contained in this structure
24 // --- function declarations --- //
25 void signal_handler(int signal);
29 // --- function definitions --- //
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.
39 int main(int argc, char ** argv)
41 options_setup(argc, argv, &options); // fill in the options
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");
56 // Start the appropriate actual main function
57 if (options.master_addr == NULL) // This is not a "remote" instance
59 if (options.daemon_wrapper)
61 // There is a daemon running; send commands to the daemon
62 Daemon_wrapper(&options);
66 // Daemons and interactive instances run this function
67 Master_main(&options);
81 * @funct signal_handler
82 * @purpose Handle any signals that would cause the program to terminate
83 * @param sig - The signal number
85 void signal_handler(int sig)
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?
93 log_print(2, "Signal_handler", "Got signal %d; exiting", sig);