2011-08-05 13:13:12 +00:00
|
|
|
/*
|
|
|
|
* virpidfile.c: manipulation of pidfiles
|
|
|
|
*
|
2014-09-07 17:52:34 +00:00
|
|
|
* Copyright (C) 2010-2012, 2014 Red Hat, Inc.
|
2011-08-05 13:13:12 +00:00
|
|
|
* Copyright (C) 2006, 2007 Binary Karma
|
|
|
|
* Copyright (C) 2006 Shuveb Hussain
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2011-08-05 13:13:12 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
2011-08-05 13:41:25 +00:00
|
|
|
#include <signal.h>
|
2011-07-06 16:12:26 +00:00
|
|
|
#include <sys/stat.h>
|
2011-08-05 13:13:12 +00:00
|
|
|
|
|
|
|
#include "virpidfile.h"
|
|
|
|
#include "virfile.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-13 17:44:57 +00:00
|
|
|
#include "virutil.h"
|
2011-07-06 16:12:26 +00:00
|
|
|
#include "intprops.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-02-13 13:27:15 +00:00
|
|
|
#include "c-ctype.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2014-10-12 11:40:36 +00:00
|
|
|
#include "virprocess.h"
|
2011-08-05 13:13:12 +00:00
|
|
|
|
2011-07-06 16:12:26 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.pidfile");
|
|
|
|
|
2011-08-05 13:13:12 +00:00
|
|
|
char *virPidFileBuildPath(const char *dir, const char* name)
|
|
|
|
{
|
2015-06-01 08:10:04 +00:00
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
2011-08-05 13:13:12 +00:00
|
|
|
|
2015-06-01 08:10:04 +00:00
|
|
|
virBufferAsprintf(&buf, "%s", dir);
|
|
|
|
virBufferEscapeString(&buf, "/%s.pid", name);
|
2011-08-05 13:13:12 +00:00
|
|
|
|
2015-06-01 08:10:04 +00:00
|
|
|
return virBufferContentAndReset(&buf);
|
2011-08-05 13:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int virPidFileWritePath(const char *pidfile,
|
|
|
|
pid_t pid)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int fd;
|
2012-02-02 00:28:30 +00:00
|
|
|
char pidstr[INT_BUFSIZE_BOUND(pid)];
|
2011-08-05 13:13:12 +00:00
|
|
|
|
|
|
|
if ((fd = open(pidfile,
|
|
|
|
O_WRONLY | O_CREAT | O_TRUNC,
|
|
|
|
S_IRUSR | S_IWUSR)) < 0) {
|
|
|
|
rc = -errno;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2012-02-02 00:28:30 +00:00
|
|
|
snprintf(pidstr, sizeof(pidstr), "%lld", (long long) pid);
|
2011-08-05 13:13:12 +00:00
|
|
|
|
2012-02-02 00:28:30 +00:00
|
|
|
if (safewrite(fd, pidstr, strlen(pidstr)) < 0) {
|
2011-08-05 13:13:12 +00:00
|
|
|
rc = -errno;
|
2012-02-02 00:28:30 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
2011-08-05 13:13:12 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2012-02-02 00:28:30 +00:00
|
|
|
if (VIR_CLOSE(fd) < 0)
|
2011-08-05 13:13:12 +00:00
|
|
|
rc = -errno;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int virPidFileWrite(const char *dir,
|
|
|
|
const char *name,
|
|
|
|
pid_t pid)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *pidfile = NULL;
|
2011-08-05 13:13:12 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
if (name == NULL || dir == NULL)
|
|
|
|
return -EINVAL;
|
2011-08-05 13:13:12 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
if (virFileMakePath(dir) < 0)
|
|
|
|
return -errno;
|
2011-08-05 13:13:12 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
if (!(pidfile = virPidFileBuildPath(dir, name)))
|
|
|
|
return -ENOMEM;
|
2011-08-05 13:13:12 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
return virPidFileWritePath(pidfile, pid);
|
2011-08-05 13:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int virPidFileReadPath(const char *path,
|
|
|
|
pid_t *pid)
|
|
|
|
{
|
2012-02-02 00:28:30 +00:00
|
|
|
int fd;
|
2011-08-05 13:13:12 +00:00
|
|
|
int rc;
|
2012-02-02 00:28:30 +00:00
|
|
|
ssize_t bytes;
|
|
|
|
long long pid_value = 0;
|
|
|
|
char pidstr[INT_BUFSIZE_BOUND(pid_value)];
|
2012-02-13 13:27:15 +00:00
|
|
|
char *endptr = NULL;
|
2011-08-05 13:13:12 +00:00
|
|
|
|
|
|
|
*pid = 0;
|
|
|
|
|
2012-02-02 00:28:30 +00:00
|
|
|
if ((fd = open(path, O_RDONLY)) < 0) {
|
2011-08-05 13:13:12 +00:00
|
|
|
rc = -errno;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2012-02-02 00:28:30 +00:00
|
|
|
bytes = saferead(fd, pidstr, sizeof(pidstr));
|
|
|
|
if (bytes < 0) {
|
|
|
|
rc = -errno;
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
2011-08-05 13:13:12 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-02-02 00:28:30 +00:00
|
|
|
pidstr[bytes] = '\0';
|
2011-08-05 13:13:12 +00:00
|
|
|
|
2012-02-13 13:27:15 +00:00
|
|
|
if (virStrToLong_ll(pidstr, &endptr, 10, &pid_value) < 0 ||
|
|
|
|
!(*endptr == '\0' || c_isspace(*endptr)) ||
|
2012-02-02 00:28:30 +00:00
|
|
|
(pid_t) pid_value != pid_value) {
|
|
|
|
rc = -1;
|
2011-08-05 13:13:12 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2012-02-02 00:28:30 +00:00
|
|
|
*pid = pid_value;
|
2011-08-05 13:13:12 +00:00
|
|
|
rc = 0;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2012-02-02 00:28:30 +00:00
|
|
|
if (VIR_CLOSE(fd) < 0)
|
|
|
|
rc = -errno;
|
|
|
|
|
2011-08-05 13:13:12 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int virPidFileRead(const char *dir,
|
|
|
|
const char *name,
|
|
|
|
pid_t *pid)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *pidfile = NULL;
|
2011-08-05 13:13:12 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
*pid = 0;
|
2011-08-05 13:13:12 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
if (name == NULL || dir == NULL)
|
|
|
|
return -EINVAL;
|
2011-08-05 13:13:12 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
if (!(pidfile = virPidFileBuildPath(dir, name)))
|
|
|
|
return -ENOMEM;
|
2011-08-05 13:13:12 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
return virPidFileReadPath(pidfile, pid);
|
2011-08-05 13:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-05 13:41:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virPidFileReadPathIfAlive:
|
|
|
|
* @path: path to pidfile
|
|
|
|
* @pid: variable to return pid in
|
|
|
|
* @binpath: path of executable associated with the pidfile
|
|
|
|
*
|
|
|
|
* This will attempt to read a pid from @path, and store it
|
|
|
|
* in @pid. The @pid will only be set, however, if the
|
|
|
|
* pid in @path is running, and its executable path
|
|
|
|
* resolves to @binpath. This adds protection against
|
|
|
|
* recycling of previously reaped pids.
|
|
|
|
*
|
2012-01-26 15:25:38 +00:00
|
|
|
* If @binpath is NULL the check for the executable path
|
|
|
|
* is skipped.
|
|
|
|
*
|
2019-11-13 12:08:58 +00:00
|
|
|
* Returns -1 upon error, or zero on successful
|
2011-08-05 13:41:25 +00:00
|
|
|
* reading of the pidfile. If the PID was not still
|
|
|
|
* alive, zero will be returned, but @pid will be
|
|
|
|
* set to -1.
|
|
|
|
*/
|
|
|
|
int virPidFileReadPathIfAlive(const char *path,
|
|
|
|
pid_t *pid,
|
util: do a better job of matching up pids with their binaries
This patch resolves: https://bugzilla.redhat.com/show_bug.cgi?id=871201
If libvirt is restarted after updating the dnsmasq or radvd packages,
a subsequent "virsh net-destroy" will fail to kill the dnsmasq/radvd
process.
The problem is that when libvirtd restarts, it re-reads the dnsmasq
and radvd pidfiles, then does a sanity check on each pid it finds,
including checking that the symbolic link in /proc/$pid/exe actually
points to the same file as the path used by libvirt to execute the
binary in the first place. If this fails, libvirt assumes that the
process is no longer alive.
But if the original binary has been replaced, the link in /proc is set
to "$binarypath (deleted)" (it literally has the string " (deleted)"
appended to the link text stored in the filesystem), so even if a new
binary exists in the same location, attempts to resolve the link will
fail.
In the end, not only is the old dnsmasq/radvd not terminated when the
network is stopped, but a new dnsmasq can't be started when the
network is later restarted (because the original process is still
listening on the ports that the new process wants).
The solution is, when the initial "use stat to check for identical
inodes" check for identity between /proc/$pid/exe and $binpath fails,
to check /proc/$pid/exe for a link ending with " (deleted)" and if so,
truncate that part of the link and compare what's left with the
original binarypath.
A twist to this problem is that on systems with "merged" /sbin and
/usr/sbin (i.e. /sbin is really just a symlink to /usr/sbin; Fedora
17+ is an example of this), libvirt may have started the process using
one path, but /proc/$pid/exe lists a different path (indeed, on F17
this is the case - libvirtd uses /sbin/dnsmasq, but /proc/$pid/exe
shows "/usr/sbin/dnsmasq"). The further bit of code to resolve this is
to call virFileResolveAllLinks() on both the original binarypath and
on the truncated link we read from /proc/$pid/exe, and compare the
results.
The resulting code still succeeds in all the same cases it did before,
but also succeeds if the binary was deleted or replaced after it was
started.
2012-10-29 22:05:41 +00:00
|
|
|
const char *binPath)
|
2011-08-05 13:41:25 +00:00
|
|
|
{
|
2019-11-13 12:08:58 +00:00
|
|
|
int rc;
|
|
|
|
bool isLink = false;
|
util: do a better job of matching up pids with their binaries
This patch resolves: https://bugzilla.redhat.com/show_bug.cgi?id=871201
If libvirt is restarted after updating the dnsmasq or radvd packages,
a subsequent "virsh net-destroy" will fail to kill the dnsmasq/radvd
process.
The problem is that when libvirtd restarts, it re-reads the dnsmasq
and radvd pidfiles, then does a sanity check on each pid it finds,
including checking that the symbolic link in /proc/$pid/exe actually
points to the same file as the path used by libvirt to execute the
binary in the first place. If this fails, libvirt assumes that the
process is no longer alive.
But if the original binary has been replaced, the link in /proc is set
to "$binarypath (deleted)" (it literally has the string " (deleted)"
appended to the link text stored in the filesystem), so even if a new
binary exists in the same location, attempts to resolve the link will
fail.
In the end, not only is the old dnsmasq/radvd not terminated when the
network is stopped, but a new dnsmasq can't be started when the
network is later restarted (because the original process is still
listening on the ports that the new process wants).
The solution is, when the initial "use stat to check for identical
inodes" check for identity between /proc/$pid/exe and $binpath fails,
to check /proc/$pid/exe for a link ending with " (deleted)" and if so,
truncate that part of the link and compare what's left with the
original binarypath.
A twist to this problem is that on systems with "merged" /sbin and
/usr/sbin (i.e. /sbin is really just a symlink to /usr/sbin; Fedora
17+ is an example of this), libvirt may have started the process using
one path, but /proc/$pid/exe lists a different path (indeed, on F17
this is the case - libvirtd uses /sbin/dnsmasq, but /proc/$pid/exe
shows "/usr/sbin/dnsmasq"). The further bit of code to resolve this is
to call virFileResolveAllLinks() on both the original binarypath and
on the truncated link we read from /proc/$pid/exe, and compare the
results.
The resulting code still succeeds in all the same cases it did before,
but also succeeds if the binary was deleted or replaced after it was
started.
2012-10-29 22:05:41 +00:00
|
|
|
size_t procLinkLen;
|
|
|
|
const char deletedText[] = " (deleted)";
|
|
|
|
size_t deletedTextLen = strlen(deletedText);
|
2012-10-31 17:00:50 +00:00
|
|
|
pid_t retPid;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *procPath = NULL;
|
|
|
|
g_autofree char *procLink = NULL;
|
|
|
|
g_autofree char *resolvedBinPath = NULL;
|
|
|
|
g_autofree char *resolvedProcLink = NULL;
|
2011-08-05 13:41:25 +00:00
|
|
|
|
util: do a better job of matching up pids with their binaries
This patch resolves: https://bugzilla.redhat.com/show_bug.cgi?id=871201
If libvirt is restarted after updating the dnsmasq or radvd packages,
a subsequent "virsh net-destroy" will fail to kill the dnsmasq/radvd
process.
The problem is that when libvirtd restarts, it re-reads the dnsmasq
and radvd pidfiles, then does a sanity check on each pid it finds,
including checking that the symbolic link in /proc/$pid/exe actually
points to the same file as the path used by libvirt to execute the
binary in the first place. If this fails, libvirt assumes that the
process is no longer alive.
But if the original binary has been replaced, the link in /proc is set
to "$binarypath (deleted)" (it literally has the string " (deleted)"
appended to the link text stored in the filesystem), so even if a new
binary exists in the same location, attempts to resolve the link will
fail.
In the end, not only is the old dnsmasq/radvd not terminated when the
network is stopped, but a new dnsmasq can't be started when the
network is later restarted (because the original process is still
listening on the ports that the new process wants).
The solution is, when the initial "use stat to check for identical
inodes" check for identity between /proc/$pid/exe and $binpath fails,
to check /proc/$pid/exe for a link ending with " (deleted)" and if so,
truncate that part of the link and compare what's left with the
original binarypath.
A twist to this problem is that on systems with "merged" /sbin and
/usr/sbin (i.e. /sbin is really just a symlink to /usr/sbin; Fedora
17+ is an example of this), libvirt may have started the process using
one path, but /proc/$pid/exe lists a different path (indeed, on F17
this is the case - libvirtd uses /sbin/dnsmasq, but /proc/$pid/exe
shows "/usr/sbin/dnsmasq"). The further bit of code to resolve this is
to call virFileResolveAllLinks() on both the original binarypath and
on the truncated link we read from /proc/$pid/exe, and compare the
results.
The resulting code still succeeds in all the same cases it did before,
but also succeeds if the binary was deleted or replaced after it was
started.
2012-10-29 22:05:41 +00:00
|
|
|
/* only set this at the very end on success */
|
|
|
|
*pid = -1;
|
|
|
|
|
2019-11-13 12:08:58 +00:00
|
|
|
if (virPidFileReadPath(path, &retPid) < 0)
|
|
|
|
return -1;
|
2011-08-05 13:41:25 +00:00
|
|
|
|
2011-08-17 17:46:35 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
/* Check that it's still alive. Safe to skip this sanity check on
|
|
|
|
* mingw, which lacks kill(). */
|
util: do a better job of matching up pids with their binaries
This patch resolves: https://bugzilla.redhat.com/show_bug.cgi?id=871201
If libvirt is restarted after updating the dnsmasq or radvd packages,
a subsequent "virsh net-destroy" will fail to kill the dnsmasq/radvd
process.
The problem is that when libvirtd restarts, it re-reads the dnsmasq
and radvd pidfiles, then does a sanity check on each pid it finds,
including checking that the symbolic link in /proc/$pid/exe actually
points to the same file as the path used by libvirt to execute the
binary in the first place. If this fails, libvirt assumes that the
process is no longer alive.
But if the original binary has been replaced, the link in /proc is set
to "$binarypath (deleted)" (it literally has the string " (deleted)"
appended to the link text stored in the filesystem), so even if a new
binary exists in the same location, attempts to resolve the link will
fail.
In the end, not only is the old dnsmasq/radvd not terminated when the
network is stopped, but a new dnsmasq can't be started when the
network is later restarted (because the original process is still
listening on the ports that the new process wants).
The solution is, when the initial "use stat to check for identical
inodes" check for identity between /proc/$pid/exe and $binpath fails,
to check /proc/$pid/exe for a link ending with " (deleted)" and if so,
truncate that part of the link and compare what's left with the
original binarypath.
A twist to this problem is that on systems with "merged" /sbin and
/usr/sbin (i.e. /sbin is really just a symlink to /usr/sbin; Fedora
17+ is an example of this), libvirt may have started the process using
one path, but /proc/$pid/exe lists a different path (indeed, on F17
this is the case - libvirtd uses /sbin/dnsmasq, but /proc/$pid/exe
shows "/usr/sbin/dnsmasq"). The further bit of code to resolve this is
to call virFileResolveAllLinks() on both the original binarypath and
on the truncated link we read from /proc/$pid/exe, and compare the
results.
The resulting code still succeeds in all the same cases it did before,
but also succeeds if the binary was deleted or replaced after it was
started.
2012-10-29 22:05:41 +00:00
|
|
|
if (kill(retPid, 0) < 0) {
|
2019-11-13 12:08:58 +00:00
|
|
|
*pid = -1;
|
|
|
|
return 0;
|
2011-08-05 13:41:25 +00:00
|
|
|
}
|
2011-08-17 17:46:35 +00:00
|
|
|
#endif
|
2011-08-05 13:41:25 +00:00
|
|
|
|
util: do a better job of matching up pids with their binaries
This patch resolves: https://bugzilla.redhat.com/show_bug.cgi?id=871201
If libvirt is restarted after updating the dnsmasq or radvd packages,
a subsequent "virsh net-destroy" will fail to kill the dnsmasq/radvd
process.
The problem is that when libvirtd restarts, it re-reads the dnsmasq
and radvd pidfiles, then does a sanity check on each pid it finds,
including checking that the symbolic link in /proc/$pid/exe actually
points to the same file as the path used by libvirt to execute the
binary in the first place. If this fails, libvirt assumes that the
process is no longer alive.
But if the original binary has been replaced, the link in /proc is set
to "$binarypath (deleted)" (it literally has the string " (deleted)"
appended to the link text stored in the filesystem), so even if a new
binary exists in the same location, attempts to resolve the link will
fail.
In the end, not only is the old dnsmasq/radvd not terminated when the
network is stopped, but a new dnsmasq can't be started when the
network is later restarted (because the original process is still
listening on the ports that the new process wants).
The solution is, when the initial "use stat to check for identical
inodes" check for identity between /proc/$pid/exe and $binpath fails,
to check /proc/$pid/exe for a link ending with " (deleted)" and if so,
truncate that part of the link and compare what's left with the
original binarypath.
A twist to this problem is that on systems with "merged" /sbin and
/usr/sbin (i.e. /sbin is really just a symlink to /usr/sbin; Fedora
17+ is an example of this), libvirt may have started the process using
one path, but /proc/$pid/exe lists a different path (indeed, on F17
this is the case - libvirtd uses /sbin/dnsmasq, but /proc/$pid/exe
shows "/usr/sbin/dnsmasq"). The further bit of code to resolve this is
to call virFileResolveAllLinks() on both the original binarypath and
on the truncated link we read from /proc/$pid/exe, and compare the
results.
The resulting code still succeeds in all the same cases it did before,
but also succeeds if the binary was deleted or replaced after it was
started.
2012-10-29 22:05:41 +00:00
|
|
|
if (!binPath) {
|
|
|
|
/* we only knew the pid, and that pid is alive, so we can
|
|
|
|
* return it.
|
|
|
|
*/
|
2019-11-13 12:08:58 +00:00
|
|
|
*pid = retPid;
|
|
|
|
return 0;
|
util: do a better job of matching up pids with their binaries
This patch resolves: https://bugzilla.redhat.com/show_bug.cgi?id=871201
If libvirt is restarted after updating the dnsmasq or radvd packages,
a subsequent "virsh net-destroy" will fail to kill the dnsmasq/radvd
process.
The problem is that when libvirtd restarts, it re-reads the dnsmasq
and radvd pidfiles, then does a sanity check on each pid it finds,
including checking that the symbolic link in /proc/$pid/exe actually
points to the same file as the path used by libvirt to execute the
binary in the first place. If this fails, libvirt assumes that the
process is no longer alive.
But if the original binary has been replaced, the link in /proc is set
to "$binarypath (deleted)" (it literally has the string " (deleted)"
appended to the link text stored in the filesystem), so even if a new
binary exists in the same location, attempts to resolve the link will
fail.
In the end, not only is the old dnsmasq/radvd not terminated when the
network is stopped, but a new dnsmasq can't be started when the
network is later restarted (because the original process is still
listening on the ports that the new process wants).
The solution is, when the initial "use stat to check for identical
inodes" check for identity between /proc/$pid/exe and $binpath fails,
to check /proc/$pid/exe for a link ending with " (deleted)" and if so,
truncate that part of the link and compare what's left with the
original binarypath.
A twist to this problem is that on systems with "merged" /sbin and
/usr/sbin (i.e. /sbin is really just a symlink to /usr/sbin; Fedora
17+ is an example of this), libvirt may have started the process using
one path, but /proc/$pid/exe lists a different path (indeed, on F17
this is the case - libvirtd uses /sbin/dnsmasq, but /proc/$pid/exe
shows "/usr/sbin/dnsmasq"). The further bit of code to resolve this is
to call virFileResolveAllLinks() on both the original binarypath and
on the truncated link we read from /proc/$pid/exe, and compare the
results.
The resulting code still succeeds in all the same cases it did before,
but also succeeds if the binary was deleted or replaced after it was
started.
2012-10-29 22:05:41 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
procPath = g_strdup_printf("/proc/%lld/exe", (long long)retPid);
|
util: do a better job of matching up pids with their binaries
This patch resolves: https://bugzilla.redhat.com/show_bug.cgi?id=871201
If libvirt is restarted after updating the dnsmasq or radvd packages,
a subsequent "virsh net-destroy" will fail to kill the dnsmasq/radvd
process.
The problem is that when libvirtd restarts, it re-reads the dnsmasq
and radvd pidfiles, then does a sanity check on each pid it finds,
including checking that the symbolic link in /proc/$pid/exe actually
points to the same file as the path used by libvirt to execute the
binary in the first place. If this fails, libvirt assumes that the
process is no longer alive.
But if the original binary has been replaced, the link in /proc is set
to "$binarypath (deleted)" (it literally has the string " (deleted)"
appended to the link text stored in the filesystem), so even if a new
binary exists in the same location, attempts to resolve the link will
fail.
In the end, not only is the old dnsmasq/radvd not terminated when the
network is stopped, but a new dnsmasq can't be started when the
network is later restarted (because the original process is still
listening on the ports that the new process wants).
The solution is, when the initial "use stat to check for identical
inodes" check for identity between /proc/$pid/exe and $binpath fails,
to check /proc/$pid/exe for a link ending with " (deleted)" and if so,
truncate that part of the link and compare what's left with the
original binarypath.
A twist to this problem is that on systems with "merged" /sbin and
/usr/sbin (i.e. /sbin is really just a symlink to /usr/sbin; Fedora
17+ is an example of this), libvirt may have started the process using
one path, but /proc/$pid/exe lists a different path (indeed, on F17
this is the case - libvirtd uses /sbin/dnsmasq, but /proc/$pid/exe
shows "/usr/sbin/dnsmasq"). The further bit of code to resolve this is
to call virFileResolveAllLinks() on both the original binarypath and
on the truncated link we read from /proc/$pid/exe, and compare the
results.
The resulting code still succeeds in all the same cases it did before,
but also succeeds if the binary was deleted or replaced after it was
started.
2012-10-29 22:05:41 +00:00
|
|
|
|
2019-11-13 12:08:58 +00:00
|
|
|
if ((rc = virFileIsLink(procPath)) < 0)
|
|
|
|
return -1;
|
2018-07-28 18:01:43 +00:00
|
|
|
|
2019-11-13 12:08:58 +00:00
|
|
|
if (rc == 1)
|
|
|
|
isLink = true;
|
2011-08-16 19:36:22 +00:00
|
|
|
|
util: do a better job of matching up pids with their binaries
This patch resolves: https://bugzilla.redhat.com/show_bug.cgi?id=871201
If libvirt is restarted after updating the dnsmasq or radvd packages,
a subsequent "virsh net-destroy" will fail to kill the dnsmasq/radvd
process.
The problem is that when libvirtd restarts, it re-reads the dnsmasq
and radvd pidfiles, then does a sanity check on each pid it finds,
including checking that the symbolic link in /proc/$pid/exe actually
points to the same file as the path used by libvirt to execute the
binary in the first place. If this fails, libvirt assumes that the
process is no longer alive.
But if the original binary has been replaced, the link in /proc is set
to "$binarypath (deleted)" (it literally has the string " (deleted)"
appended to the link text stored in the filesystem), so even if a new
binary exists in the same location, attempts to resolve the link will
fail.
In the end, not only is the old dnsmasq/radvd not terminated when the
network is stopped, but a new dnsmasq can't be started when the
network is later restarted (because the original process is still
listening on the ports that the new process wants).
The solution is, when the initial "use stat to check for identical
inodes" check for identity between /proc/$pid/exe and $binpath fails,
to check /proc/$pid/exe for a link ending with " (deleted)" and if so,
truncate that part of the link and compare what's left with the
original binarypath.
A twist to this problem is that on systems with "merged" /sbin and
/usr/sbin (i.e. /sbin is really just a symlink to /usr/sbin; Fedora
17+ is an example of this), libvirt may have started the process using
one path, but /proc/$pid/exe lists a different path (indeed, on F17
this is the case - libvirtd uses /sbin/dnsmasq, but /proc/$pid/exe
shows "/usr/sbin/dnsmasq"). The further bit of code to resolve this is
to call virFileResolveAllLinks() on both the original binarypath and
on the truncated link we read from /proc/$pid/exe, and compare the
results.
The resulting code still succeeds in all the same cases it did before,
but also succeeds if the binary was deleted or replaced after it was
started.
2012-10-29 22:05:41 +00:00
|
|
|
if (isLink && virFileLinkPointsTo(procPath, binPath)) {
|
|
|
|
/* the link in /proc/$pid/exe is a symlink to a file
|
|
|
|
* that has the same inode as the file at binpath.
|
|
|
|
*/
|
2019-11-13 12:08:58 +00:00
|
|
|
*pid = retPid;
|
|
|
|
return 0;
|
util: do a better job of matching up pids with their binaries
This patch resolves: https://bugzilla.redhat.com/show_bug.cgi?id=871201
If libvirt is restarted after updating the dnsmasq or radvd packages,
a subsequent "virsh net-destroy" will fail to kill the dnsmasq/radvd
process.
The problem is that when libvirtd restarts, it re-reads the dnsmasq
and radvd pidfiles, then does a sanity check on each pid it finds,
including checking that the symbolic link in /proc/$pid/exe actually
points to the same file as the path used by libvirt to execute the
binary in the first place. If this fails, libvirt assumes that the
process is no longer alive.
But if the original binary has been replaced, the link in /proc is set
to "$binarypath (deleted)" (it literally has the string " (deleted)"
appended to the link text stored in the filesystem), so even if a new
binary exists in the same location, attempts to resolve the link will
fail.
In the end, not only is the old dnsmasq/radvd not terminated when the
network is stopped, but a new dnsmasq can't be started when the
network is later restarted (because the original process is still
listening on the ports that the new process wants).
The solution is, when the initial "use stat to check for identical
inodes" check for identity between /proc/$pid/exe and $binpath fails,
to check /proc/$pid/exe for a link ending with " (deleted)" and if so,
truncate that part of the link and compare what's left with the
original binarypath.
A twist to this problem is that on systems with "merged" /sbin and
/usr/sbin (i.e. /sbin is really just a symlink to /usr/sbin; Fedora
17+ is an example of this), libvirt may have started the process using
one path, but /proc/$pid/exe lists a different path (indeed, on F17
this is the case - libvirtd uses /sbin/dnsmasq, but /proc/$pid/exe
shows "/usr/sbin/dnsmasq"). The further bit of code to resolve this is
to call virFileResolveAllLinks() on both the original binarypath and
on the truncated link we read from /proc/$pid/exe, and compare the
results.
The resulting code still succeeds in all the same cases it did before,
but also succeeds if the binary was deleted or replaced after it was
started.
2012-10-29 22:05:41 +00:00
|
|
|
}
|
2011-08-16 19:36:22 +00:00
|
|
|
|
util: do a better job of matching up pids with their binaries
This patch resolves: https://bugzilla.redhat.com/show_bug.cgi?id=871201
If libvirt is restarted after updating the dnsmasq or radvd packages,
a subsequent "virsh net-destroy" will fail to kill the dnsmasq/radvd
process.
The problem is that when libvirtd restarts, it re-reads the dnsmasq
and radvd pidfiles, then does a sanity check on each pid it finds,
including checking that the symbolic link in /proc/$pid/exe actually
points to the same file as the path used by libvirt to execute the
binary in the first place. If this fails, libvirt assumes that the
process is no longer alive.
But if the original binary has been replaced, the link in /proc is set
to "$binarypath (deleted)" (it literally has the string " (deleted)"
appended to the link text stored in the filesystem), so even if a new
binary exists in the same location, attempts to resolve the link will
fail.
In the end, not only is the old dnsmasq/radvd not terminated when the
network is stopped, but a new dnsmasq can't be started when the
network is later restarted (because the original process is still
listening on the ports that the new process wants).
The solution is, when the initial "use stat to check for identical
inodes" check for identity between /proc/$pid/exe and $binpath fails,
to check /proc/$pid/exe for a link ending with " (deleted)" and if so,
truncate that part of the link and compare what's left with the
original binarypath.
A twist to this problem is that on systems with "merged" /sbin and
/usr/sbin (i.e. /sbin is really just a symlink to /usr/sbin; Fedora
17+ is an example of this), libvirt may have started the process using
one path, but /proc/$pid/exe lists a different path (indeed, on F17
this is the case - libvirtd uses /sbin/dnsmasq, but /proc/$pid/exe
shows "/usr/sbin/dnsmasq"). The further bit of code to resolve this is
to call virFileResolveAllLinks() on both the original binarypath and
on the truncated link we read from /proc/$pid/exe, and compare the
results.
The resulting code still succeeds in all the same cases it did before,
but also succeeds if the binary was deleted or replaced after it was
started.
2012-10-29 22:05:41 +00:00
|
|
|
/* Even if virFileLinkPointsTo returns a mismatch, it could be
|
|
|
|
* that the binary was deleted/replaced after it was executed. In
|
|
|
|
* that case the link in /proc/$pid/exe will contain
|
|
|
|
* "$procpath (deleted)". Read that link, remove the " (deleted)"
|
|
|
|
* part, and see if it has the same canonicalized name as binpath.
|
|
|
|
*/
|
2019-11-13 12:24:47 +00:00
|
|
|
if (!(procLink = g_file_read_link(procPath, NULL)))
|
2019-11-13 12:08:58 +00:00
|
|
|
return -1;
|
2018-07-28 18:01:43 +00:00
|
|
|
|
util: do a better job of matching up pids with their binaries
This patch resolves: https://bugzilla.redhat.com/show_bug.cgi?id=871201
If libvirt is restarted after updating the dnsmasq or radvd packages,
a subsequent "virsh net-destroy" will fail to kill the dnsmasq/radvd
process.
The problem is that when libvirtd restarts, it re-reads the dnsmasq
and radvd pidfiles, then does a sanity check on each pid it finds,
including checking that the symbolic link in /proc/$pid/exe actually
points to the same file as the path used by libvirt to execute the
binary in the first place. If this fails, libvirt assumes that the
process is no longer alive.
But if the original binary has been replaced, the link in /proc is set
to "$binarypath (deleted)" (it literally has the string " (deleted)"
appended to the link text stored in the filesystem), so even if a new
binary exists in the same location, attempts to resolve the link will
fail.
In the end, not only is the old dnsmasq/radvd not terminated when the
network is stopped, but a new dnsmasq can't be started when the
network is later restarted (because the original process is still
listening on the ports that the new process wants).
The solution is, when the initial "use stat to check for identical
inodes" check for identity between /proc/$pid/exe and $binpath fails,
to check /proc/$pid/exe for a link ending with " (deleted)" and if so,
truncate that part of the link and compare what's left with the
original binarypath.
A twist to this problem is that on systems with "merged" /sbin and
/usr/sbin (i.e. /sbin is really just a symlink to /usr/sbin; Fedora
17+ is an example of this), libvirt may have started the process using
one path, but /proc/$pid/exe lists a different path (indeed, on F17
this is the case - libvirtd uses /sbin/dnsmasq, but /proc/$pid/exe
shows "/usr/sbin/dnsmasq"). The further bit of code to resolve this is
to call virFileResolveAllLinks() on both the original binarypath and
on the truncated link we read from /proc/$pid/exe, and compare the
results.
The resulting code still succeeds in all the same cases it did before,
but also succeeds if the binary was deleted or replaced after it was
started.
2012-10-29 22:05:41 +00:00
|
|
|
procLinkLen = strlen(procLink);
|
|
|
|
if (procLinkLen > deletedTextLen)
|
|
|
|
procLink[procLinkLen - deletedTextLen] = 0;
|
2011-08-05 13:41:25 +00:00
|
|
|
|
2019-11-13 12:08:58 +00:00
|
|
|
if (virFileResolveAllLinks(binPath, &resolvedBinPath) < 0)
|
|
|
|
return -1;
|
|
|
|
if (virFileResolveAllLinks(procLink, &resolvedProcLink) < 0)
|
|
|
|
return -1;
|
util: do a better job of matching up pids with their binaries
This patch resolves: https://bugzilla.redhat.com/show_bug.cgi?id=871201
If libvirt is restarted after updating the dnsmasq or radvd packages,
a subsequent "virsh net-destroy" will fail to kill the dnsmasq/radvd
process.
The problem is that when libvirtd restarts, it re-reads the dnsmasq
and radvd pidfiles, then does a sanity check on each pid it finds,
including checking that the symbolic link in /proc/$pid/exe actually
points to the same file as the path used by libvirt to execute the
binary in the first place. If this fails, libvirt assumes that the
process is no longer alive.
But if the original binary has been replaced, the link in /proc is set
to "$binarypath (deleted)" (it literally has the string " (deleted)"
appended to the link text stored in the filesystem), so even if a new
binary exists in the same location, attempts to resolve the link will
fail.
In the end, not only is the old dnsmasq/radvd not terminated when the
network is stopped, but a new dnsmasq can't be started when the
network is later restarted (because the original process is still
listening on the ports that the new process wants).
The solution is, when the initial "use stat to check for identical
inodes" check for identity between /proc/$pid/exe and $binpath fails,
to check /proc/$pid/exe for a link ending with " (deleted)" and if so,
truncate that part of the link and compare what's left with the
original binarypath.
A twist to this problem is that on systems with "merged" /sbin and
/usr/sbin (i.e. /sbin is really just a symlink to /usr/sbin; Fedora
17+ is an example of this), libvirt may have started the process using
one path, but /proc/$pid/exe lists a different path (indeed, on F17
this is the case - libvirtd uses /sbin/dnsmasq, but /proc/$pid/exe
shows "/usr/sbin/dnsmasq"). The further bit of code to resolve this is
to call virFileResolveAllLinks() on both the original binarypath and
on the truncated link we read from /proc/$pid/exe, and compare the
results.
The resulting code still succeeds in all the same cases it did before,
but also succeeds if the binary was deleted or replaced after it was
started.
2012-10-29 22:05:41 +00:00
|
|
|
|
2019-11-13 12:08:58 +00:00
|
|
|
if (STRNEQ(resolvedBinPath, resolvedProcLink))
|
|
|
|
return -1;
|
util: do a better job of matching up pids with their binaries
This patch resolves: https://bugzilla.redhat.com/show_bug.cgi?id=871201
If libvirt is restarted after updating the dnsmasq or radvd packages,
a subsequent "virsh net-destroy" will fail to kill the dnsmasq/radvd
process.
The problem is that when libvirtd restarts, it re-reads the dnsmasq
and radvd pidfiles, then does a sanity check on each pid it finds,
including checking that the symbolic link in /proc/$pid/exe actually
points to the same file as the path used by libvirt to execute the
binary in the first place. If this fails, libvirt assumes that the
process is no longer alive.
But if the original binary has been replaced, the link in /proc is set
to "$binarypath (deleted)" (it literally has the string " (deleted)"
appended to the link text stored in the filesystem), so even if a new
binary exists in the same location, attempts to resolve the link will
fail.
In the end, not only is the old dnsmasq/radvd not terminated when the
network is stopped, but a new dnsmasq can't be started when the
network is later restarted (because the original process is still
listening on the ports that the new process wants).
The solution is, when the initial "use stat to check for identical
inodes" check for identity between /proc/$pid/exe and $binpath fails,
to check /proc/$pid/exe for a link ending with " (deleted)" and if so,
truncate that part of the link and compare what's left with the
original binarypath.
A twist to this problem is that on systems with "merged" /sbin and
/usr/sbin (i.e. /sbin is really just a symlink to /usr/sbin; Fedora
17+ is an example of this), libvirt may have started the process using
one path, but /proc/$pid/exe lists a different path (indeed, on F17
this is the case - libvirtd uses /sbin/dnsmasq, but /proc/$pid/exe
shows "/usr/sbin/dnsmasq"). The further bit of code to resolve this is
to call virFileResolveAllLinks() on both the original binarypath and
on the truncated link we read from /proc/$pid/exe, and compare the
results.
The resulting code still succeeds in all the same cases it did before,
but also succeeds if the binary was deleted or replaced after it was
started.
2012-10-29 22:05:41 +00:00
|
|
|
|
2019-11-13 12:08:58 +00:00
|
|
|
*pid = retPid;
|
|
|
|
return 0;
|
2011-08-05 13:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virPidFileReadIfAlive:
|
|
|
|
* @dir: directory containing pidfile
|
|
|
|
* @name: base filename of pidfile
|
|
|
|
* @pid: variable to return pid in
|
|
|
|
* @binpath: path of executable associated with the pidfile
|
|
|
|
*
|
|
|
|
* This will attempt to read a pid from the pidfile @name
|
|
|
|
* in directory @dir, and store it in @pid. The @pid will
|
|
|
|
* only be set, however, if the pid in @name is running,
|
|
|
|
* and its executable path resolves to @binpath. This adds
|
|
|
|
* protection against recycling of previously reaped pids.
|
|
|
|
*
|
2019-11-13 11:57:01 +00:00
|
|
|
* Returns -1 upon error, or zero on successful
|
2011-08-05 13:41:25 +00:00
|
|
|
* reading of the pidfile. If the PID was not still
|
|
|
|
* alive, zero will be returned, but @pid will be
|
|
|
|
* set to -1.
|
|
|
|
*/
|
|
|
|
int virPidFileReadIfAlive(const char *dir,
|
|
|
|
const char *name,
|
|
|
|
pid_t *pid,
|
|
|
|
const char *binpath)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *pidfile = NULL;
|
2011-08-05 13:41:25 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
if (name == NULL || dir == NULL)
|
2019-11-13 11:57:01 +00:00
|
|
|
return -1;
|
2011-08-05 13:41:25 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
if (!(pidfile = virPidFileBuildPath(dir, name)))
|
2019-11-13 11:57:01 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virPidFileReadPathIfAlive(pidfile, pid, binpath) < 0)
|
|
|
|
return -1;
|
2011-08-05 13:41:25 +00:00
|
|
|
|
2019-11-13 11:57:01 +00:00
|
|
|
return 0;
|
2011-08-05 13:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-05 13:13:12 +00:00
|
|
|
int virPidFileDeletePath(const char *pidfile)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if (unlink(pidfile) < 0 && errno != ENOENT)
|
|
|
|
rc = -errno;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int virPidFileDelete(const char *dir,
|
|
|
|
const char *name)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *pidfile = NULL;
|
2011-08-05 13:13:12 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
if (name == NULL || dir == NULL)
|
|
|
|
return -EINVAL;
|
2011-08-05 13:13:12 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
if (!(pidfile = virPidFileBuildPath(dir, name)))
|
|
|
|
return -ENOMEM;
|
2011-08-05 13:13:12 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
return virPidFileDeletePath(pidfile);
|
2011-08-05 13:13:12 +00:00
|
|
|
}
|
2011-07-06 16:12:26 +00:00
|
|
|
|
|
|
|
int virPidFileAcquirePath(const char *path,
|
2014-03-17 14:17:36 +00:00
|
|
|
bool waitForLock,
|
2011-07-06 16:12:26 +00:00
|
|
|
pid_t pid)
|
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
char pidstr[INT_BUFSIZE_BOUND(pid)];
|
|
|
|
|
|
|
|
if (path[0] == '\0')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
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);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virSetCloseExec(fd) < 0) {
|
|
|
|
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);
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-17 14:17:36 +00:00
|
|
|
if (virFileLock(fd, false, 0, 1, waitForLock) < 0) {
|
2011-07-06 16:12:26 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to acquire pid file '%s'"),
|
|
|
|
path);
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now make sure the pidfile we locked is the same
|
|
|
|
* one that now exists on the filesystem
|
|
|
|
*/
|
|
|
|
if (stat(path, &a) < 0) {
|
2019-10-14 12:45:33 +00:00
|
|
|
char ebuf[1024] G_GNUC_UNUSED;
|
2011-07-06 16:12:26 +00:00
|
|
|
VIR_DEBUG("Pid file '%s' disappeared: %s",
|
2012-03-29 09:52:04 +00:00
|
|
|
path, virStrerror(errno, ebuf, sizeof(ebuf)));
|
2011-07-06 16:12:26 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
2012-10-11 16:31:20 +00:00
|
|
|
/* Someone else must be racing with us, so try again */
|
2011-07-06 16:12:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.st_ino == b.st_ino)
|
|
|
|
break;
|
|
|
|
|
|
|
|
VIR_DEBUG("Pid file '%s' was recreated", path);
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
2012-10-11 16:31:20 +00:00
|
|
|
/* Someone else must be racing with us, so try again */
|
2011-07-06 16:12:26 +00:00
|
|
|
}
|
|
|
|
|
2012-02-02 00:28:30 +00:00
|
|
|
snprintf(pidstr, sizeof(pidstr), "%lld", (long long) pid);
|
2011-07-06 16:12:26 +00:00
|
|
|
|
2018-07-04 03:55:51 +00:00
|
|
|
if (ftruncate(fd, 0) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to truncate pid file '%s'"),
|
|
|
|
path);
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-07-06 16:12:26 +00:00
|
|
|
if (safewrite(fd, pidstr, strlen(pidstr)) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to write to pid file '%s'"),
|
|
|
|
path);
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int virPidFileAcquire(const char *dir,
|
|
|
|
const char *name,
|
2014-03-17 14:17:36 +00:00
|
|
|
bool waitForLock,
|
2011-07-06 16:12:26 +00:00
|
|
|
pid_t pid)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *pidfile = NULL;
|
2011-07-06 16:12:26 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
if (name == NULL || dir == NULL)
|
|
|
|
return -EINVAL;
|
2011-07-06 16:12:26 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
if (!(pidfile = virPidFileBuildPath(dir, name)))
|
|
|
|
return -ENOMEM;
|
2011-07-06 16:12:26 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
return virPidFileAcquirePath(pidfile, waitForLock, pid);
|
2011-07-06 16:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int virPidFileReleasePath(const char *path,
|
|
|
|
int fd)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
/*
|
|
|
|
* We need to unlink before closing the FD to avoid
|
|
|
|
* a race, but Win32 won't let you unlink an open
|
|
|
|
* file handle. So on that platform we do the reverse
|
|
|
|
* and just have to live with the possible race.
|
|
|
|
*/
|
|
|
|
#ifdef WIN32
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
if (unlink(path) < 0 && errno != ENOENT)
|
|
|
|
rc = -errno;
|
|
|
|
#else
|
|
|
|
if (unlink(path) < 0 && errno != ENOENT)
|
|
|
|
rc = -errno;
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
#endif
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int virPidFileRelease(const char *dir,
|
|
|
|
const char *name,
|
|
|
|
int fd)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *pidfile = NULL;
|
2011-07-06 16:12:26 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
if (name == NULL || dir == NULL)
|
|
|
|
return -EINVAL;
|
2011-07-06 16:12:26 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
if (!(pidfile = virPidFileBuildPath(dir, name)))
|
|
|
|
return -ENOMEM;
|
2011-07-06 16:12:26 +00:00
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
return virPidFileReleasePath(pidfile, fd);
|
2011-07-06 16:12:26 +00:00
|
|
|
}
|
2014-09-07 17:52:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virPidFileConstructPath(bool privileged,
|
2019-08-20 15:05:12 +00:00
|
|
|
const char *runstatedir,
|
2014-09-07 17:52:34 +00:00
|
|
|
const char *progname,
|
|
|
|
char **pidfile)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *rundir = NULL;
|
2014-09-07 18:07:49 +00:00
|
|
|
|
2014-09-07 17:52:34 +00:00
|
|
|
if (privileged) {
|
|
|
|
/*
|
|
|
|
* This is here just to allow calling this function with
|
|
|
|
* statedir == NULL; of course only when !privileged.
|
|
|
|
*/
|
2019-08-20 15:05:12 +00:00
|
|
|
if (!runstatedir) {
|
2014-09-07 17:52:34 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2019-08-20 15:05:12 +00:00
|
|
|
"%s", _("No runstatedir specified"));
|
2018-07-28 18:01:43 +00:00
|
|
|
return -1;
|
2014-09-07 17:52:34 +00:00
|
|
|
}
|
2019-10-22 13:26:14 +00:00
|
|
|
*pidfile = g_strdup_printf("%s/%s.pid", runstatedir, progname);
|
2014-09-07 17:52:34 +00:00
|
|
|
} else {
|
|
|
|
if (!(rundir = virGetUserRuntimeDirectory()))
|
2018-07-28 18:01:43 +00:00
|
|
|
return -1;
|
2014-09-07 17:52:34 +00:00
|
|
|
|
2014-09-07 18:09:36 +00:00
|
|
|
if (virFileMakePathWithMode(rundir, 0700) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Cannot create user runtime directory '%s'"),
|
|
|
|
rundir);
|
2018-07-28 18:01:43 +00:00
|
|
|
return -1;
|
2014-09-07 17:52:34 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
*pidfile = g_strdup_printf("%s/%s.pid", rundir, progname);
|
2014-09-07 17:52:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 18:01:43 +00:00
|
|
|
return 0;
|
2014-09-07 17:52:34 +00:00
|
|
|
}
|
2014-10-12 11:40:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virPidFileForceCleanupPath:
|
|
|
|
*
|
|
|
|
* Check if the pidfile is left around and clean it up whatever it
|
|
|
|
* takes. This doesn't raise an error. This function must not be
|
|
|
|
* called multiple times with the same path, be it in threads or
|
|
|
|
* processes. This function does not raise any errors.
|
|
|
|
*
|
|
|
|
* Returns 0 if the pidfile was successfully cleaned up, -1 otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virPidFileForceCleanupPath(const char *path)
|
|
|
|
{
|
|
|
|
pid_t pid = 0;
|
|
|
|
int fd = -1;
|
|
|
|
|
|
|
|
if (!virFileExists(path))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (virPidFileReadPath(path, &pid) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2014-11-04 09:46:41 +00:00
|
|
|
fd = virPidFileAcquirePath(path, false, 0);
|
|
|
|
if (fd < 0) {
|
2014-10-12 11:40:36 +00:00
|
|
|
virResetLastError();
|
|
|
|
|
|
|
|
/* Only kill the process if the pid is valid one. 0 means
|
|
|
|
* there is somebody else doing the same pidfile cleanup
|
|
|
|
* machinery. */
|
|
|
|
if (pid)
|
|
|
|
virProcessKillPainfully(pid, true);
|
|
|
|
|
|
|
|
if (virPidFileDeletePath(path) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-04 09:46:41 +00:00
|
|
|
if (fd)
|
|
|
|
virPidFileReleasePath(path, fd);
|
|
|
|
|
2014-10-12 11:40:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|