FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
main.c
Go to the documentation of this file.
1 /***************************************************************
2  Copyright (C) 2010-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  ***************************************************************/
74 #include "pkgagent.h"
75 
76 #ifdef COMMIT_HASH_S
77 char BuildVersion[]="pkgagent build version: " VERSION_S " r(" COMMIT_HASH_S ").\n";
78 #else
79 char BuildVersion[]="pkgagent build version: NULL.\n";
80 #endif
81 
88 int main (int argc, char *argv[])
89 {
90  int c;
91  char *agent_desc = "Pulls metadata out of RPM or DEBIAN packages";
92  //struct rpmpkginfo *glb_rpmpi;
93  //struct debpkginfo *glb_debpi;
94  int Agent_pk;
95  int ars_pk = 0;
96 
97  int upload_pk = 0; // the upload primary key
98  int user_pk = 0; // the upload primary key
99  char *AgentARSName = "pkgagent_ars";
100  int rv;
101  PGresult *ars_result;
102  char sqlbuf[1024];
103  char *COMMIT_HASH;
104  char *VERSION;
105  char agent_rev[MAXCMD];
106  int CmdlineFlag = 0; /* run from command line flag, 1 yes, 0 not */
107 
108  fo_scheduler_connect(&argc, argv, &db_conn);
109 
110  //glb_rpmpi = (struct rpmpkginfo *)malloc(sizeof(struct rpmpkginfo));
111  //glb_debpi = (struct debpkginfo *)malloc(sizeof(struct debpkginfo));
112 
113  COMMIT_HASH = fo_sysconfig("pkgagent", "COMMIT_HASH");
114  VERSION = fo_sysconfig("pkgagent", "VERSION");
115  sprintf(agent_rev, "%s.%s", VERSION, COMMIT_HASH);
116  Agent_pk = fo_GetAgentKey(db_conn, basename(argv[0]), 0, agent_rev, agent_desc);
117 
118  /* Process command-line */
119  while((c = getopt(argc,argv,"ic:CvVh")) != -1)
120  {
121  switch(c)
122  {
123  case 'i':
124  PQfinish(db_conn); /* DB was opened above, now close it and exit */
125  exit(0);
126  case 'v':
127  Verbose++;
128  break;
129  case 'c':
130  break; /* handled by fo_scheduler_connect() */
131  case 'C':
132  CmdlineFlag = 1;
133  break;
134  case 'V':
135  printf("%s", BuildVersion);
136  PQfinish(db_conn);
137  return(0);
138  default:
139  Usage(argv[0]);
140  PQfinish(db_conn);
141  exit(-1);
142  }
143  }
144  /* If no args, run from scheduler! */
145  if (CmdlineFlag == 0)
146  {
147  user_pk = fo_scheduler_userID(); /* get user_pk for user who queued the agent */
148 
149  while(fo_scheduler_next())
150  {
151  upload_pk = atoi(fo_scheduler_current());
152 
153  /* Check Permissions */
154  if (GetUploadPerm(db_conn, upload_pk, user_pk) < PERM_WRITE)
155  {
156  LOG_ERROR("You have no update permissions on upload %d", upload_pk);
157  continue;
158  }
159 
160  if (Verbose) { printf("PKG: pkgagent read %d\n", upload_pk);}
161  if (upload_pk ==0) continue;
162 
163  /* check if pkgagent ars table exist?
164  * if exist, check duplicate request
165  * if not exist, don't check duplicate request
166  */
167  rv = fo_tableExists(db_conn, AgentARSName);
168  if (rv)
169  {
170  /* check ars table to see if this is duplicate request*/
171  snprintf(sqlbuf, sizeof(sqlbuf),
172  "select ars_pk from pkgagent_ars,agent \
173  where agent_pk=agent_fk and ars_success=true \
174  and upload_fk='%d' and agent_fk='%d'",
175  upload_pk, Agent_pk);
176  ars_result = PQexec(db_conn, sqlbuf);
177  if (fo_checkPQresult(db_conn, ars_result, sqlbuf, __FILE__, __LINE__)) exit(-1);
178  if (PQntuples(ars_result) > 0)
179  {
180  PQclear(ars_result);
181  LOG_WARNING("Ignoring requested pkgagent analysis of upload %d - Results are already in database.\n",upload_pk);
182  continue;
183  }
184  PQclear(ars_result);
185  }
186  /* Record analysis start in pkgagent_ars, the pkgagent audit trail. */
187  ars_pk = fo_WriteARS(db_conn, ars_pk, upload_pk, Agent_pk, AgentARSName, 0, 0);
188 
189  /* process the upload_pk pkgagent */
190  if(ProcessUpload(upload_pk) != 0) return -1;
191 
192  /* Record analysis success in pkgagent_ars. */
193  if (ars_pk) fo_WriteARS(db_conn, ars_pk, upload_pk, Agent_pk, AgentARSName, 0, 1);
194  }
195  }
196  else
197  {
198  if (Verbose) { printf("DEBUG: running in cli mode, processing file(s)\n");}
199  for (; optind < argc; optind++)
200  {
201  struct rpmpkginfo *rpmpi;
202  rpmpi = (struct rpmpkginfo *)malloc(sizeof(struct rpmpkginfo));
203  rpmReadConfigFiles(NULL, NULL);
204  //if(ProcessUpload(atoi(argv[optind])) == 0)
205  if(GetMetadata(argv[optind],rpmpi) != -1)
206  printf("OK\n");
207  else
208  printf("Fail\n");
209  rpmFreeCrypto();
210  int i;
211  for(i=0; i< rpmpi->req_size;i++)
212  free(rpmpi->requires[i]);
213  free(rpmpi->requires);
214  free(rpmpi);
215  rpmFreeMacros(NULL);
216  }
217  }
218 
219  PQfinish(db_conn);
221  return(0);
222 } /* 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
int GetMetadata(char *pkg, struct rpmpkginfo *pi)
Get RPM package info.
Definition: pkgagent.c:517
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
int Verbose
Verbose level.
Definition: util.c:28
char * fo_scheduler_current()
Get the last read string from the scheduler.
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
Holds meta info of rpm packages.
Definition: pkgagent.h:49
char ** requires
Package dependency list.
Definition: pkgagent.h:68
pkgagent header
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
int Agent_pk
agent identifier
Definition: finder.c:30
int main(int argc, char *argv[])
main function for the pkgagent
Definition: main.c:88
int fo_tableExists(PGconn *pgConn, const char *tableName)
Check if table exists. Note, this assumes the database name is &#39;fossology&#39;.
Definition: libfossdb.c:243
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
PGconn * db_conn
The connection to Database.
Definition: pkgagent.c:34
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
int req_size
Package dependency list size.
Definition: pkgagent.h:69
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