FOSSology  4.4.0
Open Source License Compliance by Open Source Software
database.c File Reference

Database related operations. More...

#include <agent.h>
#include <database.h>
#include <logging.h>
#include <emailformatter.h>
#include <libfossdb.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sqlstatements.h>
Include dependency graph for database.c:

Go to the source code of this file.

Classes

struct  email_replace_args
 
struct  reqcols
 Data type used to check if the database is correct. More...
 

Macros

#define EMAIL_ERROR(...)
 
#define EMAIL_BUILD_CMD   "%s %s -s '%s' %s"
 Email command format.
 
#define DEFAULT_HEADER   "FOSSology scan complete\nmessage:\""
 Default email header.
 
#define DEFAULT_FOOTER   "\""
 Default email footer.
 
#define DEFAULT_SUBJECT   "FOSSology scan complete\n"
 Default email subject.
 
#define DEFAULT_COMMAND   "/usr/bin/mailx"
 Default email command to use.
 
#define min(x, y)   (x < y ? x : y)
 Return the minimum of x, y.
 

Functions

static gboolean email_replace (const GMatchInfo *match, GString *ret, email_replace_args *args)
 Replaces the variables that are in the header and footer files. More...
 
static gint email_checkjobstatus (scheduler_t *scheduler, job_t *job)
 Checks the database for the status of the job. More...
 
static void email_notification (scheduler_t *scheduler, job_t *job)
 
void email_init (scheduler_t *scheduler)
 Loads information about the email that will be sent for job notifications. More...
 
static void check_tables (scheduler_t *scheduler)
 Checks that any part of the database used by the scheduler is correct. More...
 
static PGconn * database_connect (gchar *configdir)
 Creates and performs error checking for a new database connection. More...
 
void database_init (scheduler_t *scheduler)
 
PGresult * database_exec (scheduler_t *scheduler, const char *sql)
 Executes an sql statement for the scheduler. More...
 
void database_exec_event (scheduler_t *scheduler, char *sql)
 
void database_update_event (scheduler_t *scheduler, void *unused)
 Checks the job queue for any new entries. More...
 
void database_reset_queue (scheduler_t *scheduler)
 Resets any jobs in the job queue that are not completed. More...
 
void database_update_job (scheduler_t *scheduler, job_t *job, job_status status)
 Change the status of a job in the database. More...
 
void database_job_processed (int j_id, int num)
 Updates the number of items that a job queue entry has processed. More...
 
void database_job_log (int j_id, char *log_name)
 Enters the name of the log file for a job into the database. More...
 
void database_job_priority (scheduler_t *scheduler, job_t *job, int priority)
 Changes the priority of a job queue entry in the database. More...
 
int check_mta_support ()
 Find s-nail version to check if mta is supported. More...
 
char * get_email_command (scheduler_t *scheduler, char *user_email)
 Build command to run to send email. More...
 

Detailed Description

Database related operations.

Definition in file database.c.

Macro Definition Documentation

◆ EMAIL_ERROR

#define EMAIL_ERROR (   ...)
Value:
{ \
WARNING(__VA_ARGS__); \
email_notify = 0; \
error = NULL; }

Definition at line 37 of file database.c.

Function Documentation

◆ check_mta_support()

int check_mta_support ( )

Find s-nail version to check if mta is supported.

Returns
1 if mta is supported, 0 if not

Definition at line 1039 of file database.c.

◆ check_tables()

static void check_tables ( scheduler_t scheduler)
static

Checks that any part of the database used by the scheduler is correct.

This has a static list of all tables and the associated columns used by the scheduler. If any changes that affect the tables and columns used by the scheduler are made, this static list should be updated.

Parameters
schedulerthe scheduler_t* that holds the connection

Definition at line 655 of file database.c.

◆ database_connect()

static PGconn* database_connect ( gchar *  configdir)
static

Creates and performs error checking for a new database connection.

Parameters
configdirthe location of the Db.conf file
Returns
a new database connection

Definition at line 749 of file database.c.

◆ database_exec()

PGresult* database_exec ( scheduler_t scheduler,
const char *  sql 
)

Executes an sql statement for the scheduler.

This is used in case the database connection is lost. The scheduler requires a connection to the database to correctly operate. So if the connection is ever lost we automatically try to reconnect and if we are unable to, the scheduler will die.

