![]() |
RTXI 1.3
|
00001 /* 00002 Copyright (C) 2011 Georgia Institute of Technology, University of Utah, Weill Cornell Medical College 00003 00004 This program is free software: you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation, either version 3 of the License, or 00007 (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 00017 */ 00018 00019 #include <unistd.h> 00020 #include <sys/wait.h> 00021 #include <debug.h> 00022 #include <cmdline.h> 00023 #include <stdlib.h> 00024 #include <string.h> 00025 00026 CmdLine::CmdLine(void) 00027 : done(false), mutex(Mutex::RECURSIVE) { 00028 00029 pipe(fdm); 00030 pipe(fds); 00031 00032 if(!(child = fork())) { 00033 size_t size; 00034 00035 read(fds[0],&size,sizeof(size)); 00036 while(size) { 00037 int retval; 00038 char cmd[size]; 00039 00040 read(fds[0],cmd,size); 00041 DEBUG_MSG("executing : \"%s\"\n",cmd); 00042 retval = system(cmd); 00043 00044 write(fdm[1],&retval,sizeof(retval)); 00045 00046 read(fds[0],&size,sizeof(size)); 00047 } 00048 00049 _exit(0); 00050 } 00051 } 00052 00053 CmdLine::~CmdLine(void) { 00054 int zero = 0; 00055 write(fds[1],&zero,sizeof(zero)); 00056 00057 waitpid(child,0,0); 00058 } 00059 00060 int CmdLine::execute(const std::string &cmd) 00061 { 00062 size_t size = strlen(cmd.c_str())+1; 00063 00064 write(fds[1],&size,sizeof(size)); 00065 write(fds[1],cmd.c_str(),size); 00066 00067 int retval; 00068 read(fdm[0],&retval,sizeof(retval)); 00069 00070 return retval; 00071 } 00072 00073 static Mutex mutex; 00074 CmdLine *CmdLine::instance = 0; 00075 00076 CmdLine *CmdLine::getInstance(void) { 00077 if(instance) 00078 return instance; 00079 00080 Mutex::Locker lock(&::mutex); 00081 if(!instance) { 00082 static CmdLine cmdline; 00083 instance = &cmdline; 00084 } 00085 00086 return instance; 00087 }