X-Git-Url: http://git.onelab.eu/?p=planetlab-umts-tools.git;a=blobdiff_plain;f=frontend%2Fumts.c;fp=frontend%2Fumts.c;h=f6c4d87485e1f6164d71c1fe4728590d11b1798b;hp=0000000000000000000000000000000000000000;hb=7abdabb14fe1491268751db2acd6118286d78265;hpb=dda5aa18b81035f203fa53020c372f773e31c690 diff --git a/frontend/umts.c b/frontend/umts.c new file mode 100644 index 0000000..f6c4d87 --- /dev/null +++ b/frontend/umts.c @@ -0,0 +1,135 @@ +#define _XOPEN_SOURCE +#define _XOPEN_SOURCE_EXTENDED +#define _SVID_SOURCE +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +#include "umts.h" + +//#define DEBUG + +int vfd0, vfd1; +//char *inf,*outf; +int i = 0; +//int c=0; + +#define BUFSIZE 2048 + +char lineread[BUFSIZE]; +char command[BUFSIZE]; + +char * lock_file="/var/run/umts_lock"; + +int ret; + +char * vsys_in="/vsys/umts_backend.in"; +char * vsys_out="/vsys/umts_backend.out"; + +fd_set set; +char lineread[BUFSIZE]; + +int main(int argc, char **argv, char **envp){ + + if (argc < 2) { + printf("Usage: umts [ argument ]\n"); + exit(1); + } else { + + + strcpy(command, argv[1]); + + if (argc > 2){ + strcat(command, " "); + strcat(command, argv[2]); + } + + strcat(command, "\n"); + + int lock_fd=open(lock_file,O_WRONLY|O_CREAT); + + if (lock_fd==-1){ + printf("Error in creating lock file %s. Are you root of the slice?\n", lock_file); + exit(1); + } + + + if (flock(lock_fd, LOCK_EX | LOCK_NB)){ + if (errno == EWOULDBLOCK){ + printf("An operation is already being performed"); + close(lock_fd); + exit(1); + } else if (errno == EBADF){ + printf("Error in lock file: 1\n"); + close(lock_fd); + exit(1); + } else { + printf("Error in lock file: 2\n"); + close(lock_fd); + exit(1); + } + } + + vfd0 = open(vsys_out, O_RDONLY | O_NONBLOCK); + #ifdef DEBUG + printf("Opened %s\n", vsys_out); + #endif + + vfd1 = open(vsys_in, O_WRONLY); + #ifdef DEBUG + printf("Opened %s\n", vsys_in); + #endif + + + if (vfd0 == -1 || vfd1 == -1) { + printf("Error opening vsys umts entry.\n"); + exit(1); + } + + write(vfd1, command, strlen(command)); + + FD_ZERO(&set); + + while(1){ + FD_SET(vfd0, &set); + ret = select(vfd0+1, &set, NULL, NULL, NULL); + ret=read(vfd0,lineread,2048); + if (strstr(lineread,"EOF")) break; + //if (ret==0) break; + write(1,lineread,ret); + fflush(stdout); + FD_CLR(vfd0,&set); + }; + + close(vfd0); + close(vfd1); + + flock(lock_fd, LOCK_UN); + + close(lock_fd); + + } + + return 0; +} +