diff --git a/src/util/virnetdevveth.c b/src/util/virnetdevveth.c index 25eb282278..e698ce2c83 100644 --- a/src/util/virnetdevveth.c +++ b/src/util/virnetdevveth.c @@ -39,6 +39,19 @@ /* Functions */ +virMutex virNetDevVethCreateMutex; + +static int virNetDevVethCreateMutexOnceInit(void) +{ + if (virMutexInit(&virNetDevVethCreateMutex) < 0) { + virReportSystemError(errno, "%s", _("unable to init mutex")); + return -1; + } + return 0; +} + +VIR_ONCE_GLOBAL_INIT(virNetDevVethCreateMutex); + static int virNetDevVethExists(int devNum) { int ret; @@ -117,6 +130,10 @@ int virNetDevVethCreate(char** veth1, char** veth2) * We might race with other containers, but this is reasonably * unlikely, so don't do too many retries for device creation */ + if (virNetDevVethCreateMutexInitialize() < 0) + return -1; + + virMutexLock(&virNetDevVethCreateMutex); #define MAX_VETH_RETRIES 10 for (i = 0; i < MAX_VETH_RETRIES; i++) { @@ -179,6 +196,7 @@ int virNetDevVethCreate(char** veth1, char** veth2) MAX_VETH_RETRIES); cleanup: + virMutexUnlock(&virNetDevVethCreateMutex); virCommandFree(cmd); VIR_FREE(veth1auto); VIR_FREE(veth2auto);