00001 /* 00002 * Copyright (C) 2004-2007, 2009, 2013-2015 Internet Systems Consortium, Inc. ("ISC") 00003 * Copyright (C) 1999-2001 Internet Software Consortium. 00004 * 00005 * Permission to use, copy, modify, and/or distribute this software for any 00006 * purpose with or without fee is hereby granted, provided that the above 00007 * copyright notice and this permission notice appear in all copies. 00008 * 00009 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 00010 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 00011 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 00012 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 00013 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 00014 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00015 * PERFORMANCE OF THIS SOFTWARE. 00016 */ 00017 00018 /* $Id: app.h,v 1.11 2009/09/02 23:48:03 tbox Exp $ */ 00019 00020 #ifndef ISC_APP_H 00021 #define ISC_APP_H 1 00022 00023 /***** 00024 ***** Module Info 00025 *****/ 00026 00027 /*! \file isc/app.h 00028 * \brief ISC Application Support 00029 * 00030 * Dealing with program termination can be difficult, especially in a 00031 * multithreaded program. The routines in this module help coordinate 00032 * the shutdown process. They are used as follows by the initial (main) 00033 * thread of the application: 00034 * 00035 *\li isc_app_start(); Call very early in main(), before 00036 * any other threads have been created. 00037 * 00038 *\li isc_app_run(); This will post any on-run events, 00039 * and then block until application 00040 * shutdown is requested. A shutdown 00041 * request is made by calling 00042 * isc_app_shutdown(), or by sending 00043 * SIGINT or SIGTERM to the process. 00044 * After isc_app_run() returns, the 00045 * application should shutdown itself. 00046 * 00047 *\li isc_app_finish(); Call very late in main(). 00048 * 00049 * Applications that want to use SIGHUP/isc_app_reload() to trigger reloading 00050 * should check the result of isc_app_run() and call the reload routine if 00051 * the result is ISC_R_RELOAD. They should then call isc_app_run() again 00052 * to resume waiting for reload or termination. 00053 * 00054 * Use of this module is not required. In particular, isc_app_start() is 00055 * NOT an ISC library initialization routine. 00056 * 00057 * This module also supports per-thread 'application contexts'. With this 00058 * mode, a thread-based application will have a separate context, in which 00059 * it uses other ISC library services such as tasks or timers. Signals are 00060 * not caught in this mode, so that the application can handle the signals 00061 * in its preferred way. 00062 * 00063 * \li MP: 00064 * Clients must ensure that isc_app_start(), isc_app_run(), and 00065 * isc_app_finish() are called at most once. isc_app_shutdown() 00066 * is safe to use by any thread (provided isc_app_start() has been 00067 * called previously). 00068 * 00069 * The same note applies to isc_app_ctxXXX() functions, but in this case 00070 * it's a per-thread restriction. For example, a thread with an 00071 * application context must ensure that isc_app_ctxstart() with the 00072 * context is called at most once. 00073 * 00074 * \li Reliability: 00075 * No anticipated impact. 00076 * 00077 * \li Resources: 00078 * None. 00079 * 00080 * \li Security: 00081 * No anticipated impact. 00082 * 00083 * \li Standards: 00084 * None. 00085 */ 00086 00087 #include <isc/eventclass.h> 00088 #include <isc/lang.h> 00089 #include <isc/magic.h> 00090 #include <isc/result.h> 00091 00092 /*** 00093 *** Types 00094 ***/ 00095 00096 typedef isc_event_t isc_appevent_t; 00097 00098 #define ISC_APPEVENT_FIRSTEVENT (ISC_EVENTCLASS_APP + 0) 00099 #define ISC_APPEVENT_SHUTDOWN (ISC_EVENTCLASS_APP + 1) 00100 #define ISC_APPEVENT_LASTEVENT (ISC_EVENTCLASS_APP + 65535) 00101 00102 /*% 00103 * app module methods. Only app driver implementations use this structure. 00104 * Other clients should use the top-level interfaces (i.e., isc_app_xxx 00105 * functions). magic must be ISCAPI_APPMETHODS_MAGIC. 00106 */ 00107 typedef struct isc_appmethods { 00108 void (*ctxdestroy)(isc_appctx_t **ctxp); 00109 isc_result_t (*ctxstart)(isc_appctx_t *ctx); 00110 isc_result_t (*ctxrun)(isc_appctx_t *ctx); 00111 isc_result_t (*ctxsuspend)(isc_appctx_t *ctx); 00112 isc_result_t (*ctxshutdown)(isc_appctx_t *ctx); 00113 void (*ctxfinish)(isc_appctx_t *ctx); 00114 void (*settaskmgr)(isc_appctx_t *ctx, 00115 isc_taskmgr_t *timermgr); 00116 void (*setsocketmgr)(isc_appctx_t *ctx, 00117 isc_socketmgr_t *timermgr); 00118 void (*settimermgr)(isc_appctx_t *ctx, 00119 isc_timermgr_t *timermgr); 00120 isc_result_t (*ctxonrun)(isc_appctx_t *ctx, isc_mem_t *mctx, 00121 isc_task_t *task, isc_taskaction_t action, 00122 void *arg); 00123 } isc_appmethods_t; 00124 00125 /*% 00126 * This structure is actually just the common prefix of an application context 00127 * implementation's version of an isc_appctx_t. 00128 * \brief 00129 * Direct use of this structure by clients is forbidden. app implementations 00130 * may change the structure. 'magic' must be ISCAPI_APPCTX_MAGIC for any 00131 * of the isc_app_ routines to work. app implementations must maintain 00132 * all app context invariants. 00133 */ 00134 struct isc_appctx { 00135 unsigned int impmagic; 00136 unsigned int magic; 00137 isc_appmethods_t *methods; 00138 }; 00139 00140 #define ISCAPI_APPCTX_MAGIC ISC_MAGIC('A','a','p','c') 00141 #define ISCAPI_APPCTX_VALID(c) ((c) != NULL && \ 00142 (c)->magic == ISCAPI_APPCTX_MAGIC) 00143 00144 ISC_LANG_BEGINDECLS 00145 00146 isc_result_t 00147 isc_app_ctxstart(isc_appctx_t *ctx); 00148 00149 isc_result_t 00150 isc_app_start(void); 00151 /*!< 00152 * \brief Start an ISC library application. 00153 * 00154 * Notes: 00155 * This call should be made before any other ISC library call, and as 00156 * close to the beginning of the application as possible. 00157 * 00158 * Requires: 00159 *\li 'ctx' is a valid application context (for app_ctxstart()). 00160 */ 00161 00162 isc_result_t 00163 isc_app_ctxonrun(isc_appctx_t *ctx, isc_mem_t *mctx, isc_task_t *task, 00164 isc_taskaction_t action, void *arg); 00165 isc_result_t 00166 isc_app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action, 00167 void *arg); 00168 /*!< 00169 * \brief Request delivery of an event when the application is run. 00170 * 00171 * Requires: 00172 *\li isc_app_start() has been called. 00173 *\li 'ctx' is a valid application context (for app_ctxonrun()). 00174 * 00175 * Returns: 00176 * ISC_R_SUCCESS 00177 * ISC_R_NOMEMORY 00178 */ 00179 00180 isc_result_t 00181 isc_app_ctxrun(isc_appctx_t *ctx); 00182 00183 isc_result_t 00184 isc_app_run(void); 00185 /*!< 00186 * \brief Run an ISC library application. 00187 * 00188 * Notes: 00189 *\li The caller (typically the initial thread of an application) will 00190 * block until shutdown is requested. When the call returns, the 00191 * caller should start shutting down the application. 00192 * 00193 * Requires: 00194 *\li isc_app_[ctx]start() has been called. 00195 * 00196 * Ensures: 00197 *\li Any events requested via isc_app_onrun() will have been posted (in 00198 * FIFO order) before isc_app_run() blocks. 00199 *\li 'ctx' is a valid application context (for app_ctxrun()). 00200 * 00201 * Returns: 00202 *\li ISC_R_SUCCESS Shutdown has been requested. 00203 *\li ISC_R_RELOAD Reload has been requested. 00204 */ 00205 00206 isc_boolean_t 00207 isc_app_isrunning(void); 00208 /*!< 00209 * \brief Return if the ISC library application is running. 00210 * 00211 * Returns: 00212 *\li ISC_TRUE App is running. 00213 *\li ISC_FALSE App is not running. 00214 */ 00215 00216 isc_result_t 00217 isc_app_ctxshutdown(isc_appctx_t *ctx); 00218 00219 isc_result_t 00220 isc_app_shutdown(void); 00221 /*!< 00222 * \brief Request application shutdown. 00223 * 00224 * Notes: 00225 *\li It is safe to call isc_app_shutdown() multiple times. Shutdown will 00226 * only be triggered once. 00227 * 00228 * Requires: 00229 *\li isc_app_[ctx]run() has been called. 00230 *\li 'ctx' is a valid application context (for app_ctxshutdown()). 00231 * 00232 * Returns: 00233 *\li ISC_R_SUCCESS 00234 *\li ISC_R_UNEXPECTED 00235 */ 00236 00237 isc_result_t 00238 isc_app_ctxsuspend(isc_appctx_t *ctx); 00239 /*!< 00240 * \brief This has the same behavior as isc_app_ctxsuspend(). 00241 */ 00242 00243 isc_result_t 00244 isc_app_reload(void); 00245 /*!< 00246 * \brief Request application reload. 00247 * 00248 * Requires: 00249 *\li isc_app_run() has been called. 00250 * 00251 * Returns: 00252 *\li ISC_R_SUCCESS 00253 *\li ISC_R_UNEXPECTED 00254 */ 00255 00256 void 00257 isc_app_ctxfinish(isc_appctx_t *ctx); 00258 00259 void 00260 isc_app_finish(void); 00261 /*!< 00262 * \brief Finish an ISC library application. 00263 * 00264 * Notes: 00265 *\li This call should be made at or near the end of main(). 00266 * 00267 * Requires: 00268 *\li isc_app_start() has been called. 00269 *\li 'ctx' is a valid application context (for app_ctxfinish()). 00270 * 00271 * Ensures: 00272 *\li Any resources allocated by isc_app_start() have been released. 00273 */ 00274 00275 void 00276 isc_app_block(void); 00277 /*!< 00278 * \brief Indicate that a blocking operation will be performed. 00279 * 00280 * Notes: 00281 *\li If a blocking operation is in process, a call to isc_app_shutdown() 00282 * or an external signal will abort the program, rather than allowing 00283 * clean shutdown. This is primarily useful for reading user input. 00284 * 00285 * Requires: 00286 * \li isc_app_start() has been called. 00287 * \li No other blocking operations are in progress. 00288 */ 00289 00290 void 00291 isc_app_unblock(void); 00292 /*!< 00293 * \brief Indicate that a blocking operation is complete. 00294 * 00295 * Notes: 00296 * \li When a blocking operation has completed, return the program to a 00297 * state where a call to isc_app_shutdown() or an external signal will 00298 * shutdown normally. 00299 * 00300 * Requires: 00301 * \li isc_app_start() has been called. 00302 * \li isc_app_block() has been called by the same thread. 00303 */ 00304 00305 isc_result_t 00306 isc_appctx_create(isc_mem_t *mctx, isc_appctx_t **ctxp); 00307 /*!< 00308 * \brief Create an application context. 00309 * 00310 * Requires: 00311 *\li 'mctx' is a valid memory context. 00312 *\li 'ctxp' != NULL && *ctxp == NULL. 00313 */ 00314 00315 void 00316 isc_appctx_destroy(isc_appctx_t **ctxp); 00317 /*!< 00318 * \brief Destroy an application context. 00319 * 00320 * Requires: 00321 *\li '*ctxp' is a valid application context. 00322 * 00323 * Ensures: 00324 *\li *ctxp == NULL. 00325 */ 00326 00327 void 00328 isc_appctx_settaskmgr(isc_appctx_t *ctx, isc_taskmgr_t *taskmgr); 00329 /*!< 00330 * \brief Associate a task manager with an application context. 00331 * 00332 * This must be done before running tasks within the application context. 00333 * 00334 * Requires: 00335 *\li 'ctx' is a valid application context. 00336 *\li 'taskmgr' is a valid task manager. 00337 */ 00338 00339 void 00340 isc_appctx_setsocketmgr(isc_appctx_t *ctx, isc_socketmgr_t *socketmgr); 00341 /*!< 00342 * \brief Associate a socket manager with an application context. 00343 * 00344 * This must be done before handling socket events within the application 00345 * context. 00346 * 00347 * Requires: 00348 *\li 'ctx' is a valid application context. 00349 *\li 'socketmgr' is a valid socket manager. 00350 */ 00351 00352 void 00353 isc_appctx_settimermgr(isc_appctx_t *ctx, isc_timermgr_t *timermgr); 00354 /*!< 00355 * \brief Associate a socket timer with an application context. 00356 * 00357 * This must be done before handling timer events within the application 00358 * context. 00359 * 00360 * Requires: 00361 *\li 'ctx' is a valid application context. 00362 *\li 'timermgr' is a valid timer manager. 00363 */ 00364 00365 /*%< 00366 * See isc_appctx_create() above. 00367 */ 00368 typedef isc_result_t 00369 (*isc_appctxcreatefunc_t)(isc_mem_t *mctx, isc_appctx_t **ctxp); 00370 00371 isc_result_t 00372 isc_app_register(isc_appctxcreatefunc_t createfunc); 00373 /*%< 00374 * Register a new application implementation and add it to the list of 00375 * supported implementations. This function must be called when a different 00376 * event library is used than the one contained in the ISC library. 00377 */ 00378 00379 isc_result_t 00380 isc__app_register(void); 00381 /*%< 00382 * A short cut function that specifies the application module in the ISC 00383 * library for isc_app_register(). An application that uses the ISC library 00384 * usually do not have to care about this function: it would call 00385 * isc_lib_register(), which internally calls this function. 00386 */ 00387 00388 ISC_LANG_ENDDECLS 00389 00390 #endif /* ISC_APP_H */