FOSSology  4.4.0
Open Source License Compliance by Open Source Software
demomod.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2013 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
38 #include "demomod.h"
39 
40 #ifdef COMMIT_HASH_S
41 char BuildVersion[]="demomod build version: " VERSION_S " r(" COMMIT_HASH_S ").\n";
42 #else
43 char BuildVersion[]="demomod build version: NULL.\n";
44 #endif
45 
46 /********** Globals *************/
48 PGconn *pgConn = 0;
49 
50 
51 /****************************************************/
52 int main(int argc, char **argv)
53 {
54  char *agentDesc = "demomod demonstration module";
55  char *AgentARSName = "demomod_ars";
56  int cmdopt;
57  PGresult *ars_result;
58  char sqlbuf[512];
59  int agent_pk = 0;
60  int user_pk = 0;
61  char *COMMIT_HASH;
62  char *VERSION;
63  char agent_rev[myBUFSIZ];
64  int upload_pk = 0;
65  int ars_pk = 0;
66  int Unused = 0;
67  int i;
68  FileResult_t FileResult;
69 
70  /* connect to the scheduler */
71  fo_scheduler_connect(&argc, argv, &pgConn);
72  user_pk = fo_scheduler_userID(); /* get user_pk for user who queued the agent */
73 
74  /* get agent pk
75  * Note, if GetAgentKey fails, this process will exit.
76  */
77  COMMIT_HASH = fo_sysconfig("demomod", "COMMIT_HASH");
78  VERSION = fo_sysconfig("demomod", "VERSION");
79  snprintf(agent_rev, sizeof(agent_rev), "%s.%s", VERSION, COMMIT_HASH);
80  agent_pk = fo_GetAgentKey(pgConn, basename(argv[0]), Unused, agent_rev, agentDesc);
81 
82  /* command line options */
83  while ((cmdopt = getopt(argc, argv, "ivVc:")) != -1)
84  {
85  switch (cmdopt)
86  {
87  case 'i': /* "Initialize" */
88  ExitNow(0);
89  case 'v': /* verbose output for debugging */
90  agent_verbose++; // global agent verbose flag. Can be changed in running agent by the scheduler on each fo_scheduler_next() call
91  break;
92  case 'V': /* print version info */
93  printf("%s", BuildVersion);
94  ExitNow(0);
95  case 'c': break; /* handled by fo_scheduler_connect() */
96  default:
97  Usage(argv[0]);
98  ExitNow(-1);
99  }
100  }
101 
102  if (optind >= argc) // if this is true then files weren't specified on the command line, so use the scheduler
103  {
104  /* make sure the demomod and demomod_ars tables exists */
105  CheckTable(AgentARSName);
106 
107  user_pk = fo_scheduler_userID(); /* get user_pk for user who queued the agent */
108 
109  /* The following is used for debugging only!
110  * Once the user_pk is set, you can test this agent as if it were run from the scheduler by
111  * echo 158 | ./demomod -c /usr/local/etc/fossology/
112  * where 158 is the upload_pk
113  */
114  //user_pk=2; // TODO: REMOVE THIS FROM THE PRODUCTION CODE!
115 
116  /* It isn't necessary to use a loop here because this agent only reads one item
117  * from the scheduler, the upload_pk. However, it is possible to queue jobs such
118  * that the scheduler will pass multiple data items into an agent, so I'm just
119  * going to use a 'while' even though for demomod it will only run once.
120  */
121  while(fo_scheduler_next())
122  {
124  LOG_VERBOSE("demomod upload_pk is %d\n", upload_pk);
125  if (upload_pk ==0)
126  {
127  LOG_ERROR("demomod was passed a zero upload_pk. This is an invalid key.");
128  ExitNow(-2);
129  }
130 
131  /* Check Permissions */
132  if (GetUploadPerm(pgConn, upload_pk, user_pk) < PERM_WRITE)
133  {
134  LOG_ERROR("You do not have write permission on upload %d", upload_pk);
135  ExitNow(-3);
136  }
137 
138 
139  /*
140  * check if demomod has already been run on this upload.
141  */
142  snprintf(sqlbuf, sizeof(sqlbuf),
143  "select ars_pk from %s,agent where agent_pk=agent_fk and ars_success=true \
144  and upload_fk='%d' and agent_fk='%d'",
145  AgentARSName, upload_pk, agent_pk);
146  ars_result = PQexec(pgConn, sqlbuf);
147  if (fo_checkPQresult(pgConn, ars_result, sqlbuf, __FILE__, __LINE__)) break;
148  if (PQntuples(ars_result) > 0)
149  {
150  LOG_WARNING("Ignoring requested demomod scan of upload %d - Results are already in the database.\n",upload_pk);
151  PQclear(ars_result);
152  continue;
153  }
154  PQclear(ars_result);
155 
156  /* Record scan start in the agent ars table, this is the agent audit trail. */
157  ars_pk = fo_WriteARS(pgConn, ars_pk, upload_pk, agent_pk, AgentARSName, 0, 0);
158 
159  /* Create the sql copy structure for the demomod table.
160  * The fo_sqlCopy functions are used to batch copy records into the database. This is
161  * much faster than single inserts.
162  * This creates a struct with buffer size of 100,000 bytes, and three columns
163  */
164  psqlcpy = fo_sqlCopyCreate(pgConn, "demomod", 100000, 3, "pfile_fk", "agent_fk", "firstbytes" );
165  if (!psqlcpy) ExitNow(-4);
166 
167  /* process the upload_pk */
168  if(ProcessUpload(upload_pk, agent_pk) != 0) ExitNow(-5);
169 
170  /* Flush the sqlCopy buffer */
172 
173  /* Record scan success in ars table. */
174  fo_WriteARS(pgConn, ars_pk, upload_pk, agent_pk, AgentARSName, 0, 1);
175  }
176  }
177  else
178  {
179  /* loop through files on the command line */
180  for (i=optind; i<argc; i++)
181  {
182  if(ProcessFile(argv[i], &FileResult) != 0) ExitNow(-6);
183  printf("%s: %s \n", argv[i], FileResult.HexStr);
184  }
185  }
186 
187  ExitNow(0); /* success */
188  return(0); /* Never executed but prevents compiler warning */
189 } /* main() */
int agent_pk
Definition: agent.h:74
char BuildVersion[]
Definition: buckets.c:68
FUNCTION int ProcessUpload(int upload_pk, int agent_fk)
Process a single upload - read the first 32 bytes in each file.
Definition: process.c:61
FUNCTION int ProcessFile(char *FilePath, pFileResult_t FileResult)
Process a single file - read the first 32 bytes.
Definition: process.c:24
FUNCTION void ExitNow(int ExitVal)
Exit function. This does all cleanup and should be used instead of calling exit() or main() return.
Definition: utils.c:108
FUNCTION void CheckTable(char *AgentARSName)
Check to make sure the demomod and demomod_ars tables exists.
Definition: utils.c:27
PGconn * pgConn
Database connection.
Definition: demomod.c:48
psqlCopy_t psqlcpy
fo_sqlCopy struct used for fast data insertion
Definition: demomod.c:47
Usage()
Print Usage statement.
Definition: fo_dbcheck.php:63
FUNCTION int GetUploadPerm(PGconn *pgConn, long UploadPk, int user_pk)
Get users permission to this upload.
Definition: libfossagent.c:378
FUNCTION 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.
Definition: libfossagent.c:214
FUNCTION int fo_GetAgentKey(PGconn *pgConn, const char *agent_name, long Upload_pk, const char *rev, const char *agent_desc)
Get the latest enabled agent key (agent_pk) from the database.
Definition: libfossagent.c:158
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
Definition: libfossdb.c:170
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:33
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.
int agent_verbose
Common verbose flags for the agents, this is used so that the scheduler can change the verbose level ...
int fo_scheduler_userID()
Gets the id of the user that created the job that the agent is running.
char * fo_scheduler_current()
Get the last read string from the scheduler.
char * fo_scheduler_next()
Get the next data to process from the scheduler.
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
void fo_sqlCopyDestroy(psqlCopy_t pCopy, int ExecuteFlag)
Destructor for sqlCopy_struct.
Definition: sqlCopy.c:293
psqlCopy_t fo_sqlCopyCreate(PGconn *pGconn, char *TableName, int BufSize, int NumColumns,...)
Constructor for sqlCopy_struct.
Definition: sqlCopy.c:51
const char * upload_pk
Definition: sqlstatements.h:82
char HexStr[(DataSize *2)+1]
Hexadecimal string.
Definition: demomod.h:30