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.


Device File Virtualization Proof of Concept (Kernel+Preload)
[palacios.git] / gears / services / devfile / devfile_guest_fd_tracker.h
1 /* 
2    Device File Virtualization Guest Preload Library Helpers
3
4    (c) Akhil Guliani and William Gross, 2015
5      
6    Adapted from MPI module (c) 2012 Peter Dinda
7
8 */
9
10 #define MAX_DEV_NAME_LENGTH 80
11 #define MAX_DEVICES 100
12
13 //PYTHONSCRIPTBREAK1
14 #define DEV_COUNT 9
15 //PYTHONSCRIPTBREAK2
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <sys/types.h>
20 #include <errno.h>
21
22
23
24 #define MIN(a,b) \
25     ({ __typeof__ (a) _a = (a); \
26      __typeof__ (b) _b = (b); \
27      _a < _b ? _a : _b; })
28
29
30 char *strcpy(char *dest, const char *src)
31 {
32     unsigned i;
33     for (i=0; src[i] != '\0'; ++i)
34         dest[i] = src[i];
35     dest[i] = '\0';
36     return dest;
37 }
38
39 size_t strlen(const char * str)
40 {
41     const char *s;
42     for (s = str; *s; ++s) {}
43     return(s - str);
44 }
45
46
47 //taken from: https://stuff.mit.edu/afs/sipb/project/tcl80/src/tcl8.0/compat/strstr.c
48 char* strstr(const char* string,char* substring)
49     //    register char *string;  /* String to search. */
50     //    char *substring;        /* Substring to try to find in string. */
51 {
52     register char *a, *b;
53
54     /* First scan quickly through the two strings looking for a
55      *      * single-character match.  When it's found, then compare the
56      *           * rest of the substring.
57      *                */
58
59     b = substring;
60     if (*b == 0) {
61         return (char*)string;
62     }
63     for ( ; *string != 0; string += 1) {
64         if (*string != *b) {
65             continue;
66         }
67         a = (char*)string;
68         while (1) {
69             if (*b == 0) {
70                 return (char*)string;
71             }
72             if (*a++ != *b++) {
73                 break;
74             }
75         }
76         b = substring;
77     }
78     return (char *) 0;
79 }
80
81
82 //Used to keep track of the active file descriptor for the supported devices
83 //Initially the fd is -1 to indicate that the device has not yet been opened
84 typedef struct dev_file_fd_tracker {
85     char devName[MAX_DEV_NAME_LENGTH];
86     int devFD;
87 } dev_tracker;
88
89
90 dev_tracker dtrack[] = {
91 //PYTHONSCRIPTBREAK3
92 {"/dev/urandom",-1},
93 {"/dev/input/mouse0",-1},
94 {"/dev/input/event0",-1},
95 {"/dev/input/event1",-1},
96 {"/dev/ttyS0",-1},
97 {"/dev/ttyS1",-1},
98 {"/dev/ttyS2",-1},
99 {"/dev/ttyS3",-1},
100 {"/dev/newDevice",-1}
101 //PYTHONSCRIPTBREAK4
102
103 };
104
105 //returns -1, means no match is found
106 //other than -1, is the index in the dtracker array of the match
107 int check_name(const char* path, dev_tracker tracker[]){
108     int i;
109     for(i=0; i<DEV_COUNT; i++){
110         if (strstr(path,tracker[i].devName)!=NULL){
111             return i;
112         }
113     }
114     return -1;
115 }
116 //returns -1, means no match found
117 //other than -1, is the index in the dtracker array of the match
118 int check_fd(int fd, dev_tracker tracker[]){
119     int i;
120     for(i=0; i<DEV_COUNT; i++){
121         if(tracker[i].devFD == fd){
122             return i;
123         }
124     }
125     return -1;
126 }
127
128
129