gnunet_program_lib.h (8093B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2001-2013 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 21 #if ! defined (__GNUNET_UTIL_LIB_H_INSIDE__) 22 #error "Only <gnunet_util_lib.h> can be included directly." 23 #endif 24 25 /** 26 * @addtogroup libgnunetutil 27 * Multi-function utilities library for GNUnet programs 28 * @{ 29 * 30 * @author Christian Grothoff 31 * 32 * @file 33 * Functions related to starting programs 34 * 35 * @defgroup program Program library 36 * Start command-line programs. 37 * @{ 38 */ 39 40 #ifndef GNUNET_PROGRAM_LIB_H 41 #define GNUNET_PROGRAM_LIB_H 42 43 #ifdef __cplusplus 44 extern "C" 45 { 46 #if 0 /* keep Emacsens' auto-indent happy */ 47 } 48 #endif 49 #endif 50 51 52 #include "gnunet_configuration_lib.h" 53 #include "gnunet_getopt_lib.h" 54 #include "gnunet_scheduler_lib.h" 55 56 /** 57 * Main function that will be run. 58 * 59 * @param cls closure 60 * @param args remaining command-line arguments 61 * @param cfgfile name of the configuration file used (for saving, can be NULL!) 62 * @param cfg configuration 63 */ 64 typedef void 65 (*GNUNET_PROGRAM_Main) (void *cls, 66 char *const *args, 67 const char *cfgfile, 68 const struct GNUNET_CONFIGURATION_Handle *cfg); 69 70 71 /** 72 * Run a standard GNUnet command startup sequence (initialize loggers 73 * and configuration, parse options). 74 * 75 * @param pd project data for the project the program belongs to 76 * @param argc number of command line arguments in @a argv 77 * @param argv command line arguments 78 * @param binaryName our expected name 79 * @param binaryHelp help text for the program 80 * @param options command line options 81 * @param task main function to run 82 * @param task_cls closure for @a task 83 * @param run_without_scheduler #GNUNET_NO start the scheduler, 84 * #GNUNET_YES do not start the scheduler just run the main task 85 * @return #GNUNET_SYSERR on error, 86 * #GNUNET_NO if successful option processing called 87 * for the program to terminate, 88 * #GNUNET_OK on success (#a task was invoked) 89 */ 90 enum GNUNET_GenericReturnValue 91 GNUNET_PROGRAM_run2 (const struct GNUNET_OS_ProjectData *pd, 92 int argc, 93 char *const *argv, 94 const char *binaryName, 95 const char *binaryHelp, 96 const struct GNUNET_GETOPT_CommandLineOption *options, 97 GNUNET_PROGRAM_Main task, 98 void *task_cls, 99 int run_without_scheduler); 100 101 102 /** 103 * Run a standard GNUnet command startup sequence (initialize loggers 104 * and configuration, parse options). 105 * 106 * @param pd project data for the project the program belongs to 107 * @param argc number of command line arguments 108 * @param argv command line arguments 109 * @param binaryName our expected name 110 * @param binaryHelp helptext for "-h" option (about the app) 111 * @param options command line options 112 * @param task main function to run 113 * @param task_cls closure for @a task 114 * @return #GNUNET_SYSERR on error, 115 * #GNUNET_NO if successful option processing called 116 * for the program to terminate, 117 * #GNUNET_OK on success (#a task was invoked) 118 */ 119 enum GNUNET_GenericReturnValue 120 GNUNET_PROGRAM_run (const struct GNUNET_OS_ProjectData *pd, 121 int argc, 122 char *const *argv, 123 const char *binaryName, 124 const char *binaryHelp, 125 const struct GNUNET_GETOPT_CommandLineOption *options, 126 GNUNET_PROGRAM_Main task, 127 void *task_cls); 128 129 130 enum GNUNET_GenericReturnValue 131 GNUNET_DAEMON_register (const char *daemon_name, 132 const char *daemon_desc, 133 GNUNET_PROGRAM_Main task); 134 135 136 /** 137 * Start all services and daemons in a single process. 138 * 139 * @param pd project data for the project the program belongs to 140 * @param argc number of command line arguments 141 * @param argv command line arguments 142 */ 143 void 144 GNUNET_PROGRAM_monolith_main (const struct GNUNET_OS_ProjectData *pd, 145 int argc, 146 char *const *argv, 147 struct GNUNET_CONFIGURATION_Handle *cfg); 148 149 #ifndef HAVE_GNUNET_MONOLITH 150 151 #define GNUNET_DAEMON_MAIN(daemon_id, daemon_name, daemon_help, init_cb) \ 152 int main(int argc, char *const *argv) \ 153 { \ 154 int ret; \ 155 struct GNUNET_GETOPT_CommandLineOption options[] = { \ 156 GNUNET_GETOPT_OPTION_END \ 157 }; \ 158 ret = GNUNET_PROGRAM_run(GNUNET_OS_project_data_gnunet(), \ 159 argc, \ 160 argv, \ 161 daemon_name, \ 162 daemon_help, \ 163 options, \ 164 init_cb, \ 165 NULL); \ 166 return ret; \ 167 } 168 169 #else 170 171 #define GNUNET_DAEMON_MAIN(daemon_id, daemon_name, daemon_help, init_cb) \ 172 static int init_##daemon_id(void); \ 173 int __attribute__((constructor)) init_##daemon_id(void) \ 174 { \ 175 return GNUNET_DAEMON_register(daemon_name, \ 176 daemon_help, \ 177 init_cb); \ 178 } 179 180 #endif 181 182 183 /** 184 * Create configuration handle from options and configuration file. 185 * 186 * @param pd project data for the project the program belongs to 187 * @param argc number of command line arguments 188 * @param argv command line arguments 189 */ 190 enum GNUNET_GenericReturnValue 191 GNUNET_PROGRAM_conf_and_options (const struct GNUNET_OS_ProjectData *pd, 192 int argc, 193 char *const *argv, 194 struct GNUNET_CONFIGURATION_Handle *cfg); 195 196 197 /** 198 * Run the mainloop in a monolithic libgnunet. 199 * Must be called such that services are actually launched. 200 * 201 * @param pd project data for the project the program belongs to 202 * @param argc number of command line arguments 203 * @param argv command line arguments 204 */ 205 void 206 GNUNET_DAEMON_main (const struct GNUNET_OS_ProjectData *pd, 207 int argc, 208 char *const *argv, 209 struct GNUNET_CONFIGURATION_Handle *cfg, 210 enum GNUNET_GenericReturnValue with_scheduler); 211 212 213 #if 0 /* keep Emacsens' auto-indent happy */ 214 { 215 #endif 216 #ifdef __cplusplus 217 } 218 #endif 219 220 /* ifndef GNUNET_PROGRAM_LIB_H */ 221 #endif 222 223 /** @} */ /* end of group program */ 224 225 /** @} */ /* end of group addition */ 226 227 /* end of gnunet_program_lib.h */