Changeset 252 for trunk


Ignore:
Timestamp:
Oct 12, 2009, 10:40:45 AM (15 years ago)
Author:
katerina
Message:

Add code to check for stale file records on close() and fclose(), fix sl_close() to handle open stream (ticket #163).

Location:
trunk
Files:
25 edited

Legend:

Unmodified
Added
Removed
  • trunk/configure.ac

    r251 r252  
    1212dnl start
    1313dnl
    14 AM_INIT_AUTOMAKE(samhain, 2.5.9c)
     14AM_INIT_AUTOMAKE(samhain, 2.5.10)
    1515AC_DEFINE([SAMHAIN], 1, [Application is samhain])
    1616AC_CANONICAL_HOST
  • trunk/docs/Changelog

    r251 r252  
     12.5.10:
     2        * sh_suidchk.c: handle $HOME/.gvfs mount gracefully
     3        * slib.c: fix race condition caused by closing a stream and the fd
     4
    152.5.9c:
    26        * move stale file record error message closer to problem zone
  • trunk/include/slib.h

    r248 r252  
    329329#define SL_OFILE_SIZE 32
    330330
     331  char * sl_check_badfd();
    331332  char * sl_check_stale();
    332333
     334  /* Create a file record for an open file
     335   */
    333336  SL_TICKET sl_make_ticket (const char * ofile, int oline,
    334                             int fd, const char * filename);
     337                            int fd, const char * filename, FILE * stream);
    335338 
     339  /* Get the pointer to a stream. If none exists yet, open it
     340   */
     341  FILE * sl_stream (SL_TICKET ticket, char * mode);
     342
    336343  /* Open for writing.
    337344   */
     
    388395   */
    389396  int sl_close (SL_TICKET ticket);
     397
     398  /* Close file descriptor.
     399   */
     400  int sl_close_fd (const char * file, int line, int fd);
     401
     402  /* Close stream.
     403   */
     404  int sl_fclose (const char * file, int line, FILE * fp);
    390405
    391406  /* Unlink file.
  • trunk/src/samhain.c

    r248 r252  
    920920    { if (errno != ENOENT) perror(_("fopen")); return 0; }
    921921  if (NULL == fgets(line, sizeof(line), fp))
    922     { perror(_("fgets")); (void) fclose(fp); return 0; }
    923   (void) fclose(fp);
     922    { perror(_("fgets")); (void) sl_fclose(FIL__, __LINE__, fp); return 0; }
     923  (void) sl_fclose(FIL__, __LINE__, fp);
    924924  p = line;
    925925  while (*p == ' '  || *p == '\f' || *p == '\n' ||
     
    10031003        exit (1);
    10041004      case  0:
    1005         if (0 != close (0))
     1005        if (0 != sl_close_fd (FIL__, __LINE__, 0))
    10061006          {
    10071007            _exit(4);
     
    21002100
    21012101      {
    2102         char * stale = sl_check_stale();
    2103 
     2102        char * stale;
     2103
     2104        stale = sl_check_stale();
     2105        if (stale)
     2106          {
     2107            sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
     2108                            stale, _("sl_check_stale"));
     2109          }
     2110
     2111        stale = sl_check_badfd();
    21042112        if (stale)
    21052113          {
  • trunk/src/sh_entropy.c

    r235 r252  
    138138    if( do_restart ) {
    139139        if( fd != -1 ) {
    140             close( fd );
     140            sl_close_fd(FIL__, __LINE__, fd );
    141141            fd = -1;
    142142        }
     
    195195                             _("sh_entropy") );
    196196            SH_FREE(name);
    197             close(fd);
     197            sl_close_fd(FIL__, __LINE__, fd);
    198198            SL_RETURN( -1, _("sh_entropy") );
    199199          }
     
    212212                             _("cannot write to EGD"),
    213213                             _("sh_entropy") );
    214             close(fd);
     214            sl_close_fd(FIL__, __LINE__, fd);
    215215            SL_RETURN( -1, _("sh_entropy") );
    216216          }
     
    254254                             _("cannot write to EGD"),
    255255                             _("sh_entropy") );
    256             close(fd);
     256            sl_close_fd(FIL__, __LINE__, fd);
    257257            SL_RETURN( -1, _("sh_entropy") );
    258258          }
     
    274274    }
    275275    memset(buffer, 0, sizeof(buffer) );
    276     close(fd);
     276    sl_close_fd(FIL__, __LINE__, fd);
    277277    SL_RETURN( 0, _("sh_entropy") ); /* success */
    278278}
     
    310310    m_count = 0;
    311311
    312   close(fd2);
     312  sl_close_fd(FIL__, __LINE__, fd2);
    313313
    314314  TPT((0, FIL__, __LINE__, _("msg=<read_mbytes: OK>\n")));
     
    559559   */
    560560  if (source->pid == (pid_t) - 1) {
    561     close(pipedes[0]);
    562     close(pipedes[1]);
     561    sl_close_fd(FIL__, __LINE__, pipedes[0]);
     562    sl_close_fd(FIL__, __LINE__, pipedes[1]);
    563563    if (envp[0] != NULL) free(envp[0]);
    564564    SL_RETURN(NULL, _("sh_popen"));
     
    580580      /* close the pipe descriptors
    581581       */
    582       close   (pipedes[STDIN_FILENO]);
    583       close   (pipedes[STDOUT_FILENO]);
     582      sl_close_fd   (FIL__, __LINE__, pipedes[STDIN_FILENO]);
     583      sl_close_fd   (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
    584584
    585585      /* don't leak file descriptors
     
    649649      free(envp[0]);
    650650
    651     close (pipedes[STDOUT_FILENO]);
     651    sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
    652652    retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFD, FD_CLOEXEC);
    653653
     
    657657      {
    658658        aud_kill (FIL__, __LINE__, source->pid, SIGKILL);
    659         close (pipedes[STDOUT_FILENO]);
     659        sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
    660660        waitpid (source->pid, NULL, 0);
    661661        source->pid = 0;
     
    676676    SL_ENTER(_("sh_pclose"));
    677677
    678     retval = fclose(source->pipe);
     678    retval = sl_fclose(FIL__, __LINE__, source->pipe);
    679679    if (retval)
    680680      {
  • trunk/src/sh_err_console.c

    r170 r252  
    378378                val_return = write(fd[cc], "\r\n",              2);
    379379              } while (val_return < 0 && errno == EINTR);
    380               (void) close(fd[cc]);
     380              (void) sl_close_fd(FIL__, __LINE__, fd[cc]);
    381381              service_failure[cc] = 0;
    382382            }
  • trunk/src/sh_extern.c

    r248 r252  
    4040static FILE * pdbgc = NULL;
    4141#define PDBG_OPEN    if (pdbg == NULL) pdbg = fopen(PDGBFILE"main",  "a") 
    42 #define PDBG_CLOSE   fclose (pdbg); pdbg = NULL
     42#define PDBG_CLOSE   sl_fclose (FIL__, __LINE__, pdbg); pdbg = NULL
    4343#define PDBG(arg)    fprintf(pdbg,  "PDBG: step %d\n", arg); fflush(pdbg)
    4444#define PDBG_D(arg)  fprintf(pdbg,  "PDBG: %d\n", arg); fflush(pdbg)
     
    4646
    4747#define PDBGC_OPEN   if (pdbgc == NULL) pdbgc = fopen(PDGBFILE"child", "a") 
    48 #define PDBGC_CLOSE  fclose (pdbgc); pdbgc = NULL
     48#define PDBGC_CLOSE  sl_fclose (FIL__, __LINE__, pdbgc); pdbgc = NULL
    4949#define PDBGC(arg)   fprintf(pdbgc, "PDBGC: step %d\n", arg); fflush(pdbgc)
    5050#define PDBGC_D(arg) fprintf(pdbgc, "PDBGC: %d\n", arg); fflush(pdbgc)
     
    238238      PDBG_S("fork() failure");
    239239      /*@-usedef@*/
    240       (void) close(pipedes[0]);
    241       (void) close(pipedes[1]);
     240      (void) sl_close_fd(FIL__, __LINE__, pipedes[0]);
     241      (void) sl_close_fd(FIL__, __LINE__, pipedes[1]);
    242242      /*@+usedef@*/
    243243      errnum = errno;
     
    297297          /* close the pipe descriptors
    298298           */
    299           (void) close   (pipedes[STDIN_FILENO]);
    300           (void) close   (pipedes[STDOUT_FILENO]);
     299          (void) sl_close_fd   (FIL__, __LINE__, pipedes[STDIN_FILENO]);
     300          (void) sl_close_fd   (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
    301301         
    302302          /* don't leak file descriptors
     
    442442                sl_close(fd);
    443443              else if (pfd != -1)
    444                 close(pfd);
     444                sl_close_fd(FIL__, __LINE__, pfd);
    445445            }
    446446#endif
     
    496496    {
    497497      PDBG_S("is w");
    498       (void) close (pipedes[STDIN_FILENO]);
     498      (void) sl_close_fd (FIL__, __LINE__, pipedes[STDIN_FILENO]);
    499499      (void) retry_fcntl (FIL__, __LINE__, pipedes[STDOUT_FILENO],
    500500                          F_SETFD, FD_CLOEXEC);
     
    504504    {
    505505      PDBG_S("is r");
    506       (void) close (pipedes[STDOUT_FILENO]);
     506      (void) sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
    507507      (void) retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO],
    508508                          F_SETFD, FD_CLOEXEC);
     
    526526
    527527      (void) aud_kill (FIL__, __LINE__, task->pid, SIGKILL);
    528       (void) close (pipedes[STDOUT_FILENO]);
    529       (void) close (pipedes[STDIN_FILENO]);
     528      (void) sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
     529      (void) sl_close_fd (FIL__, __LINE__, pipedes[STDIN_FILENO]);
    530530      (void) waitpid (task->pid, NULL, 0);
    531531      task->pid = 0;
     
    541541  PDBG_D(task->pipeFD);
    542542
    543   task->pipeTI = sl_make_ticket(FIL__, __LINE__, task->pipeFD, _("pipe"));
     543  task->pipeTI = sl_make_ticket(FIL__, __LINE__, task->pipeFD, _("pipe"), outf);
    544544
    545545  flags = (int) retry_fcntl (FIL__, __LINE__, task->pipeFD, F_GETFL, 0);
     
    572572  PDBG_S(" -> pclose");
    573573  (void) fflush(task->pipe);
    574   (void) fclose(task->pipe);
    575574  if (!SL_ISERROR(task->pipeTI))
    576575    (void) sl_close(task->pipeTI);
  • trunk/src/sh_forward.c

    r248 r252  
    17531753                }
    17541754
    1755               (void) close (sockfd);
     1755              (void) sl_close_fd (FIL__, __LINE__, sockfd);
    17561756              memset(answer, 0, TRANS_BYTES + 256);
    17571757              MUNLOCK(answer, TRANS_BYTES + 256);
     
    17631763        }
    17641764
    1765       (void) close (sockfd);
     1765      (void) sl_close_fd (FIL__, __LINE__, sockfd);
    17661766      memset(answer, 0, TRANS_BYTES + 256);
    17671767      MUNLOCK(answer, TRANS_BYTES + 256);
     
    17751775     
    17761776 
    1777   (void) close (sockfd);
     1777  (void) sl_close_fd (FIL__, __LINE__, sockfd);
    17781778  memset(answer, 0, 512);
    17791779  MUNLOCK(answer, 512);
     
    22032203  if (conn->fd != (-1))
    22042204    {
    2205       close (conn->fd);
     2205      sl_close_fd (FIL__, __LINE__, conn->fd);
    22062206      conn->fd            = -1;
    22072207    }
     
    46814681          newconn->fd    = -1;
    46824682          newconn->state = CONN_FREE;
    4683           close(rc);
     4683          sl_close_fd(FIL__, __LINE__, rc);
    46844684          SL_RETURN( (-1), _("sh_forward_accept"));
    46854685        }
     
    46984698          newconn->fd    = -1;
    46994699          newconn->state = CONN_FREE;
    4700           close(rc);
     4700          sl_close_fd(FIL__, __LINE__, rc);
    47014701          SL_RETURN( (-1), _("sh_forward_accept"));
    47024702        }
     
    57805780           */
    57815781          TPT(( 0, FIL__, __LINE__, _("msg=<close syslog socket>\n")));
    5782           close(syslog_sock);
     5782          sl_close_fd(FIL__, __LINE__, syslog_sock);
    57835783          syslog_sock = -1;
    57845784        }
     
    58305830      errnum = errno;
    58315831      sh_forward_printerr (_("syslog bind"), errnum, 514, __LINE__);
    5832       close(sock);
     5832      sl_close_fd(FIL__, __LINE__, sock);
    58335833      SL_RETURN((-1), _("create_syslog_socket"));
    58345834    }
  • trunk/src/sh_gpg.c

    r248 r252  
    8888FILE * pdbgc;
    8989#define PDBG_OPEN    pdbg = fopen(PDGBFILE"main",  "a") 
    90 #define PDBG_CLOSE   fclose (pdbg)
     90#define PDBG_CLOSE   sl_fclose (FIL__, __LINE__, pdbg)
    9191#define PDBG(arg)    fprintf(pdbg,  "PDBG: step %d\n", arg); fflush(pdbg)
    9292#define PDBG_D(arg)  fprintf(pdbg,  "PDBG: %d\n", arg); fflush(pdbg)
     
    9494
    9595#define PDBGC_OPEN   pdbgc = fopen(PDGBFILE"child", "a") 
    96 #define PDBGC_CLOSE  fclose (pdbgc)
     96#define PDBGC_CLOSE  sl_fclose (FIL__, __LINE__, pdbgc)
    9797#define PDBGC(arg)   fprintf(pdbgc, "PDBG: step %d\n", arg); fflush(pdbgc)
    9898#define PDBGC_D(arg) fprintf(pdbgc, "PDBG: %d\n", arg); fflush(pdbgc)
     
    446446  if (source->pid == (pid_t) - 1)
    447447    {
    448       close(pipedes[0]);
    449       close(pipedes[1]);
     448      sl_close_fd(FIL__, __LINE__, pipedes[0]);
     449      sl_close_fd(FIL__, __LINE__, pipedes[1]);
    450450      if (envp[0] != NULL)
    451451        free(envp[0]);
     
    468468      /* close the pipe descriptors
    469469       */
    470       close (pipedes[STDIN_FILENO]);
    471       close (pipedes[STDOUT_FILENO]);
     470      sl_close_fd (FIL__, __LINE__, pipedes[STDIN_FILENO]);
     471      sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
    472472     
    473473
     
    618618    free(envp[0]);
    619619
    620   close (pipedes[STDOUT_FILENO]);
     620  sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
    621621  retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFD, FD_CLOEXEC);
    622622  retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFL,  O_NONBLOCK);
     
    627627    {
    628628      aud_kill (FIL__, __LINE__, source->pid, SIGKILL);
    629       close (pipedes[STDOUT_FILENO]);
     629      sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
    630630      waitpid (source->pid, NULL, 0);
    631631      source->pid = 0;
     
    643643  SL_ENTER(_("sh_gpg_pclose"));
    644644
    645   status = fclose(source->pipe);
     645  status = sl_fclose(FIL__, __LINE__, source->pipe);
    646646  if (status)
    647647    SL_RETURN( (-1), _("sh_gpg_pclose"));
  • trunk/src/sh_hash.c

    r248 r252  
    786786static void sh_hash_getline_end (void)
    787787{
    788   fclose (sh_fin_fd);
     788  sl_fclose (FIL__, __LINE__, sh_fin_fd);
    789789  sh_fin_fd = NULL;
    790790  return;
     
    816816  if (sh_fin_fd != NULL)
    817817    {
    818       fclose (sh_fin_fd);
     818      sl_fclose (FIL__, __LINE__, sh_fin_fd);
    819819      sh_fin_fd = NULL;
    820820    }
     
    13071307  SH_FREE(buf);
    13081308  sl_close(fd);
    1309   fclose(fin_cp);
     1309  sl_fclose(FIL__, __LINE__, fin_cp); /* fin_cp = fdopen(dup(), "rb"); */
    13101310
    13111311  fd = fdTmp;
  • trunk/src/sh_kern.c

    r223 r252  
    559559  struct proc_dir_entry   proc_root_dir;
    560560
    561   int status = close(mpipe[0]);
     561  int status = sl_close_fd(FIL__, __LINE__, mpipe[0]);
    562562
    563563  setpgid(0, 0);
     
    710710  /* Close reading side of pipe, and wait some milliseconds
    711711   */
    712   close (mpipe[1]);
     712  sl_close_fd (FIL__, __LINE__, mpipe[1]);
    713713  retry_msleep (0, ShKernDelay); /* milliseconds */
    714714
     
    789789        status = WEXITSTATUS(status);
    790790    }
    791   close (mpipe[0]);
     791  sl_close_fd (FIL__, __LINE__, mpipe[0]);
    792792  if (res <= 0)
    793793    {
     
    989989            status = write( fd, "1", 1 );
    990990          } while (status < 0 && errno == EINTR);
    991           close ( fd );
     991          sl_close_fd (FIL__, __LINE__, fd );
    992992
    993993          if (status > 0)
     
    10011001                    status = write( fd, "0", 1 );
    10021002                  } while (status < 0 && errno == EINTR);
    1003                   close ( fd );
     1003                  sl_close_fd (FIL__, __LINE__, fd );
    10041004                }
    10051005            }
     
    13851385           */
    13861386        default:
    1387           close(kd);
     1387          sl_close_fd(FIL__, __LINE__, kd);
    13881388          status = read_from_child(mpid, mpipe, &kinfo);
    13891389          break;
  • trunk/src/sh_log_check.c

    r237 r252  
    108108          if (1 != fwrite(&save_rec, sizeof(struct logfile_record), 1, fd))
    109109            {
    110               (void) fclose(fd);
     110              (void) sl_fclose(FIL__, __LINE__, fd);
    111111              (void) remove(path);
    112112            }
    113113          else
    114114            {
    115               (void) fclose(fd);
     115              (void) sl_fclose(FIL__, __LINE__, fd);
    116116            }
    117117        }
     
    144144                }
    145145            }
    146           (void) fclose(fd);
     146          (void) sl_fclose(FIL__, __LINE__, fd);
    147147        }
    148148      SH_FREE(path);
     
    329329
    330330      if (thisfile->fp)
    331         fclose(thisfile->fp);
     331        sl_fclose(FIL__, __LINE__, thisfile->fp);
    332332      if (thisfile->filename)
    333333        SH_FREE(thisfile->filename);
     
    614614        {
    615615          fgetpos(logfile->fp, &(logfile->offset));
    616           fclose(logfile->fp);
     616          sl_fclose(FIL__, __LINE__, logfile->fp);
    617617          logfile->fp = NULL;
    618618          sh_string_destroy(&s);
     
    690690        {
    691691          fgetpos(logfile->fp, &(logfile->offset));
    692           fclose(logfile->fp);
     692          sl_fclose(FIL__, __LINE__, logfile->fp);
    693693          logfile->fp = NULL;
    694694          sh_string_destroy(&s);
     
    744744            }
    745745          fgetpos(logfile->fp, &(logfile->offset));
    746           fclose(logfile->fp);
     746          sl_fclose(FIL__, __LINE__, logfile->fp);
    747747          logfile->fp = NULL;
    748748          memset(s, '\0', size);
     
    11681168    }
    11691169
    1170   fclose(fp); remove(template);
     1170  sl_fclose(FIL__, __LINE__, fp); remove(template);
    11711171
    11721172
  • trunk/src/sh_mail.c

    r248 r252  
    734734                if (connfile != NULL)
    735735                  {
    736                     (void) fclose (connfile);
     736                    (void) sl_fclose (FIL__, __LINE__, connfile);
    737737                    connfile = NULL;
    738738                  }
     
    795795        if (connfile != NULL)
    796796          {
    797             (void) fclose (connfile);
     797            (void) sl_fclose (FIL__, __LINE__, connfile);
    798798            connfile = NULL;
    799799          }
     
    10911091    {
    10921092      TPT(( 0, FIL__, __LINE__, _("msg=<fdopen() failed>\n")));
    1093       (void) close(fd);
     1093      (void) sl_close_fd(FIL__, __LINE__, fd);
    10941094      SL_RETURN( NULL, _("sh_mail_start_conn"));
    10951095    }
     
    11051105                      _("mail"), sh.host.name);
    11061106      TPT(( 0, FIL__, __LINE__, _("msg=<Timeout>\n")));
    1107       (void) fclose(connFile);
     1107      (void) sl_fclose(FIL__, __LINE__, connFile);
    11081108      SL_RETURN( NULL, _("sh_mail_start_conn"));
    11091109    }
     
    11351135
    11361136      TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
    1137       (void) fclose(connFile);
     1137      (void) sl_fclose(FIL__, __LINE__, connFile);
    11381138      SL_RETURN( NULL, _("sh_mail_start_conn"));
    11391139    }
     
    11671167                      _("mail"), this_address);
    11681168      TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
    1169       (void) fclose(connFile);
     1169      (void) sl_fclose(FIL__, __LINE__, connFile);
    11701170      SL_RETURN( NULL, _("sh_mail_start_conn"));
    11711171    }
     
    11881188                          _("mail"), address);
    11891189          TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
    1190           (void) fclose(connFile);
     1190          (void) sl_fclose(FIL__, __LINE__, connFile);
    11911191          SL_RETURN( NULL, _("sh_mail_start_conn"));
    11921192        }
     
    12341234      if (ecount == address_num)
    12351235        {
    1236           (void) fclose(connFile);
     1236          (void) sl_fclose(FIL__, __LINE__, connFile);
    12371237          SL_RETURN( NULL, _("sh_mail_start_conn"));
    12381238        }
     
    12531253                      _("mail"), address);
    12541254      TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
    1255       (void) fclose(connFile);
     1255      (void) sl_fclose(FIL__, __LINE__, connFile);
    12561256      SL_RETURN( NULL, _("sh_mail_start_conn"));
    12571257    }
  • trunk/src/sh_mem.c

    r170 r252  
    100100      this = this->next;
    101101    }
    102   fclose(fd);
     102  sl_fclose(FIL__, __LINE__, fd);
    103103
    104104  SH_MUTEX_RECURSIVE_UNLOCK(mutex_mem);
  • trunk/src/sh_mounts.c

    r184 r252  
    749749        }
    750750
    751         fclose(tab);
     751        sl_fclose(FIL__, __LINE__, tab);
    752752}
    753753#endif
     
    806806        }
    807807
    808         (void) fclose(tab);
     808        (void) sl_fclose(FIL__, __LINE__, tab);
    809809        aix_fs_get (NULL, NULL); /* reset */
    810810}
  • trunk/src/sh_port2proc.c

    r251 r252  
    182182              if (*ptr == '\0' || *ptr == '\n')
    183183                {
    184                   fclose(fd);
     184                  sl_fclose(FIL__, __LINE__, fd);
    185185                  *procpid = (size_t) pid;
    186186                  SL_RETURN(0, _("proc_max_pid"));
    187187                }
    188188            }
    189           fclose(fd);
     189          sl_fclose(FIL__, __LINE__, fd);
    190190        }
    191191    }
     
    415415                          }
    416416#endif
    417                           fclose(fd);
     417                          sl_fclose(FIL__, __LINE__, fd);
    418418                          *pid = (unsigned long) new->pid;
    419419                          if (new->path)
     
    432432            }
    433433        }
    434       fclose(fd);
     434      sl_fclose(FIL__, __LINE__, fd);
    435435    }
    436436 err_out:
  • trunk/src/sh_portcheck.c

    r237 r252  
    312312                      SH_FREE(splits);
    313313                      sh_string_destroy(&s);
    314                       fclose(fp);
     314                      sl_fclose(FIL__, __LINE__, fp);
    315315                      return buf;
    316316                    }
     
    320320        }
    321321      sh_string_destroy(&s);
    322       fclose(fp);
     322      sl_fclose(FIL__, __LINE__, fp);
    323323    }
    324324  return NULL;
     
    360360                          SH_FREE(splits);
    361361                          sh_string_destroy(&s);
    362                           fclose(fp);
     362                          sl_fclose(FIL__, __LINE__, fp);
    363363                          return buf;
    364364                        }
     
    369369        }
    370370      sh_string_destroy(&s);
    371       fclose(fp);
     371      sl_fclose(FIL__, __LINE__, fp);
    372372    }
    373373  return NULL;
     
    874874        }
    875875    }
    876   close (fd);
     876  sl_close_fd (FIL__, __LINE__, fd);
    877877  return 0;
    878878}
     
    988988                port);
    989989     }
    990   close (fd);
     990  sl_close_fd (FIL__, __LINE__, fd);
    991991  return 0;
    992992}
     
    12261226  for (port = min_port; port <= max_port; ++port)
    12271227    {
    1228 
    12291228      if ((sock = socket(AF_INET, type, protocol)) < 0 )
    12301229        {
     
    12651264          /* we can bind the port, thus it is unused
    12661265           */
    1267           close (sock);
     1266          sl_close_fd (FIL__, __LINE__, sock);
    12681267        }
    12691268      else
     
    12901289#endif
    12911290            }
    1292           close (sock);
     1291          sl_close_fd (FIL__, __LINE__, sock);
    12931292        }
    12941293    }
  • trunk/src/sh_processcheck.c

    r230 r252  
    683683              if (*ptr == '\0' || *ptr == '\n')
    684684                {
    685                   fclose(fd);
     685                  sl_fclose(FIL__, __LINE__, fd);
    686686                  *procpid = (size_t) pid;
    687687                  SL_RETURN(0, _("proc_max_pid"));
    688688                }
    689689            }
    690           fclose(fd);
     690          sl_fclose(FIL__, __LINE__, fd);
    691691        }
    692692    }
  • trunk/src/sh_socket.c

    r200 r252  
    396396  if (sizeof(name.sun_path) < (1 + sl_strlen(sh_sockname)))
    397397    {
    398       close(pf_unix_fd); pf_unix_fd = -1;
     398      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
    399399      sh_error_handle ((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
    400400                       _("PID dir path too long"),
     
    418418      if (sh_socket_remove() < 0)
    419419        {
    420           close(pf_unix_fd); pf_unix_fd = -1;
     420          sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
    421421          sh_error_handle ((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
    422422                           _("Unlink of socket failed, maybe path not trusted"),
     
    428428  if (bind ((pf_unix_fd), (struct sockaddr *) &name, size) < 0)
    429429    {
    430       close(pf_unix_fd); pf_unix_fd = -1;
     430      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
    431431      sh_error_handle ((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
    432432                       sh_error_message (errno, errbuf, sizeof(errbuf)),
     
    439439                      &optval, sizeof(optval)))
    440440    {
    441       close(pf_unix_fd); pf_unix_fd = -1;
     441      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
    442442      sh_error_handle ((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
    443443                       sh_error_message (errno, errbuf, sizeof(errbuf)),
     
    450450  if (flags < 0)
    451451    {
    452       close(pf_unix_fd); pf_unix_fd = -1;
     452      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
    453453      sh_error_handle ((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
    454454                       sh_error_message (errno, errbuf, sizeof(errbuf)),
     
    460460  if (flags < 0)
    461461    {
    462       close(pf_unix_fd); pf_unix_fd = -1;
     462      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
    463463      sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
    464464                      sh_error_message (errno, errbuf, sizeof(errbuf)),
     
    469469  if (0 != listen(pf_unix_fd, 5))
    470470    {
    471       close(pf_unix_fd); pf_unix_fd = -1;
     471      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
    472472      sh_error_handle ((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
    473473                       sh_error_message (errno, errbuf, sizeof(errbuf)),
     
    630630                        sh_error_message (errno, errbuf, sizeof(errbuf)),
    631631                        _("sh_socket_read: setsockopt"));
    632         close(talkfd);
     632        sl_close_fd(FIL__, __LINE__, talkfd);
    633633        return -1;
    634634      }
     
    643643                        sh_error_message (errno, errbuf, sizeof(errbuf)),
    644644                        _("sh_socket_read: recvmsg"));
    645         close(talkfd); 
     645        sl_close_fd(FIL__, __LINE__, talkfd);   
    646646        return -1;
    647647      }
     
    666666        {
    667667          /* no data */
    668           close(talkfd);
     668          sl_close_fd(FIL__, __LINE__, talkfd);
    669669          return 0;
    670670        }
     
    672672                      sh_error_message (errno, errbuf, sizeof(errbuf)),
    673673                      _("sh_socket_read: recvfrom"));
    674       close(talkfd);
     674      sl_close_fd(FIL__, __LINE__, talkfd);
    675675      return -1;
    676676    }
     
    682682                      sh_error_message (errno, errbuf, sizeof(errbuf)),
    683683                      _("sh_socket_read: getpeereid"));
    684       close(talkfd);
     684      sl_close_fd(FIL__, __LINE__, talkfd);
    685685      return -1;
    686686    }
     
    693693                      sh_error_message (errno, errbuf, sizeof(errbuf)),
    694694                      _("sh_socket_read: getsockopt"));
    695       close(talkfd);
     695      sl_close_fd(FIL__, __LINE__, talkfd);
    696696      return -1;
    697697    }
     
    744744              fdmax = data_size / sizeof(int);
    745745              for (fdcount = 0; fdcount < fdmax; ++fdcount)
    746                 (void) close(fdsbuf[fdcount]);
     746                (void) sl_close_fd(FIL__, __LINE__, fdsbuf[fdcount]);
    747747            }
    748748        }
    749749     
    750       close(talkfd);
     750      sl_close_fd(FIL__, __LINE__, talkfd);
    751751      return -1;
    752752    }
     
    775775                      _("sh_socket_read"));
    776776      sh_set_sockpass();
    777       close(talkfd);
     777      sl_close_fd(FIL__, __LINE__, talkfd);
    778778      return -1;
    779779    }
     
    782782                  _("Socket credentials not supported on this OS"),
    783783                  _("sh_socket_read"));
    784   close(talkfd);
     784  sl_close_fd(FIL__, __LINE__, talkfd);
    785785  return -1;
    786786#endif
     
    796796                      _("client does not have required uid"),
    797797                      _("sh_socket_read: getsockopt"));
    798       close(talkfd);
     798      sl_close_fd(FIL__, __LINE__, talkfd);
    799799      return -1;
    800800    }
     
    816816                          _("Bad message format: command too long"),
    817817                          _("sh_socket_read"));
    818           close(talkfd);
     818          sl_close_fd(FIL__, __LINE__, talkfd);
    819819          return -1;
    820820        }
     
    824824                          _("Bad message format: hostname too long"),
    825825                          _("sh_socket_read"));
    826           close(talkfd);
     826          sl_close_fd(FIL__, __LINE__, talkfd);
    827827          return -1;
    828828        }
     
    848848                      _("Bad message format"),
    849849                      _("sh_socket_read"));
    850       close(talkfd);
     850      sl_close_fd(FIL__, __LINE__, talkfd);
    851851      return -1;
    852852    }
     
    865865  */
    866866  nbytes = send (talkfd, cmd, strlen(cmd) + 1, 0);
    867   close(talkfd);
     867  sl_close_fd(FIL__, __LINE__, talkfd);
    868868  if (nbytes < 0)
    869869    {
     
    904904                              sh_error_message (errno, errbuf, sizeof(errbuf)),
    905905                              _("sh_socket_read: sendto"));
    906               close(talkfd);
     906              sl_close_fd(FIL__, __LINE__, talkfd);
    907907              return -1;
    908908            }
     
    935935                          sh_error_message (errno, errbuf, sizeof(errbuf)),
    936936                          _("sh_socket_read: sendto"));
    937           close(talkfd);
     937          sl_close_fd(FIL__, __LINE__, talkfd);
    938938          return -1;
    939939        }
     
    946946  */
    947947  nbytes = send (talkfd, _("END"), 4, 0);
    948   close(talkfd);
     948  sl_close_fd(FIL__, __LINE__, talkfd);
    949949  return 0;
    950950}
  • trunk/src/sh_static.c

    r170 r252  
    5454#include "sh_pthread.h"
    5555
     56extern  int sl_close_fd (const char * file, int line, int fd);
     57extern  int sl_fclose (const char * file, int line, FILE * fp);
     58
     59
    5660#ifndef _PATH_PASSWD
    5761#define _PATH_PASSWD "/etc/passwd"
     
    6064#define _PATH_GROUP "/etc/group"
    6165#endif
     66
     67#undef  FIL__
     68#define FIL__  _("sh_static.c")
    6269
    6370extern  int sl_strlcpy(char * dst, /*@null@*/const char * src, size_t siz);
     
    123130      }
    124131    } while (1);
    125     fclose(stream);
     132    sl_fclose(FIL__, __LINE__, stream);
    126133  }
    127134 
     
    171178      }
    172179    } while (1);
    173     fclose(stream);
     180    sl_fclose(FIL__, __LINE__, stream);
    174181  }
    175182 
     
    219226      }
    220227    } while (1);
    221     fclose(stream);
     228    sl_fclose(FIL__, __LINE__, stream);
    222229  }
    223230 
     
    267274      }
    268275    } while (1);
    269     fclose(stream);
     276    sl_fclose(FIL__, __LINE__, stream);
    270277  }
    271278 
     
    341348        SH_MUTEX_LOCK(pwf_lock);
    342349        if (pwf) {
    343                 fclose(pwf);
     350                sl_fclose(FIL__, __LINE__, pwf);
    344351                pwf = NULL;
    345352        }
     
    395402        SH_MUTEX_LOCK(grf_lock);
    396403        if (grf) {
    397                 fclose(grf);
     404                sl_fclose(FIL__, __LINE__, grf);
    398405                grf = NULL;
    399406        }
     
    498505                rv = setgroups(num_groups, group_list);
    499506        DO_CLOSE:
    500                 fclose(grf);
     507                sl_fclose(FIL__, __LINE__, grf);
    501508        }
    502509
     
    863870#include <arpa/inet.h>
    864871
    865 /* close()
     872/* sl_close_fd(FIL__, __LINE__, )
    866873 */
    867874#include <unistd.h>
     
    11851192        while (retries++ < MAX_RETRIES) {
    11861193                if (fd != -1)
    1187                         close(fd);
     1194                        sl_close_fd(FIL__, __LINE__, fd);
    11881195
    11891196                memset(packet, 0, PACKETSZ);
     
    13441351                DPRINTF("Answer type = |%d|\n", a->atype);
    13451352
    1346                 close(fd);
     1353                sl_close_fd(FIL__, __LINE__, fd);
    13471354
    13481355                if (outpacket)
     
    13901397fail:
    13911398        if (fd != -1)
    1392             close(fd);
     1399            sl_close_fd(FIL__, __LINE__, fd);
    13931400        if (lookup)
    13941401            free(lookup);
     
    15461553       
    15471554                if (action!=GETHOSTENT) {
    1548                         fclose(fp);
     1555                        sl_fclose(FIL__, __LINE__, fp);
    15491556                }
    15501557                return ret;
    15511558        }
    15521559        if (action!=GETHOSTENT) {
    1553                 fclose(fp);
     1560                sl_fclose(FIL__, __LINE__, fp);
    15541561        }
    15551562        return ret;
     
    16191626                        }
    16201627                }
    1621                 fclose(fp);
     1628                sl_fclose(FIL__, __LINE__, fp);
    16221629        } else {
    16231630            DPRINTF("failed to open %s\n", "resolv.conf");
  • trunk/src/sh_suidchk.c

    r249 r252  
    294294                           MSG_SUID_ERROR,
    295295                           sh_error_message(errno, errbuf, sizeof(errbuf)));
    296           (void) close(fd);
     296          (void) sl_close_fd(FIL__, __LINE__, fd);
    297297          return -1;
    298298        }
     
    303303                           MSG_SUID_ERROR,
    304304                           _("Possible race: lstat != fstat"));
    305           (void) close(fd);
     305          (void) sl_close_fd(FIL__, __LINE__, fd);
    306306          return -1;
    307307        }
     
    311311                           MSG_SUID_ERROR,
    312312                           _("Possible race: not a regular file"));
    313           (void) close(fd);
     313          (void) sl_close_fd(FIL__, __LINE__, fd);
    314314          return -1;
    315315        }
     
    319319                           MSG_SUID_ERROR,
    320320                           _("Possible race: not a suid/sgid file"));
    321           (void) close(fd);
     321          (void) sl_close_fd(FIL__, __LINE__, fd);
    322322          return -1;
    323323        }
     
    334334                               MSG_SUID_ERROR,
    335335                               sh_error_message(errno, errbuf, sizeof(errbuf)));
    336               (void) close(fd);
     336              (void) sl_close_fd(FIL__, __LINE__, fd);
    337337              return -1;
    338338            }
     
    341341                           MSG_SUID_ERROR,
    342342                           _("The fchmod() function is not available"));
    343           (void) close(fd);
     343          (void) sl_close_fd(FIL__, __LINE__, fd);
    344344          return -1;
    345345#endif
     
    349349                               MSG_SUID_ERROR,
    350350                               _("Not truncated because hardlink count gt 1"));
    351               (void) close(fd);
     351              (void) sl_close_fd(FIL__, __LINE__, fd);
    352352              return -1;
    353353            }
     
    359359                               MSG_SUID_ERROR,
    360360                               sh_error_message(errno, errbuf, sizeof(errbuf)));
    361               (void) close(fd);
     361              (void) sl_close_fd(FIL__, __LINE__, fd);
    362362              return -1;
    363363            }
     
    370370                               MSG_SUID_ERROR,
    371371                               sh_error_message(errno, errbuf, sizeof(errbuf)));
    372               (void) close(fd);
     372              (void) sl_close_fd(FIL__, __LINE__, fd);
    373373              return -1;
    374374            }
    375375        }
    376       (void) close (fd);
     376      (void) sl_close_fd (FIL__, __LINE__, fd);
    377377      return (0);
    378378    }
     
    568568            }
    569569
    570           (void) close (readFile);
     570          (void) sl_close_fd (FIL__, __LINE__, readFile);
    571571          (void) fchmod(writeFile, S_IRUSR | S_IWUSR | S_IXUSR);
    572           (void) close (writeFile);
     572          (void) sl_close_fd (FIL__, __LINE__, writeFile);
    573573
    574574          if (do_truncate (fullpath) == -1)
     
    613613                          theFile->c_group, (int) theFile->group,
    614614                          timestrc, timestra, timestrm);
    615                   (void) fclose (filePtr);
     615                  (void) sl_fclose (FIL__, __LINE__, filePtr);
    616616                }
    617617              /*@+usedef@*/
     
    788788    {
    789789      do {
    790         status = close (file_d);
     790        status = sl_close_fd (FIL__, __LINE__, file_d);
    791791      } while (status == -1 && errno == EINTR);
    792792     
     
    985985    if (status != 0)
    986986      {
     987        int elevel = SH_ERR_ERR;
     988        size_t tlen;
     989
    987990        status = errno;
    988991        tmp = sh_util_safe_name(tmpcat);
    989         sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, status, MSG_ERR_LSTAT,
     992        tlen = strlen(tmp);
     993        if (tlen >= 6 && 0 == strcmp(&tmp[tlen-6], _("/.gvfs")))
     994          elevel = SH_ERR_NOTICE;
     995        sh_error_handle (elevel, FIL__, __LINE__, status, MSG_ERR_LSTAT,
    990996                         sh_error_message(status, errbuf, sizeof(errbuf)),
    991                          tmpcat );
     997                         tmp );
    992998        SH_FREE(tmp);
    993999      }
     
    20342040          if (stat (mnt->mnt_dir, &disk_stats) == -1)
    20352041            {
    2036               sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
    2037                                _("stat() failed"),
     2042              char errmsg[256];
     2043              int  elevel = SH_ERR_ERR;
     2044              size_t tlen = strlen(mnt->mnt_dir);
     2045              if (tlen >= 6 && 0 == strcmp(&((mnt->mnt_dir)[tlen-6]), _("/.gvfs")))
     2046                elevel = SH_ERR_NOTICE;
     2047              sl_snprintf(errmsg, sizeof(errmsg), _("stat(%s) failed"),
     2048                          mnt->mnt_dir);
     2049              sh_error_handle (elevel, FIL__, __LINE__, 0, MSG_E_SUBGEN,
     2050                               errmsg,
    20382051                               _("filesystem_type_uncached") );
    20392052              return NULL;
  • trunk/src/sh_tools.c

    r248 r252  
    149149          if (errno == EACCES || errno == EADDRINUSE)
    150150            retval = 1;
    151           close (sd);
     151          sl_close_fd (FIL__, __LINE__, sd);
    152152          return retval;
    153153        }
     
    155155      /* bind() succeeds, iface exists
    156156       */
    157       close(sd);
     157      sl_close_fd(FIL__, __LINE__, sd);
    158158      return 1;
    159159    }
     
    702702        sl_strlcat(errmsg, _(", address "), errsiz);
    703703        sl_strlcat(errmsg, address, errsiz);
    704         close(fd);
     704        sl_close_fd(FIL__, __LINE__, fd);
    705705        fail = (-1);
    706706      }
  • trunk/src/sh_unix.c

    r251 r252  
    12931293        fd++;
    12941294      else
    1295         close(fd++);
     1295        sl_close_fd(FIL__, __LINE__, fd++);
    12961296    }
    12971297
     
    19711971      /* Close first tree file descriptors
    19721972       */
    1973       close (0);  /* if running as daemon */
    1974       close (1);  /* if running as daemon */
    1975       close (2);  /* if running as daemon */
     1973      sl_close_fd (FIL__, __LINE__, 0);  /* if running as daemon */
     1974      sl_close_fd (FIL__, __LINE__, 1);  /* if running as daemon */
     1975      sl_close_fd (FIL__, __LINE__, 2);  /* if running as daemon */
    19761976
    19771977      /* Enable full error logging
     
    23062306                                   _("time"), sh.srvtime.name);
    23072307                }
    2308               close(fd);
     2308              sl_close_fd(FIL__, __LINE__, fd);
    23092309            }
    23102310          else
     
    29102910 
    29112911  r = ioctl (fd, EXT2_IOC_GETFLAGS, &f);
    2912   /* close (fd); */
     2912  /* sl_close_fd (FIL__, __LINE__, fd); */
    29132913
    29142914  if (r == -1)
     
    34393439  int           fstat_return;
    34403440  int           fstat_errno = 0;
     3441  int           try         = 0;
    34413442
    34423443  sh_string   * content = NULL;
     
    34643465    stat_errno = errno;
    34653466
     3467  theFile->link_path = NULL;
     3468
     3469 try_again:
     3470
    34663471  fd           = -1;
    34673472  fstat_return = -1;
    34683473  rval_open    = -1;
    34693474
    3470   theFile->link_path = NULL;
    3471 
    34723475  if (stat_return == 0 && S_ISREG(buf.st_mode))
    34733476    {
     
    34753478                                    path /* theFile->fullpath */, SL_YESPRIV);
    34763479      if (SL_ISERROR(rval_open))
    3477         err_open = errno;
    3478 
    3479       {
    3480         char * stale = sl_check_stale();
    3481 
    3482         if (stale)
    3483           {
    3484             sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, err_open, MSG_E_SUBGEN,
    3485                             stale, _("sh_unix_getinfo_open"));
    3486           }
    3487       }
     3480        {
     3481          char * stale = sl_check_stale();
     3482         
     3483          if (stale)
     3484            {
     3485              sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, err_open, MSG_E_SUBGEN,
     3486                              stale, _("sh_unix_getinfo_open"));
     3487            }
     3488
     3489          if (errno == EBADF && try == 0) /* obsolete, but we keep this, just in case */
     3490            {
     3491              ++try;
     3492              goto try_again;
     3493            }
     3494          err_open = errno;
     3495        }
    34883496
    34893497      alert_timeout = 120; /* this is per 8K block now ! */
     
    35293537                              MSG_E_SUBGEN,
    35303538                              stale, _("sh_unix_getinfo_fstat"));
     3539            }
     3540
     3541          if (try == 0) /* obsolete, but we keep this, just in case */
     3542            {
     3543              ++try;
     3544              sl_close(rval_open);
     3545              goto try_again;
    35313546            }
    35323547        }
  • trunk/src/sh_utmp.c

    r170 r252  
    283283  if (sh_utmpfile != NULL)
    284284    {
    285       (void) fclose (sh_utmpfile);
     285      (void) sl_fclose (FIL__, __LINE__, sh_utmpfile);
    286286      sh_utmpfile = NULL;
    287287    }
     
    341341  SL_ENTER(_("sh_utmp_endutent"));
    342342  if (NULL != sh_utmpfile)
    343     (void) fclose(sh_utmpfile);
     343    (void) sl_fclose(FIL__, __LINE__, sh_utmpfile);
    344344  sh_utmpfile = NULL;
    345345  SL_RET0(_("sh_utmp_endutent"));
  • trunk/src/slib.c

    r248 r252  
    15591559  SL_TICKET ticket;          /* The unique  ID.      */
    15601560  int fd;                    /* The file descriptor. */
     1561  FILE * stream;             /* The file descriptor. */
    15611562  char * path;               /* The file path.       */
    15621563  int flush;                 /* Whether we want to flush the cache */
     
    15711572static int  stale_orig_line = -1;
    15721573static char stale_orig_mesg[128];
     1574
     1575static char badfd_orig_file[64] = { '\0' };
     1576static int  badfd_orig_line = -1;
     1577static char badfd_orig_mesg[128];
    15731578
    15741579SH_MUTEX_STATIC(mutex_ticket, PTHREAD_MUTEX_INITIALIZER);
     
    15871592}
    15881593
     1594char * sl_check_badfd()
     1595{
     1596  if (badfd_orig_line == -1)
     1597    return NULL;
     1598  sl_snprintf(badfd_orig_mesg, sizeof(badfd_orig_mesg),
     1599              _("close on file descriptor with allocated handle, %s, %d"),
     1600              badfd_orig_file, badfd_orig_line);
     1601  badfd_orig_file[0] = '\0';
     1602  badfd_orig_line    = -1;
     1603  return badfd_orig_mesg;
     1604}
     1605
    15891606static
    15901607SL_TICKET sl_create_ticket (unsigned int myindex)
     
    16661683
    16671684SL_TICKET sl_make_ticket (const char * ofile, int oline,
    1668                           int fd, const char * filename)
     1685                          int fd, const char * filename, FILE * stream)
    16691686{
    16701687  size_t    len;
     
    17211738  ofiles[fd]->fd      = fd;
    17221739  ofiles[fd]->content = NULL;
     1740  ofiles[fd]->stream  = stream;
    17231741  ofiles[fd]->flush   = SL_FALSE;
    17241742
     
    17861804   * the mask is returned."
    17871805   */
    1788   (void) umask (0); 
     1806  (void) umask (0);
    17891807
    17901808  if (mode == SL_OPEN_FOR_FASTREAD)
     
    19271945  if (stat_return < 0)
    19281946    {
    1929       close (fd);
     1947      sl_close_fd (FIL__, __LINE__, fd);
    19301948      errno = errval;
    19311949      SL_IRETURN(SL_EFSTAT, _("sl_open_file"));
     
    19361954  if (lstat_return != ENOENT && buf.st_ino != lbuf.st_ino)
    19371955    {
    1938       close (fd);
     1956      sl_close_fd (FIL__, __LINE__, fd);
    19391957      SL_IRETURN(SL_EBOGUS, _("sl_open_file"));
    19401958    }
     
    19461964  if (fd >= MAXFD)
    19471965     {
    1948         close(fd);
     1966        sl_close_fd(FIL__, __LINE__, fd);
    19491967        SL_IRETURN(SL_TOOMANY, _("sl_open_file"));
    19501968     }
     
    19531971    {
    19541972      /*
    1955       close(fd);
     1973      sl_close_fd(FIL__, __LINE__, fd);
    19561974      SL_IRETURN(SL_EINTERNAL09, _("sl_open_file"));
    19571975      */
     
    19681986  if ( (ofiles[fd] = (SL_OFILE *) malloc(sizeof(SL_OFILE))) == NULL)
    19691987    {
    1970       close(fd);
     1988      sl_close_fd(FIL__, __LINE__, fd);
    19711989      SL_IRETURN(SL_EMEM, _("sl_open_file"));
    19721990    }
     
    19781996      free (ofiles[fd]);
    19791997      ofiles[fd] = NULL;
    1980       close(fd);
     1998      sl_close_fd(FIL__, __LINE__, fd);
    19811999      SL_IRETURN(SL_EMEM, _("sl_open_file"));
    19822000    }
     
    19912009      (void) free (ofiles[fd]);
    19922010      ofiles[fd] = NULL;
    1993       close(fd);
     2011      sl_close_fd(FIL__, __LINE__, fd);
    19942012      SL_IRETURN(ticket, _("sl_open_file"));
    19952013    }
     
    19992017  ofiles[fd]->fd      = fd;
    20002018  ofiles[fd]->content = NULL;
     2019  ofiles[fd]->stream  = NULL;
    20012020  ofiles[fd]->flush   = SL_FALSE;
    20022021
     
    20052024
    20062025  SL_IRETURN(ticket, _("sl_open_file"));
     2026}
     2027
     2028FILE * sl_stream (SL_TICKET ticket, char * mode)
     2029{
     2030  int    fd;
     2031
     2032  if (SL_ISERROR(fd = sl_read_ticket(ticket)))
     2033    return (NULL);
     2034
     2035  if (ofiles[fd] == NULL || fd != ofiles[fd]->fd ||
     2036      ticket != ofiles[fd]->ticket || fd < 0)
     2037    return (NULL);
     2038
     2039  if (!ofiles[fd]->stream)
     2040    ofiles[fd]->stream = fdopen(fd, mode);
     2041
     2042  return ofiles[fd]->stream;
    20072043}
    20082044
     
    22592295{
    22602296  register int fd;
     2297  FILE * fp = NULL;
    22612298
    22622299  SL_ENTER(_("sl_close"));
     
    22762313        sh_string_destroy(&(ofiles[fd]->content));
    22772314      (void) free (ofiles[fd]->path);
     2315      fp = ofiles[fd]->stream;
    22782316      (void) free (ofiles[fd]);
    22792317      ofiles[fd] = NULL;
     
    22822320  /* This may fail, but what to do then ?
    22832321   */
    2284   if (0 != close(fd))
    2285     {
    2286       TPT((0, FIL__, __LINE__,
    2287            _("msg=<Error closing file.>, fd=<%d>, err=<%s>\n"),
    2288            fd, strerror(errno)));
     2322  if (fp)
     2323    {
     2324      if (0 != fclose (fp)) /* within sl_close */
     2325        {
     2326          TPT((0, FIL__, __LINE__,
     2327               _("msg=<Error fclosing file.>, fd=<%d>, err=<%s>\n"),
     2328               fd, strerror(errno)));
     2329        }
     2330    }
     2331  else
     2332    {
     2333      if (0 != close(fd)) /* within sl_close */
     2334        {
     2335          TPT((0, FIL__, __LINE__,
     2336               _("msg=<Error closing file.>, fd=<%d>, err=<%s>\n"),
     2337               fd, strerror(errno)));
     2338        }
    22892339    }
    22902340
    22912341  SL_IRETURN(SL_ENONE, _("sl_close"));
     2342}
     2343
     2344int sl_close_fd (const char * file, int line, int fd)
     2345{
     2346  int ret = -1;
     2347
     2348  SL_ENTER(_("sl_close_fd"));
     2349
     2350  if (fd >= 0 && fd < MAXFD && ofiles[fd] != NULL) /* stale ofiles[fd] handle */
     2351    {
     2352      sl_strlcpy(badfd_orig_file, file, sizeof(badfd_orig_file));
     2353      badfd_orig_line = line;
     2354    }
     2355
     2356  ret = close(fd); /* within sl_close_fd wrapper */
     2357
     2358  SL_IRETURN(ret, _("sl_close_fd"));
     2359}
     2360
     2361int sl_fclose (const char * file, int line, FILE * fp)
     2362{
     2363  int ret = -1;
     2364  int fd;
     2365
     2366  SL_ENTER(_("sl_fclose"));
     2367
     2368  fd = fileno(fp);
     2369
     2370  if (fd >= 0 && fd < MAXFD && ofiles[fd] != NULL) /* stale ofiles[fd] handle */
     2371    {
     2372      sl_strlcpy(badfd_orig_file, file, sizeof(badfd_orig_file));
     2373      badfd_orig_line = line;
     2374    }
     2375
     2376  ret = fclose(fp); /* within sl_fclose wrapper */
     2377
     2378  SL_IRETURN(ret, _("sl_fclose"));
    22922379}
    22932380
Note: See TracChangeset for help on using the changeset viewer.