modules_init.txt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. Kamailio Module intialization description
  2. ===========================================
  3. This document is a very brief overview on what possibilities are for a module
  4. to run some initializations (or put in another way: What's safe and what's
  5. not safe to do in mod_init and mod_child_init).
  6. The interesting function are the mod_init (the init_f memeber of
  7. the module_exports structure) and the mod_child_init (init_child_f in
  8. module_exports) functions.
  9. mod_init
  10. --------
  11. mod_init is called after parsing the config, loading all the modules and
  12. going into daemon mode. mod_init is called in the main process context,
  13. before changing the uid or gid (so if you want to do something requiring
  14. higher privileges this is the place to do it). This is not the right place
  15. to fork more Kamailio processes, but instead is the only place where one can
  16. register future forked processes (see register_procs()).
  17. mod_init is ideal for initializing per process variables, assuming that you
  18. don't need different values for each Kamailio child (in which case see
  19. mod_child_init below) and shared memory variables assuming that you don't
  20. need Kamailio's number of processes (which is not available at this point).
  21. mod_child_init
  22. ---------------
  23. mod_child_init is called for each forked Kamailio process with 2 exceptions:
  24. - it's called for the main process too and it's called twice
  25. - it's not called for Kamailio processes forked with PROC_NOCHLDINIT
  26. mod_child_init is always called after mod_init. It takes one parameter, the
  27. process rank. This parameter can be used to differentiate between normal
  28. new-forked-process calls and some special calls.
  29. There are two very special rank values: PROC_INIT (as of 2007-06-06) and
  30. PROC_MAIN:
  31. mod_child_init(PROC_INIT) is the first mod_child_init call made, and it's
  32. guaranteed to happen before any child process is forked. The process context
  33. is the "main" process (as for mod_init), but this time we have a little bit
  34. more information: we know the number of Kamailio processes. This is the right
  35. place to initialize things like shared arrays[get_max_procs()].
  36. Note also that everything done here will be inherited in all the future
  37. children processes (since it's executed in the "main" process context before
  38. forking).
  39. mod_child_init(PROC_MAIN) is another call that is done in the same "main"
  40. process context. This call happens just before initializing the main tcp
  41. process. This is the only right place for forking more Kamailio processes
  42. (see fork_process()). WARNING: the context is the same "main" process as
  43. for mod_child_init(PROC_INIT) and mod_init() so care must be taken not to
  44. initialize things twice or three times for the "main" process.
  45. Except for the "main" process case mod_child_init(rank) will be called only
  46. once for each new child process, just after forking. A positive non-zero rank
  47. means the current process is a normal Kamailio process and a negative rank has
  48. some special meaning (grep PROC_ sr_module.h for more info).
  49. mod_child_init(rank) is the best place for initializing per process variables,
  50. opening per process database connections, new sockets a.s.o. Note however
  51. that several mod_child_init()s can execute in the same time (with the
  52. PROC_INIT exceptions) so this is not a good place for initializing shared
  53. memory variables.
  54. mod_child_init in the no-fork case
  55. ----------------------------------
  56. If Kamailio is started in no-fork mode it will try to start as few processes as
  57. possible (as of this date it will start 3 processes the main process and the
  58. 2 timers). In this case mod_child_init() will be called 3 times in the
  59. same "main" process context: mod_child_init(PROC_INIT);
  60. mod_child_init(PROC_MAIN) and mod_child_init(1) (since the first process is
  61. also the "main" one).
  62. Forking new Kamailio processes from a module
  63. ---------------------------------------
  64. static int mod_init()
  65. {
  66. register_procs(2); /* we want to create 2 extra processes */
  67. return 0;
  68. }
  69. static int mod_child(int rank)
  70. {
  71. int pid;
  72. if (rank==PROC_MAIN){
  73. pid=fork_process(some_positive_number, "name", 1);
  74. if (pid<0)
  75. return -1; /* error */
  76. else if (pid ==0){
  77. /* child1_main_loop(); */
  78. }else{ /* parent */
  79. pid=fork_process(some_other_postivie_number, "name2", 1);
  80. if (pid<0)
  81. /* ... same as above */
  82. ...
  83. }
  84. }
  85. return 0;
  86. }
  87. The forked child process shall also update its local configuration,
  88. please read the section "Refreshing the configuration" in doc/cfg.txt.
  89. Summary
  90. -------
  91. mod_init():
  92. - register the number of processes that will be forked from
  93. mod_child_init(PROC_MAIN) (if any) , see register_procs().
  94. - initialize things requiring higher privileges
  95. - initialize per process variables with common value (they will be
  96. inherited by all the future children)
  97. - initialize shared memory variables if possible (no need for things that
  98. are available latter like the process count)
  99. mod_child_init(PROC_INIT)
  100. - same as mod_init except for registering processes & privileged stuff
  101. - the process number is now available so for example initializing shared
  102. per process statistics arrays (int stats[proc_no]) is possible
  103. - guaranteed to be run before any process is forked (like mod_init()).
  104. WARNING: same process context as mod_init() and mod_child_init(PROC_MAIN) so
  105. care must be taken not to initialize things 2 or 3 times for the "main"
  106. process.
  107. mod_child_init(PROC_MAIN)
  108. - the only place from where another Kamailio process can be forked
  109. (see fork_process ()), but remember first to register the number of
  110. to-be-forked processes in mod_init()
  111. mod_child_init(rank!=PROC_INIT && rank!=PROC_MAIN)
  112. - initialize other per process variables (e.g. different values), whose
  113. initial values will be visible only in the current process (they won't
  114. be inherited anymore).
  115. - open per process db connections, sockets a.s.o.
  116. - seed custom random generators
  117. WARNINGs: - several mod_child_inits() can run in parallel
  118. - the rank number is not necessarily unique
  119. -