return max_len;
}
+static void config_error(Monitor *mon, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ if (mon) {
+ monitor_vprintf(mon, fmt, ap);
+ } else {
+ fprintf(stderr, "qemu: ");
+ vfprintf(stderr, fmt, ap);
+ exit(1);
+ }
+ va_end(ap);
+}
+
#if defined(CONFIG_SLIRP)
/* slirp network adapter */
{
int is_udp;
char buf[256], *r;
- const char *p, *errmsg;
+ const char *p;
struct in_addr guest_addr;
int host_port, guest_port;
goto fail_syntax;
if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
- errmsg = "could not set up redirection\n";
- goto fail;
+ config_error(mon, "could not set up redirection '%s'\n", redir_str);
}
return;
fail_syntax:
- errmsg = "invalid redirection format\n";
- fail:
- if (mon) {
- monitor_printf(mon, "%s", errmsg);
- } else {
- fprintf(stderr, "qemu: %s", errmsg);
- exit(1);
- }
+ config_error(mon, "invalid redirection format '%s'\n", redir_str);
}
#ifndef _WIN32
qemu_free(s);
}
-static int net_dump_init(VLANState *vlan, const char *device,
+static int net_dump_init(Monitor *mon, VLANState *vlan, const char *device,
const char *name, const char *filename, int len)
{
struct pcap_file_hdr hdr;
s->fd = open(filename, O_CREAT | O_WRONLY, 0644);
if (s->fd < 0) {
- fprintf(stderr, "-net dump: can't open %s\n", filename);
+ config_error(mon, "-net dump: can't open %s\n", filename);
return -1;
}
hdr.linktype = 1;
if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
- perror("-net dump write error");
+ config_error(mon, "-net dump write error: %s\n", strerror(errno));
close(s->fd);
qemu_free(s);
return -1;
exit(exit_status);
}
-int net_client_init(const char *device, const char *p)
+int net_client_init(Monitor *mon, const char *device, const char *p)
{
static const char * const fd_params[] = {
"vlan", "name", "fd", NULL
vlan = qemu_find_vlan(vlan_id);
if (get_param_value(buf, sizeof(buf), "name", p)) {
- name = strdup(buf);
+ name = qemu_strdup(buf);
}
if (!strcmp(device, "nic")) {
static const char * const nic_params[] = {
int idx = nic_get_free_idx();
if (check_params(buf, sizeof(buf), nic_params, p) < 0) {
- fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
- buf, p);
- return -1;
+ config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
+ ret = -1;
+ goto out;
}
if (idx == -1 || nb_nics >= MAX_NICS) {
- fprintf(stderr, "Too Many NICs\n");
+ config_error(mon, "Too Many NICs\n");
ret = -1;
goto out;
}
if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
if (parse_macaddr(macaddr, buf) < 0) {
- fprintf(stderr, "invalid syntax for ethernet address\n");
+ config_error(mon, "invalid syntax for ethernet address\n");
ret = -1;
goto out;
}
} else
if (!strcmp(device, "none")) {
if (*p != '\0') {
- fprintf(stderr, "qemu: 'none' takes no parameters\n");
- return -1;
+ config_error(mon, "'none' takes no parameters\n");
+ ret = -1;
+ goto out;
}
/* does nothing. It is needed to signal that no network cards
are wanted */
"vlan", "name", "hostname", "restrict", "ip", NULL
};
if (check_params(buf, sizeof(buf), slirp_params, p) < 0) {
- fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
- buf, p);
- return -1;
+ config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
+ ret = -1;
+ goto out;
}
if (get_param_value(buf, sizeof(buf), "hostname", p)) {
pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
port = strtol(p, &devname, 10);
devname++;
if (port < 1 || port > 65535) {
- fprintf(stderr, "vmchannel wrong port number\n");
+ config_error(mon, "vmchannel wrong port number\n");
ret = -1;
goto out;
}
snprintf(name, 20, "vmchannel%ld", port);
vmc->hd = qemu_chr_open(name, devname, NULL);
if (!vmc->hd) {
- fprintf(stderr, "qemu: could not open vmchannel device"
- "'%s'\n", devname);
+ config_error(mon, "could not open vmchannel device '%s'\n",
+ devname);
ret = -1;
goto out;
}
char ifname[64];
if (check_params(buf, sizeof(buf), tap_params, p) < 0) {
- fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
- buf, p);
- return -1;
+ config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
+ ret = -1;
+ goto out;
}
if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
- fprintf(stderr, "tap: no interface name\n");
+ config_error(mon, "tap: no interface name\n");
ret = -1;
goto out;
}
vlan->nb_host_devs++;
if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
- fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
- chkbuf, p);
- return -1;
+ config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
+ ret = -1;
+ goto out;
}
fd = strtol(buf, NULL, 0);
fcntl(fd, F_SETFL, O_NONBLOCK);
"vlan", "name", "ifname", "script", "downscript", NULL
};
if (check_params(chkbuf, sizeof(chkbuf), tap_params, p) < 0) {
- fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
- chkbuf, p);
- return -1;
+ config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
+ ret = -1;
+ goto out;
}
if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
ifname[0] = '\0';
if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
int fd;
if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
- fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
- chkbuf, p);
- return -1;
+ config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
+ ret = -1;
+ goto out;
}
fd = strtol(buf, NULL, 0);
ret = -1;
"vlan", "name", "listen", NULL
};
if (check_params(chkbuf, sizeof(chkbuf), listen_params, p) < 0) {
- fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
- chkbuf, p);
- return -1;
+ config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
+ ret = -1;
+ goto out;
}
ret = net_socket_listen_init(vlan, device, name, buf);
} else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
"vlan", "name", "connect", NULL
};
if (check_params(chkbuf, sizeof(chkbuf), connect_params, p) < 0) {
- fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
- chkbuf, p);
- return -1;
+ config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
+ ret = -1;
+ goto out;
}
ret = net_socket_connect_init(vlan, device, name, buf);
} else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
"vlan", "name", "mcast", NULL
};
if (check_params(chkbuf, sizeof(chkbuf), mcast_params, p) < 0) {
- fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
- chkbuf, p);
- return -1;
+ config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
+ ret = -1;
+ goto out;
}
ret = net_socket_mcast_init(vlan, device, name, buf);
} else {
- fprintf(stderr, "Unknown socket options: %s\n", p);
+ config_error(mon, "Unknown socket options: %s\n", p);
ret = -1;
goto out;
}
int vde_port, vde_mode;
if (check_params(buf, sizeof(buf), vde_params, p) < 0) {
- fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
- buf, p);
- return -1;
+ config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
+ ret = -1;
+ goto out;
}
vlan->nb_host_devs++;
if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
if (!get_param_value(buf, sizeof(buf), "file", p)) {
snprintf(buf, sizeof(buf), "qemu-vlan%d.pcap", vlan_id);
}
- ret = net_dump_init(vlan, device, name, buf, len);
+ ret = net_dump_init(mon, vlan, device, name, buf, len);
} else {
- fprintf(stderr, "Unknown network device: %s\n", device);
+ config_error(mon, "Unknown network device: %s\n", device);
ret = -1;
goto out;
}
if (ret < 0) {
- fprintf(stderr, "Could not initialize device '%s'\n", device);
+ config_error(mon, "Could not initialize device '%s'\n", device);
}
out:
- if (name)
- free(name);
+ qemu_free(name);
return ret;
}
monitor_printf(mon, "invalid host network device %s\n", device);
return;
}
- if (net_client_init(device, opts ? opts : "") < 0) {
+ if (net_client_init(mon, device, opts ? opts : "") < 0) {
monitor_printf(mon, "adding host network device %s failed\n", device);
}
}
if (*p == ',')
p++;
- return net_client_init(device, p);
+ return net_client_init(NULL, device, p);
}
void do_info_network(Monitor *mon)