Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2023 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 3, or (at your option) any later version.
8 :
9 : TALER is distributed in the hope that it will be useful, but WITHOUT ANY
10 : WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 : A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 :
13 : You should have received a copy of the GNU General Public License along with
14 : TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15 : */
16 : /**
17 : * @file challengerdb/challenger-dbinit.c
18 : * @brief Create tables for the challenger database.
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include <gnunet/gnunet_util_lib.h>
23 : #include "challenger_util.h"
24 : #include "challenger_database_lib.h"
25 : #include "challenger-database/create_tables.h"
26 : #include "challenger-database/drop_tables.h"
27 : #include "challenger-database/gc.h"
28 :
29 :
30 : /**
31 : * Return value from main().
32 : */
33 : static int global_ret;
34 :
35 : /**
36 : * -r option: do full DB reset
37 : */
38 : static int reset_db;
39 :
40 : /**
41 : * -g option: do GC reset
42 : */
43 : static int gc_db;
44 :
45 :
46 : /**
47 : * Main function that will be run.
48 : *
49 : * @param cls closure
50 : * @param args remaining command-line arguments
51 : * @param cfgfile name of the configuration file used (for saving, can be NULL!)
52 : * @param cfg configuration
53 : */
54 : static void
55 15 : run (void *cls,
56 : char *const *args,
57 : const char *cfgfile,
58 : const struct GNUNET_CONFIGURATION_Handle *cfg)
59 : {
60 : struct CHALLENGERDB_PostgresContext *db;
61 :
62 : (void) cls;
63 : (void) args;
64 : (void) cfgfile;
65 15 : if (NULL ==
66 15 : (db = CHALLENGERDB_connect_admin (cfg)))
67 : {
68 0 : fprintf (stderr,
69 : "Failed to initialize database connection.\n");
70 0 : global_ret = EXIT_NOTINSTALLED;
71 0 : return;
72 : }
73 15 : if (reset_db)
74 : {
75 15 : if (GNUNET_OK != CHALLENGERDB_drop_tables (db))
76 : {
77 1 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
78 : "Could not drop tables as requested. Either database was not yet initialized, or permission denied. Consult the logs. Will still try to create new tables.\n");
79 : }
80 : }
81 15 : if (GNUNET_OK !=
82 15 : CHALLENGERDB_create_tables (db))
83 : {
84 0 : global_ret = EXIT_FAILURE;
85 0 : CHALLENGERDB_disconnect (db);
86 0 : return;
87 : }
88 15 : if (gc_db)
89 : {
90 : struct GNUNET_TIME_Absolute now;
91 :
92 0 : now = GNUNET_TIME_absolute_get ();
93 0 : if (0 >
94 0 : CHALLENGERDB_gc (db,
95 : now))
96 : {
97 0 : fprintf (stderr,
98 : "Garbage collection failed!\n");
99 0 : global_ret = EXIT_FAILURE;
100 : }
101 : }
102 15 : CHALLENGERDB_disconnect (db);
103 : }
104 :
105 :
106 : /**
107 : * The main function of the database initialization tool.
108 : * Used to initialize the Challenger' database.
109 : *
110 : * @param argc number of arguments from the command line
111 : * @param argv command line arguments
112 : * @return 0 ok, non-zero on error
113 : */
114 : int
115 15 : main (int argc,
116 : char *const *argv)
117 : {
118 15 : struct GNUNET_GETOPT_CommandLineOption options[] = {
119 15 : GNUNET_GETOPT_option_flag ('r',
120 : "reset",
121 : "reset database (DANGEROUS: all existing data is lost!)",
122 : &reset_db),
123 : /* Note: challenger-httpd never runs the garbage collection itself, so
124 : expired validations and tokens accumulate until an operator runs this
125 : (typically from cron or a systemd timer). */
126 15 : GNUNET_GETOPT_option_flag ('g',
127 : "garbagecollect",
128 : "remove expired validations and access tokens from the database; challenger-httpd does not do this on its own, so run this periodically",
129 : &gc_db),
130 : GNUNET_GETOPT_OPTION_END
131 : };
132 : enum GNUNET_GenericReturnValue ret;
133 :
134 15 : ret = GNUNET_PROGRAM_run (CHALLENGER_project_data (),
135 : argc, argv,
136 : "challenger-dbinit",
137 : "Initialize challenger database",
138 : options,
139 : &run, NULL);
140 15 : if (GNUNET_SYSERR == ret)
141 0 : return EXIT_INVALIDARGUMENT;
142 15 : if (GNUNET_NO == ret)
143 0 : return EXIT_SUCCESS;
144 15 : return global_ret;
145 : }
146 :
147 :
148 : /* end of challenger-dbinit.c */
|