#include <arpa/inet.h>
#ifdef _BSD
#include <sys/stat.h>
-#ifndef __APPLE__
+#if !defined(__APPLE__) && !defined(__OpenBSD__)
#include <libutil.h>
#endif
+#ifdef __OpenBSD__
+#include <net/if.h>
+#endif
#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
#include <freebsd/stdlib.h>
#else
#endif
#if defined(__linux__) || defined(__sun__)
+
+typedef struct {
+ int fd;
+ int connected;
+ int polling;
+ int read_bytes;
+ QEMUTimer *timer;
+} PtyCharDriver;
+
+static void pty_chr_update_read_handler(CharDriverState *chr);
+static void pty_chr_state(CharDriverState *chr, int connected);
+
+static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
+{
+ PtyCharDriver *s = chr->opaque;
+
+ if (!s->connected) {
+ /* guest sends data, check for (re-)connect */
+ pty_chr_update_read_handler(chr);
+ return 0;
+ }
+ return unix_write(s->fd, buf, len);
+}
+
+static int pty_chr_read_poll(void *opaque)
+{
+ CharDriverState *chr = opaque;
+ PtyCharDriver *s = chr->opaque;
+
+ s->read_bytes = qemu_chr_can_read(chr);
+ return s->read_bytes;
+}
+
+static void pty_chr_read(void *opaque)
+{
+ CharDriverState *chr = opaque;
+ PtyCharDriver *s = chr->opaque;
+ int size, len;
+ uint8_t buf[1024];
+
+ len = sizeof(buf);
+ if (len > s->read_bytes)
+ len = s->read_bytes;
+ if (len == 0)
+ return;
+ size = read(s->fd, buf, len);
+ if ((size == -1 && errno == EIO) ||
+ (size == 0)) {
+ pty_chr_state(chr, 0);
+ return;
+ }
+ if (size > 0) {
+ pty_chr_state(chr, 1);
+ qemu_chr_read(chr, buf, size);
+ }
+}
+
+static void pty_chr_update_read_handler(CharDriverState *chr)
+{
+ PtyCharDriver *s = chr->opaque;
+
+ qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
+ pty_chr_read, NULL, chr);
+ s->polling = 1;
+ /*
+ * Short timeout here: just need wait long enougth that qemu makes
+ * it through the poll loop once. When reconnected we want a
+ * short timeout so we notice it almost instantly. Otherwise
+ * read() gives us -EIO instantly, making pty_chr_state() reset the
+ * timeout to the normal (much longer) poll interval before the
+ * timer triggers.
+ */
+ qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
+}
+
+static void pty_chr_state(CharDriverState *chr, int connected)
+{
+ PtyCharDriver *s = chr->opaque;
+
+ if (!connected) {
+ qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
+ s->connected = 0;
+ s->polling = 0;
+ /* (re-)connect poll interval for idle guests: once per second.
+ * We check more frequently in case the guests sends data to
+ * the virtual device linked to our pty. */
+ qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
+ } else {
+ if (!s->connected)
+ qemu_chr_reset(chr);
+ s->connected = 1;
+ }
+}
+
+void pty_chr_timer(void *opaque)
+{
+ struct CharDriverState *chr = opaque;
+ PtyCharDriver *s = chr->opaque;
+
+ if (s->connected)
+ return;
+ if (s->polling) {
+ /* If we arrive here without polling being cleared due
+ * read returning -EIO, then we are (re-)connected */
+ pty_chr_state(chr, 1);
+ return;
+ }
+
+ /* Next poll ... */
+ pty_chr_update_read_handler(chr);
+}
+
+static void pty_chr_close(struct CharDriverState *chr)
+{
+ PtyCharDriver *s = chr->opaque;
+
+ qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
+ close(s->fd);
+ qemu_free(s);
+}
+
static CharDriverState *qemu_chr_open_pty(void)
{
+ CharDriverState *chr;
+ PtyCharDriver *s;
struct termios tty;
- int master_fd, slave_fd;
+ int slave_fd;
- if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) < 0) {
+ chr = qemu_mallocz(sizeof(CharDriverState));
+ if (!chr)
+ return NULL;
+ s = qemu_mallocz(sizeof(PtyCharDriver));
+ if (!s) {
+ qemu_free(chr);
+ return NULL;
+ }
+
+ if (openpty(&s->fd, &slave_fd, NULL, NULL, NULL) < 0) {
return NULL;
}
/* Set raw attributes on the pty. */
cfmakeraw(&tty);
tcsetattr(slave_fd, TCSAFLUSH, &tty);
+ close(slave_fd);
+
+ fprintf(stderr, "char device redirected to %s\n", ptsname(s->fd));
+
+ chr->opaque = s;
+ chr->chr_write = pty_chr_write;
+ chr->chr_update_read_handler = pty_chr_update_read_handler;
+ chr->chr_close = pty_chr_close;
- fprintf(stderr, "char device redirected to %s\n", ptsname(master_fd));
- return qemu_chr_open_fd(master_fd, master_fd);
+ s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
+
+ return chr;
}
static void tty_serial_init(int fd, int speed,
tcsendbreak(s->fd_in, 1);
}
break;
+ case CHR_IOCTL_SERIAL_GET_TIOCM:
+ {
+ int sarg = 0;
+ int *targ = (int *)arg;
+ ioctl(s->fd_in, TIOCMGET, &sarg);
+ *targ = 0;
+ if (sarg | TIOCM_CTS)
+ *targ |= CHR_TIOCM_CTS;
+ if (sarg | TIOCM_CAR)
+ *targ |= CHR_TIOCM_CAR;
+ if (sarg | TIOCM_DSR)
+ *targ |= CHR_TIOCM_DSR;
+ if (sarg | TIOCM_RI)
+ *targ |= CHR_TIOCM_RI;
+ if (sarg | TIOCM_DTR)
+ *targ |= CHR_TIOCM_DTR;
+ if (sarg | TIOCM_RTS)
+ *targ |= CHR_TIOCM_RTS;
+ }
+ break;
+ case CHR_IOCTL_SERIAL_SET_TIOCM:
+ {
+ int sarg = *(int *)arg;
+ int targ = 0;
+ if (sarg | CHR_TIOCM_DTR)
+ targ |= TIOCM_DTR;
+ if (sarg | CHR_TIOCM_RTS)
+ targ |= TIOCM_RTS;
+ ioctl(s->fd_in, TIOCMSET, &targ);
+ }
+ break;
default:
return -ENOTSUP;
}
return 0;
}
-
-static int net_client_init(const char *str)
+static int net_client_init(const char *device, const char *p)
{
- const char *p;
- char *q;
- char device[64];
char buf[1024];
int vlan_id, ret;
VLANState *vlan;
- p = str;
- q = device;
- while (*p != '\0' && *p != ',') {
- if ((q - device) < sizeof(device) - 1)
- *q++ = *p;
- p++;
- }
- *q = '\0';
- if (*p == ',')
- p++;
vlan_id = 0;
if (get_param_value(buf, sizeof(buf), "vlan", p)) {
vlan_id = strtol(buf, NULL, 0);
return ret;
}
+static int net_client_parse(const char *str)
+{
+ const char *p;
+ char *q;
+ char device[64];
+
+ p = str;
+ q = device;
+ while (*p != '\0' && *p != ',') {
+ if ((q - device) < sizeof(device) - 1)
+ *q++ = *p;
+ p++;
+ }
+ *q = '\0';
+ if (*p == ',')
+ p++;
+
+ return net_client_init(device, p);
+}
+
void do_info_network(void)
{
VLANState *vlan;
bdrv_flags |= BDRV_O_SNAPSHOT;
if (!cache)
bdrv_flags |= BDRV_O_DIRECT;
- if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) {
+ if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
fprintf(stderr, "qemu: could not open disk image %s\n",
file);
return -1;
dev = usb_baum_init();
#endif
} else if (strstart(devname, "net:", &p)) {
- int nicidx = strtoul(p, NULL, 0);
+ int nic = nb_nics;
- if (nicidx >= nb_nics || strcmp(nd_table[nicidx].model, "usb"))
+ if (net_client_init("nic", p) < 0)
return -1;
- dev = usb_net_init(&nd_table[nicidx]);
+ nd_table[nic].model = "usb";
+ dev = usb_net_init(&nd_table[nic]);
} else {
return -1;
}
return -EPERM;
}
-static BlockDriverState *get_bdrv(int index)
-{
- if (index > nb_drives)
- return NULL;
- return drives_table[index].bdrv;
-}
-
static void read_passwords(void)
{
BlockDriverState *bs;
int i;
- for(i = 0; i < 6; i++) {
- bs = get_bdrv(i);
- if (bs)
- qemu_key_check(bs, bdrv_get_device_name(bs));
+ for(i = 0; i < nb_drives; i++) {
+ bs = drives_table[i].bdrv;
+ qemu_key_check(bs, bdrv_get_device_name(bs));
}
}
int optind;
const char *r, *optarg;
CharDriverState *monitor_hd;
+ int has_monitor;
const char *monitor_device;
const char *serial_devices[MAX_SERIAL_PORTS];
int serial_device_index;
}
break;
case QEMU_OPTION_nographic:
- serial_devices[0] = "stdio";
- parallel_devices[0] = "null";
- monitor_device = "stdio";
nographic = 1;
break;
#ifdef CONFIG_CURSES
}
}
+ if (nographic) {
+ if (serial_device_index == 0)
+ serial_devices[0] = "stdio";
+ if (parallel_device_index == 0)
+ parallel_devices[0] = "null";
+ if (strncmp(monitor_device, "vc", 2) == 0)
+ monitor_device = "stdio";
+ }
+
#ifndef _WIN32
if (daemonize) {
pid_t pid;
linux_boot = (kernel_filename != NULL);
net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
- /* XXX: this should not be: some embedded targets just have flash */
if (!linux_boot && net_boot == 0 &&
- nb_drives_opt == 0)
+ !machine->nodisk_ok && nb_drives_opt == 0)
help(1);
if (!linux_boot && *kernel_cmdline != '\0') {
}
for(i = 0;i < nb_net_clients; i++) {
- if (net_client_init(net_clients[i]) < 0)
+ if (net_client_parse(net_clients[i]) < 0)
exit(1);
}
for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
continue;
- if (vlan->nb_guest_devs == 0) {
- fprintf(stderr, "Invalid vlan (%d) with no nics\n", vlan->id);
- exit(1);
- }
+ if (vlan->nb_guest_devs == 0)
+ fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
if (vlan->nb_host_devs == 0)
fprintf(stderr,
"Warning: vlan %d is not connected to host network\n",
}
/* Maintain compatibility with multiple stdio monitors */
+
+ has_monitor = 0;
if (!strcmp(monitor_device,"stdio")) {
for (i = 0; i < MAX_SERIAL_PORTS; i++) {
const char *devname = serial_devices[i];
break;
}
}
+ has_monitor = 1;
}
if (monitor_device) {
monitor_hd = qemu_chr_open(monitor_device);
exit(1);
}
monitor_init(monitor_hd, !nographic);
+ has_monitor = 1;
}
for(i = 0; i < MAX_SERIAL_PORTS; i++) {
}
#endif
+ read_passwords();
+
+ if (has_monitor)
+ monitor_start_input();
+
if (loadvm)
do_loadvm(loadvm);
{
/* XXX: simplify init */
- read_passwords();
if (autostart) {
vm_start();
}