network: Add virNetworkObj Get/Set API's for @dnsmasqPid and @radvdPid

In preparation for making the object private, create/use a couple of API's
to get/set the obj->dnsmasqPid and obj->radvdPid.

NB: Since the pid's can sometimes changed based on intervening functions,
be sure to always fetch the latest value.

Signed-off-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
John Ferlan 2017-05-09 17:22:43 -04:00
parent 4f5e32d305
commit 8489d31cee
4 changed files with 108 additions and 34 deletions

View File

@ -107,6 +107,36 @@ virNetworkObjEndAPI(virNetworkObjPtr *net)
} }
pid_t
virNetworkObjGetDnsmasqPid(virNetworkObjPtr obj)
{
return obj->dnsmasqPid;
}
void
virNetworkObjSetDnsmasqPid(virNetworkObjPtr obj,
pid_t dnsmasqPid)
{
obj->dnsmasqPid = dnsmasqPid;
}
pid_t
virNetworkObjGetRadvdPid(virNetworkObjPtr obj)
{
return obj->radvdPid;
}
void
virNetworkObjSetRadvdPid(virNetworkObjPtr obj,
pid_t radvdPid)
{
obj->radvdPid = radvdPid;
}
virMacMapPtr virMacMapPtr
virNetworkObjGetMacMap(virNetworkObjPtr obj) virNetworkObjGetMacMap(virNetworkObjPtr obj)
{ {

View File

@ -53,6 +53,20 @@ virNetworkObjNew(void);
virMacMapPtr virMacMapPtr
virNetworkObjGetMacMap(virNetworkObjPtr obj); virNetworkObjGetMacMap(virNetworkObjPtr obj);
pid_t
virNetworkObjGetDnsmasqPid(virNetworkObjPtr obj);
void
virNetworkObjSetDnsmasqPid(virNetworkObjPtr obj,
pid_t dnsmasqPid);
pid_t
virNetworkObjGetRadvdPid(virNetworkObjPtr obj);
void
virNetworkObjSetRadvdPid(virNetworkObjPtr obj,
pid_t radvdPid);
void void
virNetworkObjSetMacMap(virNetworkObjPtr obj, virNetworkObjSetMacMap(virNetworkObjPtr obj,
virMacMapPtr macmap); virMacMapPtr macmap);

View File

@ -942,8 +942,10 @@ virNetworkObjDeleteConfig;
virNetworkObjEndAPI; virNetworkObjEndAPI;
virNetworkObjFindByName; virNetworkObjFindByName;
virNetworkObjFindByUUID; virNetworkObjFindByUUID;
virNetworkObjGetDnsmasqPid;
virNetworkObjGetMacMap; virNetworkObjGetMacMap;
virNetworkObjGetPersistentDef; virNetworkObjGetPersistentDef;
virNetworkObjGetRadvdPid;
virNetworkObjListExport; virNetworkObjListExport;
virNetworkObjListForEach; virNetworkObjListForEach;
virNetworkObjListGetNames; virNetworkObjListGetNames;
@ -959,7 +961,9 @@ virNetworkObjRemoveInactive;
virNetworkObjReplacePersistentDef; virNetworkObjReplacePersistentDef;
virNetworkObjSaveStatus; virNetworkObjSaveStatus;
virNetworkObjSetDefTransient; virNetworkObjSetDefTransient;
virNetworkObjSetDnsmasqPid;
virNetworkObjSetMacMap; virNetworkObjSetMacMap;
virNetworkObjSetRadvdPid;
virNetworkObjTaint; virNetworkObjTaint;
virNetworkObjUnrefMacMap; virNetworkObjUnrefMacMap;
virNetworkObjUnsetDefTransient; virNetworkObjUnsetDefTransient;

View File

@ -484,12 +484,15 @@ networkUpdateState(virNetworkObjPtr obj,
/* Try and read dnsmasq/radvd pids of active networks */ /* Try and read dnsmasq/radvd pids of active networks */
if (obj->active && obj->def->ips && (obj->def->nips > 0)) { if (obj->active && obj->def->ips && (obj->def->nips > 0)) {
pid_t radvdPid;
pid_t dnsmasqPid;
char *radvdpidbase; char *radvdpidbase;
ignore_value(virPidFileReadIfAlive(driver->pidDir, ignore_value(virPidFileReadIfAlive(driver->pidDir,
obj->def->name, obj->def->name,
&obj->dnsmasqPid, &dnsmasqPid,
dnsmasqCapsGetBinaryPath(dnsmasq_caps))); dnsmasqCapsGetBinaryPath(dnsmasq_caps)));
virNetworkObjSetDnsmasqPid(obj, dnsmasqPid);
radvdpidbase = networkRadvdPidfileBasename(obj->def->name); radvdpidbase = networkRadvdPidfileBasename(obj->def->name);
if (!radvdpidbase) if (!radvdpidbase)
@ -497,7 +500,8 @@ networkUpdateState(virNetworkObjPtr obj,
ignore_value(virPidFileReadIfAlive(driver->pidDir, ignore_value(virPidFileReadIfAlive(driver->pidDir,
radvdpidbase, radvdpidbase,
&obj->radvdPid, RADVD)); &radvdPid, RADVD));
virNetworkObjSetRadvdPid(obj, radvdPid);
VIR_FREE(radvdpidbase); VIR_FREE(radvdpidbase);
} }
@ -1480,7 +1484,7 @@ networkBuildDhcpDaemonCommandLine(virNetworkDriverStatePtr driver,
char *configstr = NULL; char *configstr = NULL;
char *leaseshelper_path = NULL; char *leaseshelper_path = NULL;
obj->dnsmasqPid = -1; virNetworkObjSetDnsmasqPid(obj, -1);
if (networkDnsmasqConfContents(obj, pidfile, &configstr, if (networkDnsmasqConfContents(obj, pidfile, &configstr,
dctx, dnsmasq_caps) < 0) dctx, dnsmasq_caps) < 0)
@ -1533,6 +1537,7 @@ networkStartDhcpDaemon(virNetworkDriverStatePtr driver,
bool needDnsmasq = false; bool needDnsmasq = false;
virCommandPtr cmd = NULL; virCommandPtr cmd = NULL;
char *pidfile = NULL; char *pidfile = NULL;
pid_t dnsmasqPid;
int ret = -1; int ret = -1;
dnsmasqContext *dctx = NULL; dnsmasqContext *dctx = NULL;
@ -1601,9 +1606,10 @@ networkStartDhcpDaemon(virNetworkDriverStatePtr driver,
* pid * pid
*/ */
ret = virPidFileRead(driver->pidDir, obj->def->name, &obj->dnsmasqPid); ret = virPidFileRead(driver->pidDir, obj->def->name, &dnsmasqPid);
if (ret < 0) if (ret < 0)
goto cleanup; goto cleanup;
virNetworkObjSetDnsmasqPid(obj, dnsmasqPid);
ret = 0; ret = 0;
cleanup: cleanup:
@ -1627,6 +1633,7 @@ networkRefreshDhcpDaemon(virNetworkDriverStatePtr driver,
{ {
int ret = -1; int ret = -1;
size_t i; size_t i;
pid_t dnsmasqPid;
virNetworkIPDefPtr ipdef, ipv4def, ipv6def; virNetworkIPDefPtr ipdef, ipv4def, ipv6def;
dnsmasqContext *dctx = NULL; dnsmasqContext *dctx = NULL;
@ -1635,7 +1642,8 @@ networkRefreshDhcpDaemon(virNetworkDriverStatePtr driver,
return 0; return 0;
/* if there's no running dnsmasq, just start it */ /* if there's no running dnsmasq, just start it */
if (obj->dnsmasqPid <= 0 || (kill(obj->dnsmasqPid, 0) < 0)) dnsmasqPid = virNetworkObjGetDnsmasqPid(obj);
if (dnsmasqPid <= 0 || (kill(dnsmasqPid, 0) < 0))
return networkStartDhcpDaemon(driver, obj); return networkStartDhcpDaemon(driver, obj);
VIR_INFO("Refreshing dnsmasq for network %s", obj->def->bridge); VIR_INFO("Refreshing dnsmasq for network %s", obj->def->bridge);
@ -1676,7 +1684,8 @@ networkRefreshDhcpDaemon(virNetworkDriverStatePtr driver,
if ((ret = dnsmasqSave(dctx)) < 0) if ((ret = dnsmasqSave(dctx)) < 0)
goto cleanup; goto cleanup;
ret = kill(obj->dnsmasqPid, SIGHUP); dnsmasqPid = virNetworkObjGetDnsmasqPid(obj);
ret = kill(dnsmasqPid, SIGHUP);
cleanup: cleanup:
dnsmasqContextFree(dctx); dnsmasqContextFree(dctx);
return ret; return ret;
@ -1694,10 +1703,12 @@ static int
networkRestartDhcpDaemon(virNetworkDriverStatePtr driver, networkRestartDhcpDaemon(virNetworkDriverStatePtr driver,
virNetworkObjPtr obj) virNetworkObjPtr obj)
{ {
pid_t dnsmasqPid = virNetworkObjGetDnsmasqPid(obj);
/* if there is a running dnsmasq, kill it */ /* if there is a running dnsmasq, kill it */
if (obj->dnsmasqPid > 0) { if (dnsmasqPid > 0) {
networkKillDaemon(obj->dnsmasqPid, "dnsmasq", obj->def->name); networkKillDaemon(dnsmasqPid, "dnsmasq", obj->def->name);
obj->dnsmasqPid = -1; virNetworkObjSetDnsmasqPid(obj, -1);
} }
/* now start dnsmasq if it should be started */ /* now start dnsmasq if it should be started */
return networkStartDhcpDaemon(driver, obj); return networkStartDhcpDaemon(driver, obj);
@ -1837,13 +1848,14 @@ networkStartRadvd(virNetworkDriverStatePtr driver,
virNetworkObjPtr obj) virNetworkObjPtr obj)
{ {
dnsmasqCapsPtr dnsmasq_caps = networkGetDnsmasqCaps(driver); dnsmasqCapsPtr dnsmasq_caps = networkGetDnsmasqCaps(driver);
pid_t radvdPid;
char *pidfile = NULL; char *pidfile = NULL;
char *radvdpidbase = NULL; char *radvdpidbase = NULL;
char *configfile = NULL; char *configfile = NULL;
virCommandPtr cmd = NULL; virCommandPtr cmd = NULL;
int ret = -1; int ret = -1;
obj->radvdPid = -1; virNetworkObjSetRadvdPid(obj, -1);
/* Is dnsmasq handling RA? */ /* Is dnsmasq handling RA? */
if (DNSMASQ_RA_SUPPORT(dnsmasq_caps)) { if (DNSMASQ_RA_SUPPORT(dnsmasq_caps)) {
@ -1907,8 +1919,9 @@ networkStartRadvd(virNetworkDriverStatePtr driver,
if (virCommandRun(cmd, NULL) < 0) if (virCommandRun(cmd, NULL) < 0)
goto cleanup; goto cleanup;
if (virPidFileRead(driver->pidDir, radvdpidbase, &obj->radvdPid) < 0) if (virPidFileRead(driver->pidDir, radvdpidbase, &radvdPid) < 0)
goto cleanup; goto cleanup;
virNetworkObjSetRadvdPid(obj, radvdPid);
ret = 0; ret = 0;
cleanup: cleanup:
@ -1927,26 +1940,29 @@ networkRefreshRadvd(virNetworkDriverStatePtr driver,
{ {
dnsmasqCapsPtr dnsmasq_caps = networkGetDnsmasqCaps(driver); dnsmasqCapsPtr dnsmasq_caps = networkGetDnsmasqCaps(driver);
char *radvdpidbase; char *radvdpidbase;
pid_t radvdPid;
/* Is dnsmasq handling RA? */ /* Is dnsmasq handling RA? */
if (DNSMASQ_RA_SUPPORT(dnsmasq_caps)) { if (DNSMASQ_RA_SUPPORT(dnsmasq_caps)) {
virObjectUnref(dnsmasq_caps); virObjectUnref(dnsmasq_caps);
if (obj->radvdPid <= 0) radvdPid = virNetworkObjGetRadvdPid(obj);
if (radvdPid <= 0)
return 0; return 0;
/* radvd should not be running but in case it is */ /* radvd should not be running but in case it is */
if ((networkKillDaemon(obj->radvdPid, "radvd", obj->def->name) >= 0) && if ((networkKillDaemon(radvdPid, "radvd", obj->def->name) >= 0) &&
((radvdpidbase = networkRadvdPidfileBasename(obj->def->name)) ((radvdpidbase = networkRadvdPidfileBasename(obj->def->name))
!= NULL)) { != NULL)) {
virPidFileDelete(driver->pidDir, radvdpidbase); virPidFileDelete(driver->pidDir, radvdpidbase);
VIR_FREE(radvdpidbase); VIR_FREE(radvdpidbase);
} }
obj->radvdPid = -1; virNetworkObjSetRadvdPid(obj, -1);
return 0; return 0;
} }
virObjectUnref(dnsmasq_caps); virObjectUnref(dnsmasq_caps);
/* if there's no running radvd, just start it */ /* if there's no running radvd, just start it */
if (obj->radvdPid <= 0 || (kill(obj->radvdPid, 0) < 0)) radvdPid = virNetworkObjGetRadvdPid(obj);
if (radvdPid <= 0 || (kill(radvdPid, 0) < 0))
return networkStartRadvd(driver, obj); return networkStartRadvd(driver, obj);
if (!virNetworkDefGetIPByIndex(obj->def, AF_INET6, 0)) { if (!virNetworkDefGetIPByIndex(obj->def, AF_INET6, 0)) {
@ -1957,7 +1973,7 @@ networkRefreshRadvd(virNetworkDriverStatePtr driver,
if (networkRadvdConfWrite(driver, obj, NULL) < 0) if (networkRadvdConfWrite(driver, obj, NULL) < 0)
return -1; return -1;
return kill(obj->radvdPid, SIGHUP); return kill(radvdPid, SIGHUP);
} }
@ -1967,21 +1983,22 @@ static int
networkRestartRadvd(virNetworkObjPtr obj) networkRestartRadvd(virNetworkObjPtr obj)
{ {
char *radvdpidbase; char *radvdpidbase;
pid_t radvdPid = virNeworkObjGetRadvdPid(obj);
/* if there is a running radvd, kill it */ /* if there is a running radvd, kill it */
if (obj->radvdPid > 0) { if (radvdPid > 0) {
/* essentially ignore errors from the following two functions, /* essentially ignore errors from the following two functions,
* since there's really no better recovery to be done than to * since there's really no better recovery to be done than to
* just push ahead (and that may be exactly what's needed). * just push ahead (and that may be exactly what's needed).
*/ */
if ((networkKillDaemon(obj->radvdPid, "radvd", if ((networkKillDaemon(radvdPid, "radvd",
obj->def->name) >= 0) && obj->def->name) >= 0) &&
((radvdpidbase = networkRadvdPidfileBasename(obj->def->name)) ((radvdpidbase = networkRadvdPidfileBasename(obj->def->name))
!= NULL)) { != NULL)) {
virPidFileDelete(driver->pidDir, radvdpidbase); virPidFileDelete(driver->pidDir, radvdpidbase);
VIR_FREE(radvdpidbase); VIR_FREE(radvdpidbase);
} }
obj->radvdPid = -1; virNetworkObjSetRadvdPid(obj, -1);
} }
/* now start radvd if it should be started */ /* now start radvd if it should be started */
return networkStartRadvd(obj); return networkStartRadvd(obj);
@ -2265,6 +2282,7 @@ networkStartNetworkVirtual(virNetworkDriverStatePtr driver,
virMacMapPtr macmap; virMacMapPtr macmap;
char *macMapFile = NULL; char *macMapFile = NULL;
int tapfd = -1; int tapfd = -1;
pid_t dnsmasqPid;
/* Check to see if any network IP collides with an existing route */ /* Check to see if any network IP collides with an existing route */
if (networkCheckRouteCollision(obj->def) < 0) if (networkCheckRouteCollision(obj->def) < 0)
@ -2432,9 +2450,10 @@ networkStartNetworkVirtual(virNetworkDriverStatePtr driver,
if (!save_err) if (!save_err)
save_err = virSaveLastError(); save_err = virSaveLastError();
if (obj->dnsmasqPid > 0) { dnsmasqPid = virNetworkObjGetDnsmasqPid(obj);
kill(obj->dnsmasqPid, SIGTERM); if (dnsmasqPid > 0) {
obj->dnsmasqPid = -1; kill(dnsmasqPid, SIGTERM);
virNetworkObjSetDnsmasqPid(obj, -1);
} }
err3: err3:
@ -2477,15 +2496,19 @@ static int
networkShutdownNetworkVirtual(virNetworkDriverStatePtr driver, networkShutdownNetworkVirtual(virNetworkDriverStatePtr driver,
virNetworkObjPtr obj) virNetworkObjPtr obj)
{ {
pid_t radvdPid;
pid_t dnsmasqPid;
if (obj->def->bandwidth) if (obj->def->bandwidth)
virNetDevBandwidthClear(obj->def->bridge); virNetDevBandwidthClear(obj->def->bridge);
virNetworkObjUnrefMacMap(obj); virNetworkObjUnrefMacMap(obj);
if (obj->radvdPid > 0) { radvdPid = virNetworkObjGetRadvdPid(obj);
if (radvdPid > 0) {
char *radvdpidbase; char *radvdpidbase;
kill(obj->radvdPid, SIGTERM); kill(radvdPid, SIGTERM);
/* attempt to delete the pidfile we created */ /* attempt to delete the pidfile we created */
if ((radvdpidbase = networkRadvdPidfileBasename(obj->def->name))) { if ((radvdpidbase = networkRadvdPidfileBasename(obj->def->name))) {
virPidFileDelete(driver->pidDir, radvdpidbase); virPidFileDelete(driver->pidDir, radvdpidbase);
@ -2493,8 +2516,9 @@ networkShutdownNetworkVirtual(virNetworkDriverStatePtr driver,
} }
} }
if (obj->dnsmasqPid > 0) dnsmasqPid = virNetworkObjGetDnsmasqPid(obj);
kill(obj->dnsmasqPid, SIGTERM); if (dnsmasqPid > 0)
kill(dnsmasqPid, SIGTERM);
if (obj->def->mac_specified) { if (obj->def->mac_specified) {
char *macTapIfName = networkBridgeDummyNicName(obj->def->bridge); char *macTapIfName = networkBridgeDummyNicName(obj->def->bridge);
@ -2512,15 +2536,17 @@ networkShutdownNetworkVirtual(virNetworkDriverStatePtr driver,
ignore_value(virNetDevBridgeDelete(obj->def->bridge)); ignore_value(virNetDevBridgeDelete(obj->def->bridge));
/* See if its still alive and really really kill it */ /* See if its still alive and really really kill it */
if (obj->dnsmasqPid > 0 && dnsmasqPid = virNetworkObjGetDnsmasqPid(obj);
(kill(obj->dnsmasqPid, 0) == 0)) if (dnsmasqPid > 0 &&
kill(obj->dnsmasqPid, SIGKILL); (kill(dnsmasqPid, 0) == 0))
obj->dnsmasqPid = -1; kill(dnsmasqPid, SIGKILL);
virNetworkObjSetDnsmasqPid(obj, -1);
if (obj->radvdPid > 0 && radvdPid = virNetworkObjGetRadvdPid(obj);
(kill(obj->radvdPid, 0) == 0)) if (radvdPid > 0 &&
kill(obj->radvdPid, SIGKILL); (kill(radvdPid, 0) == 0))
obj->radvdPid = -1; kill(radvdPid, SIGKILL);
virNetworkObjSetRadvdPid(obj, -1);
return 0; return 0;
} }