Changeset 252 for trunk/src/slib.c
- Timestamp:
- Oct 12, 2009, 10:40:45 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/slib.c
r248 r252 1559 1559 SL_TICKET ticket; /* The unique ID. */ 1560 1560 int fd; /* The file descriptor. */ 1561 FILE * stream; /* The file descriptor. */ 1561 1562 char * path; /* The file path. */ 1562 1563 int flush; /* Whether we want to flush the cache */ … … 1571 1572 static int stale_orig_line = -1; 1572 1573 static char stale_orig_mesg[128]; 1574 1575 static char badfd_orig_file[64] = { '\0' }; 1576 static int badfd_orig_line = -1; 1577 static char badfd_orig_mesg[128]; 1573 1578 1574 1579 SH_MUTEX_STATIC(mutex_ticket, PTHREAD_MUTEX_INITIALIZER); … … 1587 1592 } 1588 1593 1594 char * 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 1589 1606 static 1590 1607 SL_TICKET sl_create_ticket (unsigned int myindex) … … 1666 1683 1667 1684 SL_TICKET sl_make_ticket (const char * ofile, int oline, 1668 int fd, const char * filename )1685 int fd, const char * filename, FILE * stream) 1669 1686 { 1670 1687 size_t len; … … 1721 1738 ofiles[fd]->fd = fd; 1722 1739 ofiles[fd]->content = NULL; 1740 ofiles[fd]->stream = stream; 1723 1741 ofiles[fd]->flush = SL_FALSE; 1724 1742 … … 1786 1804 * the mask is returned." 1787 1805 */ 1788 (void) umask (0); 1806 (void) umask (0); 1789 1807 1790 1808 if (mode == SL_OPEN_FOR_FASTREAD) … … 1927 1945 if (stat_return < 0) 1928 1946 { 1929 close (fd);1947 sl_close_fd (FIL__, __LINE__, fd); 1930 1948 errno = errval; 1931 1949 SL_IRETURN(SL_EFSTAT, _("sl_open_file")); … … 1936 1954 if (lstat_return != ENOENT && buf.st_ino != lbuf.st_ino) 1937 1955 { 1938 close (fd);1956 sl_close_fd (FIL__, __LINE__, fd); 1939 1957 SL_IRETURN(SL_EBOGUS, _("sl_open_file")); 1940 1958 } … … 1946 1964 if (fd >= MAXFD) 1947 1965 { 1948 close(fd);1966 sl_close_fd(FIL__, __LINE__, fd); 1949 1967 SL_IRETURN(SL_TOOMANY, _("sl_open_file")); 1950 1968 } … … 1953 1971 { 1954 1972 /* 1955 close(fd);1973 sl_close_fd(FIL__, __LINE__, fd); 1956 1974 SL_IRETURN(SL_EINTERNAL09, _("sl_open_file")); 1957 1975 */ … … 1968 1986 if ( (ofiles[fd] = (SL_OFILE *) malloc(sizeof(SL_OFILE))) == NULL) 1969 1987 { 1970 close(fd);1988 sl_close_fd(FIL__, __LINE__, fd); 1971 1989 SL_IRETURN(SL_EMEM, _("sl_open_file")); 1972 1990 } … … 1978 1996 free (ofiles[fd]); 1979 1997 ofiles[fd] = NULL; 1980 close(fd);1998 sl_close_fd(FIL__, __LINE__, fd); 1981 1999 SL_IRETURN(SL_EMEM, _("sl_open_file")); 1982 2000 } … … 1991 2009 (void) free (ofiles[fd]); 1992 2010 ofiles[fd] = NULL; 1993 close(fd);2011 sl_close_fd(FIL__, __LINE__, fd); 1994 2012 SL_IRETURN(ticket, _("sl_open_file")); 1995 2013 } … … 1999 2017 ofiles[fd]->fd = fd; 2000 2018 ofiles[fd]->content = NULL; 2019 ofiles[fd]->stream = NULL; 2001 2020 ofiles[fd]->flush = SL_FALSE; 2002 2021 … … 2005 2024 2006 2025 SL_IRETURN(ticket, _("sl_open_file")); 2026 } 2027 2028 FILE * 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; 2007 2043 } 2008 2044 … … 2259 2295 { 2260 2296 register int fd; 2297 FILE * fp = NULL; 2261 2298 2262 2299 SL_ENTER(_("sl_close")); … … 2276 2313 sh_string_destroy(&(ofiles[fd]->content)); 2277 2314 (void) free (ofiles[fd]->path); 2315 fp = ofiles[fd]->stream; 2278 2316 (void) free (ofiles[fd]); 2279 2317 ofiles[fd] = NULL; … … 2282 2320 /* This may fail, but what to do then ? 2283 2321 */ 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 } 2289 2339 } 2290 2340 2291 2341 SL_IRETURN(SL_ENONE, _("sl_close")); 2342 } 2343 2344 int 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 2361 int 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")); 2292 2379 } 2293 2380
Note:
See TracChangeset
for help on using the changeset viewer.