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

Dummy implementations of library functions for stand-alone operations. More...

#include "standalone.h"
Include dependency graph for standalone.c:

Go to the source code of this file.

Functions

void fo_scheduler_heart (int i)
 This function must be called by agents to let the scheduler know they are alive and how many items they have processed. More...
 
void fo_scheduler_connect (int *argc, char **argv, PGconn **db_conn)
 Establish a connection between an agent and the scheduler. More...
 
void fo_scheduler_disconnect (int retcode)
 Disconnect the scheduler connection. More...
 
char * fo_scheduler_next ()
 Get the next data to process from the scheduler. More...
 
char * fo_scheduler_current ()
 Get the last read string from the scheduler. More...
 
int fo_scheduler_userID ()
 Gets the id of the user that created the job that the agent is running. More...
 
void fo_scheduler_set_special (int option, int value)
 Sets something special about the agent within the scheduler. More...
 
int fo_scheduler_get_special (int option)
 Gets if a particular special attribute is set in the scheduler. More...
 
char * fo_sysconfig (const char *sectionname, const char *variablename)
 gets a system configuration variable from the configuration data. More...
 
int fo_GetAgentKey (PGconn *pgConn, const char *agent_name, long unused, const char *cpunused, const char *agent_desc)
 Get the latest enabled agent key (agent_pk) from the database. More...
 
int fo_WriteARS (PGconn *pgConn, int ars_pk, int upload_pk, int agent_pk, const char *tableName, const char *ars_status, int ars_success)
 Write ars record. More...
 
PGconn * fo_dbconnect (char *DBConfFile, char **ErrorBuf)
 Connect to a database. The default is Db.conf. More...
 
