FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
maintagent.c
Go to the documentation of this file.
1 /***************************************************************
2  Copyright (C) 2013 Hewlett-Packard Development Company, L.P.
3  Copyright (C) 2014,2019 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  ***************************************************************/
58 #include "maintagent.h"
59 
60 #ifdef COMMIT_HASH_S
61 char BuildVersion[]="maintagent build version: " VERSION_S " r(" COMMIT_HASH_S ").\n";
62 #else
63 char BuildVersion[]="maintagent build version: NULL.\n";
64 #endif
65 
69 int main(int argc, char **argv)
70 {
71  int cmdopt;
72  char *COMMIT_HASH;
73  char *VERSION;
74  char agentRev[myBUFSIZ];
75  char *agentDesc = "Maintenance Agent";
76 
77  /* connect to the scheduler */
78  fo_scheduler_connect(&argc, argv, &pgConn);
80 
81  /* get agent pk
82  * Note, if GetAgentKey fails, this process will exit.
83  */
84  COMMIT_HASH = fo_sysconfig("maintagent", "COMMIT_HASH");
85  VERSION = fo_sysconfig("maintagent", "VERSION");
86  snprintf(agentRev, sizeof(agentRev), "%s.%s", VERSION, COMMIT_HASH);
87  /* insert/update agent data if necessary */
88  fo_GetAgentKey(pgConn, basename(argv[0]), 0, agentRev, agentDesc);
89 
90  int validateFoldersExe = 0;
91  int verifyFilePermsExe = 0;
92  int removeUploadsExe = 0;
93  int normalizeUploadPrioritiesExe = 0;
94  int removeTempsExe = 0;
95  int vacAnalyzeExe = 0;
96  int processExpiredExe = 0;
97  int removeOrphanedFilesExe = 0;
98  int reIndexAllTablesExe = 0;
99  int removeOrphanedRowsExe = 0;
100  int removeOrphanedLogs = 0;
101 
102  /* command line options */
103  while ((cmdopt = getopt(argc, argv, "aAc:DEFghiILNpPRTUvVZ")) != -1)
104  {
105  switch (cmdopt)
106  {
107  case 'a': /* All non slow operations */
108  if (validateFoldersExe == 0)
109  {
110  validateFolders();
111  validateFoldersExe = 1;
112  }
113  if (verifyFilePermsExe == 0)
114  {
115  verifyFilePerms(1);
116  verifyFilePermsExe = 1;
117  }
118  if (removeUploadsExe == 0)
119  {
120  removeUploads();
121  removeUploadsExe = 1;
122  }
123  if (normalizeUploadPrioritiesExe == 0)
124  {
126  normalizeUploadPrioritiesExe = 1;
127  }
128  if (removeTempsExe == 0)
129  {
130  removeTemps();
131  removeTempsExe = 1;
132  }
133  if (vacAnalyzeExe == 0)
134  {
135  vacAnalyze();
136  vacAnalyzeExe = 1;
137  }
138  if (removeOrphanedLogs == 0)
139  {
141  removeOrphanedLogs = 1;
142  }
143  break;
144  case 'A': /* All operations */
145  if (validateFoldersExe == 0)
146  {
147  validateFolders();
148  validateFoldersExe = 1;
149  }
150  if (verifyFilePermsExe == 0)
151  {
152  verifyFilePerms(1);
153  verifyFilePermsExe = 1;
154  }
155  if (removeUploadsExe == 0)
156  {
157  removeUploads();
158  removeUploadsExe = 1;
159  }
160  if (normalizeUploadPrioritiesExe == 0)
161  {
163  normalizeUploadPrioritiesExe = 1;
164  }
165  if (removeTempsExe == 0)
166  {
167  removeTemps();
168  removeTempsExe = 1;
169  }
170  if (vacAnalyzeExe == 0)
171  {
172  vacAnalyze();
173  vacAnalyzeExe = 1;
174  }
175  if (processExpiredExe == 0)
176  {
177  processExpired();
178  processExpiredExe = 1;
179  }
180  if (removeOrphanedFilesExe == 0)
181  {
183  removeOrphanedFilesExe = 1;
184  }
185  if (reIndexAllTablesExe == 0)
186  {
188  reIndexAllTablesExe = 1;
189  }
190  if (removeOrphanedRowsExe == 0)
191  {
193  removeOrphanedRowsExe = 1;
194  }
195  if (removeOrphanedLogs == 0)
196  {
198  removeOrphanedLogs = 1;
199  }
200  break;
201  case 'D': /* Vac/Analyze (slow) */
202  if (vacAnalyzeExe == 0)
203  {
204  vacAnalyze();
205  vacAnalyzeExe = 1;
206  }
207  break;
208  case 'F': /* Validate folder contents */
209  if (validateFoldersExe == 0)
210  {
211  validateFolders();
212  validateFoldersExe = 1;
213  }
214  break;
215  case 'g': /* Delete orphan gold files */
217  break;
218  case 'h':
219  usage(argv[0]);
220  exitNow(0);
221  case 'N': /* Remove uploads with no pfiles */
222  if (normalizeUploadPrioritiesExe == 0)
223  {
225  normalizeUploadPrioritiesExe = 1;
226  }
227  break;
228  case 'p': /* Verify file permissions */
229  verifyFilePerms(0);
230  break;
231  case 'P': /* Verify and fix file permissions */
232  if (verifyFilePermsExe == 0)
233  {
234  verifyFilePerms(1);
235  verifyFilePermsExe = 1;
236  }
237  break;
238  case 'R': /* Remove uploads with no pfiles */
239  if (removeUploadsExe == 0)
240  {
241  removeUploads();
242  removeUploadsExe = 1;
243  }
244  break;
245  case 'T': /* Remove orphaned temp tables */
246  if (removeTempsExe == 0)
247  {
248  removeTemps();
249  removeTempsExe = 1;
250  }
251  break;
252  case 'U': /* Process expired uploads (slow) */
253  if (processExpiredExe == 0)
254  {
255  processExpired();
256  processExpiredExe = 1;
257  }
258  break;
259  case 'Z': /* Remove orphaned files from the repository (slow) */
260  if (removeOrphanedFilesExe == 0)
261  {
263  removeOrphanedFilesExe = 1;
264  }
265  break;
266  case 'I': /* Reindexing of database */
267  if (reIndexAllTablesExe == 0)
268  {
270  reIndexAllTablesExe = 1;
271  }
272  break;
273  case 'E': /* Remove orphaned files from the database */
274  if (removeOrphanedRowsExe == 0)
275  {
277  removeOrphanedRowsExe = 1;
278  }
279  break;
280  case 'L': /* Remove orphaned log files from file system */
281  if (removeOrphanedLogs == 0)
282  {
284  removeOrphanedLogs = 1;
285  }
286  break;
287  case 'i': /* "Initialize" */
288  exitNow(0);
289  case 'v': /* verbose output for debugging */
290  agent_verbose++;
291  break;
292  case 'V': /* print version info */
293  printf("%s", BuildVersion);
294  exitNow(0);
295  case 'c':
296  break; /* handled by fo_scheduler_connect() */
297  default:
298  usage(argv[0]);
299  exitNow(-1);
300  }
301  }
302 
303  exitNow(0); /* success */
304  return (0); /* Never executed but prevents compiler warning */
305 } /* main() */
char BuildVersion[]
Definition: buckets.c:79
FUNCTION void removeOrphanedFiles()
Remove orphaned files from the repository (slow) Loop through each file in the repository and make su...
Definition: process.c:258
FUNCTION void removeTemps()
Remove orphaned temp tables from deprecated pkgmettagetta and old delagent.
Definition: process.c:195
FUNCTION void deleteOrphanGold()
Delete orphaned gold files from the repository Loop through each gold file in the repository and make...
Definition: process.c:282
FUNCTION void reIndexAllTables()
reindex of all indexes in fossology database
Definition: process.c:329
PGconn * pgConn
Database connection.
Definition: adj2nest.c:98
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
FUNCTION void vacAnalyze()
Do database vacuum and analyze.
Definition: process.c:65
FUNCTION void removeOrphanedRows()
remove orphaned rows from fossology database
Definition: process.c:357
FUNCTION void verifyFilePerms(int fix)
Verify and optionally fix file permissions.
Definition: process.c:140
FUNCTION void normalizeUploadPriorities()
Normalize priority of Uploads.
Definition: process.c:304
int agent_verbose
Common verbose flags for the agents, this is used so that the scheduler can change the verbose level ...
fo_dbManager * fo_dbManager_new(PGconn *dbConnection)
Create and initialize new fo_dbManager object.
Definition: standalone.c:28
int main(int argc, char **argv)
Main entry point for the agent.
Definition: maintagent.c:69
FUNCTION void processExpired()
Process expired uploads (slow)
Definition: process.c:233
FUNCTION void validateFolders()
Validate folder and foldercontents tables.
Definition: process.c:87
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
FUNCTION void removeOrphanedLogFiles()
Definition: process.c:460
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:172
FUNCTION void usage(char *name)
Definition: usage.c:30
FUNCTION void removeUploads()
Remove Uploads with no pfiles.
Definition: process.c:167
void exitNow(int exitVal)
Exit function. This does all cleanup and should be used instead of calling exit() or main() return...
Definition: util.c:1099
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.