FOSSology  4.4.0
Open Source License Compliance by Open Source Software
libfodbreposysconf.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2011 Hewlett-Packard Development Company, L.P.
3  SPDX-FileCopyrightText: © 2015, 2018 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdarg.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 
21 #include <libfossscheduler.h>
22 #include <libfossdb.h>
23 #include "libfodbreposysconf.h"
24 
25 #ifndef TESTDBDIR
26 // this is only to make IDEs happy
27 #define TESTDBDIR "../../../testing/db/"
28 #error
29 #endif
30 
31 static char* Sysconf = NULL;
32 static char DBName[ARRAY_LENGTH];
33 static char DBConf[ARRAY_LENGTH];
34 static char RepoDir[ARRAY_LENGTH];
35 static char confFile[ARRAY_LENGTH];
36 
37 fo_dbManager* createTestEnvironment(const char* srcDirs, const char* doConnectAsAgent, int initDbTables) {
38  GString* gString = g_string_new(TESTDBDIR "/createTestEnvironment.php");
39  if (srcDirs) {
40  g_string_append_printf(gString, " -d '%s'", srcDirs);
41  }
42  if (initDbTables) {
43  g_string_append_printf(gString, " -f");
44  }
45  gchar* cmd = g_string_free(gString, FALSE);
46 
47  FILE* pipe = popen(cmd, "r");
48 
49  if (!pipe) {
50  printf("cannot run create test environment script: %s\n", cmd);
51  goto createError;
52  }
53 
54  Sysconf = calloc(1, ARRAY_LENGTH + 1);
55  size_t count = fread(Sysconf, 1, ARRAY_LENGTH, pipe);
56 
57  int rv = pclose(pipe);
58 
59  if (rv != 0 || count == 0) {
60  printf("command %s failed with output:\n%s\n", cmd, Sysconf);
61  goto createError;
62  }
63 
64  g_free(cmd);
65 
66  fo_dbManager* result = NULL;
67  if (doConnectAsAgent) {
68  char* argv[] = {(char*) doConnectAsAgent, "-c", Sysconf};
69  int argc = 3;
70 
71  fo_scheduler_connect_dbMan(&argc, argv, &result);
72  } else {
73  char buffer[ARRAY_LENGTH + 1];
74  snprintf(buffer, ARRAY_LENGTH, "%s/Db.conf", Sysconf);
75  char* errorMsg = NULL;
76  PGconn* conn = fo_dbconnect(buffer, &errorMsg);
77 
78  if (!errorMsg) {
79  result = fo_dbManager_new_withConf(conn, buffer);
80  } else {
81  printf("error connecting: %s\n", errorMsg);
82  }
83  }
84  return result;
85 
86 createError:
87  if (cmd) {
88  g_free(cmd);
89  }
90  return NULL;
91 }
92 
93 void dropTestEnvironment(fo_dbManager* dbManager, const char* srcDir, const char* doConnectAsAgent) {
94  if (dbManager) {
95  fo_dbManager_finish(dbManager);
96  }
97  if (doConnectAsAgent) {
99  }
100 
101  if (Sysconf) {
102  char buffer[ARRAY_LENGTH];
103  snprintf(buffer, ARRAY_LENGTH, TESTDBDIR "/purgeTestEnvironment.php -d '%s' -c '%s'", srcDir, Sysconf);
104  FILE* pipe = popen(buffer, "r");
105 
106  if (!pipe) {
107  printf("cannot run purge test environment script: %s\n", buffer);
108  return;
109  }
110 
111  pclose(pipe);
112  free(Sysconf);
113  }
114 }
115 
121 static void command_output(char* command) {
122  FILE* stream;
123  char tmp[ARRAY_LENGTH];
124  int i = 0;
125  int status = 0;
126 
127  stream = popen(command, "r");
128  if (!stream) status = 1;
129  memset(tmp, '\0', sizeof(tmp));
130  if (fgets(tmp, ARRAY_LENGTH, stream) != NULL) {
131  while ((tmp[i] != '\n') && (tmp[i] != ' ') && (tmp[i] != EOF))
132  i++;
133  Sysconf = malloc(i);
134  memcpy(Sysconf, tmp, i);
135  Sysconf[i] = '\0';
136  }
137  int rc = pclose(stream);
138  if (rc != 0) status = 1;
139  if (status == 1) {
140  printf("Failed to run %s, exit code is:%d .\n", command, rc >> 8);
141  exit(1);
142  }
143  return;
144 }
145 
154 int create_db_repo_sysconf(int type, char* agent_name, char* sysconfdir) {
155 #if 0
156  char *sysconfdir;
158  sysconfdir = getenv ("SYSCONFDIR");
159  if (sysconfdir == NULL)
160  {
161  printf ("The SYSCONFDIR enviroment variable is not existed.\n");
162  return 1;
163  }
164 #endif
165 #define CREATEDB TESTDBDIR "/createTestDB.php"
166  const char INIT_CMD[] = CREATEDB;
167  char *CMD, *tmp;
168 
169  CMD = (char *)malloc(strlen(INIT_CMD) + 1);
170  if (!CMD)
171  {
172  return -1;
173  }
174  sprintf(CMD, "%s", INIT_CMD);
175 
176  if (sysconfdir)
177  {
178  tmp = (char *)malloc(strlen(CMD) + 4 + strlen(sysconfdir) + 1);
179  if (!tmp)
180  {
181  free(CMD);
182  return -1;
183  }
184  sprintf(tmp, "%s -c %s", CMD, sysconfdir);
185  free(CMD);
186  CMD = tmp;
187  }
188 
189  switch(type)
190  {
191  case 0:
192  tmp = (char *)malloc(strlen(CMD) + 4);
193  if (!tmp)
194  {
195  free(CMD);
196  return -1;
197  }
198  sprintf(tmp, "%s -e", CMD);
199  free(CMD);
200  CMD = tmp;
201  case 1:
203  break;
204  default:
205  break;
206  }
207 
208  free(CMD);
209 
210  int argc = 3;
211  char* argv[] = {agent_name, "-c", Sysconf};
212 
213  PGconn* unused;
214  fo_scheduler_connect(&argc, argv, &unused);
215 
216 #ifdef TEST
217  printf("create_db_repo_sysconf sucessfully\n");
218 #endif
219  return 0;
220 }
221 
227 void drop_db_repo_sysconf(char* DBName) {
228  char CMD[ARRAY_LENGTH];
229  memset(CMD, '\0', sizeof(CMD));
230  sprintf(CMD, "%s/createTestDB.php -d %s", TESTDBDIR, DBName);
232 #ifdef TEST
233  printf("remove DBName is:%s\n", DBName);
234 #endif
236  free(Sysconf);
237  Sysconf = NULL;
238 #ifdef TEST
239  printf("drop_db_repo_sysconf sucessfully\n");
240 #endif
241 }
242 
248 char* get_test_name() {
249  char* TestName = strstr(Sysconf, "Conf") + 4;
250 #ifdef TEST
251  printf("TestName is:%s\n", TestName);
252 #endif
253  return TestName;
254 }
255 
261 char* get_db_name() {
262  memset(DBName, '\0', sizeof(DBName));
263  char* TestName = get_test_name();
264  sprintf(DBName, "fosstest%s", TestName);
265 #ifdef TEST
266  printf("DBName is:%s\n", DBName);
267 #endif
268  return DBName;
269 }
270 
276 char* get_sysconfdir() {
277 #ifdef TEST
278  printf("Sysconf is:%s\n", Sysconf);
279 #endif
280  return Sysconf;
281 }
282 
288 char* get_dbconf() {
289  memset(DBConf, '\0', sizeof(DBConf));
290  sprintf(DBConf, "%s/Db.conf", Sysconf);
291  return DBConf;
292 }
293 
294 char* get_confFile() {
295  memset(confFile, '\0', sizeof(confFile));
296  sprintf(confFile, "%s/fossology.conf", Sysconf);
297  return confFile;
298 }
304 char* get_repodir() {
305  strncpy(RepoDir, Sysconf, ARRAY_LENGTH);
306  RepoDir[ARRAY_LENGTH-1] = '\0';
307 
308  char* test_name_tmp = strstr(RepoDir, "testDbConf");
309  if (test_name_tmp)
310  {
311  *test_name_tmp = '\0';
312  }
313 
314  char *tmp = malloc(strlen(RepoDir) + 1);
315  if (!tmp) {
316  return NULL;
317  }
318 
319  sprintf(tmp, "%s", RepoDir);
320  sprintf(RepoDir, "%stestDbRepo%s", tmp, get_test_name());
321 
322  free(tmp);
323 
324 #ifdef TEST
325  printf("RepoDir is:%s\n", RepoDir);
326 #endif
327  return RepoDir;
328 }
329 
338 char *createTestConfDir(char* cwd, char* agentName)
339 {
340  struct stat st = {0};
341  int rc;
342  char CMD[2048];
343  FILE *testConfFile;
344 
345  char *confDir = malloc((strlen(cwd) + 10) * sizeof(char));
346  char confFile[1024];
347  char agentDir[1024];
348 
349  if(cwd == NULL || agentName == NULL || cwd[0] == '\0' || agentName[0] == '\0')
350  {
351  return NULL;
352  }
353 
354  sprintf(confDir, "%s/testconf", cwd);
355  sprintf(confFile, "%s/fossology.conf", confDir);
356  sprintf(agentDir, "%s/..", cwd);
357 
358  if (stat(confDir, &st) == -1)
359  {
360  mkdir(confDir, 0775);
361  }
362 
363  memset(CMD, '\0', sizeof(CMD));
364  sprintf(CMD, "%s/mods-enabled/%s", confDir, agentName);
365  if (stat(CMD, &st) == -1)
366  {
367  mkdir(CMD, 0775);
368  }
369 
370  testConfFile = fopen(confFile,"w");
371  fprintf(testConfFile, ";fossology.conf for testing\n");
372  fprintf(testConfFile, "[FOSSOLOGY]\nport = 24693\n");
373  fprintf(testConfFile, "address = localhost\n");
374  fprintf(testConfFile, "depth = 0\n");
375  fprintf(testConfFile, "path = %s\n", confDir);
376  fprintf(testConfFile, "[HOSTS]\n");
377  fprintf(testConfFile, "localhost = localhost AGENT_DIR 10\n");
378  fprintf(testConfFile, "[REPOSITORY]\n");
379  fprintf(testConfFile, "localhost = * 00 ff\n");
380  fprintf(testConfFile, "[DIRECTORIES]\n");
381  fprintf(testConfFile, "PROJECTUSER=fossy\n");
382  fprintf(testConfFile, "PROJECTGROUP=fossy\n");
383  fprintf(testConfFile, "MODDIR=%s/../../../..\n", cwd);
384  fprintf(testConfFile, "LOGDIR=%s\n", confDir);
385  fclose(testConfFile);
386 
387  memset(CMD, '\0', sizeof(CMD));
388  sprintf(CMD, "install -D %s/../VERSION %s/VERSION", cwd, confDir);
389  rc = system(CMD);
390 
391  memset(CMD, '\0', sizeof(CMD));
392  sprintf(CMD, "install -D %s/../../../install/gen/Db.conf %s/Db.conf", cwd, confDir);
393  rc = system(CMD);
394 
395  memset(CMD, '\0', sizeof(CMD));
396  sprintf(CMD, "install -D %s/VERSION %s/mods-enabled/%s/VERSION", agentDir, confDir, agentName);
397  rc = system(CMD);
398 
399  memset(CMD, '\0', sizeof(CMD));
400  sprintf(CMD, "ln -fs %s/agent %s/mods-enabled/%s", agentDir, confDir, agentName);
401  rc = system(CMD);
402 
403  if (rc != -1)
404  return confDir;
405  else
406  return NULL;
407 }
408 
409 #if 0
410 int main()
411 {
413  get_test_name();
414  get_sysconfdir();
415  get_db_name();
416  drop_db_repo_sysconf(DBName);
417 }
418 #endif
cmdlist CMD[]
Global command table.
char * get_test_name()
get the test name just created by create_db_repo_sysconf()
char * get_repodir()
get repo path just created by create_db_repo_sysconf()
static void command_output(char *command)
get command output
char * get_dbconf()
get Db.conf path just created by create_db_repo_sysconf()
void drop_db_repo_sysconf(char *DBName)
drop db, sysconfig dir and repo
char * get_db_name()
get the DB name just created by create_db_repo_sysconf()
char * createTestConfDir(char *cwd, char *agentName)
create a dummy sysConfDir for a given agent
int create_db_repo_sysconf(int type, char *agent_name, char *sysconfdir)
char * get_sysconfdir()
get sysconfig dir path just created by create_db_repo_sysconf()
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.
Definition: libfossdb.c:29
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
void fo_scheduler_connect_dbMan(int *argc, char **argv, fo_dbManager **dbManager)
Make a connection from an agent to the scheduler and create a DB manager as well.
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
char buffer[2048]
The last thing received from the scheduler.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16