This fixes two sets of issues related to the use of transient files in
the backend:
1) OpenTransientFile() has been used in some code paths with read-write
flags while read-only is sufficient, so switch those calls to be
read-only where necessary. These have been reported by Joe Conway.
2) When opening transient files, it is up to the caller to close the
file descriptors opened. In error code paths, CloseTransientFile() gets
called to clean up things before issuing an error. However in normal
exit paths, a lot of callers of CloseTransientFile() never actually
reported errors, which could leave a file descriptor open without
knowing about it. This is an issue I complained about a couple of
times, but never had the courage to write and submit a patch, so here we
go.
Note that one frontend code path is impacted by this commit so as an
error is issued when fetching control file data, making backend and
frontend to be treated consistently.
Reported-by: Joe Conway, Michael Paquier
Author: Michael Paquier
Reviewed-by: Álvaro Herrera, Georgios Kokolatos, Joe Conway
Discussion: https://postgr.es/m/
20190301023338.GD1348@paquier.xyz
Discussion: https://postgr.es/m/
c49b69ec-e2f7-ff33-4f17-
0eaa4f2cef27@joeconway.com
return NULL;
}
- CloseTransientFile(fd);
+ if (CloseTransientFile(fd))
+ ereport(LOG,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m", PGSS_TEXT_FILE)));
*buffer_size = stat.st_size;
return buf;
errmsg("could not fsync file \"%s\": %m", path)));
pgstat_report_wait_end();
- CloseTransientFile(fd);
+ if (CloseTransientFile(fd))
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m", path)));
}
/* ---
(errcode_for_file_access(),
errmsg("could not fsync file \"%s\": %m", path)));
pgstat_report_wait_end();
- CloseTransientFile(fd);
+
+ if (CloseTransientFile(fd))
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m", path)));
}
}
FreeDir(mappings_dir);
SlruFileName(ctl, path, segno);
- fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
+ fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
if (fd < 0)
{
/* expected: file doesn't exist */
result = endpos >= (off_t) (offset + BLCKSZ);
- CloseTransientFile(fd);
+ if (CloseTransientFile(fd))
+ {
+ slru_errcause = SLRU_CLOSE_FAILED;
+ slru_errno = errno;
+ return false;
+ }
+
return result;
}
* SlruPhysicalWritePage). Hence, if we are InRecovery, allow the case
* where the file doesn't exist, and return zeroes instead.
*/
- fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
+ fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
if (fd < 0)
{
if (errno != ENOENT || !InRecovery)
}
pgstat_report_wait_end();
}
- CloseTransientFile(srcfd);
+
+ if (CloseTransientFile(srcfd))
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m", path)));
}
/*
(errcode_for_file_access(),
errmsg("could not close file \"%s\": %m", tmppath)));
-
/*
* Now move the completed history file into place with its final name.
*/
(errcode_for_file_access(),
errmsg("could not close file \"%s\": %m", tmppath)));
-
/*
* Now move the completed history file into place with its final name.
*/
}
pgstat_report_wait_end();
- CloseTransientFile(fd);
+
+ if (CloseTransientFile(fd))
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m", path)));
hdr = (TwoPhaseFileHeader *) buf;
if (hdr->magic != TWOPHASE_MAGIC)
(errcode_for_file_access(),
errmsg("could not close file \"%s\": %m", tmppath)));
- CloseTransientFile(srcfd);
+ if (CloseTransientFile(srcfd))
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m", path)));
/*
* Now move the segment into place with its final name.
fnamebuf)));
inv_close(lobj);
- CloseTransientFile(fd);
+
+ if (CloseTransientFile(fd))
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m",
+ fnamebuf)));
return oid;
}
fnamebuf)));
}
- CloseTransientFile(fd);
+ if (CloseTransientFile(fd))
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m",
+ fnamebuf)));
+
inv_close(lobj);
PG_RETURN_INT32(1);
tmppath)));
}
- CloseTransientFile(tmpfd);
+ if (CloseTransientFile(tmpfd))
+ ereport(PANIC,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m",
+ tmppath)));
/* fsync, rename to permanent file, fsync file and directory */
durable_rename(tmppath, path, PANIC);
errmsg("replication slot checkpoint has wrong checksum %u, expected %u",
crc, file_crc)));
- CloseTransientFile(fd);
+ if (CloseTransientFile(fd))
+ ereport(PANIC,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m",
+ path)));
}
void
}
}
- CloseTransientFile(fd);
+ if (CloseTransientFile(fd))
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m", path)));
}
errmsg("could not fsync file \"%s\": %m", tmppath)));
}
pgstat_report_wait_end();
- CloseTransientFile(fd);
+
+ if (CloseTransientFile(fd))
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m", tmppath)));
fsync_fname("pg_logical/snapshots", true);
}
COMP_CRC32C(checksum, ondisk.builder.committed.xip, sz);
- CloseTransientFile(fd);
+ if (CloseTransientFile(fd))
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m", path)));
FIN_CRC32C(checksum);
}
pgstat_report_wait_end();
- CloseTransientFile(fd);
+ if (CloseTransientFile(fd))
+ ereport(elevel,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m",
+ tmppath)));
/* rename to permanent file, fsync file and directory */
if (rename(tmppath, path) != 0)
elog(DEBUG1, "restoring replication slot from \"%s\"", path);
- fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
+ fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
/*
* We do not need to handle this as we are rename()ing the directory into
path, readBytes, (Size) cp.length)));
}
- CloseTransientFile(fd);
+ if (CloseTransientFile(fd))
+ ereport(PANIC,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m", path)));
/* now verify the CRC */
INIT_CRC32C(checksum);
pq_sendbytes(&buf, rbuf.data, nread);
bytesleft -= nread;
}
- CloseTransientFile(fd);
+
+ if (CloseTransientFile(fd))
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m", path)));
pq_endmessage(&buf);
}
(errcode_for_file_access(),
errmsg("could not close file \"%s\": %m", tofile)));
- CloseTransientFile(srcfd);
+ if (CloseTransientFile(srcfd))
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m", fromfile)));
pfree(buffer);
}
errmsg("could not fsync file \"%s\": %m", newfile)));
return -1;
}
- CloseTransientFile(fd);
+
+ if (CloseTransientFile(fd))
+ {
+ ereport(elevel,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m", newfile)));
+ return -1;
+ }
}
/* Time to do the real deal... */
*/
pg_flush_data(fd, 0, 0);
- (void) CloseTransientFile(fd);
+ if (CloseTransientFile(fd))
+ ereport(elevel,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m", fname)));
}
#endif /* PG_FLUSH_DATA_WORKS */
return -1;
}
- (void) CloseTransientFile(fd);
+ if (CloseTransientFile(fd))
+ {
+ ereport(elevel,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m", fname)));
+ return -1;
+ }
return 0;
}
}
*mapped_address = address;
*mapped_size = request_size;
- CloseTransientFile(fd);
+
+ if (CloseTransientFile(fd))
+ {
+ ereport(elevel,
+ (errcode_for_file_access(),
+ errmsg("could not close shared memory segment \"%s\": %m",
+ name)));
+ return false;
+ }
return true;
}
}
pgstat_report_wait_end();
- CloseTransientFile(fd);
+ if (CloseTransientFile(fd))
+ ereport(FATAL,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m",
+ mapfilename)));
/* check for correct magic number, etc */
if (map->magic != RELMAPPER_FILEMAGIC ||
}
#ifndef FRONTEND
- CloseTransientFile(fd);
+ if (CloseTransientFile(fd))
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not close file \"%s\": %m",
+ ControlFilePath)));
#else
- close(fd);
+ if (close(fd))
+ {
+ fprintf(stderr, _("%s: could not close file \"%s\": %s\n"),
+ progname, ControlFilePath, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
#endif
/* Check the CRC. */