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 strncpy(cur.
pFile, PQgetvalue(result, i, 1),
sizeof(cur.
pFile) - 1);
110 cur.
pFileFk = atoi(PQgetvalue(result, i, 0));
114 LOG_FATAL(
"Nomos unable to open pfile_pk: %ld, file: %s", cur.
pFileFk, cur.
pFile);
124 LOG_FATAL(
"nomos terminating upload %d scan due to previous errors.",
upload_pk);
143 void list_dir (
const char * dir_name,
int process_count,
int *distribute_count, FILE **
pFile)
145 struct dirent *dirent_handler;
148 if ((dir_handler = opendir(dir_name)) == NULL)
150 fprintf(stderr,
"Can't open: %s, error: %s\n", dir_name, strerror(errno));
154 char filename_buf[PATH_MAX] = {};
155 struct stat stat_buf ;
157 while ((dirent_handler = readdir(dir_handler)) != NULL)
161 snprintf(filename_buf,
sizeof(filename_buf),
"%s/%s", dir_name, dirent_handler->d_name);
163 if (stat(filename_buf, &stat_buf) == -1)
165 LOG_FATAL(
"Unable to stat file: %s, error message: %s\n", filename_buf, strerror(errno)) ;
166 closedir(dir_handler);
173 if (strcmp (dirent_handler->d_name,
"..") != 0 && strcmp (dirent_handler->d_name,
".") != 0)
176 if ((stat_buf.st_mode & S_IFMT) == S_IFDIR)
178 list_dir(filename_buf, process_count, distribute_count, pFile);
181 strncat(filename_buf,
"\n", PATH_MAX - 1);
183 file_number = *distribute_count%process_count;
184 fwrite (filename_buf,
sizeof(
char), strlen(filename_buf), pFile[file_number]);
185 (*distribute_count)++;
187 if (process_count == *distribute_count) *distribute_count = 0;
192 closedir(dir_handler);
209 while ((read = getline(&line, &len, pFile[file_number])) != -1) {
212 lenth_tmp = strlen(line);
214 while(isspace(line[lenth_tmp - 1])) line[--lenth_tmp] = 0;
215 while(isspace(*line)) ++line;
222 if (line) free(line);
231 void myFork(
int proc_num, FILE **pFile) {
237 LOG_FATAL(
"fork failed\n");
247 myFork(proc_num - 1, pFile);
259 int main(
int argc,
char **argv)
265 char sErrorBuf[1024];
266 char *agent_desc =
"License Scanner";
267 char **files_to_be_scanned;
268 char *COMMIT_HASH = NULL;
269 char *VERSION = NULL;
270 char agent_rev[myBUFSIZ];
272 char *scanning_directory= NULL;
273 int process_count = 0;
274 bool ignoreFilesWithMimeType =
false;
281 traceFunc(
"== main(%d, %p)\n", argc, argv);
284 #ifdef MEMORY_TRACING
288 gl.DEEBUG = gl.MEM_DEEBUG = 0;
291 files_to_be_scanned = calloc(argc,
sizeof(
char *));
295 snprintf(agent_rev,
sizeof(agent_rev),
"%s.%s", VERSION, COMMIT_HASH);
300 if ((cp = strrchr(*argv,
'/')) ==
NULL_STR)
306 while (*cp ==
'.' || *cp ==
'/')
311 if (putenv(
"LANG=C") < 0)
313 char * estr = strerror_r(errno, sErrorBuf,
sizeof(sErrorBuf));
314 LOG_FATAL(
"Cannot set LANG=C in environment. Error: %s", estr)
321 char *estr = strerror_r(errno, sErrorBuf,
sizeof(sErrorBuf));
322 LOG_FATAL(
"Cannot obtain starting directory. Error: %s", estr)
341 while ((c = getopt(argc, argv,
"VJSNvhiIlc:d:n:")) != -1)
350 gl.
progOpts |= OPTS_LONG_CMD_OUTPUT;
358 gl.
progOpts |= OPTS_HIGHLIGHT_STDOUT;
361 gl.
progOpts |= OPTS_NO_HIGHLIGHTINFO;
367 gl.
progOpts |= OPTS_SCANNING_DIRECTORY;
368 scanning_directory = optarg;
370 int ret = stat(scanning_directory, &dir_sta);
371 if (-1 == ret || S_IFDIR != (dir_sta.st_mode & S_IFMT))
373 if (-1 == ret) printf(
"stat('%s') error message: %s.\n",scanning_directory, strerror(errno));
374 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);
380 process_count = atoi(optarg);
383 ignoreFilesWithMimeType =
true;
394 for (i = optind; i < argc; i++)
396 files_to_be_scanned[file_count] = argv[i];
403 if (file_count == 0 && !scanning_directory)
410 char (*pTempFileName)[50] = NULL;
415 if (scanning_directory) {
416 if (process_count < 2) process_count = 2;
420 printf(
"{\n\"results\":[\n");
423 pFile = malloc(process_count*(
sizeof(FILE*)));
424 pTempFileName = malloc(process_count*
sizeof(
char[50]));
426 int file_descriptor = 0;
427 for(i = 0; i < process_count; i++)
430 char file_template[] =
"/tmp/foss-XXXXXX";
431 file_descriptor = mkstemp(file_template);
434 pFile[i] = fdopen(file_descriptor,
"w");
437 LOG_FATAL(
"failed to open %s, %s\n", file_template, strerror(errno));
439 strncpy(pTempFileName[i], file_template,
sizeof(pTempFileName[i]) - 1);
440 pTempFileName[i][
sizeof(pTempFileName[i]) - 1] =
'\0';
445 int distribute_count = 0;
446 list_dir(scanning_directory, process_count, &distribute_count, pFile);
450 for(i = 0; i < process_count; i++)
452 if (pFile[i]) fclose(pFile[i]);
453 pFile[i] = fopen(pTempFileName[i],
"r");
458 myFork(process_count - 1, pFile);
461 if (mainPid == getpid())
465 wpid = wait(&status);
466 if (-1 == wpid)
break;
470 for(i = 0; i < process_count; i++)
475 unlink(pTempFileName[i]);
491 if (0 != process_count)
493 printf(
"Warning: -n {nprocs} ONLY works with -d {directory}.\n");
498 printf(
"{\n\"results\":[\n");
501 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.