22 #include "nomos_utils.h"
39 char BuildVersion[]=
"nomos build version: " VERSION_S
" r(" COMMIT_HASH_S
").\n";
66 char *AgentARSName =
"nomos_ars";
83 LOG_ERROR(
"You have no update permissions on upload %d",
upload_pk);
89 if (PQntuples(result) != 0)
91 LOG_NOTICE(
"Ignoring requested nomos analysis of upload %d - Results are already in database.",
upload_pk);
103 numrows = PQntuples(result);
105 for (i = 0; i < numrows; i++)
108 strcpy(cur.
pFile, PQgetvalue(result, i, 1));
109 cur.
pFileFk = atoi(PQgetvalue(result, i, 0));
113 LOG_FATAL(
"Nomos unable to open pfile_pk: %ld, file: %s", cur.
pFileFk, cur.
pFile);
123 LOG_FATAL(
"nomos terminating upload %d scan due to previous errors.",
upload_pk);
142 void list_dir (
const char * dir_name,
int process_count,
int *distribute_count, FILE **
pFile)
144 struct dirent *dirent_handler;
147 if ((dir_handler = opendir(dir_name)) == NULL)
149 fprintf(stderr,
"Can't open: %s, error: %s\n", dir_name, strerror(errno));
153 char filename_buf[PATH_MAX] = {};
154 struct stat stat_buf ;
156 while ((dirent_handler = readdir(dir_handler)) != NULL)
160 sprintf( filename_buf ,
"%s/%s",dir_name, dirent_handler->d_name);
162 if (stat(filename_buf, &stat_buf) == -1)
164 LOG_FATAL(
"Unable to stat file: %s, error message: %s\n", filename_buf, strerror(errno)) ;
165 closedir(dir_handler);
172 if (strcmp (dirent_handler->d_name,
"..") != 0 && strcmp (dirent_handler->d_name,
".") != 0)
175 if ((stat_buf.st_mode & S_IFMT) == S_IFDIR)
177 list_dir(filename_buf, process_count, distribute_count, pFile);
180 strncat(filename_buf,
"\n", PATH_MAX - 1);
182 file_number = *distribute_count%process_count;
183 fwrite (filename_buf,
sizeof(
char), strlen(filename_buf), pFile[file_number]);
184 (*distribute_count)++;
186 if (process_count == *distribute_count) *distribute_count = 0;
191 closedir(dir_handler);
208 while ((read = getline(&line, &len, pFile[file_number])) != -1) {
211 lenth_tmp = strlen(line);
213 while(isspace(line[lenth_tmp - 1])) line[--lenth_tmp] = 0;
214 while(isspace(*line)) ++line;
221 if (line) free(line);
230 void myFork(
int proc_num, FILE **pFile) {
236 LOG_FATAL(
"fork failed\n");
246 myFork(proc_num - 1, pFile);
258 int main(
int argc,
char **argv)
264 char sErrorBuf[1024];
265 char *agent_desc =
"License Scanner";
266 char **files_to_be_scanned;
267 char *COMMIT_HASH = NULL;
268 char *VERSION = NULL;
269 char agent_rev[myBUFSIZ];
271 char *scanning_directory= NULL;
272 int process_count = 0;
273 bool ignoreFilesWithMimeType =
false;
280 traceFunc(
"== main(%d, %p)\n", argc, argv);
283 #ifdef MEMORY_TRACING
287 gl.DEEBUG = gl.MEM_DEEBUG = 0;
290 files_to_be_scanned = calloc(argc,
sizeof(
char *));
294 sprintf(agent_rev,
"%s.%s", VERSION, COMMIT_HASH);
299 if ((cp = strrchr(*argv,
'/')) ==
NULL_STR)
305 while (*cp ==
'.' || *cp ==
'/')
310 if (putenv(
"LANG=C") < 0)
312 char * estr = strerror_r(errno, sErrorBuf,
sizeof(sErrorBuf));
313 LOG_FATAL(
"Cannot set LANG=C in environment. Error: %s", estr)
320 char *estr = strerror_r(errno, sErrorBuf,
sizeof(sErrorBuf));
321 LOG_FATAL(
"Cannot obtain starting directory. Error: %s", estr)
340 while ((c = getopt(argc, argv,
"VJSNvhiIlc:d:n:")) != -1)
349 gl.
progOpts |= OPTS_LONG_CMD_OUTPUT;
357 gl.
progOpts |= OPTS_HIGHLIGHT_STDOUT;
360 gl.
progOpts |= OPTS_NO_HIGHLIGHTINFO;
366 gl.
progOpts |= OPTS_SCANNING_DIRECTORY;
367 scanning_directory = optarg;
369 int ret = stat(scanning_directory, &dir_sta);
370 if (-1 == ret || S_IFDIR != (dir_sta.st_mode & S_IFMT))
372 if (-1 == ret) printf(
"stat('%s') error message: %s.\n",scanning_directory, strerror(errno));
373 else printf(
"Warning: '%s' from -d is not a good directory(dir_sta.st_mode & S_IFMT = %o).\n", scanning_directory, dir_sta.st_mode & S_IFMT);
379 process_count = atoi(optarg);
382 ignoreFilesWithMimeType =
true;
393 for (i = optind; i < argc; i++)
395 files_to_be_scanned[file_count] = argv[i];
402 if (file_count == 0 && !scanning_directory)
409 char (*pTempFileName)[50] = NULL;
414 if (scanning_directory) {
415 if (process_count < 2) process_count = 2;
419 printf(
"{\n\"results\":[\n");
422 pFile = malloc(process_count*(
sizeof(FILE*)));
423 pTempFileName = malloc(process_count*
sizeof(
char[50]));
425 int file_descriptor = 0;
426 for(i = 0; i < process_count; i++)
429 char file_template[] =
"/tmp/foss-XXXXXX";
430 file_descriptor = mkstemp(file_template);
433 pFile[i] = fdopen(file_descriptor,
"w");
436 LOG_FATAL(
"failed to open %s, %s\n", file_template, strerror(errno));
438 strcpy(pTempFileName[i], file_template);
443 int distribute_count = 0;
444 list_dir(scanning_directory, process_count, &distribute_count, pFile);
448 for(i = 0; i < process_count; i++)
450 if (pFile[i]) fclose(pFile[i]);
451 pFile[i] = fopen(pTempFileName[i],
"r");
456 myFork(process_count - 1, pFile);
459 if (mainPid == getpid())
463 wpid = wait(&status);
464 if (-1 == wpid)
break;
468 for(i = 0; i < process_count; i++)
473 unlink(pTempFileName[i]);
489 if (0 != process_count)
491 printf(
"Warning: -n {nprocs} ONLY works with -d {directory}.\n");
496 printf(
"{\n\"results\":[\n");
499 for (i = 0; i < file_count; i++) {
Usage()
Print Usage statement.
FUNCTION int GetUploadPerm(PGconn *pgConn, long UploadPk, int user_pk)
Get users permission to this upload.
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.
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.
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.
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.
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
#define PERM_WRITE
Read-Write permission.
char * fo_RepMkPath(const char *Type, char *Filename)
Given a filename, construct the full path to the file.
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 th...
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.
FUNCTION void lrcache_free(cacheroot_t *pcroot)
Free the hash table.
void licenseInit()
license initialization
fo_dbManager * fo_dbManager_new(PGconn *dbConnection)
Create and initialize new fo_dbManager object.
int isFILE(char *pathname)
Check if an inode is a file.
void arsNomos(cacheroot_t *cacheroot, bool ignoreFilesWithMimeType)
Make entry in ars table for audit.
int main(int argc, char **argv)
char debugStr[myBUFSIZ]
Debug string.
void read_file_grab_license(int file_number, FILE **pFile)
read line by line, then call processFile to grab license line by line
int Verbose
Verbose level.
size_t hashEntries
Hash entries.
char dbErrString[myBUFSIZ]
DB error string.
void myFork(int proc_num, FILE **pFile)
the recursive create process and process grabbing licenses
void list_dir(const char *dir_name, int process_count, int *distribute_count, FILE **pFile)
list all files and store file paths from the specified directory
#define NULL_STR
NULL string.
void Bail(int exitval)
Close connections and exit.
int optionIsSet(int val)
Check if an CLI option is set.
FUNCTION void freeAndClearScan(struct curScan *thisScan)
Clean-up all the per scan data structures, freeing any old data.
sem_t * mutexJson
Mutex to handle JSON writes.
FUNCTION void initializeCurScan(struct curScan *cur)
Initialize the scanner.
FUNCTION int recordScanToDB(cacheroot_t *pcroot, struct curScan *scanRecord)
Write out the information about the scan to the FOSSology database.
FUNCTION int initLicRefCache(cacheroot_t *pcroot)
build a cache the license ref db table.
int maxnodes
No. of nodes in the list.
cachenode_t * nodes
Array of nodes.
Struct that tracks state related to current file being scanned.
Structure holding data truly global in that it remains consistent for each file scanned.
PGconn * pgConn
DB Connection.
fo_dbManager * dbManager
FOSSology DB manager.
char initwd[myBUFSIZ]
CDB, would like to workaround/eliminate.
char progName[64]
Program name.
FUNCTION int processFile(PGconn *pgConn, pbucketdef_t bucketDefArray, puploadtree_t puploadtree, int agent_pk, int hasPrules)
Process a file.