From afb093cd9bc9d0b2abb6a87d1b98785183a6452d Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 9 May 2012 19:16:39 +0200 Subject: * apt-pkg/contrib/fileutl.cc: - check that the fd which are closed are valid --- apt-pkg/contrib/fileutl.cc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index e9d1ba1ce..1cf225329 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1065,7 +1065,10 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C // We don't need the file open - instead let the compressor open it // as he properly knows better how to efficiently read from 'his' file if (FileName.empty() == false) + { close(iFd); + iFd = -1; + } } // Create a data pipe @@ -1133,7 +1136,7 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C close(Pipe[0]); else close(Pipe[1]); - if (Comp == true || FileName.empty() == true) + if ((Comp == true || FileName.empty() == true) && d->compressed_fd != -1) close(d->compressed_fd); return true; @@ -1355,8 +1358,9 @@ bool FileFd::Seek(unsigned long long To) if (d->bz2 != NULL) BZ2_bzclose(d->bz2); #endif - close(iFd); - iFd = 0; + if (iFd != -1) + close(iFd); + iFd = -1; if (TemporaryFileName.empty() == false) iFd = open(TemporaryFileName.c_str(), O_RDONLY); else if (FileName.empty() == false) -- cgit v1.2.3 From ba667cf74b94846c0696ae7a27e53b28492554b6 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 9 May 2012 22:05:18 +0200 Subject: ensure that we do init d only once and especially not with its own content as this causes some "interesting" hickups resulting in segfaults as it seems (Closes: #554387, #670979) --- apt-pkg/contrib/fileutl.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 1cf225329..14c0b561f 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -905,8 +905,6 @@ bool FileFd::Open(string FileName,unsigned int const Mode,CompressMode Compress, bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor, unsigned long const Perms) { Close(); - d = new FileFdPrivate; - d->openmode = Mode; Flags = AutoClose; if ((Mode & WriteOnly) != WriteOnly && (Mode & (Atomic | Create | Empty | Exclusive)) != 0) @@ -1000,8 +998,6 @@ bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, CompressMode Compre bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration::Compressor const &compressor, bool AutoClose) { Close(); - d = new FileFdPrivate; - d->openmode = Mode; Flags = (AutoClose) ? FileFd::AutoClose : 0; iFd = Fd; this->FileName = ""; @@ -1015,7 +1011,12 @@ bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration: } bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor) { - d->compressor = compressor; + if (d == NULL) + { + d = new FileFdPrivate(); + d->openmode = Mode; + d->compressor = compressor; + } if (compressor.Name == "." || compressor.Binary.empty() == true) return true; #ifdef HAVE_ZLIB -- cgit v1.2.3 From adbd7eb4db58ce38965bf6340b8b059a07fdb5bc Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 10 May 2012 00:14:50 +0200 Subject: collect zombie (de)compressor processes on reopen --- apt-pkg/contrib/fileutl.cc | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 14c0b561f..4dc087983 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1022,6 +1022,11 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C #ifdef HAVE_ZLIB else if (compressor.Name == "gzip") { + if (d->gz != NULL) + { + gzclose(d->gz); + d->gz = NULL; + } if ((Mode & ReadWrite) == ReadWrite) d->gz = gzdopen(iFd, "r+"); else if ((Mode & WriteOnly) == WriteOnly) @@ -1037,6 +1042,11 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C #ifdef HAVE_BZ2 else if (compressor.Name == "bzip2") { + if (d->bz2 != NULL) + { + BZ2_bzclose(d->bz2); + d->bz2 = NULL; + } if ((Mode & ReadWrite) == ReadWrite) d->bz2 = BZ2_bzdopen(iFd, "r+"); else if ((Mode & WriteOnly) == WriteOnly) @@ -1050,14 +1060,17 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C } #endif + // collect zombies here in case we reopen + if (d->compressor_pid > 0) + ExecWait(d->compressor_pid, "FileFdCompressor", true); if ((Mode & ReadWrite) == ReadWrite) return _error->Error("ReadWrite mode is not supported for file %s", FileName.c_str()); bool const Comp = (Mode & WriteOnly) == WriteOnly; - // Handle 'decompression' of empty files if (Comp == false) { + // Handle 'decompression' of empty files struct stat Buf; fstat(iFd, &Buf); if (Buf.st_size == 0 && S_ISFIFO(Buf.st_mode) == false) -- cgit v1.2.3 From fbb89d946da063ec6e1c4b0034cb479c10a37375 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 10 May 2012 01:54:21 +0200 Subject: ensure that in error conditions the Fail flag is set --- apt-pkg/contrib/fileutl.cc | 49 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 4dc087983..7a24b6bb0 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1065,7 +1065,10 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C ExecWait(d->compressor_pid, "FileFdCompressor", true); if ((Mode & ReadWrite) == ReadWrite) + { + Flags |= Fail; return _error->Error("ReadWrite mode is not supported for file %s", FileName.c_str()); + } bool const Comp = (Mode & WriteOnly) == WriteOnly; if (Comp == false) @@ -1088,7 +1091,10 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C // Create a data pipe int Pipe[2] = {-1,-1}; if (pipe(Pipe) != 0) + { + Flags |= Fail; return _error->Errno("pipe",_("Failed to create subprocess IPC")); + } for (int J = 0; J != 2; J++) SetCloseExec(Pipe[J],true); @@ -1367,7 +1373,10 @@ bool FileFd::Seek(unsigned long long To) return Skip(To - seekpos); if ((d->openmode & ReadOnly) != ReadOnly) + { + Flags |= Fail; return _error->Error("Reopen is only implemented for read-only files!"); + } #ifdef HAVE_BZ2 if (d->bz2 != NULL) BZ2_bzclose(d->bz2); @@ -1385,11 +1394,17 @@ bool FileFd::Seek(unsigned long long To) if (lseek(d->compressed_fd, 0, SEEK_SET) != 0) iFd = d->compressed_fd; if (iFd <= 0) + { + Flags |= Fail; return _error->Error("Reopen is not implemented for pipes opened with FileFd::OpenDescriptor()!"); + } } if (OpenInternDescriptor(d->openmode, d->compressor) == false) + { + Flags |= Fail; return _error->Error("Seek on file %s because it couldn't be reopened", FileName.c_str()); + } if (To != 0) return Skip(To); @@ -1431,7 +1446,10 @@ bool FileFd::Skip(unsigned long long Over) { unsigned long long toread = std::min((unsigned long long) sizeof(buffer), Over); if (Read(buffer, toread) == false) + { + Flags |= Fail; return _error->Error("Unable to seek ahead %llu",Over); + } Over -= toread; } return true; @@ -1499,7 +1517,10 @@ unsigned long long FileFd::Tell() #endif Res = lseek(iFd,0,SEEK_CUR); if (Res == (off_t)-1) + { + Flags |= Fail; _error->Errno("lseek","Failed to determine the current file position"); + } d->seekpos = Res; return Res; } @@ -1511,7 +1532,10 @@ unsigned long long FileFd::FileSize() { struct stat Buf; if (d->pipe == false && fstat(iFd,&Buf) != 0) + { + Flags |= Fail; return _error->Errno("fstat","Unable to determine the file size"); + } // for compressor pipes st_size is undefined and at 'best' zero if (d->pipe == true || S_ISFIFO(Buf.st_mode)) @@ -1520,7 +1544,10 @@ unsigned long long FileFd::FileSize() // in theory the Open-methods should take care of it already d->pipe = true; if (stat(FileName.c_str(), &Buf) != 0) + { + Flags |= Fail; return _error->Errno("stat","Unable to determine the file size"); + } } return Buf.st_size; @@ -1562,10 +1589,16 @@ unsigned long long FileFd::Size() * bits of the file */ // FIXME: Size for gz-files is limited by 32bit… no largefile support if (lseek(iFd, -4, SEEK_END) < 0) - return _error->Errno("lseek","Unable to seek to end of gzipped file"); + { + Flags |= Fail; + return _error->Errno("lseek","Unable to seek to end of gzipped file"); + } size = 0L; if (read(iFd, &size, 4) != 4) - return _error->Errno("read","Unable to read original size of gzipped file"); + { + Flags |= Fail; + return _error->Errno("read","Unable to read original size of gzipped file"); + } #ifdef WORDS_BIGENDIAN uint32_t tmp_size = size; @@ -1575,7 +1608,10 @@ unsigned long long FileFd::Size() #endif if (lseek(iFd, oldPos, SEEK_SET) < 0) - return _error->Errno("lseek","Unable to seek in gzipped file"); + { + Flags |= Fail; + return _error->Errno("lseek","Unable to seek in gzipped file"); + } return size; } @@ -1592,6 +1628,7 @@ time_t FileFd::ModificationTime() struct stat Buf; if (d->pipe == false && fstat(iFd,&Buf) != 0) { + Flags |= Fail; _error->Errno("fstat","Unable to determine the modification time of file %s", FileName.c_str()); return 0; } @@ -1604,6 +1641,7 @@ time_t FileFd::ModificationTime() d->pipe = true; if (stat(FileName.c_str(), &Buf) != 0) { + Flags |= Fail; _error->Errno("fstat","Unable to determine the modification time of file %s", FileName.c_str()); return 0; } @@ -1663,6 +1701,8 @@ bool FileFd::Close() d = NULL; } + if (Res == false) + Flags |= Fail; return Res; } /*}}}*/ @@ -1673,7 +1713,10 @@ bool FileFd::Sync() { #ifdef _POSIX_SYNCHRONIZED_IO if (fsync(iFd) != 0) + { + Flags |= Fail; return _error->Errno("sync",_("Problem syncing the file")); + } #endif return true; } -- cgit v1.2.3 From ff477ee133fb448622cc86f658f1a9b23577e413 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 10 May 2012 03:09:18 +0200 Subject: ensure that d is set before accessing it --- apt-pkg/contrib/fileutl.cc | 91 +++++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 41 deletions(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 7a24b6bb0..c8e685a5a 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1011,16 +1011,18 @@ bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration: } bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor) { + if (compressor.Name == "." || compressor.Binary.empty() == true) + return true; + if (d == NULL) { d = new FileFdPrivate(); d->openmode = Mode; d->compressor = compressor; } - if (compressor.Name == "." || compressor.Binary.empty() == true) - return true; + #ifdef HAVE_ZLIB - else if (compressor.Name == "gzip") + if (compressor.Name == "gzip") { if (d->gz != NULL) { @@ -1040,7 +1042,7 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C } #endif #ifdef HAVE_BZ2 - else if (compressor.Name == "bzip2") + if (compressor.Name == "bzip2") { if (d->bz2 != NULL) { @@ -1185,12 +1187,12 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) do { #ifdef HAVE_ZLIB - if (d->gz != NULL) + if (d != NULL && d->gz != NULL) Res = gzread(d->gz,To,Size); else #endif #ifdef HAVE_BZ2 - if (d->bz2 != NULL) + if (d != NULL && d->bz2 != NULL) Res = BZ2_bzread(d->bz2,To,Size); else #endif @@ -1202,7 +1204,7 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) continue; Flags |= Fail; #ifdef HAVE_ZLIB - if (d->gz != NULL) + if (d != NULL && d->gz != NULL) { int err; char const * const errmsg = gzerror(d->gz, &err); @@ -1211,7 +1213,7 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) } #endif #ifdef HAVE_BZ2 - if (d->bz2 != NULL) + if (d != NULL && d->bz2 != NULL) { int err; char const * const errmsg = BZ2_bzerror(d->bz2, &err); @@ -1224,7 +1226,8 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) To = (char *)To + Res; Size -= Res; - d->seekpos += Res; + if (d != NULL) + d->seekpos += Res; if (Actual != 0) *Actual += Res; } @@ -1252,7 +1255,7 @@ char* FileFd::ReadLine(char *To, unsigned long long const Size) { *To = '\0'; #ifdef HAVE_ZLIB - if (d->gz != NULL) + if (d != NULL && d->gz != NULL) return gzgets(d->gz, To, Size); #endif @@ -1283,12 +1286,12 @@ bool FileFd::Write(const void *From,unsigned long long Size) do { #ifdef HAVE_ZLIB - if (d->gz != NULL) + if (d != NULL && d->gz != NULL) Res = gzwrite(d->gz,From,Size); else #endif #ifdef HAVE_BZ2 - if (d->bz2 != NULL) + if (d != NULL && d->bz2 != NULL) Res = BZ2_bzwrite(d->bz2,(void*)From,Size); else #endif @@ -1299,7 +1302,7 @@ bool FileFd::Write(const void *From,unsigned long long Size) { Flags |= Fail; #ifdef HAVE_ZLIB - if (d->gz != NULL) + if (d != NULL && d->gz != NULL) { int err; char const * const errmsg = gzerror(d->gz, &err); @@ -1308,7 +1311,7 @@ bool FileFd::Write(const void *From,unsigned long long Size) } #endif #ifdef HAVE_BZ2 - if (d->bz2 != NULL) + if (d != NULL && d->bz2 != NULL) { int err; char const * const errmsg = BZ2_bzerror(d->bz2, &err); @@ -1321,7 +1324,8 @@ bool FileFd::Write(const void *From,unsigned long long Size) From = (char *)From + Res; Size -= Res; - d->seekpos += Res; + if (d != NULL) + d->seekpos += Res; } while (Res > 0 && Size > 0); @@ -1359,11 +1363,11 @@ bool FileFd::Write(int Fd, const void *From, unsigned long long Size) /* */ bool FileFd::Seek(unsigned long long To) { - if (d->pipe == true + if (d != NULL && (d->pipe == true #ifdef HAVE_BZ2 - || d->bz2 != NULL + || d->bz2 != NULL #endif - ) + )) { // Our poor man seeking in pipes is costly, so try to avoid it unsigned long long seekpos = Tell(); @@ -1414,7 +1418,7 @@ bool FileFd::Seek(unsigned long long To) } int res; #ifdef HAVE_ZLIB - if (d->gz) + if (d != NULL && d->gz) res = gzseek(d->gz,To,SEEK_SET); else #endif @@ -1425,7 +1429,8 @@ bool FileFd::Seek(unsigned long long To) return _error->Error("Unable to seek to %llu", To); } - d->seekpos = To; + if (d != NULL) + d->seekpos = To; return true; } /*}}}*/ @@ -1434,11 +1439,11 @@ bool FileFd::Seek(unsigned long long To) /* */ bool FileFd::Skip(unsigned long long Over) { - if (d->pipe == true + if (d != NULL && (d->pipe == true #ifdef HAVE_BZ2 - || d->bz2 != NULL + || d->bz2 != NULL #endif - ) + )) { d->seekpos += Over; char buffer[1024]; @@ -1457,7 +1462,7 @@ bool FileFd::Skip(unsigned long long Over) int res; #ifdef HAVE_ZLIB - if (d->gz != NULL) + if (d != NULL && d->gz != NULL) res = gzseek(d->gz,Over,SEEK_CUR); else #endif @@ -1467,7 +1472,8 @@ bool FileFd::Skip(unsigned long long Over) Flags |= Fail; return _error->Error("Unable to seek ahead %llu",Over); } - d->seekpos = res; + if (d != NULL) + d->seekpos = res; return true; } @@ -1478,7 +1484,7 @@ bool FileFd::Skip(unsigned long long Over) bool FileFd::Truncate(unsigned long long To) { #if defined HAVE_ZLIB || defined HAVE_BZ2 - if (d->gz != NULL || d->bz2 != NULL) + if (d != NULL && (d->gz != NULL || d->bz2 != NULL)) { Flags |= Fail; return _error->Error("Truncating compressed files is not implemented (%s)", FileName.c_str()); @@ -1502,16 +1508,16 @@ unsigned long long FileFd::Tell() // seeking around, but not all users of FileFd use always Seek() and co // so d->seekpos isn't always true and we can just use it as a hint if // we have nothing else, but not always as an authority… - if (d->pipe == true + if (d != NULL && (d->pipe == true #ifdef HAVE_BZ2 - || d->bz2 != NULL + || d->bz2 != NULL #endif - ) + )) return d->seekpos; off_t Res; #ifdef HAVE_ZLIB - if (d->gz != NULL) + if (d != NULL && d->gz != NULL) Res = gztell(d->gz); else #endif @@ -1521,7 +1527,8 @@ unsigned long long FileFd::Tell() Flags |= Fail; _error->Errno("lseek","Failed to determine the current file position"); } - d->seekpos = Res; + if (d != NULL) + d->seekpos = Res; return Res; } /*}}}*/ @@ -1531,18 +1538,19 @@ unsigned long long FileFd::Tell() unsigned long long FileFd::FileSize() { struct stat Buf; - if (d->pipe == false && fstat(iFd,&Buf) != 0) + if ((d == NULL || d->pipe == false) && fstat(iFd,&Buf) != 0) { Flags |= Fail; return _error->Errno("fstat","Unable to determine the file size"); } // for compressor pipes st_size is undefined and at 'best' zero - if (d->pipe == true || S_ISFIFO(Buf.st_mode)) + if ((d != NULL && d->pipe == true) || S_ISFIFO(Buf.st_mode)) { // we set it here, too, as we get the info here for free // in theory the Open-methods should take care of it already - d->pipe = true; + if (d != NULL) + d->pipe = true; if (stat(FileName.c_str(), &Buf) != 0) { Flags |= Fail; @@ -1562,11 +1570,11 @@ unsigned long long FileFd::Size() // for compressor pipes st_size is undefined and at 'best' zero, // so we 'read' the content and 'seek' back - see there - if (d->pipe == true + if (d != NULL && (d->pipe == true #ifdef HAVE_BZ2 - || (d->bz2 && size > 0) + || (d->bz2 && size > 0) #endif - ) + )) { unsigned long long const oldSeek = Tell(); char ignore[1000]; @@ -1581,7 +1589,7 @@ unsigned long long FileFd::Size() // only check gzsize if we are actually a gzip file, just checking for // "gz" is not sufficient as uncompressed files could be opened with // gzopen in "direct" mode as well - else if (d->gz && !gzdirect(d->gz) && size > 0) + else if (d != NULL && d->gz && !gzdirect(d->gz) && size > 0) { off_t const oldPos = lseek(iFd,0,SEEK_CUR); /* unfortunately zlib.h doesn't provide a gzsize(), so we have to do @@ -1626,7 +1634,7 @@ unsigned long long FileFd::Size() time_t FileFd::ModificationTime() { struct stat Buf; - if (d->pipe == false && fstat(iFd,&Buf) != 0) + if ((d == NULL || d->pipe == false) && fstat(iFd,&Buf) != 0) { Flags |= Fail; _error->Errno("fstat","Unable to determine the modification time of file %s", FileName.c_str()); @@ -1634,11 +1642,12 @@ time_t FileFd::ModificationTime() } // for compressor pipes st_size is undefined and at 'best' zero - if (d->pipe == true || S_ISFIFO(Buf.st_mode)) + if ((d != NULL && d->pipe == true) || S_ISFIFO(Buf.st_mode)) { // we set it here, too, as we get the info here for free // in theory the Open-methods should take care of it already - d->pipe = true; + if (d != NULL) + d->pipe = true; if (stat(FileName.c_str(), &Buf) != 0) { Flags |= Fail; -- cgit v1.2.3