int fo_checkPQcommand (PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
 Check the result status of a postgres commands (not select) If an error occured, write the error to stdout. More...
 
int fo_checkPQresult (PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
 Check the result status of a postgres SELECT. More...
 
int fo_tableExists (PGconn *pgConn, const char *tableName)
 Check if table exists. Note, this assumes the database name is 'fossology'. More...
 
char * fo_RepMkPath (char *Type, char *Filename)
 
int GetUploadPerm (PGconn *pgConn, long UploadPk, int user_pk)
 Get users permission to this upload. More...
 
fo_dbManagerfo_dbManager_new (PGconn *dbConnection)
 Create and initialize new fo_dbManager object. More...
 
void fo_dbManager_free (fo_dbManager *dbManager)
 Un-allocate the memory from a DB manager. More...
 
fo_dbManager_PreparedStatementfo_dbManager_PrepareStamement_str (fo_dbManager *dbManager, const char *name, const char *query, const char *paramtypes)
 Create a prepared statement. More...
 
PGresult * fo_dbManager_ExecPrepared (fo_dbManager_PreparedStatement *preparedStatement,...)
 Execute a prepared statement. More...
 
PGresult * getSelectedPFiles (PGconn *pgConn, int uploadPk, int agentPk, bool ignoreFilesWithMimeType)
 Get the upload_pk, agent_pk and ignoreFilesWithMimeType to get all the file Ids for nomos. More...
 
PGresult * checkDuplicateReq (PGconn *pgConn, int uploadPk, int agentPk)
 Get the upload_pk and agent_pk to find out the agent has already scanned the package. More...
 
int PQresultStatus (const PGresult *res)
 
char * PQresultErrorMessage (const PGresult *res)
 
char * PQresultErrorField (const PGresult *res, int fieldcode)
 
int PQntuples (const PGresult *res)
 
PGresult * PQexec (PGconn *conn, const char *query)
 
void PQclear (PGresult *res)
 
char * PQgetvalue (const PGresult *res, int tup_num, int field_num)
 
size_t PQescapeStringConn (PGconn *conn, char *to, const char *from, size_t length, int *error)
 
void PQfinish (PGconn *conn)
 

Variables

int result = 0
 

Detailed Description

Dummy implementations of library functions for stand-alone operations.

Definition in file standalone.c.

Function Documentation

◆ checkDuplicateReq()

PGresult* checkDuplicateReq ( PGconn *  pgConn,
int  uploadPk,
int  agentPk 
)

Get the upload_pk and agent_pk to find out the agent has already scanned the package.

Parameters
pgConnDatabase connection object pointer.
upload_pkUpload ID
agent_pkagentPk
Returns
result, ars_pk if the agent has already scanned the package.
Note
Caller must free the (non-null) returned value.

Definition at line 39 of file standalone.c.

◆ fo_checkPQcommand()

int fo_checkPQcommand ( PGconn *  pgConn,
PGresult *  result,
char *  sql,
char *  FileID,
int  LineNumb 
)

Check the result status of a postgres commands (not select) If an error occured, write the error to stdout.

Parameters
pgConnDatabase connection object
resultPostgres result object
sqlthe sql query
FileIDis a file identifier string to write into the error message. Typically the caller will use FILE, but any identifier string is ok.
LineNumbthe line number of the caller (LINE)
Returns
0 on OK, -1 on failure. On failure, result will be freed.

Definition at line 27 of file standalone.c.

◆ fo_checkPQresult()

int fo_checkPQresult ( PGconn *  pgConn,
PGresult *  result,
char *  sql,
char *  FileID,
int  LineNumb 
)

Check the result status of a postgres SELECT.

If an error occured, write the error to stdout

Parameters
pgConnDatabase connection object
resultPostgres result object
sqlthe sql query
FileIDis a file identifier string to write into the error message. Typically the caller will use FILE, but any identifier string is ok.
LineNumbthe line number of the caller (LINE)
Returns
0 on OK, -1 on failure. On failure, result will be freed.

Definition at line 28 of file standalone.c.

◆ fo_dbconnect()

PGconn* fo_dbconnect ( char *  DBConfFile,
char **  ErrorBuf 
)

Connect to a database. The default is Db.conf.

Parameters
DBConfFileFile path of the Db.conf file to use. If NULL, use the default Db.conf
ErrorBufAddress of pointer to error buffer. fo_dbconnect will allocate this if needed. The caller should free it.
Returns
PGconn*, or NULL on failure to process the config file. If NULL, ErrorBuff will contain the error message. If NULL is returned and ErrorBuf is NULL, then there was insufficient memory to allocate ErrorBuf.

Definition at line 26 of file standalone.c.

◆ fo_dbManager_ExecPrepared()

PGresult* fo_dbManager_ExecPrepared ( fo_dbManager_PreparedStatement preparedStatement,
  ... 
)

Execute a prepared statement.

Parameters
preparedStatementPrepared statement
Returns
Result on success; NULL otherwise
See also
fo_dbManager_ExecPreparedv()

Definition at line 36 of file standalone.c.

◆ fo_dbManager_free()

void fo_dbManager_free ( fo_dbManager dbManager)

Un-allocate the memory from a DB manager.

The function applies following actions on the manager before calling free

  1. Unref the cached table
  2. Free the DB conf file location
  3. Close the log file FP
    Parameters
    dbManagerThe DB manager to be free-ed

Definition at line 34 of file standalone.c.

◆ fo_dbManager_new()

fo_dbManager* fo_dbManager_new ( PGconn *  dbConnection)

Create and initialize new fo_dbManager object.

Parameters
dbConnectionDB connection object
Returns
New DB manager object

Definition at line 33 of file standalone.c.

◆ fo_dbManager_PrepareStamement_str()

fo_dbManager_PreparedStatement* fo_dbManager_PrepareStamement_str ( fo_dbManager dbManager,
const char *  name,
const char *  query,
const char *  paramtypes 
)

Create a prepared statement.

Parameters
dbManagerDB manager to use
nameName of the prepared statement
queryQuery to be perpared
paramtypesCSV list of parameter types
Returns
Prepared statement on success;
NULL otherwise;
See also
parseParamStr()

Definition at line 35 of file standalone.c.

◆ fo_GetAgentKey()

int fo_GetAgentKey ( PGconn *  pgConn,
const char *  agent_name,
long  unused,
const char *  cpunused,
const char *  agent_desc 
)

Get the latest enabled agent key (agent_pk) from the database.

Parameters
pgConnDatabase connection object pointer.
agent_nameName of agent to look up.
Upload_pkis no longer used.
revAgent revision, if given this is the exact revision of the agent being requested.
agent_descDescription of the agent. Used to write a new agent record in the case where no enabled agent records exist for this agent_name.
Returns
On success return agent_pk. On sql failure, return 0, and the error will be written to stdout.
Todo:
This function is not checking if the agent is enabled. And it is not setting agent version when an agent record is inserted.

Definition at line 23 of file standalone.c.

◆ fo_scheduler_connect()

void fo_scheduler_connect ( int *  argc,
char **  argv,
PGconn **  db_conn 
)

Establish a connection between an agent and the scheduler.

Steps taken by this function:

  • initialize memory associated with agent connection
  • send "SPAWNED" to the scheduler
  • receive the number of items between notifications
  • check the nfs mounts for the agent
  • set up the heartbeat()

Making a call to this function should be the first thing that an agent does after parsing its command line arguments.

If the database connection passed is NULL, then this will not return a database connection, and will not check the agent's database record.

Parameters
argcpointer to the number of arguments passed to the agent
argvthe command line arguments for the agent
db_connpointer to the location for the database connection
Returns
void

Definition at line 15 of file standalone.c.

◆ fo_scheduler_current()

char* fo_scheduler_current ( )

Get the last read string from the scheduler.

Returns
Returns the string buffer if it is valid. If it is not valid, return NULL. The buffer is not valid if the last received data from the scheduler was a command, rather than data to operate on.

Definition at line 18 of file standalone.c.

◆ fo_scheduler_disconnect()

void fo_scheduler_disconnect ( int  retcode)

Disconnect the scheduler connection.

Making a call to this function should be the last thing that an agent does before exiting. Any error reporting to stdout or stderr will not work after this function has finished execution.

Parameters
retcodeReturn code to the scheduler

Definition at line 16 of file standalone.c.

◆ fo_scheduler_get_special()

int fo_scheduler_get_special ( int  option)

Gets if a particular special attribute is set in the scheduler.

Possible Options:

  • SPECIAL_NOKILL : the agent will not be killed if it stops updating status
  • SPECIAL_EXCLUSIVE: the agent cannot run simultaneously with any other agent
  • SPECIAL_NOEMAIL : the scheduler will not send notification emails for this agent
  • SPECIAL_LOCAL : the agent is required to run on the same machine as the scheduler
Parameters
optionthe relevant option to the get the value of
Returns
if the value of the special option was true

Definition at line 21 of file standalone.c.

◆ fo_scheduler_heart()

void fo_scheduler_heart ( int  i)

This function must be called by agents to let the scheduler know they are alive and how many items they have processed.

Parameters
iThis is the number of itmes processed since the last call to fo_scheduler_heart()
Returns
void

Definition at line 14 of file standalone.c.

◆ fo_scheduler_next()

char* fo_scheduler_next ( )

Get the next data to process from the scheduler.

It is the job of the agent to decide how this string is interpreted.

Steps taken by this function:

  • get the next line from the scheduler
    • if the scheduler has paused this agent this will block till unpaused
  • check for "CLOSE" from scheduler, return NULL if received
  • check for "VERBOSE" from scheduler
    • if this is received turn the verbose flag to whatever is specified
    • a new line must be received, perform same task (i.e. recursive call)
  • check for "END" from scheduler, if received print OK and recurse
    • this is used to simplify communications within the scheduler
  • return whatever has been received
Returns
char* for the next thing to analyze, NULL if there is nothing left in this job, in which case the agent should close

Definition at line 17 of file standalone.c.

◆ fo_scheduler_set_special()

void fo_scheduler_set_special ( int  option,
int  value 
)

Sets something special about the agent within the scheduler.

Possible Options: SPECIAL_NOKILL: instruct the scheduler to not kill the agent

Parameters
optionthe option to set
valuewhether to set the option to true or false

Definition at line 20 of file standalone.c.

◆ fo_scheduler_userID()

int fo_scheduler_userID ( )

Gets the id of the user that created the job that the agent is running.

Returns
the user id

Definition at line 19 of file standalone.c.

◆ fo_sysconfig()

char* fo_sysconfig ( const char *  sectionname,
const char *  variablename 
)

gets a system configuration variable from the configuration data.

This function should be called after fo_scheduler_connect has been called. This is because the configuration data it not loaded until after that.

Parameters
sectionnamethe group of the variable
variablenamethe name of the variable
Returns
the value of the variable

Definition at line 22 of file standalone.c.

◆ fo_tableExists()

int fo_tableExists ( PGconn *  pgConn,
const char *  tableName 
)

Check if table exists. Note, this assumes the database name is 'fossology'.

Parameters
pgConndatabase connection
tableNameThe table in question
Returns
1 if table exists, 0 on error (which is logged) or if table does not exist.

Definition at line 29 of file standalone.c.

◆ fo_WriteARS()

int fo_WriteARS ( PGconn *  pgConn,
int  ars_pk,
int  upload_pk,
int  agent_pk,
const char *  tableName,
const char *  ars_status,
int  ars_success 
)

Write ars record.

If the ars table does not exist, one is created by inheriting the ars_master table. The new table is called {tableName}. For example, "unpack_ars". If ars_pk is zero a new ars record will be created. Otherwise, it is updated.

Parameters
pgConnDatabase connection object pointer.
ars_pkIf zero, a new record will be created.
upload_pkID of the upload
agent_pkAgents should get this from fo_GetAgentKey()
tableNamears table name
ars_statusStatus to update ars_status. May be null.
ars_successAutomatically set to false if ars_pk is zero.
Returns
On success write the ars record and return the ars_pk. On sql failure, return 0, and the error will be written to stdout.

Definition at line 24 of file standalone.c.

◆ getSelectedPFiles()

PGresult* getSelectedPFiles ( PGconn *  pgConn,
int  uploadPk,
int  agentPk,
bool  ignoreFilesWithMimeType 
)

Get the upload_pk, agent_pk and ignoreFilesWithMimeType to get all the file Ids for nomos.

Parameters
pgConnDatabase connection object pointer.
upload_pkuploadPk
agent_pkagentPk
ignoreFilesWithMimeTypeTo ignore Files With MimeType
Returns
the result, the list of pfiles, require to be scan by nomos.
Note
Caller must free the (non-null) returned value.

Definition at line 38 of file standalone.c.

◆ GetUploadPerm()

int GetUploadPerm ( PGconn *  pgConn,
long  UploadPk,
int  user_pk 
)

Get users permission to this upload.

Parameters
pgConnDatabase connection object pointer.
upload_pkUpload ID
user_pkUser ID
Returns
permission (PERM_) this user has for UploadPk

Definition at line 31 of file standalone.c.