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