Some file moved and little changes
[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 //#define DEBUG
31
32 int vfd0, vfd1;
33 //char *inf,*outf;
34 int i = 0;
35 //int c=0;
36
37 #define BUFSIZE 2048
38
39 char lineread[BUFSIZE];
40 char command[BUFSIZE];
41
42 char * lock_file="/var/run/umts_lock";
43
44 int ret;
45
46 char * vsys_in="/vsys/umts_backend.in";
47 char * vsys_out="/vsys/umts_backend.out";
48
49 fd_set set;
50 char lineread[BUFSIZE];
51
52 int main(int argc, char **argv, char **envp){
53    
54     if (argc < 2) {
55         printf("Usage: umts <cmd> [ argument ]\n");
56         exit(1);
57     } else {
58
59
60         strcpy(command, argv[1]);
61     
62         if (argc > 2){
63                 strcat(command, " ");
64                 strcat(command, argv[2]);
65         }
66
67         strcat(command, "\n");
68
69         int lock_fd=open(lock_file,O_WRONLY|O_CREAT);
70
71         if (lock_fd==-1){
72                 printf("Error in creating lock file %s. Are you root of the slice?\n", lock_file);
73                 exit(1);
74         }
75
76
77         if (flock(lock_fd, LOCK_EX |  LOCK_NB)){
78                 if (errno == EWOULDBLOCK){
79                         printf("An operation is already being performed.\n");
80                         close(lock_fd);
81                         exit(1);
82                 } else if (errno == EBADF){
83                         printf("Error in lock file: 1\n");
84                         close(lock_fd);
85                         exit(1);
86                 } else {
87                         printf("Error in lock file: 2\n");
88                         close(lock_fd);
89                         exit(1);
90                 }
91         }
92
93         vfd0 = open(vsys_out, O_RDONLY | O_NONBLOCK);
94         #ifdef DEBUG
95         printf("Opened %s\n", vsys_out);
96         #endif
97  
98         vfd1 = open(vsys_in, O_WRONLY);
99         #ifdef DEBUG
100         printf("Opened %s\n", vsys_in);
101         #endif
102
103
104         if (vfd0 == -1 || vfd1 == -1) {
105                 printf("Error opening vsys umts entry.\n");
106                 exit(1);
107         }
108
109         write(vfd1, command, strlen(command));  
110         
111         FD_ZERO(&set);
112
113         while(1){
114                 FD_SET(vfd0, &set);
115                 ret = select(vfd0+1, &set, NULL, NULL, NULL);
116                 ret=read(vfd0,lineread,2048);
117                 if (strstr(lineread,"EOF")) break;
118                 //if (ret==0) break;
119                 write(1,lineread,ret);
120                 fflush(stdout);
121                 FD_CLR(vfd0,&set);
122         };
123
124         close(vfd0);
125         close(vfd1);
126    
127         flock(lock_fd, LOCK_UN);
128         
129         close(lock_fd);
130
131    } 
132
133    return 0;
134 }
135