ns-3 Direct Code Execution
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dce-manager.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2008 INRIA
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 version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #ifndef DCE_MANAGER_H
22 #define DCE_MANAGER_H
23 
24 #include <string>
25 #include <map>
26 #include "ns3/object.h"
27 #include "ns3/nstime.h"
28 #include "ns3/traced-callback.h"
29 #include "ns3/simulator.h"
30 #include "task-manager.h"
31 
32 extern "C" struct Libc;
33 
34 namespace ns3 {
35 
36 struct Process;
37 struct Thread;
38 struct SignalHandler;
39 class Loader;
40 
41 
73 class DceManager : public Object
74 {
75 public:
76  typedef Callback<void> Runnable;
77  typedef enum
78  {
80  PEC_NS3_END, // NO MORE EVENTS
81  PEC_NS3_STOP, // STOP AT PREDEFINED TIME
83 
84  static TypeId GetTypeId (void);
85 
86  DceManager ();
87  virtual ~DceManager ();
88 
89  uint16_t Start (std::string name, std::string stdinfilename, std::vector<std::string> args,
90  std::vector<std::pair<std::string,std::string> > envs,
91  uid_t uid, uid_t euid, uid_t gid, uid_t egid);
92  uint16_t Start (std::string name, std::string stdinfilename, uint32_t stackSize,
93  std::vector<std::string> args,
94  std::vector<std::pair<std::string,std::string> > envs,
95  uid_t uid, uid_t euid, uid_t gid, uid_t egid);
96  uint16_t StartInternalTask ();
97  void SetFinishedCallback (uint16_t pid, Callback<void,uint16_t,int> cb);
98  void Stop (uint16_t pid);
99 
100  // internal methods
101  struct Thread * CreateThread (struct Process *process);
102  void DeleteProcess (struct Process *process, ProcessEndCause type);
103  void DeleteThread (struct Thread *thread);
104 
105  Thread * SearchThread (uint16_t pid, uint16_t tid);
106  Process * SearchProcess (uint16_t pid);
107  void FinishChild (uint16_t pid); // A wait success on this proc
108 
109  void Exit (void);
110  void Wakeup (Thread *thread);
111  void Wait (void);
112  Time Wait (Time timeout);
113  void Yield (void);
114  uint16_t Clone (Thread *thread);
115  std::map<uint16_t, Process *> GetProcs ();
116  static void AppendStatusFile (uint16_t pid, uint32_t nodeId, std::string &line);
117  int Execve (const char *path, const char *argv0, char *const argv[], char *const envp[]);
118  // Path used by simulated methods 'execvp' and 'execlp'
119  void SetVirtualPath (std::string p);
120  std::string GetVirtualPath () const;
121  static void AppendProcFile (Process *p);
122  uint16_t StartTemporaryTask ();
123  void StopTemporaryTask (uint16_t pid);
124  void ResumeTemporaryTask (uint16_t pid);
125  void SuspendTemporaryTask (uint16_t pid);
126  struct Process* CreateProcess (std::string name, std::string stdinfilename, std::vector<std::string> args,
127  std::vector<std::pair<std::string,std::string> > envs, int pid);
128 
129 private:
130  // inherited from Object.
131  virtual void DoDispose (void);
132 
133  static int (*PrepareDoStartProcess (Thread * current)) (int, char **);
134  static void DoStartProcess (void *context);
135  bool CheckProcessContext (void) const;
136  uint16_t AllocatePid (void);
137  uint16_t AllocateTid (const struct Process *process) const;
138  static void SigkillHandler (int signal);
139  static void SigabrtHandler (int signal);
140  bool ThreadExists (Thread *thread);
141  static struct ::Libc * GetLibc (void);
142  void SetArgv (struct Process *process, std::string filename, std::vector<std::string> args);
143  void SetEnvp (struct Process *process, std::vector<std::pair<std::string,std::string> > envp);
144  static void EnsureDirectoryExists (struct Thread *current, std::string dirName);
145  static int CreatePidFile (struct Thread *current, std::string prefix);
146  static void TaskSwitch (enum Task::SwitchType type, void *context);
147  static void StartProcessDebugHook (void);
148  void ChildFinished (uint16_t pid);
149  bool WakeupChildWaiters (struct Process *p);
150  // Remove memory used by thread poll table and iowait, remove from wait queues
151  void CleanupThread (struct Thread *thread);
152  std::vector<std::string> CopyArgs (char *const argv[]);
153  int CopyEnv (char *const envp[], std::vector<std::pair<std::string,std::string> > &res);
154  static void* LoadMain (Loader *ld, std::string filename, Process *proc, int &err);
155  static void DoExecProcess (void *c);
156  static void SetDefaultSigHandler (std::vector<SignalHandler> &signalHandlers);
157 
158  std::map<uint16_t, Process *> m_processes; // Key is the pid
159  uint16_t m_nextPid;
160  TracedCallback<uint16_t, int> m_processExit;
161  // If true close stderr and stdout between writes .
163  std::string m_virtualPath;
164 };
165 
166 } // namespace ns3
167 
168 #endif /* DCE_MANAGER_H */