Parameters
schedulerthe scheduler_t* that holds the connection
sqlthe sql that will be performed
Returns
the PGresult struct that is returned by PQexec

Definition at line 803 of file database.c.

◆ database_exec_event()

void database_exec_event ( scheduler_t scheduler,
char *  sql 
)
Todo:
Parameters
scheduler
sql

Definition at line 827 of file database.c.

◆ database_init()

void database_init ( scheduler_t scheduler)

Initializes any one-time attributes relating to the database. Currently this includes creating the db connection and checking the URL of the FOSSology instance out of the db.

Definition at line 770 of file database.c.

◆ database_job_log()

void database_job_log ( int  j_id,
char *  log_name 
)

Enters the name of the log file for a job into the database.

Parameters
j_idthe id number for the relevant job
log_namethe name of the log file

Definition at line 1006 of file database.c.

◆ database_job_priority()

void database_job_priority ( scheduler_t scheduler,
job_t job,
int  priority 
)

Changes the priority of a job queue entry in the database.

Parameters
schedulerThe scheduler_t* that holds the connection
jobthe job to change the priority for
prioritythe new priority of the job

Definition at line 1021 of file database.c.

◆ database_job_processed()

void database_job_processed ( int  j_id,
int  num 
)

Updates the number of items that a job queue entry has processed.

Parameters
j_idthe id number of the job queue entry
numthe number of items processed in total

Definition at line 992 of file database.c.

◆ database_reset_queue()

void database_reset_queue ( scheduler_t scheduler)

Resets any jobs in the job queue that are not completed.

This is to make sure that any jobs that were running with the scheduler shutdown are run correctly when it starts up again.

Definition at line 931 of file database.c.

◆ database_update_event()

void database_update_event ( scheduler_t scheduler,
void *  unused 
)

Checks the job queue for any new entries.

Parameters
schedulerThe scheduler_t* that holds the connection
unused

Definition at line 841 of file database.c.

◆ database_update_job()

void database_update_job ( scheduler_t scheduler,
job_t job,
job_status  status 
)

Change the status of a job in the database.

Parameters
schedulerThe scheduler_t* that holds the connection
jobjob_t* object for the job
statusthe new status of the job

Definition at line 945 of file database.c.

◆ email_checkjobstatus()

static gint email_checkjobstatus ( scheduler_t scheduler,
job_t job 
)
static

Checks the database for the status of the job.

Parameters
schedulerCurrent scheduler reference
jobThe job to check for the job status on
Returns
0: job is not finished, 1: job has finished, 2: job has failed

Definition at line 365 of file database.c.

◆ email_init()

void email_init ( scheduler_t scheduler)

Loads information about the email that will be sent for job notifications.

This loads the header and footer configuration files, loads the subject and client info, and compiles the regex that is used to replace variables in the header and footer files.

Parameters
[in,out]schedulerCurrent scheduler to init
Returns
void, no return

Definition at line 557 of file database.c.

◆ email_notification()

static void email_notification ( scheduler_t scheduler,
job_t job 
)
static

Sends an email notification that a particular job has completed correctly. This compiles the email based upon the header file, footer file, and the job that just completed.

Parameters
schedulerCurrent scheduler reference
jobThe job that just finished
Returns
void, no return

Definition at line 429 of file database.c.

◆ email_replace()

static gboolean email_replace ( const GMatchInfo *  match,
GString *  ret,
email_replace_args args 
)
static

Replaces the variables that are in the header and footer files.

This is a callback function that is passed to the glib function g_regex_replace_eval(). This reads what was matched by the regex and then appends the correct information onto the GString that is passed to the function.

Variables:
$UPLOADNAME
$BROWSELINK
$SCHEDULERLOG
$UPLOADFOLDERNAME
$JOBRESULT
$JOBQUEUELINK
$AGENTSTATUS
$DB.table.column [not implemented]

Parameters
matchthe regex match that glib found
retthe GString* that results should be appended to.
argsThe email replace arguments with foss url and job reference
Returns
always FALSE so that g_regex_replace_eval() will continue
Todo:
needs implementation of $DB.table.column

Definition at line 84 of file database.c.

◆ get_email_command()

char* get_email_command ( scheduler_t scheduler,
char *  user_email 
)

Build command to run to send email.

Parameters
schedulerCurrent scheduler object
user_emailEmail id to send mail to
Returns
The command to run

Definition at line 1073 of file database.c.