Changeset 131 for trunk/src/sh_mail.c


Ignore:
Timestamp:
Oct 22, 2007, 11:19:15 PM (17 years ago)
Author:
rainer
Message:

Use thread-safe libc functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sh_mail.c

    r34 r131  
    654654/* The mailer.
    655655 */
    656 static int sh_mail_end_conn (FILE * connfile);
    657 static FILE * sh_mail_start_conn (int aFlag);
     656static int sh_mail_end_conn (FILE * connfile, int fd);
     657static FILE * sh_mail_start_conn (int aFlag, int * fd);
    658658
    659659static
     
    797797
    798798    static  int ma_block = 0;
     799
     800    int       ma_socket = -1;
    799801
    800802    SH_FIFO * fifo_temp = NULL;
     
    10651067        while (address_list[i] != NULL && i < address_num)
    10661068          {
    1067             connfile = sh_mail_start_conn (i);
     1069            connfile = sh_mail_start_conn (i, &ma_socket);
    10681070           
    10691071            if (NULL != connfile)
     
    10731075                wrlen -= sl_strlen(mailMsg);
    10741076                if (wrlen == 0)
    1075                   status = sh_mail_end_conn (connfile);
     1077                  status = sh_mail_end_conn (connfile, ma_socket);
    10761078                else
    10771079                  status = -1;
     
    11011103    else
    11021104      {
    1103         connfile = sh_mail_start_conn ( -9 );
     1105        connfile = sh_mail_start_conn ( -9 , &ma_socket);
    11041106       
    11051107        if (NULL != connfile)
     
    11081110            wrlen -= sl_strlen(mailMsg);
    11091111            if (wrlen == 0)
    1110               status = sh_mail_end_conn (connfile);
     1112              status = sh_mail_end_conn (connfile, ma_socket);
    11111113            else
    11121114              status = -1;
     
    12871289static time_t time_wait = 300;
    12881290
    1289 static FILE * sh_mail_start_conn (int aFlag)
     1291static FILE * sh_mail_start_conn (int aFlag, int * ma_socket)
    12901292{
    12911293  char       * address;
     
    13021304  FILE       * connFile = NULL;
    13031305  struct tm  * my_tm;
     1306#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
     1307  struct tm    time_tm;
     1308#endif
    13041309  time_t       my_time;
    13051310  char         my_tbuf[128];
     
    13121317  SL_ENTER(_("sh_mail_start_conn"));
    13131318
    1314   time_wait = 300;
     1319  *ma_socket = -1;
     1320  time_wait  = 300;
    13151321
    13161322  if (aFlag >= 0)
     
    14161422  /* say HELO to the other socket
    14171423   */
    1418   if (0 == sh_mail_wait (220, connFile))
     1424  if (0 == sh_mail_wait (220, fd))
    14191425    {
    14201426      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
     
    14461452  (void) fflush(connFile);
    14471453
    1448   if (0 == sh_mail_wait(250, connFile))
     1454  if (0 == sh_mail_wait(250, fd))
    14491455    {
    14501456      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
     
    14791485  (void) fflush(connFile);
    14801486
    1481   if (0 == sh_mail_wait(250, connFile))
     1487  if (0 == sh_mail_wait(250, fd))
    14821488    {
    14831489      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
     
    15001506      (void) fflush(connFile);
    15011507
    1502       if (0 == sh_mail_wait(250, connFile))
     1508      if (0 == sh_mail_wait(250, fd))
    15031509        {
    15041510          sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
     
    15241530          (void) fflush(connFile);
    15251531         
    1526           if (0 == sh_mail_wait(250, connFile))
     1532          if (0 == sh_mail_wait(250, fd))
    15271533            {
    15281534              sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
     
    15491555  (void) fflush(connFile);
    15501556
    1551   if (0 == sh_mail_wait(354, connFile))
     1557  if (0 == sh_mail_wait(354, fd))
    15521558    {
    15531559      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
     
    15611567
    15621568  my_time = time(NULL);
     1569#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
     1570  my_tm   = localtime_r(&my_time, &time_tm);
     1571#else
    15631572  my_tm   = localtime(&my_time);
     1573#endif
    15641574  (void)    strftime(my_tbuf, 127, _("%a, %d %b %Y %H:%M:%S %Z"), my_tm);
    15651575
     
    15761586          my_tbuf, 13, 10);
    15771587
     1588  *ma_socket = fd;
    15781589  SL_RETURN( connFile, _("sh_mail_start_conn"));
    15791590}
     
    15851596 */
    15861597
    1587 static int sh_mail_end_conn (FILE * connFile)
     1598static int sh_mail_end_conn (FILE * connFile, int fd)
    15881599{
    15891600  SL_ENTER(_("sh_mail_end_conn"));
     
    15971608  TPT(( 0, FIL__, __LINE__, _("msg=<message end written>\n")));
    15981609
    1599   if (0 != sh_mail_wait(250, connFile))
     1610  if (0 != sh_mail_wait(250, fd))
    16001611    { 
    16011612      (void) fflush(connFile);
     
    16251636 */
    16261637
    1627 static jmp_buf wait_timeout;
    1628 
    1629 static void sh_mail_alarmhandle (int mysignal)
    1630 {
    1631   /*@-noeffect@*/
    1632   (void) mysignal; /* avoid compiler warning */
    1633   /*@+noeffect@*/
    1634 
    1635   longjmp(wait_timeout, 1);
    1636 }
    1637 
    1638 static int sh_mail_wait(int code, FILE * ma_socket)
     1638static int sh_mail_wait(int code, int ma_socket)
    16391639{
    16401640  int rcode, g;
     1641
     1642  char c;
    16411643
    16421644  char errmsg[128];
     
    16511653  time_t waited_time;
    16521654
    1653   struct   sigaction          old_act;
    1654   volatile unsigned int       old_alarm = 0;
    1655 
    1656   struct  sigaction  new_act;
    1657   sigset_t           unblock;
    1658 
    1659   (void) sigemptyset(&unblock);
    1660   (void) sigaddset  (&unblock, SIGALRM);
    1661 
    1662   new_act.sa_handler = sh_mail_alarmhandle;
    1663   (void) sigemptyset( &new_act.sa_mask );       /* set an empty mask       */
    1664   new_act.sa_flags = 0;                         /* init sa_flags           */
    1665 
    16661655  SL_ENTER(_("mail_wait"));
    16671656 
    1668   /* alarm was triggered
    1669    */
    1670   if (setjmp(wait_timeout) != 0)
    1671     {
    1672       (void) alarm(0);
    1673       (void) sigaction (SIGALRM, &old_act, NULL);
    1674       (void) alarm(old_alarm);
    1675       (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
    1676       TPT((0, FIL__, __LINE__, _("msg=<mail_wait: timeout>\n")));
    1677       SL_RETURN( 0, _("mail_wait"));
    1678     }
    1679 
    16801657  waited_time = time(NULL);
    16811658
    16821659  /* timeout after 5 minutes
    16831660   */
    1684   old_alarm = alarm(0);
    1685   (void) sigaction (SIGALRM, &new_act, &old_act);
    1686   (void) alarm((unsigned int) time_wait);
    16871661
    16881662  rcode = 0;
    16891663  state = WAIT_CODE_START;
    16901664
    1691   while (0 == feof(ma_socket) && 0 == ferror(ma_socket)) {
     1665  while (sl_read_timeout_fd (ma_socket, &c, 1, time_wait, SL_FALSE) > 0) {
     1666
     1667    g = (int) c;
    16921668
    16931669    if ( (g=fgetc(ma_socket)) == EOF)
    16941670      {
    1695         (void) alarm(0);
    1696         (void) sigaction (SIGALRM, &old_act, NULL);
    1697         (void) alarm(old_alarm);
    1698         (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
    16991671        TPT((0, FIL__, __LINE__, _("msg=<mail_wait: EOF>\n")));
    17001672        SL_RETURN( 0, _("mail_wait"));
     
    17331705        break;
    17341706      /*@-charintliteral@*/
    1735       (void) alarm(0);
    1736       (void) sigaction (SIGALRM, &old_act, NULL);
    1737       (void) alarm(old_alarm);
    1738       (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
    17391707
    17401708      TPT((0, FIL__, __LINE__,
     
    17691737     
    17701738    default:
    1771       (void) alarm(0);
    1772       (void) sigaction (SIGALRM, &old_act, NULL);
    1773       (void) alarm(old_alarm);
    1774       (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
    17751739
    17761740      TPT((0, FIL__, __LINE__, _("msg=<mail_wait: bad>\n")));
     
    17791743    }
    17801744  }
    1781 
    1782   (void) alarm(0);                            /* Disable alarm       */
    1783   (void) sigaction (SIGALRM, &old_act, NULL);
    1784   (void) alarm(old_alarm);
    1785   (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
    17861745
    17871746  TPT((0, FIL__, __LINE__, _("msg=<mail_wait: failed>\n")));
Note: See TracChangeset for help on using the changeset viewer.