util: add quiet parameter to virPidFileAcquirePathFull

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Ján Tomko 2023-03-07 15:43:40 +01:00
parent d3d16f829b
commit 4479a307df
3 changed files with 34 additions and 20 deletions

View File

@ -159,7 +159,7 @@ main(int argc, char **argv)
pid_file = g_strdup(RUNSTATEDIR "/leaseshelper.pid");
/* Try to claim the pidfile, exiting if we can't */
if ((pid_file_fd = virPidFileAcquirePathFull(pid_file, true, getpid())) < 0) {
if ((pid_file_fd = virPidFileAcquirePathFull(pid_file, true, false, getpid())) < 0) {
fprintf(stderr,
_("Unable to acquire PID file: %s\n errno=%d"),
pid_file, errno);

View File

@ -364,6 +364,7 @@ int virPidFileDelete(const char *dir,
int virPidFileAcquirePathFull(const char *path,
bool waitForLock,
bool quiet,
pid_t pid)
{
int fd = -1;
@ -375,32 +376,40 @@ int virPidFileAcquirePathFull(const char *path,
while (1) {
struct stat a, b;
if ((fd = open(path, O_WRONLY|O_CREAT, 0644)) < 0) {
virReportSystemError(errno,
_("Failed to open pid file '%s'"),
path);
if (!quiet) {
virReportSystemError(errno,
_("Failed to open pid file '%s'"),
path);
}
return -1;
}
if (virSetCloseExec(fd) < 0) {
virReportSystemError(errno,
_("Failed to set close-on-exec flag '%s'"),
path);
if (!quiet) {
virReportSystemError(errno,
_("Failed to set close-on-exec flag '%s'"),
path);
}
VIR_FORCE_CLOSE(fd);
return -1;
}
if (fstat(fd, &b) < 0) {
virReportSystemError(errno,
_("Unable to check status of pid file '%s'"),
path);
if (!quiet) {
virReportSystemError(errno,
_("Unable to check status of pid file '%s'"),
path);
}
VIR_FORCE_CLOSE(fd);
return -1;
}
if (virFileLock(fd, false, 0, 1, waitForLock) < 0) {
virReportSystemError(errno,
_("Failed to acquire pid file '%s'"),
path);
if (!quiet) {
virReportSystemError(errno,
_("Failed to acquire pid file '%s'"),
path);
}
VIR_FORCE_CLOSE(fd);
return -1;
}
@ -427,17 +436,21 @@ int virPidFileAcquirePathFull(const char *path,
g_snprintf(pidstr, sizeof(pidstr), "%lld", (long long) pid);
if (ftruncate(fd, 0) < 0) {
virReportSystemError(errno,
_("Failed to truncate pid file '%s'"),
path);
if (!quiet) {
virReportSystemError(errno,
_("Failed to truncate pid file '%s'"),
path);
}
VIR_FORCE_CLOSE(fd);
return -1;
}
if (safewrite(fd, pidstr, strlen(pidstr)) < 0) {
virReportSystemError(errno,
_("Failed to write to pid file '%s'"),
path);
if (!quiet) {
virReportSystemError(errno,
_("Failed to write to pid file '%s'"),
path);
}
VIR_FORCE_CLOSE(fd);
}
@ -448,7 +461,7 @@ int virPidFileAcquirePathFull(const char *path,
int virPidFileAcquirePath(const char *path,
pid_t pid)
{
return virPidFileAcquirePathFull(path, false, pid);
return virPidFileAcquirePathFull(path, false, false, pid);
}

View File

@ -58,6 +58,7 @@ int virPidFileDelete(const char *dir,
int virPidFileAcquirePathFull(const char *path,
bool waitForLock,
bool quiet,
pid_t pid) G_GNUC_WARN_UNUSED_RESULT;
int virPidFileAcquirePath(const char *path,
pid_t pid) G_GNUC_WARN_UNUSED_RESULT;