Clamav AntiVirus software source code analysis notes [7]

Source: Internet
Author: User
Clamav AntiVirus software source code analysis notes [7]


Hedgehog @ http://blog.csdn.net/littlehedgehog





[Accept loop processing]


I could have finished the main Clamd functions in Article 5 last time, but the thread is not running and the fire is not yet reached. The end of Clamd will be reached.

  1. If (thr_pool = thrmgr_new (max_threads, idletimeout, scanner_thread) = NULL)
  2. {
  3. Logg ("! Thrmgr_new failed/n ");
  4. Exit (-1 );
  5. }
  6. Time (& start_time); // record current time saved in start_time
  7. For (;;)
  8. {
  9. New_sd = accept (socketd, NULL, NULL );
  10. /* Note that the interrupt will interrupt accept blocking. Here we only need to check whether errno is EINTR. If the returned value is-1 (indicating accpet failure)
  11. * This is not due to interruption, but to other problems.
  12. */
  13. If (new_sd =-1) & (errno! = EINTR ))
  14. {
  15. /* Very bad-need to exit or restart */
  16. # Ifdef HAVE_STRERROR_R
  17. Logg ("! Accept () failed: % s/n ", strerror_r (errno, buff, BUFFSIZE ));
  18. # Else
  19. Logg ("! Accept () failed/n ");
  20. # Endif
  21. Continue;
  22. }
  23. /* This signal indicates that the terminal is disconnected. We need to re-open the log file */
  24. If (sighup)
  25. {
  26. Logg ("sighup caught: re-opening log file./N ");
  27. Logg_close ();
  28. Sighup = 0;
  29. If (! Logg_file & (CPT = partition OPT (COPT, "logfile ")))
  30. Logg_file = CPT-> strarg;
  31. }
  32. /* Progexit is the identifier that our process receives the SIGINT-Related Signal settings. If it is unfortunate that it is received, we will exit */
  33. If (! Progexit & new_sd> = 0)
  34. {
  35. Client_conn = (client_conn_t *) mmalloc (sizeof (struct client_conn_tag); // here is the userdata parameter for assembling thrmgr_dispatch
  36. Client_conn-> sd = new_sd; // socket descriptor! This is always used later as a channel for communications between the server and the client.
  37. Client_conn-> options = options;
  38. Client_conn-> copt = copt;
  39. Client_conn-> root = cl_dup (root );
  40. Client_conn-> root_timestamp = reloaded_time;
  41. Client_conn-> limits = & limits;
  42. Client_conn-> mainpid = mainpid;
  43. If (! Thrmgr_dispatch (thr_pool, client_conn ))
  44. {
  45. Close (client_conn-> SD );
  46. Free (client_conn );
  47. Logg ("! Thread dispatch failed/N ");
  48. }
  49. }
  50. Pthread_mutex_lock (& exit_mutex );
  51. If (progexit)
  52. {
  53. If (new_sd> = 0)
  54. {
  55. Close (new_sd );
  56. }
  57. Pthread_mutex_unlock (& exit_mutex );
  58. Break;
  59. }
  60. Pthread_mutex_unlock (& exit_mutex );
  61. // If self-check is set
  62. If (selfchk)
  63. {
  64. Time (& current_time );
  65. If (current_time-start_time)> (time_t) selfchk) // This indicates that the time of the virus database exceeds the effective time set by us.
  66. {
  67. If (reload_db (root, copt, TRUE) // check whether the database has been changed. if you want to set reload, reload the following nodes.
  68. {
  69. Pthread_mutex_lock (& reload_mutex );
  70. Reload = 1;
  71. Pthread_mutex_unlock (& reload_mutex );
  72. }
  73. Time (& start_time );
  74. }
  75. }
  76. Pthread_mutex_lock (& reload_mutex );
  77. If (reload)
  78. {
  79. Pthread_mutex_unlock (& reload_mutex );
  80. Root = reload_db (root, COPT, false );
  81. Pthread_mutex_lock (& reload_mutex );
  82. Reload = 0;
  83. Time (& reloaded_time );
  84. Pthread_mutex_unlock (& reload_mutex );
  85. # Ifdef clamuko
  86. If (export OPT (COPT, "clamukoscanonline") | export OPT (COPT, "clamukoscanonaccess "))
  87. {
  88. Logg ("Stopping and restarting Clamuko./n ");
  89. Pthread_kill (clamuko_pid, SIGUSR1 );
  90. Pthread_join (clamuko_pid, NULL );
  91. Tharg-> root = root;
  92. Pthread_create (& clamuko_pid, & clamuko_attr, clamukoth, tharg );
  93. }
  94. # Endif
  95. }
  96. Else
  97. {
  98. Pthread_mutex_unlock (& reload_mutex );
  99. }
  100. }
  101. /* Destroy the thread manager.
  102. * This waits for all current tasks to end
  103. */
  104. Thrmgr_destroy (thr_pool );

