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