FOSSology  4.4.0
Open Source License Compliance by Open Source Software
maintagent.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2013 Hewlett-Packard Development Company, L.P.
3  SPDX-FileCopyrightText: © 2014, 2019 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
49 #include "maintagent.h"
50 
51 #ifdef COMMIT_HASH_S
52 char BuildVersion[]="maintagent build version: " VERSION_S " r(" COMMIT_HASH_S ").\n";
53 #else
54 char BuildVersion[]="maintagent build version: NULL.\n";
55 #endif
56 
60 int main(int argc, char **argv)
61 {
62  int cmdopt;
63  char *COMMIT_HASH;
64  char *VERSION;
65  char agentRev[myBUFSIZ];
66  char *agentDesc = "Maintenance Agent";
67 
68  /* connect to the scheduler */
69  fo_scheduler_connect(&argc, argv, &pgConn);
71 
72  /* get agent pk
73  * Note, if GetAgentKey fails, this process will exit.
74  */
75  COMMIT_HASH = fo_sysconfig("maintagent", "COMMIT_HASH");
76  VERSION = fo_sysconfig("maintagent", "VERSION");
77  snprintf(agentRev, sizeof(agentRev), "%s.%s", VERSION, COMMIT_HASH);
78  /* insert/update agent data if necessary */
79  fo_GetAgentKey(pgConn, basename(argv[0]), 0, agentRev, agentDesc);
80 
81  int validateFoldersExe = 0;
82  int verifyFilePermsExe = 0;
83  int removeUploadsExe = 0;
84  int normalizeUploadPrioritiesExe = 0;
85  int removeTempsExe = 0;
86  int vacAnalyzeExe = 0;
87  int processExpiredExe = 0;
88  int removeOrphanedFilesExe = 0;
89  int reIndexAllTablesExe = 0;
90  int removeOrphanedRowsExe = 0;
91  int removeOrphanedLogs = 0;
92  int removeExpiredTokensExe = 0;
93  int tokenRetentionPeriod = 30;
94  int removeOldGoldExe = 0;
95  int removeOldLogsExe = 0;
96  char goldOlder[11];
97  char oldLogsDate[11] = {};
98 
99  /* command line options */
100  while ((cmdopt = getopt(argc, argv, "aAc:DEFghiIl:LNo:pPRt:TUvVZ")) != -1)
101  {
102  switch (cmdopt)
103  {
104  case 'a': /* All non slow operations */
105  if (validateFoldersExe == 0)
106  {
107  validateFolders();
108  validateFoldersExe = 1;
109  }
110  if (verifyFilePermsExe == 0)
111  {
112  verifyFilePerms(1);
113  verifyFilePermsExe = 1;
114  }
115  if (removeUploadsExe == 0)
116  {
117  removeUploads();
118  removeUploadsExe = 1;
119  }
120  if (normalizeUploadPrioritiesExe == 0)
121  {
123  normalizeUploadPrioritiesExe = 1;
124  }
125  if (removeTempsExe == 0)
126  {
127  removeTemps();
128  removeTempsExe = 1;
129  }
130  if (vacAnalyzeExe == 0)
131  {
132  vacAnalyze();
133  vacAnalyzeExe = 1;
134  }
135  if (removeOrphanedLogs == 0)
136  {
138  removeOrphanedLogs = 1;
139  }
140  break;
141  case 'A': /* All operations */
142  if (validateFoldersExe == 0)
143  {
144  validateFolders();
145  validateFoldersExe = 1;
146  }
147  if (verifyFilePermsExe == 0)
148  {
149  verifyFilePerms(1);
150  verifyFilePermsExe = 1;
151  }
152  if (removeUploadsExe == 0)
153  {
154  removeUploads();
155  removeUploadsExe = 1;
156  }
157  if (normalizeUploadPrioritiesExe == 0)
158  {
160  normalizeUploadPrioritiesExe = 1;
161  }
162  if (removeTempsExe == 0)
163  {
164  removeTemps();
165  removeTempsExe = 1;
166  }
167  if (vacAnalyzeExe == 0)
168  {
169  vacAnalyze();
170  vacAnalyzeExe = 1;
171  }
172  if (processExpiredExe == 0)
173  {
174  processExpired();
175  processExpiredExe = 1;
176  }
177  if (removeOrphanedFilesExe == 0)
178  {
180  removeOrphanedFilesExe = 1;
181  }
182  if (reIndexAllTablesExe == 0)
183  {
185  reIndexAllTablesExe = 1;
186  }
187  if (removeOrphanedRowsExe == 0)
188  {
190  removeOrphanedRowsExe = 1;
191  }
192  if (removeOrphanedLogs == 0)
193  {
195  removeOrphanedLogs = 1;
196  }
197  break;
198  case 'D': /* Vac/Analyze (slow) */
199  if (vacAnalyzeExe == 0)
200  {
201  vacAnalyze();
202  vacAnalyzeExe = 1;
203  }
204  break;
205  case 'F': /* Validate folder contents */
206  if (validateFoldersExe == 0)
207  {
208  validateFolders();
209  validateFoldersExe = 1;
210  }
211  break;
212  case 'g': /* Delete orphan gold files */
214  break;
215  case 'h':
216  usage(argv[0]);
217  exitNow(0);
218  case 'N': /* Remove uploads with no pfiles */
219  if (normalizeUploadPrioritiesExe == 0)
220  {
222  normalizeUploadPrioritiesExe = 1;
223  }
224  break;
225  case 'o': /* Gold files older than given date */
226  if (removeOldGoldExe == 0)
227  {
228  strncpy(goldOlder, optarg, 10);
229  deleteOldGold(goldOlder);
230  removeOldGoldExe = 1;
231  }
232  break;
233  case 'p': /* Verify file permissions */
234  verifyFilePerms(0);
235  break;
236  case 'P': /* Verify and fix file permissions */
237  if (verifyFilePermsExe == 0)
238  {
239  verifyFilePerms(1);
240  verifyFilePermsExe = 1;
241  }
242  break;
243  case 'R': /* Remove uploads with no pfiles */
244  if (removeUploadsExe == 0)
245  {
246  removeUploads();
247  removeUploadsExe = 1;
248  }
249  break;
250  case 't': /* Remove expired personal access token */
251  if (removeExpiredTokensExe == 0)
252  {
253  tokenRetentionPeriod = atol(optarg);
254  removeExpiredTokens(tokenRetentionPeriod);
255  removeExpiredTokensExe = 1;
256  }
257  break;
258  case 'T': /* Remove orphaned temp tables */
259  if (removeTempsExe == 0)
260  {
261  removeTemps();
262  removeTempsExe = 1;
263  }
264  break;
265  case 'U': /* Process expired uploads (slow) */
266  if (processExpiredExe == 0)
267  {
268  processExpired();
269  processExpiredExe = 1;
270  }
271  break;
272  case 'Z': /* Remove orphaned files from the repository (slow) */
273  if (removeOrphanedFilesExe == 0)
274  {
276  removeOrphanedFilesExe = 1;
277  }
278  break;
279  case 'I': /* Reindexing of database */
280  if (reIndexAllTablesExe == 0)
281  {
283  reIndexAllTablesExe = 1;
284  }
285  break;
286  case 'E': /* Remove orphaned files from the database */
287  if (removeOrphanedRowsExe == 0)
288  {
290  removeOrphanedRowsExe = 1;
291  }
292  break;
293  case 'l': /* Remove old log files */
294  if (removeOldLogsExe == 0)
295  {
296  strncpy(oldLogsDate, optarg, 10);
297  removeOldLogFiles(oldLogsDate);
298  removeOldLogsExe = 1;
299  }
300  break;
301  case 'L': /* Remove orphaned log files from file system */
302  if (removeOrphanedLogs == 0)
303  {
305  removeOrphanedLogs = 1;
306  }
307  break;
308  case 'i': /* "Initialize" */
309  exitNow(0);
310  case 'v': /* verbose output for debugging */
311  agent_verbose++;
312  break;
313  case 'V': /* print version info */
314  printf("%s", BuildVersion);
315  exitNow(0);
316  case 'c':
317  break; /* handled by fo_scheduler_connect() */
318  default:
319  usage(argv[0]);
320  exitNow(-1);
321  }
322  }
323 
324  exitNow(0); /* success */
325  return (0); /* Never executed but prevents compiler warning */
326 } /* main() */
PGconn * pgConn
Database connection.
Definition: adj2nest.c:86
char BuildVersion[]
Definition: buckets.c:68
void exitNow(int exitVal)
Exit function. This does all cleanup and should be used instead of calling exit() or main() return.
Definition: util.c:1177
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
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.
int agent_verbose
Common verbose flags for the agents, this is used so that the scheduler can change the verbose level ...
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
FUNCTION void removeOrphanedRows()
remove orphaned rows from fossology database
Definition: process.c:366
FUNCTION void verifyFilePerms(int fix)
Verify and optionally fix file permissions.
Definition: process.c:128
FUNCTION void deleteOldGold(char *date)
Delete gold files which are older than specified date.
Definition: process.c:581
FUNCTION void removeTemps()
Remove orphaned temp tables from deprecated pkgmettagetta and old delagent.
Definition: process.c:183
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
FUNCTION void reIndexAllTables()
reindex of all indexes in fossology database
Definition: process.c:338
FUNCTION void removeOldLogFiles(const char *olderThan)
Definition: process.c:660
FUNCTION void removeUploads()
Remove Uploads with no pfiles.
Definition: process.c:155
FUNCTION void removeOrphanedLogFiles()
Definition: process.c:469
FUNCTION void processExpired()
Process expired uploads (slow)
Definition: process.c:221
FUNCTION void removeExpiredTokens(long int retentionPeriod)
remove expired personal access tokens from fossology database
Definition: process.c:542
FUNCTION void validateFolders()
Validate folder and foldercontents tables.
Definition: process.c:75
FUNCTION void removeOrphanedFiles()
Remove orphaned files from the repository (slow) Loop through each file in the repository and make su...
Definition: process.c:246
FUNCTION void vacAnalyze()
Do database vacuum and analyze.
Definition: process.c:53
FUNCTION void deleteOrphanGold()
Delete orphaned gold files from the repository.
Definition: process.c:281
FUNCTION void normalizeUploadPriorities()
Normalize priority of Uploads.
Definition: process.c:313
FUNCTION void usage(char *name)
Definition: usage.c:18
int main(int argc, char **argv)
Main entry point for the agent.
Definition: maintagent.c:60
fo_dbManager * fo_dbManager_new(PGconn *dbConnection)
Create and initialize new fo_dbManager object.
Definition: standalone.c:33