FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
nomos.h
Go to the documentation of this file.
1 /***************************************************************
2  Copyright (C) 2006-2014 Hewlett-Packard Development Company, L.P.
3  Copyright (C) 2014, Siemens AG
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 
18  ***************************************************************/
19 
78 #ifndef _NOMOS_H
79 #define _NOMOS_H 1
80 #ifndef _GNU_SOURCE
81 #define _GNU_SOURCE
82 #endif /* not defined _GNU_SOURCE */
83 #include <stdio.h>
84 #include <assert.h>
85 #include <stdlib.h>
86 #include <search.h>
87 #include <unistd.h>
88 #include <fcntl.h>
89 #include <string.h>
90 #include <strings.h>
91 #include <ctype.h>
92 #include <dirent.h>
93 #include <ftw.h>
94 #include <regex.h>
95 #include <getopt.h>
96 #include <time.h>
97 #include <libgen.h>
98 #include <errno.h>
99 #include <sys/types.h>
100 #include <sys/stat.h>
101 #include <sys/wait.h>
102 #include <sys/mman.h>
103 #include <sys/time.h>
104 #include "nomos_gap.h"
105 #include <stdbool.h>
106 #include <semaphore.h>
107 #include <stdbool.h>
108 #include "json_writer.h"
109 
111 #ifdef STANDALONE
112 #include "standalone.h"
113 #else
114 #include <libfossology.h>
115 #include <libfossdbmanager.h>
116 #endif
117 
118 #include <glib.h>
119 
123 #ifdef MEMORY_TRACING
124 #include "DMalloc.h"
125 #endif /* MEMORY_TRACING */
126 
127 #define PRECHECK
128 #define GPLV2_BEATS_GPLV3
129 #define SAVE_UNCLASSIFIED_LICENSES
130 /*#define FLAG_NO_COPYRIGHT*/
131 
132 #ifdef PROC_TRACE_SWITCH
133 #define PROC_TRACE
134 #endif /* PROC_TRACE_SWITCH */
135 
136 #define myBUFSIZ 4096
137 #define MAX_RENAME 1000
138 #define TEMP_FILE_LEN 100
139 
140 
144 #define MAX_SCANBYTES 1024*1024
145 
151 #define OPTS_DEBUG 0x1
152 #define OPTS_TRACE_SWITCH 0x2
153 #define OPTS_LONG_CMD_OUTPUT 0x4
154 #define OPTS_HIGHLIGHT_STDOUT 0x8
155 #define OPTS_NO_HIGHLIGHTINFO 0x10
156 #define OPTS_JSON_OUTPUT 0x20
157 #define OPTS_SCANNING_DIRECTORY 0x40
158 
162 
163 size_t hashEntries;
164 
168 #define FL_SAVEBASE 0x20
169 #define FL_FRAGMENT 0x40
170 #define FL_SHOWMATCH 0x80
171 #define FL_NOCOPYRIGHT 0x100
172 
176 #define FILE_FOUND "Found.txt"
177 #define FILE_SCORES "_scores"
178 #define DEBUGLOG "/tmp/NomosDebugLog"
179 
180 
184 #define NO 0
185 
188 #define YES 1
189 
190 /* List-sorting flags */
191 #define UNSORTED 0
192 #define SORT_BY_NAME 1
193 #define SORT_BY_NAME_ICASE 2
194 #define SORT_BY_COUNT_DSC 3
195 #define SORT_BY_COUNT_ASC 4
196 #define SORT_BY_ALIAS 5
197 #define SORT_BY_BASENAME 6
198 
199 /* Interest level (licenses) */
200 #define IL_HIGH 3
201 #define IL_MED 2
202 #define IL_LOW 1
203 #define IL_NONE 0
204 #define IL_INIT -1
205 
209 #define LTSR_RMASK ((char) 1)
210 #define LTSR_SMASK ((char) 2)
211 #define LTSR_YES ((char) 3)
212 #define LTSR_NO LTSR_SMASK
217 #define STR_NOTPKG "None (not an rpm-format package)"
218 
219 /*
220  * License-scanning limits
221  */
222 #define _scCOMFORT 9
223 #define _scINVALID 4
228 #define LS_NONE "None"
229 #define LS_UNLIKELY "LikelyNot"
230 #define LS_NOSUM "No_license_found"
231 #define LS_UNCL "UnclassifiedLicense"
232 #define LS_NOT_PD "NOT-public-domain"
233 #define LS_PD_CLM "Public-domain"
234 #define LS_PD_CPRT "Public-domain(C)"
235 #define LS_PD_ONLY "Public-domain-ref"
236 #define LS_CPRTONLY "Misc-Copyright"
237 #define LS_TDMKONLY "Trademark-ref"
238 #define LS_LICRONLY "License-ref"
239 #define LS_PATRONLY "Patent-ref"
240 
241 /*
242  * NULL values
243  */
244 #define NULL_ITEM (item_t *) NULL
245 #define NULL_LIST (list_t *) NULL
246 #define NULL_FH (fh_t *) NULL
247 #define NULL_CHAR '\0'
248 #define NULL_STR (char *) NULL
249 
250 
253 #define isEOL(x) (((x == '\n') || (x == '\r') || (x == '\v')))
254 #define IS_HUGE(x) (x >= gl.blkUpperLimit)
255 
256 
257 
258 #define NOMOS_TEMP "/tmp/nomos.tempdir"
259 #define NOMOS_TLOCK "/tmp/nomos.tempdir/.lock.tmp,"
265 struct mm_cache {
266  int inUse;
267  int fd;
268  unsigned long size;
269  void *mmPtr;
270  char label[myBUFSIZ];
271 };
272 
273 
274 /*
275  CDB - This is kind of tricky, the way it uses the same fields for
276  different meanings. If we had objects, we could subclass. It works
277  okay, but is just a PITA for debugging.
278  */
279 
287 struct listitem {
288  int val;
289  int val2;
290  int val3;
291  char *str;
292  void *buf;
293 };
294 typedef struct listitem item_t;
295 
296 
300 #define seqNo val
301 #define foundTool val
302 #define refCount val
303 #define num val
304 #define bStart val
305 #define iFlag val
306 #define ssComp val2
307 #define isProcessed val2
308 #define iLevel val2
309 #define nMatch val2
310 #define bLen val2
311 #define bDocLen val3
312 #define bIndex val3
313 #define bList buf
314 
321 struct list {
322  char name[64];
323  int used;
324  int size;
325  int ix;
326  int sorted;
329  int desc;
331 };
332 typedef struct list list_t;
333 
337 struct searchString {
338  int csLen;
339  char *csData;
340 };
341 typedef struct searchString searchString_t;
342 
346 struct licenseSpec {
349 };
350 typedef struct licenseSpec licSpec_t;
351 
352 
357 struct globals {
358  char initwd[myBUFSIZ];
359  char progName[64];
360  int progOpts;
361  int flags;
362  int uPsize;
363 #ifdef GLOBAL_DEBUG
364  int DEEBUG;
365  int MEM_DEEBUG;
366 #endif /* GLOBAL_DEBUG */
367 #ifdef PROC_TRACE_SWITCH
368  int ptswitch;
369 #endif /* PROC_TRACE_SWITCH */
371  /* Agent-specific Things */
372  int agentPk;
373  long uploadFk;
374  int arsPk;
375  PGconn *pgConn;
377 };
378 
382 typedef struct {
383  int start;
384  int end;
385  int index;
387 
391 typedef struct {
392  GArray* matchPositions;
393  GArray* indexList;
394  char* licenceName;
397 
398 
399 
404 struct curScan {
405  char cwd[myBUFSIZ];
406  char targetDir[myBUFSIZ]; /* check */
407  char targetFile[myBUFSIZ]; /* check */
408  char filePath[myBUFSIZ];
409  long pFileFk;
410  char pFile[myBUFSIZ];
411  char *licPara;
412  char *matchBase;
413  size_t targetLen;
414  size_t cwdLen;
415  struct stat stbuf;
416  regmatch_t regm;
417  list_t regfList;
418  list_t fLicFoundMap;
419  list_t parseList;
420  list_t offList;
421  list_t lList;
422  char compLic[myBUFSIZ];
424  int nLines;
425  int cliMode;
426  char *tmpLics;
427  char *licenseList[512];
429  GArray* indexList;
430  GArray* theMatches;
432  GArray* docBufferPositionsAndOffsets;
433  int currentLicenceIndex;
434 };
435 
439 struct license {
440  int len;
441  char *patt;
442 };
443 
447 struct licensetext {
448  char *regex;
449  char *tseed;
450  int nAbove;
451  int nBelow;
452  int compiled;
453  int plain;
454 };
455 typedef struct licensetext licText_t;
456 
460 #define _REGEX(x) licText[x].regex
461 
464 #define _SEED(x) licText[x].tseed
465 
469 struct scanResults {
470  int score;
471  int kwbm;
472  int size;
473  int flag;
474  int dataOffset;
475  char fullpath[myBUFSIZ];
476  char linkname[16];
477  char *licenses;
478  char *relpath;
479  size_t nameOffset;
480 };
481 typedef struct scanResults scanres_t;
482 
486 #define MTAG_UNSORTKEY "list/str (initially-UNsorted key)"
487 #define MTAG_SORTKEY "list/str (initially-sorted key)"
488 #define MTAG_LISTKEY "list/str (sorted/unsorted key)"
489 #define MTAG_REPLKEY "list/str (replaced primary key)"
490 #define MTAG_LISTBUF "list/buf (any data)"
491 #define MTAG_PATHBASE "list/buf (path basename)"
492 #define MTAG_PKGINFO "list/buf (pkg rname/type/name/vers/lic)"
493 #define MTAG_PKG_NV "list/buf (pkg name/vers)"
494 #define MTAG_MD5SUM "list/buf (distro-arch MD5SUM)"
495 #define MTAG_COUNTER "list/buf integer (counter)"
496 #define MTAG_PKGNAME "list/buf (package-name)"
497 #define MTAG_PKGVERS "list/buf (package-vers)"
498 #define MTAG_CLAIMLIC "list/buf (claimlic copy)"
499 #define MTAG_COMPLIC "list/buf (pkg compLic copy)"
500 #define MTAG_URLCOPY "list/buf (pkg URL copy)"
501 #define MTAG_FILELIC "list/buf (file-license copy)"
502 #define MTAG_FIXNAME "list/buf (fixed-package name)"
503 
506 #define MTAG_SEEDTEXT "search-seed text"
507 #define MTAG_SRCHTEXT "license-search text"
508 #define MTAG_MMAPFILE "mmap-file data"
509 #define MTAG_MAGICDATA "file magic description"
510 #define MTAG_PATTRS "pkg-attr buffer"
511 #define MTAG_DOUBLED "doubled (reallocated) data"
512 #define MTAG_SEARCHBUF "initial search-data buffer"
513 #define MTAG_TOOSMALL "too-small half-size buffer"
514 #define MTAG_TEXTPARA "paragraph text"
515 #define MTAG_LIST "dynamically-allocated list"
516 #define MTAG_ENV "environment variable"
517 #define MTAG_SCANRES "scan-results list"
518 
519 
520 /*
521  Functions defined in nomos.c, used in other files
522  */
523 void Bail(int exitval);
524 int optionIsSet(int val);
525 
526 /*
527  Global Declarations
528  */
529 extern struct globals gl;
530 extern struct curScan cur;
531 extern licText_t licText[];
532 extern licSpec_t licSpec[];
533 extern int schedulerMode; /* Non-zero if being run by scheduler */
534 
538 #ifdef MEMORY_TRACING
539 char *memAllocTagged();
540 void memFreeTagged();
541 #define memFree(x,y) memFreeTagged(x, y)
542 #define memAlloc(x,y) memAllocTagged(x, y)
543 #else /* NOT MEMORY_TRACING */
544 #define memFree(x,/*notused*/y) free(x)
545 #define memAlloc(x,y) calloc(x, 1)
546 #endif /* NOT MEMORY_TRACING */
547 
548 /*
549  * Macros for timing - refer to findPhrase() for usage examples
550  */
551 /* need TIMING_DECL in the declarations section of function */
552 #define DECL_TIMER struct timeval bTV, eTV; float proctime
553 #define ZERO_TIMER memcpy((void *) &bTV, (void *) &eTV, sizeof(eTV))
554 #define RESET_TIMER END_TIMER; ZERO_TIMER
555 #define START_TIMER RECORD_TIMER(bTV)
556 #define END_TIMER RECORD_TIMER(eTV) ; \
557  proctime = (float) (eTV.tv_sec - bTV.tv_sec) + \
558  ((float) (eTV.tv_usec - bTV.tv_usec) * 0.000001)
559 #define RECORD_TIMER(x) (void) gettimeofday(&x, (struct timezone *) NULL)
560 #define PRINT_TIMER(x,y) printf("%11.6f seconds: %s\n", proctime, x); \
561  if (y) { DUMP_TIMERS; }
562 #define DUMP_TIMERS printf("[1]: %d.%06d\n", bTV.tv_sec, bTV.tv_usec); \
563  printf("[2]: %d.%06d\n", eTV.tv_sec, eTV.tv_usec)
564 
565 
566 /*
567  * Cut-and-paste this stuff to turn on timing
568  */
569 #if 0
570 #ifdef TIMING
571 DECL_TIMER; /* timer declaration */
572 #endif
573 /* */
574 #ifdef TIMING
575 START_TIMER; /* turn on the timer */
576 #endif /* TIMING */
577 /* */
578 #ifdef TIMING
579 END_TIMER; /* stop the timer */
580 PRINT_TIMER("unpack", 0); /* ... and report */
581 START_TIMER; /* optionally re-start timer */
582 #endif /* TIMING */
583 #endif
584 
585 #endif /* _NOMOS_H */
int arsPk
Agent ars id.
Definition: nomos.h:374
int desc
Definition: nomos.h:329
int len
Length of pattern.
Definition: nomos.h:440
char * str
Definition: nomos.h:291
int cliMode
Definition: nomos.h:425
int optionIsSet(int val)
Check if an CLI option is set.
Definition: nomos_utils.c:568
tricky data structure used for a list of &#39;items&#39;
Definition: nomos.h:287
searchString_t text
License text.
Definition: nomos.h:348
char * tseed
unencrypted license text
Definition: nomos.h:449
Structure holding data truly global in that it remains consistent for each file scanned.
Definition: nomos.h:357
Definition: nomos.h:439
int used
Definition: nomos.h:323
int size
Definition: nomos.h:324
int score
License match score.
Definition: nomos.h:470
long uploadFk
Upload id.
Definition: nomos.h:373
GArray * matchPositions
Match positions.
Definition: nomos.h:392
void Bail(int exitval)
Close connections and exit.
Definition: nomos_utils.c:541
int start
Start position of match.
Definition: nomos.h:383
char * patt
License pattern to use.
Definition: nomos.h:441
int csLen
String length.
Definition: nomos.h:338
GArray * theMatches
Definition: nomos.h:430
char * csData
String data.
Definition: nomos.h:339
int schedulerMode
Definition: nomos.c:40
int flags
Flags.
Definition: nomos.h:361
fo_dbManager * dbManager
FOSSology DB manager.
Definition: nomos.h:376
int uPsize
Size.
Definition: nomos.h:362
searchString_t seed
License seed.
Definition: nomos.h:347
Handle JSON outputs.
char debugStr[myBUFSIZ]
Debug string.
Definition: nomos.h:159
unsigned long size
Size.
Definition: nomos.h:268
int inUse
Cache in use.
Definition: nomos.h:266
int progOpts
CLI options.
Definition: nomos.h:360
int flag
Flags.
Definition: nomos.h:473
int sorted
Definition: nomos.h:326
int fd
File descriptor.
Definition: nomos.h:267
char * licenceName
License names.
Definition: nomos.h:394
The main FOSSology C library.
int index
Enums from index (Entrynumber) in STRINGS.in.
Definition: nomos.h:385
char dbErrString[myBUFSIZ]
DB error string.
Definition: nomos.h:160
licText_t licText[]
size_t hashEntries
Hash entries.
Definition: nomos.h:163
int end
End position of match.
Definition: nomos.h:384
#define myBUFSIZ
Buffer max length.
Definition: nomos.h:136
char * tmpLics
Definition: nomos.h:426
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:321
GArray * indexList
Definition: nomos.h:429
GArray * indexList
License indexes.
Definition: nomos.h:393
item_t * items
Definition: nomos.h:330
list_t sHash
Hashes.
Definition: nomos.h:370
void * mmPtr
Memory pointer.
Definition: nomos.h:269
Struct that tracks state related to current file being scanned.
Definition: nomos.h:404
void * buf
Definition: nomos.h:292
char * regex
License regex.
Definition: nomos.h:448
GArray * keywordPositions
Definition: nomos.h:431
int agentPk
Agent id.
Definition: nomos.h:372
int ix
Definition: nomos.h:325
long pFileFk
Definition: nomos.h:409
char saveLics[myBUFSIZ]
License string.
Definition: nomos.h:161
int licenseFileId
PFile id.
Definition: nomos.h:395
PGconn * pgConn
DB Connection.
Definition: nomos.h:375