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.


added O_RDWR flag to console file descriptor to allow keyboard input
[palacios.git] / linux_usr / v3_serial.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 static int cons_fd = -1;
25 static pthread_t input_handler;
26
27 void *write_handler(void *val){
28     char read;
29     printf("Write handler active\n");
30     fflush(stdout);
31     while(1){
32         read = getchar();
33         if(write(cons_fd, &read, sizeof(char)) < 0){
34             printf("WRITE ERROR");
35         }
36     }
37 }
38
39
40 int main(int argc, char* argv[]) {
41     int vm_fd;
42     fd_set rset;
43     char *vm_dev = NULL;
44     char *stream; 
45
46     if (argc < 2) {
47         printf("Usage: ./v3_cons vm_device serial_number\n");
48         return -1;
49     }
50
51     vm_dev = argv[1];
52     stream = argv[2];
53
54     vm_fd = open(vm_dev, O_RDONLY);
55     if (vm_fd == -1) {
56         printf("Error opening VM device: %s\n", vm_dev);
57         return -1;
58     }
59
60     cons_fd = ioctl(vm_fd, V3_VM_SERIAL_CONNECT, stream); 
61
62     /* Close the file descriptor.  */ 
63     close(vm_fd); 
64     if (cons_fd < 0) {
65         printf("Error opening stream Console\n");
66         return -1;
67     }
68
69
70     if(pthread_create(&input_handler,0,write_handler,0)){
71         perror("pthread_create");
72         exit(-1);
73     }
74
75
76     while (1) {
77         int ret; 
78         char cons_buf[1024];
79         memset(cons_buf, 0, sizeof(cons_buf));
80         int bytes_read = 0;
81
82         FD_ZERO(&rset);
83         FD_SET(cons_fd, &rset);
84         
85         ret = select(cons_fd + 1, &rset, NULL, NULL, NULL);
86         
87         if (ret == 1) {
88             bytes_read = read(cons_fd, cons_buf, 1024);
89             cons_buf[bytes_read]='\0';
90             printf("%s", cons_buf);
91         } else {
92             printf("v3_cons ERROR: select returned %d\n", ret);
93             return -1;
94         }
95     } 
96
97
98     return 0; 
99 }
100
101