Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Merge branch 'devel' of newskysaw.cs.northwestern.edu:/home/palacios/palacios into...
[palacios.git] / linux_usr / v3_stream.c
1 /* 
2  * V3 Console utility
3  * (c) Jack lange & Lei Xia, 2010
4  */
5
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <fcntl.h> 
10 #include <sys/ioctl.h> 
11 #include <sys/stat.h> 
12 #include <sys/types.h> 
13 #include <unistd.h>
14 #include <string.h>
15 #include <fcntl.h>
16 #include <pthread.h>
17 #include <errno.h>
18 #include<linux/unistd.h>
19 #include <curses.h>
20
21
22 #include "v3_ctrl.h"
23
24 #define BUF_LEN 1025
25 #define STREAM_NAME_LEN 128
26
27 int main(int argc, char* argv[]) {
28     int vm_fd;
29     fd_set rset;
30     char * vm_dev = NULL;
31     char stream[STREAM_NAME_LEN];
32     char cons_buf[BUF_LEN];
33     int stream_fd = 0;
34
35     if (argc < 2) {
36         printf("Usage: ./v3_cons vm_device serial_number\n");
37         return -1;
38     }
39
40     vm_dev = argv[1];
41
42     if (strlen(argv[2]) >= STREAM_NAME_LEN) {
43         printf("ERROR: Stream name longer than maximum size (%d)\n", STREAM_NAME_LEN);
44         return -1;
45     }
46
47     memcpy(stream, argv[2], strlen(argv[2]));
48
49     vm_fd = open(vm_dev, O_RDONLY);
50     if (vm_fd == -1) {
51         printf("Error opening VM device: %s\n", vm_dev);
52         return -1;
53     }
54
55     stream_fd = ioctl(vm_fd, V3_VM_SERIAL_CONNECT, stream); 
56
57     /* Close the file descriptor.  */ 
58     close(vm_fd);
59  
60     if (stream_fd < 0) {
61         printf("Error opening stream Console\n");
62         return -1;
63     }
64
65     while (1) {
66         int ret; 
67         int bytes_read = 0;
68         char in_buf[512];
69
70         memset(cons_buf, 0, BUF_LEN);
71
72
73         FD_ZERO(&rset);
74         FD_SET(stream_fd, &rset);
75         FD_SET(STDIN_FILENO, &rset);
76
77         ret = select(stream_fd + 1, &rset, NULL, NULL, NULL);
78         
79         if (ret == 0) {
80             continue;
81         } else if (ret == -1) {
82             perror("Select returned error\n");
83             return -1;
84         }
85
86         if (FD_ISSET(stream_fd, &rset)) {
87
88             bytes_read = read(stream_fd, cons_buf, BUF_LEN - 1);
89
90             cons_buf[bytes_read]='\0';
91             printf("%s", cons_buf);
92             fflush(stdout);
93
94         } else if (FD_ISSET(STDIN_FILENO, &rset)) {
95             fgets(in_buf, 512, stdin);
96
97             if (write(stream_fd, in_buf, strlen(in_buf)) != strlen(in_buf)) {
98                 fprintf(stderr, "Error sending input bufer\n");
99                 return -1;
100             }
101         } else {
102             printf("v3_cons ERROR: select returned %d\n", ret);
103             return -1;
104         }
105         
106
107     } 
108
109
110     return 0; 
111 }
112
113