GNUnet 0.29.1-dev.1-3-g346358e26
 
Loading...
Searching...
No Matches
gnunet-config.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012-2021 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
26#include "platform.h"
27#include "gnunet_util_lib.h"
28
29
35static char *backend_check;
36
40static int cflags;
41
45static int clean;
46
50static int is_experimental;
51
55static int no_defaults;
56
60static char *ram_config;
61
65static int libs;
66
70static int prefix;
71
72
81static void
83 const char *section,
84 const char *option,
85 unsigned int line)
86{
87 const char *filename = cls;
88
89 fprintf (stdout,
90 _ ("Removed [%s]/%s at %s:%u\n"),
91 section,
92 option,
94 line);
95}
96
97
105int
106main (int argc,
107 char *const *argv)
108{
109 const struct GNUNET_OS_ProjectData *pd
113 .global_ret = EXIT_SUCCESS
114 };
115 char *cfgfile = NULL;
116 char *loglev = NULL;
117 char *logfile = NULL;
121 "gnunet-config [OPTIONS]"),
127 'b',
128 "supported-backend",
129 "BACKEND",
131 "test if the current installation supports the specified BACKEND"),
132 &backend_check)),
134 'C',
135 "cflags",
137 "Provide an appropriate value for CFLAGS to applications building on top of GNUnet"),
138 &cflags),
140 'x',
141 "clean",
143 "remove redundant definitions from the main configuration file"),
144 &clean),
146 'E',
147 "is-experimental",
148 gettext_noop ("Is this an experimental build of GNUnet"),
151 'j',
152 "libs",
154 "Provide an appropriate value for LIBS to applications building on top of GNUnet"),
155 &libs),
157 'n',
158 "no-defaults",
159 gettext_noop ("Do not parse default configuration files"),
160 &no_defaults),
162 'p',
163 "prefix",
165 "Provide the path under which GNUnet was installed"),
166 &prefix),
168 'R',
169 "ram-config",
170 "CONFIG_DATA",
172 "Parse main configuration from this command-line argument and not from disk"),
173 &ram_config),
176 };
177 int iret;
178
179 if ( (NULL != pd->config_file) &&
180 (NULL != pd->user_config_file) )
182 iret = GNUNET_GETOPT_run ("gnunet-config",
183 options,
184 argc,
185 argv);
186 if (GNUNET_SYSERR == iret)
187 {
188 GNUNET_free (cfgfile);
189 GNUNET_free (loglev);
190 GNUNET_free (logfile);
192 }
193 if (GNUNET_OK !=
194 GNUNET_log_setup ("gnunet-config",
195 loglev,
196 logfile))
197 {
198 GNUNET_free (cfgfile);
199 GNUNET_free (loglev);
200 GNUNET_free (logfile);
201 return EXIT_FAILURE;
202 }
203 GNUNET_free (loglev);
204 GNUNET_free (logfile);
205 if (clean &&
206 ( (NULL == cfgfile) ||
207 (NULL != ram_config) ||
208 (NULL != backend_check) ||
210 cs.full || cs.is_filename || (NULL != cs.option) ||
211 cs.rewrite || cs.diagnostics || cs.list_sections ||
212 (NULL != cs.section) || (NULL != cs.value) ||
213 (NULL != argv[iret]) ))
214 {
215 fprintf (stderr,
216 _ ("--clean requires a file-backed configuration and cannot be combined with another operation\n"));
218 GNUNET_free (cfgfile);
220 }
221 if (1 == is_experimental)
222 {
223 GNUNET_free (cfgfile);
224#ifdef GNUNET_EXPERIMENTAL
225 return 0;
226#else
227 return 1;
228#endif
229 }
230 if (1 == cflags || 1 == libs || 1 == prefix)
231 {
232 char *prefixdir = GNUNET_OS_installation_get_path (pd,
234 char *libdir = GNUNET_OS_installation_get_path (pd,
236
237 if (1 == cflags)
238 {
239 fprintf (stdout, "-I%sinclude\n", prefixdir);
240 }
241 if (1 == libs)
242 {
243 fprintf (stdout, "-L%s -lgnunetutil\n", libdir);
244 }
245 if (1 == prefix)
246 {
247 fprintf (stdout, "%s\n", prefixdir);
248 }
249 GNUNET_free (prefixdir);
250 GNUNET_free (libdir);
251 GNUNET_free (cfgfile);
252 return 0;
253 }
254 if (NULL != backend_check)
255 {
256 char *name;
257
259 "libgnunet_plugin_%s",
261 iret = (GNUNET_OK ==
263 name)) ? 0 : 77;
265 GNUNET_free (cfgfile);
266 return iret;
267 }
268
269 {
271
273 if ( (0 != cs.diagnostics) || clean)
275
276 if (NULL != ram_config)
277 {
278 if ( (! no_defaults) &&
279 (GNUNET_SYSERR ==
281 NULL)) )
282 {
284 _ ("Failed to load default configuration, exiting ...\n"));
286 GNUNET_free (cfgfile);
287 return EXIT_FAILURE;
288 }
289 if (GNUNET_OK !=
292 strlen (ram_config),
293 NULL))
294 {
296 _ ("Failed to parse configuration, exiting ...\n"));
298 GNUNET_free (cfgfile);
299 return EXIT_FAILURE;
300 }
301 }
302 else if (NULL != cfgfile)
303 {
304 if (GNUNET_YES !=
305 GNUNET_DISK_file_test (cfgfile))
306 {
308 _ ("Unreadable configuration file `%s', exiting ...\n"),
309 cfgfile);
311 GNUNET_free (cfgfile);
312 return EXIT_FAILURE;
313 }
314 if (GNUNET_SYSERR ==
317 cfgfile)
319 cfgfile)) )
320 {
322 _ ("Malformed configuration file `%s', exiting ...\n"),
323 cfgfile);
325 GNUNET_free (cfgfile);
326 return EXIT_FAILURE;
327 }
328 }
329 else if (! no_defaults)
330 {
331 /* cfgfile is NULL, so only load defaults */
332 if (GNUNET_SYSERR ==
334 NULL))
335 {
337 _ ("Malformed configuration file `%s', exiting ...\n"),
338 cfgfile);
340 return EXIT_FAILURE;
341 }
342 }
343 if (clean)
344 {
345 unsigned int removed;
346
347 if (GNUNET_OK !=
349 cfgfile,
351 cfgfile,
352 &removed))
353 {
354 fprintf (stderr,
355 _ ("Failed to clean configuration file `%s'\n"),
356 cfgfile);
357 cs.global_ret = 2;
358 }
359 else
360 fprintf (stdout,
361 ngettext ("Removed %u duplicate definition from %s\n",
362 "Removed %u duplicate definitions from %s\n",
363 removed),
364 removed,
365 cfgfile);
366 }
367 else
369 &argv[iret],
370 cfgfile,
371 cfg);
374 }
375 GNUNET_free (cfgfile);
376 return cs.global_ret;
377}
378
379
380/* end of gnunet-config.c */
struct GNUNET_GETOPT_CommandLineOption options[]
Definition 002.c:5
int main()
Program to simulate results from GCP_get_desirability_of_path() for various plausible inputs.
#define gettext_noop(String)
Definition gettext.h:74
#define ngettext(Msgid1, Msgid2, N)
Definition gettext.h:53
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition gnunet-arm.c:108
static int clean
Remove redundant definitions from the main configuration file.
static char * ram_config
Parse configuration from this memory.
static int no_defaults
Do not load default configuration.
static int libs
If printing the value of LIBS has been requested.
static void report_cleaned_definition(void *cls, const char *section, const char *option, unsigned int line)
Report a definition removed by configuration cleanup.
static char * backend_check
Backend to check if the respective plugin is loadable.
static int cflags
If printing the value of CFLAGS has been requested.
static int is_experimental
Check if this is an experimental build.
static int prefix
If printing the value of PREFIX has been requested.
static char * line
Desired phone line (string to be converted to a hash).
static char * filename
static char * name
Name (label) of the records to list.
#define GNUNET_CONFIGURATION_CONFIG_OPTIONS(cs)
Macro that expands to a set of GNUNET-getopt directives to initialize a struct GNUNET_CONFIGURATION_C...
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(const struct GNUNET_OS_ProjectData *pd)
Create a new configuration object.
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
void GNUNET_CONFIGURATION_config_settings_free(struct GNUNET_CONFIGURATION_ConfigSettings *cs)
Free resources associated with cs.
void GNUNET_CONFIGURATION_config_tool_run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main task to run to perform operations typical for gnunet-config as per the configuration settings gi...
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_deserialize(struct GNUNET_CONFIGURATION_Handle *cfg, const char *mem, size_t size, const char *source_filename)
De-serializes configuration.
char * GNUNET_CONFIGURATION_default_filename(const struct GNUNET_OS_ProjectData *pd)
Return the filename of the default configuration filename that is used when no explicit configuration...
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_clean_file(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename, GNUNET_CONFIGURATION_CleanCallback cb, void *cb_cls, unsigned int *removed)
Remove redundant option definitions from a parsed configuration file.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_load(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Load configuration.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_parse(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Parse a configuration file, add all of the options in the file to the configuration environment.
void GNUNET_CONFIGURATION_enable_diagnostics(struct GNUNET_CONFIGURATION_Handle *cfg)
Enable extra diagnostics.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory).
Definition disk.c:557
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_cfgfile(char **fn)
Allow user to specify configuration file name (-c option)
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_logfile(char **logfn)
Allow user to specify log file name (-l option)
int GNUNET_GETOPT_run(const char *binaryOptions, const struct GNUNET_GETOPT_CommandLineOption *allOptions, unsigned int argc, char *const *argv)
Parse the command line.
Definition getopt.c:884
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_exclusive(struct GNUNET_GETOPT_CommandLineOption opt)
Make the given option mutually exclusive with other options.
#define GNUNET_GETOPT_OPTION_END
Marker for the end of the list of options.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_help(const struct GNUNET_OS_ProjectData *pd, const char *about)
Defining the option to print the command line help text (-h option).
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_loglevel(char **level)
Define the '-L' log level option.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_version(const char *version)
Define the option to print the version of the application (-v option)
#define GNUNET_log(kind,...)
#define GNUNET_UTIL_VERSION
Version of the API (for entire gnunetutil.so library).
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_SYSERR
enum GNUNET_GenericReturnValue GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
@ GNUNET_ERROR_TYPE_ERROR
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_free(ptr)
Wrapper around free.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
char * GNUNET_OS_installation_get_path(const struct GNUNET_OS_ProjectData *pd, enum GNUNET_OS_InstallationPathKind dirkind)
Get the path to a specific GNUnet installation directory or, with GNUNET_OS_IPK_SELF_PREFIX,...
@ GNUNET_OS_IPK_LIBDIR
Return the directory where libraries are installed.
@ GNUNET_OS_IPK_PREFIX
Return the "PREFIX" directory given to configure.
enum GNUNET_GenericReturnValue GNUNET_PLUGIN_test(const struct GNUNET_OS_ProjectData *pd, const char *library_name)
Test if a plugin exists.
Definition plugin.c:172
#define _(String)
GNU gettext support macro.
Definition platform.h:179
#define EXIT_INVALIDARGUMENT
Definition platform.h:258
Closure for GNUNET_CONFIGURATION_config_tool_run() with settings for what should be done with the con...
int full
Should the generated configuration file contain the whole configuration?
int global_ret
Return value from the operation, to be returned from 'main'.
int list_sections
Whether to show the sections.
int rewrite
Should we write out the configuration file, even if no value was changed?
int is_filename
Treat option as a filename.
unsigned int diagnostics
Diagnostics verbosity.
unsigned int api_version
Must be set to the API version, i.e.
const struct GNUNET_OS_ProjectData * pd
Project data for this configuration object.
Definition of a command line option.
Project-specific data used to help the OS subsystem find installation paths.
const char * user_config_file
Configuration file name to use (if $XDG_CONFIG_HOME is not set).
const char * config_file
Configuration file name (in $XDG_CONFIG_HOME) to use.
const char * version
String identifying the current project version.