README.fb_shutdown 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. fb_shutdown(), fb_shutdown_callback() - new API call in Firebird 2.5.
  2. Implements smart shutdown of engine. Primarily used when working with embedded Firebird.
  3. Author:
  4. Alex Peshkoff <[email protected]>
  5. Syntax is:
  6. typedef int (*FB_SHUTDOWN_CALLBACK)(const int reason, const int mask, void* arg);
  7. int fb_shutdown(unsigned int timeout,
  8. const int reason);
  9. ISC_STATUS fb_shutdown_callback(ISC_STATUS* status_vector,
  10. FB_SHUTDOWN_CALLBACK callback_function,
  11. const int mask,
  12. void* arg);
  13. Description:
  14. fb_shutdown() performs smart shutdown of various Firebird subsystems (yValve, engine, redirector).
  15. It DOES NOT perform shutdown of remote servers, to which you are currently attached - just
  16. terminates any Firebird activity in the current process. fb_shutdown() was primarily designed
  17. to be used by engine itself, but also can be used in user applications - for example, if you want
  18. to close all opened handles at once, fb_shutdown() may be used for it. Normally it should not be
  19. used, because Firebird libraries (both kinds - embedded or pure client) do call it automatically
  20. at exit(). To make fb_shutdown() be called at exit, you should attach at least one database (or
  21. service).
  22. fb_shutdown() accepts 2 parameters - timeout in milliseconds and reason of shutdown. Engine uses
  23. negative reason codes (they are listed in ibase.h, see constants starting with fb_shutrsn), if
  24. you need to call fb_shutdown() from your program, you must use positive value for reason. This
  25. value is passed to callback_function, passed as an argument to fb_shutdown_callback(), and you can
  26. take appropriate actions when writing callback function.
  27. Zero return value of fb_shutdown() means shutdown is successful, non-zero means some errors took
  28. place during shutdown. You should consult firebird.log for more information.
  29. fb_shutdown_callback() setups callback function, which will be called during shutdown. It has 4
  30. parameters - status vector, pointer to callback function, call mask and argument to be passed
  31. to callback function. Call mask can have the following values:
  32. fb_shut_confirmation - callback function may return non-zero value to abort shutdown
  33. fb_shut_preproviders - callback function will be called before shutting down engine
  34. fb_shut_postproviders - callback function will be called after shutting down engine
  35. fb_shut_finish - final step, callback function may wait for some activity to be terminated
  36. or ORed combination of them (to make same function be called in required cases).
  37. Non-zero mask passed to callback will be ORed with mask used in previous call (if any). Mask also
  38. may have special value 0 - in that case previous mask is reset and callback will never be called.
  39. Callback function has 3 parameters - reason of shutdown, actual value of mask with which it was
  40. called and argument passed by user to fb_shutdown_callback(). There are 2 specially interesting
  41. shutdown reasons:
  42. fb_shutrsn_exit_called - Firebird is closing due to exit() or unloaded client/embedded library
  43. fb_shutrsn_signal - signal SIGINT or SIGTERM was caught (posix only)
  44. Second parameter (actual value of mask) helps to distinguish if callback was invoked before or after
  45. engine shutdown.
  46. First and second parameters help you decide what action to be taken in your callback. Third can
  47. be used for any purporse you like and may be NULL.
  48. Zero return value of callback function means it performed it's job OK, non-zero is interpreted
  49. depending upon call mask. For fb_shut_confirmation non-zero means that shutdown will not be
  50. performed. It's bad idea to return non-zero if shutdown is due to exit() called. In all other cases
  51. it means some errors took place, and non-zero value will be returned from fb_shutdown(). It's
  52. callback function's responsibility to notify the world about exact reasons of error return.
  53. fb_shutdown_callback() almost always returns successfully, though in some cases (out of memory
  54. for example) it can return error.
  55. Sample (it will make your program do not terminate on ctrl-C pressed after attaching databases):
  56. #include <ibase.h>
  57. // callback function for shutdown
  58. static int ignoreCtrlC(const int reason, const int, void*)
  59. {
  60. return reason == fb_shutrsn_signal ? 1 : 0;
  61. }
  62. int main(int argc, char *argv[])
  63. {
  64. ISC_STATUS_ARRAY status;
  65. if (fb_shutdown_callback(status, ignoreCtrlC, fb_shut_confirmation, 0))
  66. {
  67. isc_print_status(status);
  68. return 1;
  69. }
  70. // your code continues ...
  71. }