small change in doc; some files renamed: umts_backend -> umtsd
[planetlab-umts-tools.git] / frontend / umts.c
1 #define _XOPEN_SOURCE
2 #define _XOPEN_SOURCE_EXTENDED
3 #define _SVID_SOURCE
4 #define _GNU_SOURCE
5 #include <stdio.h>
6 #include <errno.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <sys/syscall.h>
12 #include <sys/wait.h>
13 #include <sys/time.h>
14 #include <sys/select.h>
15 #include <sys/resource.h>
16 #include <sys/mount.h>
17 #include <sys/vfs.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <sched.h>
21 #include <stdarg.h>
22 #include <dirent.h>
23
24 #include <sys/file.h>
25 #include <errno.h>
26
27
28 #include "umts.h"
29
30 // vsys frontend
31
32 //#define DEBUG
33
34 int vfd0, vfd1;
35 //char *inf,*outf;
36 int i = 0;
37 //int c=0;
38
39 #define BUFSIZE 2048
40
41 char lineread[BUFSIZE];
42 char command[BUFSIZE];
43
44 char * lock_file="/var/run/umts_lock";
45
46 int ret;
47
48 char * vsys_in="/vsys/umtsd.in";
49 char * vsys_out="/vsys/umtsd.out";
50
51 fd_set set;
52 char lineread[BUFSIZE];
53
54 int main(int argc, char **argv, char **envp){
55    
56     if (argc < 2) {
57         printf("Usage: umts <cmd> [ argument ]\n");
58         exit(1);
59     } else {
60
61
62         strcpy(command, argv[1]);
63     
64         if (argc > 2){
65                 strcat(command, " ");
66                 strcat(command, argv[2]);
67         }
68
69         strcat(command, "\n");
70
71         int lock_fd=open(lock_file,O_WRONLY|O_CREAT);
72
73         if (lock_fd==-1){
74                 printf("Error in creating lock file %s. Are you root of the slice?\n", lock_file);
75                 exit(1);
76         }
77
78
79         if (flock(lock_fd, LOCK_EX |  LOCK_NB)){
80                 if (errno == EWOULDBLOCK){
81                         printf("An operation is already being performed.\n");
82                         close(lock_fd);
83                         exit(1);
84                 } else if (errno == EBADF){
85                         printf("Error in lock file: 1\n");
86                         close(lock_fd);
87                         exit(1);
88                 } else {
89                         printf("Error in lock file: 2\n");
90                         close(lock_fd);
91                         exit(1);
92                 }
93         }
94
95         vfd0 = open(vsys_out, O_RDONLY | O_NONBLOCK);
96         #ifdef DEBUG
97         printf("Opened %s\n", vsys_out);
98         #endif
99  
100         vfd1 = open(vsys_in, O_WRONLY);
101         #ifdef DEBUG
102         printf("Opened %s\n", vsys_in);
103         #endif
104
105
106         if (vfd0 == -1 || vfd1 == -1) {
107                 printf("Error opening vsys umts entry.\n");
108                 exit(1);
109         }
110
111         write(vfd1, command, strlen(command));  
112         
113         FD_ZERO(&set);
114
115         while(1){
116                 FD_SET(vfd0, &set);
117                 ret = select(vfd0+1, &set, NULL, NULL, NULL);
118                 ret=read(vfd0,lineread,2048);
119                 if (strstr(lineread,"EOF")) break;
120                 //if (ret==0) break;
121                 write(1,lineread,ret);
122                 fflush(stdout);
123                 FD_CLR(vfd0,&set);
124         };
125
126         close(vfd0);
127         close(vfd1);
128    
129         flock(lock_fd, LOCK_UN);
130         
131         close(lock_fd);
132
133    } 
134
135    return 0;
136 }
137