modules_init.txt 6.0 KB

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