FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
fo_scheduler.c
Go to the documentation of this file.
1 /* **************************************************************
2 Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 ************************************************************** */
22 /* local includes */
23 #include <agent.h>
24 #include <database.h>
25 #include <event.h>
26 #include <host.h>
27 #include <interface.h>
28 #include <logging.h>
29 #include <scheduler.h>
30 
31 /* library includes */
32 #include <libfossrepo.h>
33 
42 int main(int argc, char** argv)
43 {
44  /* locals */
45  gboolean db_reset = FALSE; // flag to reset the job queue upon database connection
46  gboolean ki_kill = FALSE; // flag that indicates all schedulers should be forcibly shutdown
47  gboolean ki_shut = FALSE; // flag that indicates all schedulers should be gracefully shutdown
48  gboolean db_init = FALSE; // flag indicating a database test
49  gboolean test_die = FALSE; // flag to run the tests then die
50  gboolean s_daemon = FALSE; // falg to run the scheduler as a daemon
51  gchar* logdir = NULL; // used when a different log from the default is used
52  GOptionContext* options; // option context used for command line parsing
53  GError* error = NULL; // error object used during parsing
54  uint16_t port = 0;
55  gchar* sysconfigdir = DEFAULT_SETUP;
56 
57  /* THE SCHEDULER */
58  scheduler_t* scheduler;
59 
60  if(getenv("FO_SYSCONFDIR") != NULL)
61  sysconfigdir = getenv("FO_SYSCONFDIR");
62 
63  /* get this done first */
64  srand(time(NULL));
65 #if !(GLIB_MAJOR_VERSION >= 2 && GLIB_MINOR_VERSION >= 32)
66  g_thread_init(NULL);
67 #endif
68 #if !(GLIB_MAJOR_VERSION >= 2 && GLIB_MINOR_VERSION >= 36)
69  g_type_init();
70 #endif
71 
72  /* the options for the command line parser */
73  GOptionEntry entries[] =
74  {
75  { "daemon", 'd', 0, G_OPTION_ARG_NONE, &s_daemon, " Run scheduler as daemon" },
76  { "database", 'i', 0, G_OPTION_ARG_NONE, &db_init, " Initialize database connection and exit" },
77  { "kill", 'k', 0, G_OPTION_ARG_NONE, &ki_kill, " Forcibly kills all running schedulers" },
78  { "shutdown", 's', 0, G_OPTION_ARG_NONE, &ki_shut, " Gracefully shutdown of all running schedulers" },
79  { "log", 'L', 0, G_OPTION_ARG_STRING, &logdir, "[str] Specify location of log file" },
80  { "port", 'p', 0, G_OPTION_ARG_INT, &port, "[num] Set the interface port" },
81  { "reset", 'R', 0, G_OPTION_ARG_NONE, &db_reset, " Reset the job queue upon startup" },
82  { "test", 't', 0, G_OPTION_ARG_NONE, &test_die, " Close the scheduler after running tests" },
83  { "verbose", 'v', 0, G_OPTION_ARG_INT, &verbose, "[num] Set the scheduler verbose level" },
84  { "config", 'c', 0, G_OPTION_ARG_STRING, &sysconfigdir, "[str] Specify system configuration directory" },
85  {NULL}
86  };
87 
88  /* ********************* */
89  /* *** parse options *** */
90  /* ********************* */
91  options = g_option_context_new("- scheduler for FOSSology");
92  g_option_context_add_main_entries(options, entries, NULL);
93  g_option_context_parse(options, &argc, &argv, &error);
94 
95  if(error)
96  {
97  fprintf(stderr, "ERROR: %s\n", error->message);
98  fprintf(stderr, "%s", g_option_context_get_help(options, FALSE, NULL));
99  fflush(stderr);
100  return -1;
101  }
102 
103  g_option_context_free(options);
104 
105  /* check changes to the process first */
106  if(ki_shut) { return kill_scheduler(FALSE); }
107  if(ki_kill) { return kill_scheduler(TRUE); }
108 
109  /* initialize the scheduler */
110  scheduler = scheduler_init(sysconfigdir,
111  log_new("stdout", "initializing", getpid()));
112 
113  if(logdir)
114  {
115  scheduler->logdir = logdir;
116  scheduler->logcmdline = TRUE;
117  scheduler->main_log = log_new(scheduler->logdir, NULL, scheduler->s_pid);
118 
120  main_log = scheduler->main_log;
121  }
122 
123  scheduler->process_name = g_strdup(argv[0]);
124  scheduler->s_daemon = s_daemon;
125 
126  scheduler_foss_config(scheduler);
127  if(s_daemon && scheduler_daemonize(scheduler) == -1) { return -1; }
128  scheduler_agent_config(scheduler);
129 
130  database_init(scheduler);
131  email_init(scheduler);
132 
133  NOTIFY("*****************************************************************");
134  NOTIFY("*** FOSSology scheduler started ***");
135  NOTIFY("*** pid: %-33d ***", getpid());
136  NOTIFY("*** verbose: %-33d ***", verbose);
137  NOTIFY("*** config: %-33s ***", sysconfigdir);
138  NOTIFY("*****************************************************************");
139 
140  interface_init(scheduler);
141  fo_RepOpenFull(scheduler->sysconfig);
142 
143  signal(SIGCHLD, scheduler_sig_handle);
144  signal(SIGTERM, scheduler_sig_handle);
145  signal(SIGQUIT, scheduler_sig_handle);
146  signal(SIGHUP, scheduler_sig_handle);
147 
148  /* ***************************************************** */
149  /* *** we have finished initialization without error *** */
150  /* ***************************************************** */
151 
152  if(db_reset)
153  database_reset_queue(scheduler);
154  if(test_die)
155  closing = 1;
157 
158  NOTIFY("*****************************************************************");
159  NOTIFY("*** FOSSology scheduler closed ***");
160  NOTIFY("*** pid: %-34d ***", scheduler->s_pid);
161  NOTIFY("*****************************************************************\n");
162 
163  interface_destroy(scheduler);
164  scheduler_destroy(scheduler);
165  return 0;
166 }
void database_reset_queue(scheduler_t *scheduler)
Resets any jobs in the job queue that are not completed.
Definition: database.c:942
int closing
Set if scheduler is shutting down.
Definition: scheduler.c:69
int scheduler_daemonize(scheduler_t *scheduler)
Daemonizes the scheduler.
Definition: scheduler.c:984
fo_conf * sysconfig
Configuration information loaded from the configuration file.
Definition: scheduler.h:160
int main(int argc, char **argv)
Definition: fo_scheduler.c:42
void interface_destroy(scheduler_t *scheduler)
Closes the server socket and thread pool that service UI connections.
Definition: interface.c:599
void scheduler_destroy(scheduler_t *scheduler)
Free any memory associated with a scheduler_t.
Definition: scheduler.c:373
void log_destroy(log_t *log)
Free memory associated with the log file.
Definition: logging.c:161
void email_init(scheduler_t *scheduler)
Loads information about the email that will be sent for job notifications.
Definition: database.c:568
void database_init(scheduler_t *scheduler)
Definition: database.c:781
gboolean logcmdline
Was the log file set by the command line.
Definition: scheduler.h:163
Log related operations.
int kill_scheduler(int force)
Kills all other running scheduler.
Definition: scheduler.c:631
void scheduler_update(scheduler_t *scheduler)
Update function called after every event.
Definition: scheduler.c:444
char * sysconfigdir
gboolean s_daemon
Is the scheduler being run as a daemon.
Definition: scheduler.h:155
log_t * log_new(gchar *log_name, gchar *pro_name, pid_t pro_pid)
Creates a new log.
Definition: logging.c:92
void scheduler_sig_handle(int signo)
Handles any signals sent to the scheduler that are not SIGCHLD.
Definition: scheduler.c:104
void scheduler_agent_config(scheduler_t *scheduler)
Loads a particular agents configuration file.
Definition: scheduler.c:754
int verbose
The verbose flag for the cli.
Definition: fo_cli.c:49
void scheduler_foss_config(scheduler_t *scheduler)
Loads the configuration data from fossology.conf.
Definition: scheduler.c:864
log_t * main_log
Definition: logging.c:44
int fo_RepOpenFull(fo_conf *config)
Loads common information from configuration files into ram.
Definition: libfossrepo.c:932
scheduler_t * scheduler_init(gchar *sysconfigdir, log_t *log)
Create a new scheduler object.
Definition: scheduler.c:260
Event handling operations.
Header file with agent related operations.
void interface_init(scheduler_t *scheduler)
Create the interface thread and thread pool that handle UI connections.
Definition: interface.c:565
gboolean s_pid
The pid of the scheduler process.
Definition: scheduler.h:154
log_t * main_log
The main log file for the scheduler.
Definition: scheduler.h:164
void scheduler_signal(scheduler_t *scheduler)
Function that handles certain signals being delivered to the scheduler.
Definition: scheduler.c:153
gchar * logdir
The directory to put the log file in.
Definition: scheduler.h:162
Header file for the scheduler.
int event_loop_enter(scheduler_t *scheduler, void(*update_call)(scheduler_t *), void(*signal_call)(scheduler_t *))
Enters the event loop.
Definition: event.c:227
gchar * process_name
The name of the scheduler process.
Definition: scheduler.h:153