34 #include "nomos_utils.h" 46 char BuildVersion[]=
"nomos build version: " VERSION_S
" r(" COMMIT_HASH_S
").\n";
73 char *AgentARSName =
"nomos_ars";
90 LOG_ERROR(
"You have no update permissions on upload %d", upload_pk);
96 if (PQntuples(result) != 0)
98 LOG_NOTICE(
"Ignoring requested nomos analysis of upload %d - Results are already in database.", upload_pk);
110 numrows = PQntuples(result);
112 for (i = 0; i < numrows; i++)
115 strcpy(cur.
pFile, PQgetvalue(result, i, 1));
116 cur.
pFileFk = atoi(PQgetvalue(result, i, 0));
120 LOG_FATAL(
"Nomos unable to open pfile_pk: %ld, file: %s", cur.
pFileFk, cur.
pFile);
130 LOG_FATAL(
"nomos terminating upload %d scan due to previous errors.", upload_pk);
149 void list_dir (
const char * dir_name,
int process_count,
int *distribute_count, FILE **
pFile)
151 struct dirent *dirent_handler;
154 if ((dir_handler = opendir(dir_name)) == NULL)
156 fprintf(stderr,
"Can't open: %s, error: %s\n", dir_name, strerror(errno));
160 char filename_buf[PATH_MAX] = {};
161 struct stat stat_buf ;
163 while ((dirent_handler = readdir(dir_handler)) != NULL)
167 sprintf( filename_buf ,
"%s/%s",dir_name, dirent_handler->d_name);
169 if (stat(filename_buf, &stat_buf) == -1)
171 LOG_FATAL(
"Unable to stat file: %s, error message: %s\n", filename_buf, strerror(errno)) ;
172 closedir(dir_handler);
179 if (strcmp (dirent_handler->d_name,
"..") != 0 && strcmp (dirent_handler->d_name,
".") != 0)
182 if ((stat_buf.st_mode & S_IFMT) == S_IFDIR)
184 list_dir(filename_buf, process_count, distribute_count, pFile);
187 sprintf(filename_buf,
"%s\n", filename_buf);
189 file_number = *distribute_count%process_count;
190 fwrite (filename_buf,
sizeof(
char), strlen(filename_buf), pFile[file_number]);
191 (*distribute_count)++;
193 if (process_count == *distribute_count) *distribute_count = 0;
198 closedir(dir_handler);
215 while ((read = getline(&line, &len, pFile[file_number])) != -1) {
218 lenth_tmp = strlen(line);
220 while(isspace(line[lenth_tmp - 1])) line[--lenth_tmp] = 0;
221 while(isspace(*line)) ++line;
228 if (line) free(line);
237 void myFork(
int proc_num, FILE **pFile) {
243 LOG_FATAL(
"fork failed\n");
253 myFork(proc_num - 1, pFile);
265 int main(
int argc,
char **argv)
271 char sErrorBuf[1024];
272 char *agent_desc =
"License Scanner";
273 char **files_to_be_scanned;
274 char *COMMIT_HASH = NULL;
275 char *VERSION = NULL;
276 char agent_rev[myBUFSIZ];
278 char *scanning_directory= NULL;
279 int process_count = 0;
280 bool ignoreFilesWithMimeType =
false;
287 traceFunc(
"== main(%d, %p)\n", argc, argv);
290 #ifdef MEMORY_TRACING 294 gl.DEEBUG = gl.MEM_DEEBUG = 0;
297 files_to_be_scanned = calloc(argc,
sizeof(
char *));
301 sprintf(agent_rev,
"%s.%s", VERSION, COMMIT_HASH);
306 if ((cp = strrchr(*argv,
'/')) ==
NULL_STR)
312 while (*cp ==
'.' || *cp ==
'/')
317 if (putenv(
"LANG=C") < 0)
319 char * estr = strerror_r(errno, sErrorBuf,
sizeof(sErrorBuf));
320 LOG_FATAL(
"Cannot set LANG=C in environment. Error: %s", estr)
327 char *estr = strerror_r(errno, sErrorBuf,
sizeof(sErrorBuf));
328 LOG_FATAL(
"Cannot obtain starting directory. Error: %s", estr)
342 LOG_FATAL(
"Nomos could not allocate %d cacheroot nodes.", cacheroot.
maxnodes)
347 while ((c = getopt(argc, argv,
"VJSNvhiIlc:d:n:")) != -1)
356 gl.
progOpts |= OPTS_LONG_CMD_OUTPUT;
364 gl.
progOpts |= OPTS_HIGHLIGHT_STDOUT;
367 gl.
progOpts |= OPTS_NO_HIGHLIGHTINFO;
373 gl.
progOpts |= OPTS_SCANNING_DIRECTORY;
374 scanning_directory = optarg;
376 int ret = stat(scanning_directory, &dir_sta);
377 if (-1 == ret || S_IFDIR != (dir_sta.st_mode & S_IFMT))
379 if (-1 == ret) printf(
"stat('%s') error message: %s.\n",scanning_directory, strerror(errno));
380 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);
386 process_count = atoi(optarg);
389 ignoreFilesWithMimeType =
true;
400 for (i = optind; i < argc; i++)
402 files_to_be_scanned[file_count] = argv[i];
409 if (file_count == 0 && !scanning_directory)
411 arsNomos(&cacheroot, ignoreFilesWithMimeType);
416 char (*pTempFileName)[50] = NULL;
421 if (scanning_directory) {
422 if (process_count < 2) process_count = 2;
423 if (mutexJson == NULL &&
optionIsSet(OPTS_JSON_OUTPUT))
426 printf(
"{\n\"results\":[\n");
429 pFile = malloc(process_count*(
sizeof(FILE*)));
430 pTempFileName = malloc(process_count*
sizeof(
char[50]));
432 int file_descriptor = 0;
433 for(i = 0; i < process_count; i++)
436 char file_template[] =
"/tmp/foss-XXXXXX";
437 file_descriptor = mkstemp(file_template);
440 pFile[i] = fdopen(file_descriptor,
"w");
443 LOG_FATAL(
"failed to open %s, %s\n", file_template, strerror(errno));
445 strcpy(pTempFileName[i], file_template);
450 int distribute_count = 0;
451 list_dir(scanning_directory, process_count, &distribute_count, pFile);
455 for(i = 0; i < process_count; i++)
457 if (pFile[i]) fclose(pFile[i]);
458 pFile[i] = fopen(pTempFileName[i],
"r");
463 myFork(process_count - 1, pFile);
466 if (mainPid == getpid())
470 wpid = wait(&status);
471 if (-1 == wpid)
break;
475 for(i = 0; i < process_count; i++)
480 unlink(pTempFileName[i]);
496 if (0 != process_count)
498 printf(
"Warning: -n {nprocs} ONLY works with -d {directory}.\n");
503 printf(
"{\n\"results\":[\n");
506 for (i = 0; i < file_count; i++) {
int Verbose
Verbose level.
int isFILE(char *pathname)
Check if an inode is a file.
FUNCTION int recordScanToDB(cacheroot_t *pcroot, struct curScan *scanRecord)
Write out the information about the scan to the FOSSology database.
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
int maxnodes
No. of nodes in the list.
cachenode_t * nodes
Array of nodes.
int optionIsSet(int val)
Check if an CLI option is set.
Structure holding data truly global in that it remains consistent for each file scanned.
char * fo_scheduler_current()
Get the last read string from the scheduler.
void Bail(int exitval)
Close connections and exit.
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
void arsNomos(cacheroot_t *cacheroot, bool ignoreFilesWithMimeType)
Make entry in ars table for audit.
char initwd[myBUFSIZ]
CDB, would like to workaround/eliminate.
FUNCTION int processFile(PGconn *pgConn, pbucketdef_t bucketDefArray, puploadtree_t puploadtree, int agent_pk, int hasPrules)
Process a file.
fo_dbManager * dbManager
FOSSology DB manager.
FUNCTION void lrcache_free(cacheroot_t *pcroot)
Free the hash table.
void myFork(int proc_num, FILE **pFile)
the recursive create process and process grabbing licenses
fo_dbManager * fo_dbManager_new(PGconn *dbConnection)
Create and initialize new fo_dbManager object.
void licenseInit()
license initialization
int fo_scheduler_userID()
Gets the id of the user that created the job that the agent is running.
Usage()
Print Usage statement.
FUNCTION void freeAndClearScan(struct curScan *thisScan)
Clean-up all the per scan data structures, freeing any old data.
char * fo_RepMkPath(const char *Type, char *Filename)
Given a filename, construct the full path to the file.
void read_file_grab_license(int file_number, FILE **pFile)
read line by line, then call processFile to grab license line by line
int main(int argc, char **argv)
#define NULL_STR
NULL string.
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.
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.
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...
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
FUNCTION int GetUploadPerm(PGconn *pgConn, long UploadPk, int user_pk)
Get users permission to this upload.
Struct that tracks state related to current file being scanned.
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...
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.
FUNCTION int initLicRefCache(cacheroot_t *pcroot)
build a cache the license ref db table.
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 void initializeCurScan(struct curScan *cur)
Initialize the scanner.
char progName[64]
Program name.
#define PERM_WRITE
Read-Write permission.
PGconn * pgConn
DB Connection.