RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cmdline.cpp
Go to the documentation of this file.
1 /*
2  The Real-Time eXperiment Interface (RTXI)
3  Copyright (C) 2011 Georgia Institute of Technology, University of Utah, Weill Cornell Medical College
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 */
19 
20 #include <unistd.h>
21 #include <sys/wait.h>
22 #include <debug.h>
23 #include <cmdline.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 CmdLine::CmdLine(void):done(false), mutex(Mutex::RECURSIVE)
28 {
29 
30  pipe(fdm);
31  pipe(fds);
32 
33  if (!(child = fork()))
34  {
35  size_t size;
36 
37  read(fds[0],&size,sizeof(size));
38  while (size)
39  {
40  int retval;
41  char cmd[size];
42 
43  read(fds[0],cmd,size);
44  DEBUG_MSG("executing : \"%s\"\n",cmd);
45  retval = system(cmd);
46 
47  write(fdm[1],&retval,sizeof(retval));
48 
49  read(fds[0],&size,sizeof(size));
50  }
51 
52  _exit(0);
53  }
54 }
55 
57 {
58  int zero = 0;
59  write(fds[1],&zero,sizeof(zero));
60  waitpid(child,0,0);
61 }
62 
63 int CmdLine::execute(const std::string &cmd)
64 {
65  size_t size = strlen(cmd.c_str())+1;
66 
67  write(fds[1],&size,sizeof(size));
68  write(fds[1],cmd.c_str(),size);
69 
70  int retval;
71  read(fdm[0],&retval,sizeof(retval));
72 
73  return retval;
74 }
75 
76 static Mutex mutex;
78 
80 {
81  if (instance)
82  return instance;
83 
84  Mutex::Locker lock(&::mutex);
85  if (!instance)
86  {
87  static CmdLine cmdline;
88  instance = &cmdline;
89  }
90 
91  return instance;
92 }
static CmdLine * getInstance(void)
Definition: cmdline.cpp:79
pid_t child
Definition: cmdline.h:50
Definition: mutex.h:28
static CmdLine * instance
Definition: cmdline.h:45
CmdLine(void)
Definition: cmdline.cpp:27
int fds[2]
Definition: cmdline.h:53
~CmdLine(void)
Definition: cmdline.cpp:56
int execute(const std::string &)
Definition: cmdline.cpp:63
#define DEBUG_MSG(fmt, args...)
Prints debug messages to standard error.
Definition: debug.h:53
int fdm[2]
Definition: cmdline.h:52