static int lxcVmCleanup(lxc_driver_t *driver,
virDomainObjPtr vm)
{
- int rc = -1;
+ int rc = 0;
int waitRc;
int childStatus = -1;
virCgroupPtr cgroup;
virReportSystemError(errno,
_("waitpid failed to wait for container %d: %d"),
vm->pid, waitRc);
- }
-
- rc = 0;
-
- if (WIFEXITED(childStatus)) {
- rc = WEXITSTATUS(childStatus);
- DEBUG("container exited with rc: %d", rc);
+ rc = -1;
+ } else if (WIFEXITED(childStatus)) {
+ DEBUG("container exited with rc: %d", WEXITSTATUS(childStatus));
+ rc = -1;
}
/* now that we know it's stopped call the hook if present */
strcpy(parentVeth, def->nets[i]->ifname);
}
DEBUG("parentVeth: %s, containerVeth: %s", parentVeth, containerVeth);
- if (0 != (rc = vethCreate(parentVeth, PATH_MAX, containerVeth, PATH_MAX))) {
- lxcError(VIR_ERR_INTERNAL_ERROR,
- _("Failed to create veth device pair: %d"), rc);
+ if (vethCreate(parentVeth, PATH_MAX, containerVeth, PATH_MAX) < 0)
goto error_exit;
- }
+
if (NULL == def->nets[i]->ifname) {
def->nets[i]->ifname = strdup(parentVeth);
}
{
char macaddr[VIR_MAC_STRING_BUFLEN];
virFormatMacAddr(def->nets[i]->mac, macaddr);
- if (0 != (rc = setMacAddr(containerVeth, macaddr))) {
- virReportSystemError(rc,
- _("Failed to set %s to %s"),
- macaddr, containerVeth);
+ if (setMacAddr(containerVeth, macaddr) < 0)
goto error_exit;
- }
}
if (0 != (rc = brAddInterface(brctl, bridge, parentVeth))) {
virReportSystemError(rc,
_("Failed to add %s device to %s"),
parentVeth, bridge);
+ rc = -1;
goto error_exit;
}
- if (0 != (rc = vethInterfaceUpOrDown(parentVeth, 1))) {
- virReportSystemError(rc,
- _("Failed to enable %s device"),
- parentVeth);
+ if (vethInterfaceUpOrDown(parentVeth, 1) < 0)
goto error_exit;
- }
-
}
rc = 0;
#include <string.h>
#include <stdio.h>
+#include <sys/types.h>
+#include <sys/wait.h>
#include "veth.h"
#include "internal.h"
#include "logging.h"
#include "memory.h"
#include "util.h"
+#include "virterror_internal.h"
+
+#define VIR_FROM_THIS VIR_FROM_LXC
+
+#define vethError(code, ...) \
+ virReportErrorHelper(NULL, VIR_FROM_LXC, code, __FILE__, \
+ __FUNCTION__, __LINE__, __VA_ARGS__)
/* Functions */
/**
int vethCreate(char* veth1, int veth1MaxLen,
char* veth2, int veth2MaxLen)
{
- int rc = -1;
+ int rc;
const char *argv[] = {
"ip", "link", "add", veth1, "type", "veth", "peer", "name", veth2, NULL
};
- int cmdResult;
+ int cmdResult = 0;
int vethDev = 0;
- if ((NULL == veth1) || (NULL == veth2)) {
- goto error_out;
- }
-
DEBUG("veth1: %s veth2: %s", veth1, veth2);
while ((1 > strlen(veth1)) || STREQ(veth1, veth2)) {
DEBUG("veth1: %s veth2: %s", veth1, veth2);
rc = virRun(argv, &cmdResult);
- if (0 == rc) {
- rc = cmdResult;
+ if (rc != 0 ||
+ (WIFEXITED(cmdResult) && WEXITSTATUS(cmdResult) != 0)) {
+ vethError(VIR_ERR_INTERNAL_ERROR,
+ _("Failed to create veth device pair '%s', '%s': %d"),
+ veth1, veth2, WEXITSTATUS(cmdResult));
+ rc = -1;
}
-error_out:
return rc;
}
*/
int vethDelete(const char *veth)
{
- int rc = -1;
+ int rc;
const char *argv[] = {"ip", "link", "del", veth, NULL};
- int cmdResult;
-
- if (NULL == veth) {
- goto error_out;
- }
+ int cmdResult = 0;
DEBUG("veth: %s", veth);
rc = virRun(argv, &cmdResult);
- if (0 == rc) {
- rc = cmdResult;
+ if (rc != 0 ||
+ (WIFEXITED(cmdResult) && WEXITSTATUS(cmdResult) != 0)) {
+ /*
+ * Prevent overwriting an error log which may be set
+ * where an actual failure occurs.
+ */
+ VIR_DEBUG(_("Failed to delete '%s' (%d)"),
+ veth, WEXITSTATUS(cmdResult));
+ rc = -1;
}
-error_out:
return rc;
}
*/
int vethInterfaceUpOrDown(const char* veth, int upOrDown)
{
- int rc = -1;
+ int rc;
const char *argv[] = {"ifconfig", veth, NULL, NULL};
- int cmdResult;
-
- if (NULL == veth) {
- goto error_out;
- }
+ int cmdResult = 0;
if (0 == upOrDown)
argv[2] = "down";
rc = virRun(argv, &cmdResult);
- if (0 == rc) {
- rc = cmdResult;
+ if (rc != 0 ||
+ (WIFEXITED(cmdResult) && WEXITSTATUS(cmdResult) != 0)) {
+ if (0 == upOrDown)
+ /*
+ * Prevent overwriting an error log which may be set
+ * where an actual failure occurs.
+ */
+ VIR_DEBUG(_("Failed to disable '%s' (%d)"),
+ veth, WEXITSTATUS(cmdResult));
+ else
+ vethError(VIR_ERR_INTERNAL_ERROR,
+ _("Failed to enable '%s' (%d)"),
+ veth, WEXITSTATUS(cmdResult));
+ rc = -1;
}
-error_out:
return rc;
}
*/
int moveInterfaceToNetNs(const char* iface, int pidInNs)
{
- int rc = -1;
+ int rc;
char *pid = NULL;
const char *argv[] = {
"ip", "link", "set", iface, "netns", NULL, NULL
};
- int cmdResult;
+ int cmdResult = 0;
- if (NULL == iface) {
- goto error_out;
+ if (virAsprintf(&pid, "%d", pidInNs) == -1) {
+ virReportOOMError();
+ return -1;
}
- if (virAsprintf(&pid, "%d", pidInNs) == -1)
- goto error_out;
-
argv[5] = pid;
rc = virRun(argv, &cmdResult);
- if (0 == rc)
- rc = cmdResult;
+ if (rc != 0 ||
+ (WIFEXITED(cmdResult) && WEXITSTATUS(cmdResult) != 0)) {
+ vethError(VIR_ERR_INTERNAL_ERROR,
+ _("Failed to move '%s' into NS(pid=%d) (%d)"),
+ iface, pidInNs, WEXITSTATUS(cmdResult));
+ rc = -1;
+ }
-error_out:
VIR_FREE(pid);
return rc;
}
*/
int setMacAddr(const char* iface, const char* macaddr)
{
- int rc = -1;
+ int rc;
const char *argv[] = {
"ip", "link", "set", iface, "address", macaddr, NULL
};
- int cmdResult;
-
- if (NULL == iface) {
- goto error_out;
- }
+ int cmdResult = 0;
rc = virRun(argv, &cmdResult);
- if (0 == rc)
- rc = cmdResult;
+ if (rc != 0 ||
+ (WIFEXITED(cmdResult) && WEXITSTATUS(cmdResult) != 0)) {
+ vethError(VIR_ERR_INTERNAL_ERROR,
+ _("Failed to set '%s' to '%s' (%d)"),
+ macaddr, iface, WEXITSTATUS(cmdResult));
+ rc = -1;
+ }
-error_out:
return rc;
}
*/
int setInterfaceName(const char* iface, const char* new)
{
- int rc = -1;
+ int rc;
const char *argv[] = {
"ip", "link", "set", iface, "name", new, NULL
};
- int cmdResult;
-
- if (NULL == iface || NULL == new) {
- goto error_out;
- }
+ int cmdResult = 0;
rc = virRun(argv, &cmdResult);
- if (0 == rc)
- rc = cmdResult;
+ if (rc != 0 ||
+ (WIFEXITED(cmdResult) && WEXITSTATUS(cmdResult) != 0)) {
+ vethError(VIR_ERR_INTERNAL_ERROR,
+ _("Failed to set '%s' to '%s' (%d)"),
+ new, iface, WEXITSTATUS(cmdResult));
+ rc = -1;
+ }
-error_out:
return rc;
}
# define VETH_H
# include <config.h>
+# include "internal.h"
/* Function declarations */
int vethCreate(char* veth1, int veth1MaxLen, char* veth2,
- int veth2MaxLen);
-int vethDelete(const char* veth);
-int vethInterfaceUpOrDown(const char* veth, int upOrDown);
-int moveInterfaceToNetNs(const char *iface, int pidInNs);
-int setMacAddr(const char* iface, const char* macaddr);
-int setInterfaceName(const char* iface, const char* new);
+ int veth2MaxLen)
+ ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
+int vethDelete(const char* veth)
+ ATTRIBUTE_NONNULL(1);
+int vethInterfaceUpOrDown(const char* veth, int upOrDown)
+ ATTRIBUTE_NONNULL(1);
+int moveInterfaceToNetNs(const char *iface, int pidInNs)
+ ATTRIBUTE_NONNULL(1);
+int setMacAddr(const char* iface, const char* macaddr)
+ ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+int setInterfaceName(const char* iface, const char* new)
+ ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
#endif /* VETH_H */