#endif
-void console_print(struct consfront_dev *dev, char *data, int length)
+void console_print(struct consfront_dev *dev, const char *data, int length)
{
char *curr_char, saved_char;
char copied_str[length+1];
{
if (dev) free_consfront(dev);
}
+
+#ifdef HAVE_LIBC
+static int consfront_read(struct file *file, void *buf, size_t nbytes)
+{
+ int ret;
+ DEFINE_WAIT(w);
+
+ while ( 1 )
+ {
+ add_waiter(w, console_queue);
+ ret = xencons_ring_recv(file->dev, buf, nbytes);
+ if ( ret )
+ break;
+ schedule();
+ }
+
+ remove_waiter(w, console_queue);
+
+ return ret;
+}
+
+static int savefile_write(struct file *file, const void *buf, size_t nbytes)
+{
+ int ret = 0, tot = nbytes;
+
+ while ( nbytes > 0 )
+ {
+ ret = xencons_ring_send(file->dev, buf, nbytes);
+ nbytes -= ret;
+ buf = (char *)buf + ret;
+ }
+
+ return tot - nbytes;
+}
+
+static int console_write(struct file *file, const void *buf, size_t nbytes)
+{
+ console_print(file->dev, buf, nbytes);
+
+ return nbytes;
+}
+
+static int consfront_close_fd(struct file *file)
+{
+ fini_consfront(file->dev);
+
+ return 0;
+}
+
+static int consfront_fstat(struct file *file, struct stat *buf)
+{
+ buf->st_mode = S_IRUSR | S_IWUSR;
+ buf->st_mode |= (file->type == FTYPE_CONSOLE) ? S_IFCHR : S_IFREG;
+ buf->st_atime = buf->st_mtime = buf->st_ctime = time(NULL);
+
+ return 0;
+}
+
+static bool consfront_select_rd(struct file *file)
+{
+ return xencons_ring_avail(file->dev);
+}
+
+static const struct file_ops savefile_ops = {
+ .name = "savefile",
+ .read = consfront_read,
+ .write = savefile_write,
+ .close = consfront_close_fd,
+ .fstat = consfront_fstat,
+ .select_rd = consfront_select_rd,
+ .select_wr = select_yes,
+};
+
+const struct file_ops console_ops = {
+ .name = "console",
+ .read = consfront_read,
+ .write = console_write,
+ .close = consfront_close_fd,
+ .fstat = consfront_fstat,
+ .select_rd = consfront_select_rd,
+ .select_wr = select_yes,
+};
+
+static unsigned int ftype_savefile;
+
+__attribute__((constructor))
+static void consfront_initialize(void)
+{
+ ftype_savefile = alloc_file_type(&savefile_ops);
+}
+
+int open_consfront(char *nodename)
+{
+ struct consfront_dev *dev;
+ struct file *file;
+
+ dev = init_consfront(nodename);
+ if ( !dev )
+ return -1;
+
+ dev->fd = alloc_fd(nodename ? ftype_savefile : FTYPE_CONSOLE);
+ file = get_file_from_fd(dev->fd);
+ if ( !file )
+ {
+ fini_consfront(dev);
+ return -1;
+ }
+ file->dev = dev;
+
+ return dev->fd;
+}
+#endif
{
struct consfront_dev *dev = (struct consfront_dev *) data;
#ifdef HAVE_LIBC
- int fd = dev ? dev->fd : -1;
+ struct file *file = dev ? get_file_from_fd(dev->fd) : NULL;
- if (fd != -1)
- files[fd].read = true;
+ if ( file )
+ file->read = true;
wake_up(&console_queue);
#else
#include <mini-os/os.h>
#include <mini-os/traps.h>
#include <mini-os/types.h>
+#include <mini-os/lib.h>
#include <xen/grant_table.h>
#include <xenbus.h>
#include <xen/io/console.h>
void get_console(void *p);
void init_console(void);
-void console_print(struct consfront_dev *dev, char *data, int length);
+void console_print(struct consfront_dev *dev, const char *data, int length);
void fini_consfront(struct consfront_dev *dev);
void suspend_console(void);
void resume_console(void);
int xencons_ring_avail(struct consfront_dev *dev);
int xencons_ring_recv(struct consfront_dev *dev, char *data, unsigned len);
void free_consfront(struct consfront_dev *dev);
+#ifdef HAVE_LIBC
+extern const struct file_ops console_ops;
+int open_consfront(char *nodename);
+#endif
#endif /* _LIB_CONSOLE_H_ */
#define FTYPE_FILE 2
#define FTYPE_SOCKET 3
#define FTYPE_MEM 4
-#define FTYPE_SAVEFILE 5
-#define FTYPE_N 6
+#define FTYPE_N 5
#define FTYPE_SPARE 16
struct file {
static const struct file_ops *file_ops[FTYPE_N + FTYPE_SPARE] = {
[FTYPE_NONE] = &file_ops_none,
+#ifdef CONFIG_CONSFRONT
+ [FTYPE_CONSOLE] = &console_ops,
+#endif
};
unsigned int alloc_file_type(const struct file_ops *ops)
#ifdef CONFIG_CONSFRONT
int posix_openpt(int flags)
{
- struct consfront_dev *dev;
+ int fd;
/* Ignore flags */
+ fd = open_consfront(NULL);
+ printk("fd(%d) = posix_openpt\n", fd);
- dev = init_consfront(NULL);
- dev->fd = alloc_fd(FTYPE_CONSOLE);
- files[dev->fd].dev = dev;
-
- printk("fd(%d) = posix_openpt\n", dev->fd);
- return(dev->fd);
+ return fd;
}
int open_savefile(const char *path, int save)
{
- struct consfront_dev *dev;
+ int fd;
char nodename[64];
snprintf(nodename, sizeof(nodename), "device/console/%d", save ? SAVE_CONSOLE : RESTORE_CONSOLE);
- dev = init_consfront(nodename);
- dev->fd = alloc_fd(FTYPE_SAVEFILE);
- files[dev->fd].dev = dev;
+ fd = open_consfront(nodename);
+ printk("fd(%d) = open_savefile\n", fd);
- printk("fd(%d) = open_savefile\n", dev->fd);
- return(dev->fd);
+ return fd;
}
#else
int posix_openpt(int flags)
return ops->read(file, buf, nbytes);
switch (file->type) {
- case FTYPE_SAVEFILE:
- case FTYPE_CONSOLE: {
- int ret;
- DEFINE_WAIT(w);
- while(1) {
- add_waiter(w, console_queue);
- ret = xencons_ring_recv(files[fd].dev, buf, nbytes);
- if (ret)
- break;
- schedule();
- }
- remove_waiter(w, console_queue);
- return ret;
- }
#ifdef HAVE_LWIP
case FTYPE_SOCKET:
return lwip_read(files[fd].fd, buf, nbytes);
return ops->write(file, buf, nbytes);
switch (file->type) {
- case FTYPE_SAVEFILE: {
- int ret = 0, tot = nbytes;
- while (nbytes > 0) {
- ret = xencons_ring_send(files[fd].dev, (char *)buf, nbytes);
- nbytes -= ret;
- buf = (char *)buf + ret;
- }
- return tot - nbytes;
- }
- case FTYPE_CONSOLE:
- console_print(files[fd].dev, (char *)buf, nbytes);
- return nbytes;
#ifdef HAVE_LWIP
case FTYPE_SOCKET:
return lwip_write(files[fd].fd, (void*) buf, nbytes);
case FTYPE_SOCKET:
res = lwip_close(files[fd].fd);
break;
-#endif
-#ifdef CONFIG_CONSFRONT
- case FTYPE_SAVEFILE:
- case FTYPE_CONSOLE:
- fini_consfront(files[fd].dev);
- break;
#endif
case FTYPE_NONE:
goto error;
return ops->fstat(file, buf);
switch (file->type) {
- case FTYPE_SAVEFILE:
- case FTYPE_CONSOLE:
case FTYPE_SOCKET: {
- if (files[fd].type == FTYPE_CONSOLE)
- buf->st_mode = S_IFCHR|S_IRUSR|S_IWUSR;
- else if (files[fd].type == FTYPE_SOCKET)
- buf->st_mode = S_IFSOCK|S_IRUSR|S_IWUSR;
- else if (files[fd].type == FTYPE_SAVEFILE)
- buf->st_mode = S_IFREG|S_IRUSR|S_IWUSR;
+ buf->st_mode = S_IFSOCK|S_IRUSR|S_IWUSR;
buf->st_uid = 0;
buf->st_gid = 0;
buf->st_size = 0;
#if defined(LIBC_DEBUG) || defined(LIBC_VERBOSE)
static const char *const file_types[] = {
[FTYPE_NONE] = "none",
- [FTYPE_CONSOLE] = "console",
[FTYPE_SOCKET] = "socket",
};
}
if (FD_ISSET(i, readfds) || FD_ISSET(i, writefds) || FD_ISSET(i, exceptfds))
+ {
printk("bogus fd %d in select\n", i);
- /* Fallthrough. */
- }
-
- case FTYPE_CONSOLE:
- if (FD_ISSET(i, readfds)) {
- if (xencons_ring_avail(files[i].dev))
- n++;
- else
- FD_CLR(i, readfds);
+ if ( FD_ISSET(i, readfds) )
+ FD_CLR(i, readfds);
+ if ( FD_ISSET(i, writefds) )
+ FD_CLR(i, writefds);
+ if ( FD_ISSET(i, exceptfds) )
+ FD_CLR(i, exceptfds);
}
- if (FD_ISSET(i, writefds))
- n++;
- FD_CLR(i, exceptfds);
break;
+ }
+
#ifdef HAVE_LWIP
case FTYPE_SOCKET:
if (FD_ISSET(i, readfds)) {