Post it here If (thr_pool = thrmgr_new (max_threads, idletimeout, scanner_thread) = NULL)

The main reason is that when we create a thread pool, we have determined the function to be executed by the thread.Scanner_threadYou can look back at the thread pool creation and processing code. The following is to assemble the parameter struct for the thread. The main reason is that the parameter passing form of the thread has been fixed, so we need to load the struct.


The processing function of the thread is only a proxy. The cadres who really do things only have command, as shown below:

  1. /* Set the main function of the thread to process the job */
  2. Void scanner_thread (void * Arg)
  3. {
  4. Client_conn_t * conn = (client_conn_t *) ARG;
  5. Sigset_t sigset;
  6. Int ret, timeout, session = false;
  7. Struct explain struct * CPT;
  8. /* Ignore all signals the author has already commented on it. Here it is set to 1, which is actually a blocked signal */
  9. Sigfillset (& sigset );
  10. Pthread_sigmask (sig_setmask, & sigset, null); // sets the thread signal shielding code. The semantics is the same as sigprocmask, however, cancel signals that cannot be blocked and restart signals that cannot be responded are protected. Blocked signals are stored in the signal queue and can be taken out by the sigpending () function.
  11. If (CPT = fig (Conn-> COPT, "readtimeout ")))
  12. {
  13. Timeout = cpt-> numarg;
  14. }
  15. Else
  16. {
  17. Timeout = CL_DEFAULT_SCANTIMEOUT;
  18. }
  19. If (! Timeout)
  20. Timeout =-1;
  21. Do
  22. {
  23. Ret = command (conn-> sd, conn-> root, conn-> limits, conn-> options, conn-> copt, timeout); // This is the real Officer
  24. If (ret =-1)
  25. {
  26. Break;
  27. }
  28. Switch (ret)
  29. {
  30. Case COMMAND_SHUTDOWN:
  31. Pthread_mutex_lock (& exit_mutex );
  32. Progexit = 1;
  33. Kill (conn-> mainpid, SIGTERM); // here, the Shutdown signal is sent to the main thread, that is, the server process.
  34. Pthread_mutex_unlock (& exit_mutex );
  35. Break;
  36. Case COMMAND_RELOAD:
  37. Pthread_mutex_lock (& reload_mutex );
  38. Reload = 1;
  39. Pthread_mutex_unlock (& reload_mutex );
  40. Break;
  41. Case COMMAND_SESSION: // the client sends the command session. We still need to continue to obtain the command loop here (but in what status do we need to send this command)
  42. Session = TRUE;
  43. Timeout = 5;
  44. Break;
  45. Case COMMAND_END:
  46. Session = FALSE;
  47. Break;
  48. }
  49. If (session)
  50. {
  51. Pthread_mutex_lock (& exit_mutex );
  52. If (progexit)
  53. {
  54. Session = FALSE;
  55. }
  56. Pthread_mutex_unlock (& exit_mutex );
  57. Pthread_mutex_lock (& reload_mutex );
  58. If (Conn-> root_timestamp! = Reloaded_time)
  59. {
  60. Session = false;
  61. }
  62. Pthread_mutex_unlock (& reload_mutex );
  63. }
  64. }
  65. While (session );
  66. Close (Conn-> SD );
  67. Cl_free (Conn-> root );
  68. Free (conn );
  69. Return;
  70. }

In this way, through command, we shirk the task to other functions to do it. command is the final function, but it will wait for the next time, because the lab is about to close.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.