提交 b7e5ca48 编写于 作者: D Daniel P. Berrange

Introduce functions for checking whether a pidfile is valid

In some cases the caller of virPidFileRead might like extra checks
to determine whether the pid just read is really the one they are
expecting. This adds virPidFileReadIfAlive which will check whether
the pid is still alive with kill(0, -1), and (on linux only) will
look at /proc/$PID/path

* libvirt_private.syms, util/virpidfile.c, util/virpidfile.h: Add
  virPidFileReadIfValid and virPidFileReadPathIfValid
* network/bridge_driver.c: Use new APIs to check PID validity
上级 f80a4ed7
...@@ -1122,7 +1122,9 @@ virFileFdopen; ...@@ -1122,7 +1122,9 @@ virFileFdopen;
# virpidfile.h # virpidfile.h
virPidFileBuildPath; virPidFileBuildPath;
virPidFileRead; virPidFileRead;
virPidFileReadIfAlive;
virPidFileReadPath; virPidFileReadPath;
virPidFileReadPathIfAlive;
virPidFileWrite; virPidFileWrite;
virPidFileWritePath; virPidFileWritePath;
virPidFileDelete; virPidFileDelete;
......
...@@ -217,40 +217,17 @@ networkFindActiveConfigs(struct network_driver *driver) { ...@@ -217,40 +217,17 @@ networkFindActiveConfigs(struct network_driver *driver) {
/* Try and read dnsmasq/radvd pids if any */ /* Try and read dnsmasq/radvd pids if any */
if (obj->def->ips && (obj->def->nips > 0)) { if (obj->def->ips && (obj->def->nips > 0)) {
char *pidpath, *radvdpidbase; char *radvdpidbase;
if (virPidFileRead(NETWORK_PID_DIR, obj->def->name, ignore_value(virPidFileReadIfAlive(NETWORK_PID_DIR, obj->def->name,
&obj->dnsmasqPid) == 0) { &obj->dnsmasqPid, DNSMASQ));
/* Check that it's still alive */
if (kill(obj->dnsmasqPid, 0) != 0)
obj->dnsmasqPid = -1;
if (virAsprintf(&pidpath, "/proc/%d/exe", obj->dnsmasqPid) < 0) {
virReportOOMError();
goto cleanup;
}
if (virFileLinkPointsTo(pidpath, DNSMASQ) == 0)
obj->dnsmasqPid = -1;
VIR_FREE(pidpath);
}
if (!(radvdpidbase = networkRadvdPidfileBasename(obj->def->name))) { if (!(radvdpidbase = networkRadvdPidfileBasename(obj->def->name))) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
if (virPidFileRead(NETWORK_PID_DIR, radvdpidbase, ignore_value(virPidFileReadIfAlive(NETWORK_PID_DIR, radvdpidbase,
&obj->radvdPid) == 0) { &obj->radvdPid, RADVD));
/* Check that it's still alive */
if (kill(obj->radvdPid, 0) != 0)
obj->radvdPid = -1;
if (virAsprintf(&pidpath, "/proc/%d/exe", obj->radvdPid) < 0) {
virReportOOMError();
VIR_FREE(radvdpidbase);
goto cleanup;
}
if (virFileLinkPointsTo(pidpath, RADVD) == 0)
obj->radvdPid = -1;
VIR_FREE(pidpath);
}
VIR_FREE(radvdpidbase); VIR_FREE(radvdpidbase);
} }
} }
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <config.h> #include <config.h>
#include <fcntl.h> #include <fcntl.h>
#include <signal.h>
#include "virpidfile.h" #include "virpidfile.h"
#include "virfile.h" #include "virfile.h"
...@@ -164,6 +165,99 @@ int virPidFileRead(const char *dir, ...@@ -164,6 +165,99 @@ int virPidFileRead(const char *dir,
} }
/**
* 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.
*
* Returns -errno upon error, or zero on successful
* 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,
const char *binpath)
{
int rc;
char *procpath = NULL;
rc = virPidFileReadPath(path, pid);
if (rc < 0)
return rc;
/* Check that it's still alive */
if (kill(*pid, 0) < 0) {
*pid = -1;
return 0;
}
if (virAsprintf(&procpath, "/proc/%d/exe", *pid) < 0) {
*pid = -1;
return 0;
}
#ifdef __linux__
if (virFileLinkPointsTo(procpath, binpath) == 0)
*pid = -1;
#endif
VIR_FREE(procpath);
return 0;
}
/**
* 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.
*
* Returns -errno upon error, or zero on successful
* 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)
{
int rc = 0;
char *pidfile = NULL;
if (name == NULL || dir == NULL) {
rc = -EINVAL;
goto cleanup;
}
if (!(pidfile = virPidFileBuildPath(dir, name))) {
rc = -ENOMEM;
goto cleanup;
}
rc = virPidFileReadPathIfAlive(pidfile, pid, binpath);
cleanup:
VIR_FREE(pidfile);
return rc;
}
int virPidFileDeletePath(const char *pidfile) int virPidFileDeletePath(const char *pidfile)
{ {
int rc = 0; int rc = 0;
......
...@@ -42,6 +42,14 @@ int virPidFileRead(const char *dir, ...@@ -42,6 +42,14 @@ int virPidFileRead(const char *dir,
const char *name, const char *name,
pid_t *pid) ATTRIBUTE_RETURN_CHECK; pid_t *pid) ATTRIBUTE_RETURN_CHECK;
int virPidFileReadPathIfAlive(const char *path,
pid_t *pid,
const char *binpath) ATTRIBUTE_RETURN_CHECK;
int virPidFileReadIfAlive(const char *dir,
const char *name,
pid_t *pid,
const char *binpath) ATTRIBUTE_RETURN_CHECK;
int virPidFileDeletePath(const char *path); int virPidFileDeletePath(const char *path);
int virPidFileDelete(const char *dir, int virPidFileDelete(const char *dir,
const char *name); const char *name);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册