FOSSology  4.4.0
Open Source License Compliance by Open Source Software
fo_scheduler.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2011 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
11 /* local includes */
12 #include <agent.h>
13 #include <database.h>
14 #include <event.h>
15 #include <host.h>
16 #include <interface.h>
17 #include <logging.h>
18 #include <scheduler.h>
19 
20 /* library includes */
21 #include <libfossrepo.h>
22 
31 int main(int argc, char** argv)
32 {
33  /* locals */
34  gboolean db_reset = FALSE; // flag to reset the job queue upon database connection
35  gboolean ki_kill = FALSE; // flag that indicates all schedulers should be forcibly shutdown
36  gboolean ki_shut = FALSE; // flag that indicates all schedulers should be gracefully shutdown
37  gboolean db_init = FALSE; // flag indicating a database test
38  gboolean test_die = FALSE; // flag to run the tests then die
39  gboolean s_daemon = FALSE; // falg to run the scheduler as a daemon
40  gchar* logdir = NULL; // used when a different log from the default is used
41  GOptionContext* options; // option context used for command line parsing
42  GError* error = NULL; // error object used during parsing
43  uint16_t port = 0;
44  gchar* sysconfigdir = DEFAULT_SETUP;
45 
46  /* THE SCHEDULER */
47  scheduler_t* scheduler;
48 
49  if(getenv("FO_SYSCONFDIR") != NULL)
50  sysconfigdir = getenv("FO_SYSCONFDIR");
51 
52  /* get this done first */
53  srand(time(NULL));
54 #if !(GLIB_MAJOR_VERSION >= 2 && GLIB_MINOR_VERSION >= 32)
55  g_thread_init(NULL);
56 #endif
57 #if !(GLIB_MAJOR_VERSION >= 2 && GLIB_MINOR_VERSION >= 36)
58  g_type_init();
59 #endif
60 
61  /* the options for the command line parser */
62  GOptionEntry entries[] =
63  {
64  { "daemon", 'd', 0, G_OPTION_ARG_NONE, &s_daemon, " Run scheduler as daemon" },
65  { "database", 'i', 0, G_OPTION_ARG_NONE, &db_init, " Initialize database connection and exit" },
66  { "kill", 'k', 0, G_OPTION_ARG_NONE, &ki_kill, " Forcibly kills all running schedulers" },
67  { "shutdown", 's', 0, G_OPTION_ARG_NONE, &ki_shut, " Gracefully shutdown of all running schedulers" },
68  { "log", 'L', 0, G_OPTION_ARG_STRING, &logdir, "[str] Specify location of log file" },
69  { "port", 'p', 0, G_OPTION_ARG_INT, &port, "[num] Set the interface port" },
70  { "reset", 'R', 0, G_OPTION_ARG_NONE, &db_reset, " Reset the job queue upon startup" },
71  { "test", 't', 0, G_OPTION_ARG_NONE, &test_die, " Close the scheduler after running tests" },
72  { "verbose", 'v', 0, G_OPTION_ARG_INT, &verbose, "[num] Set the scheduler verbose level" },
73  { "config", 'c', 0, G_OPTION_ARG_STRING, &sysconfigdir, "[str] Specify system configuration directory" },
74  {NULL}
75  };
76 
77  /* ********************* */
78  /* *** parse options *** */
79  /* ********************* */
80  options = g_option_context_new("- scheduler for FOSSology");
81  g_option_context_add_main_entries(options, entries, NULL);
82  g_option_context_parse(options, &argc, &argv, &error);
83 
84  if(error)
85  {
86  fprintf(stderr, "ERROR: %s\n", error->message);
87  fprintf(stderr, "%s", g_option_context_get_help(options, FALSE, NULL));
88  fflush(stderr);
89  return -1;
90  }
91 
92  g_option_context_free(options);
93 
94  /* check changes to the process first */
95  if(ki_shut) { return kill_scheduler(FALSE); }
96  if(ki_kill) { return kill_scheduler(TRUE); }
97 
98  /* initialize the scheduler */
99  scheduler = scheduler_init(sysconfigdir,
100  log_new("stdout", "initializing", getpid()));
101 
102  if(logdir)
103  {
104  scheduler->logdir = logdir;
105  scheduler->logcmdline = TRUE;
106  scheduler->main_log = log_new(scheduler->logdir, NULL, scheduler->s_pid);
107 
109  main_log = scheduler->main_log;
110  }
111 
112  scheduler->process_name = g_strdup(argv[0]);
113  scheduler->s_daemon = s_daemon;
114 
115  scheduler_foss_config(scheduler);
116  if(s_daemon && scheduler_daemonize(scheduler) == -1) { return -1; }
117  scheduler_agent_config(scheduler);
118 
119  database_init(scheduler);
120  email_init(scheduler);
121 
122  NOTIFY("*****************************************************************");
123  NOTIFY("*** FOSSology scheduler started ***");
124  NOTIFY("*** pid: %-33d ***", getpid());
125  NOTIFY("*** verbose: %-33d ***", verbose);
126  NOTIFY("*** config: %-33s ***", sysconfigdir);
127  NOTIFY("*****************************************************************");
128 
129  interface_init(scheduler);
130  fo_RepOpenFull(scheduler->sysconfig);
131 
132  signal(SIGCHLD, scheduler_sig_handle);
133  signal(SIGTERM, scheduler_sig_handle);
134  signal(SIGQUIT, scheduler_sig_handle);
135  signal(SIGHUP, scheduler_sig_handle);
136 
137  /* ***************************************************** */
138  /* *** we have finished initialization without error *** */
139  /* ***************************************************** */
140 
141  if(db_reset)
142  database_reset_queue(scheduler);
143  if(test_die)
144  closing = 1;
146 
147  NOTIFY("*****************************************************************");
148  NOTIFY("*** FOSSology scheduler closed ***");
149  NOTIFY("*** pid: %-34d ***", scheduler->s_pid);
150  NOTIFY("*****************************************************************\n");
151 
152  interface_destroy(scheduler);
153  scheduler_destroy(scheduler);
154  return 0;
155 }
Header file with agent related operations.
int event_loop_enter(scheduler_t *scheduler, void(*update_call)(scheduler_t *), void(*signal_call)(scheduler_t *))
Enters the event loop.
Definition: event.c:221
Event handling operations.
int verbose
The verbose flag for the cli.
Definition: fo_cli.c:38
int main(int argc, char **argv)
Definition: fo_scheduler.c:31
void interface_destroy(scheduler_t *scheduler)
Closes the server socket and thread pool that service UI connections.
Definition: interface.c:588
void interface_init(scheduler_t *scheduler)
Create the interface thread and thread pool that handle UI connections.
Definition: interface.c:554
int fo_RepOpenFull(fo_conf *config)
Loads common information from configuration files into ram.
Definition: libfossrepo.c:920
char * sysconfigdir
log_t * main_log
Definition: logging.c:33
log_t * log_new(gchar *log_name, gchar *pro_name, pid_t pro_pid)
Creates a new log.
Definition: logging.c:81
void log_destroy(log_t *log)
Free memory associated with the log file.
Definition: logging.c:150
Log related operations.
void database_reset_queue(scheduler_t *scheduler)
Resets any jobs in the job queue that are not completed.
Definition: database.c:931
void database_init(scheduler_t *scheduler)
Definition: database.c:770
void email_init(scheduler_t *scheduler)
Loads information about the email that will be sent for job notifications.
Definition: database.c:557
int closing
Set if scheduler is shutting down.
Definition: scheduler.c:58
void scheduler_agent_config(scheduler_t *scheduler)
Loads a particular agents configuration file.
Definition: scheduler.c:743
int scheduler_daemonize(scheduler_t *scheduler)
Daemonizes the scheduler.
Definition: scheduler.c:973
void scheduler_update(scheduler_t *scheduler)
Update function called after every event.
Definition: scheduler.c:433
void scheduler_sig_handle(int signo)
Handles any signals sent to the scheduler that are not SIGCHLD.
Definition: scheduler.c:93
scheduler_t * scheduler_init(gchar *sysconfigdir, log_t *log)
Create a new scheduler object.
Definition: scheduler.c:249
void scheduler_foss_config(scheduler_t *scheduler)
Loads the configuration data from fossology.conf.
Definition: scheduler.c:853
int kill_scheduler(int force)
Kills all other running scheduler.
Definition: scheduler.c:620
void scheduler_destroy(scheduler_t *scheduler)
Free any memory associated with a scheduler_t.
Definition: scheduler.c:362
void scheduler_signal(scheduler_t *scheduler)
Function that handles certain signals being delivered to the scheduler.
Definition: scheduler.c:142
Header file for the scheduler.
log_t * main_log
The main log file for the scheduler.
Definition: scheduler.h:153
gboolean s_pid
The pid of the scheduler process.
Definition: scheduler.h:143
gchar * logdir
The directory to put the log file in.
Definition: scheduler.h:151
gchar * process_name
The name of the scheduler process.
Definition: scheduler.h:142
gboolean logcmdline
Was the log file set by the command line.
Definition: scheduler.h:152
gboolean s_daemon
Is the scheduler being run as a daemon.
Definition: scheduler.h:144
fo_conf * sysconfig
Configuration information loaded from the configuration file.
Definition: scheduler.h:149