18 #include "ns3/global-value.h"
19 #include "ns3/uinteger.h"
25 GlobalValue
g_timeBase = GlobalValue (
"SimulationTimeBase",
26 "The timebase for this simulation",
28 UintegerValue (1262304000),
29 MakeUintegerChecker<uint32_t> ());
39 return Simulator::GetContext ();
43 std::ostringstream oss;
44 oss <<
"files-" << node;
54 NS_LOG_FUNCTION (
Current () << path);
58 return nodeDir + path;
64 NS_LOG_FUNCTION (
Current () << path);
76 while ((idx = realPath.find (
'/', idx)) >= 0)
85 ::DIR *dir = ::opendir (realPath.c_str ());
90 else if (
errno == ENOENT)
92 int status = ::mkdir (realPath.c_str (), S_IRWXU | S_IRWXG);
95 NS_FATAL_ERROR (
"Could not create directory " << realPath <<
96 ": " << strerror (
errno));
103 NS_LOG_FUNCTION (
Current () << path);
105 std::string::size_type slash = path.find (
"/");
143 int64_t m = time.GetMicroSeconds ();
144 tv.tv_sec = m / 1000000L;
145 tv.tv_usec = m % 1000000L;
151 int64_t n = time.GetNanoSeconds ();
152 tv.tv_sec = (time_t)(n / 1000000000L);
153 tv.tv_nsec = n % 1000000000;
158 UintegerValue uintegerValue;
159 GlobalValue::GetValueByName (
"SimulationTimeBase", uintegerValue);
160 return time + Seconds (uintegerValue.Get ());
164 UintegerValue uintegerValue;
165 GlobalValue::GetValueByName (
"SimulationTimeBase", uintegerValue);
166 return time - Seconds (uintegerValue.Get ());
170 Time time = Seconds (tv.tv_sec);
171 time += MicroSeconds (tv.tv_usec);
178 return Seconds (0.0);
184 Time time = Seconds (tm.tv_sec);
185 time += NanoSeconds (tm.tv_nsec);
192 for (std::vector<Thread *>::iterator i = process->
threads.begin ();
193 i != process->
threads.end (); ++i)
196 if (sigismember (&thread->
signalMask, signum) == 0)
222 if (sigismember (¤t->
signalMask, i->signal) == 1
223 && i->signal != SIGKILL
224 && i->signal != SIGSTOP)
233 NS_LOG_DEBUG (
"deliver signal=" << i->signal);
235 if (i->flags & SA_SIGINFO)
239 i->sigaction (i->signal, &info, &ctx);
243 i->handler (i->signal);
249 NS_LOG_DEBUG (
"deliver signal=" << i->signal);
251 if (i->flags & SA_SIGINFO)
255 i->sigaction (i->signal, &info, &ctx);
259 i->handler (i->signal);
268 NS_LOG_FUNCTION (current);
269 NS_ASSERT (current != 0);
270 std::map<int,FileUsage *>::iterator end = current->
process->
openFiles.end ();
271 std::map<int,FileUsage *>::iterator it = end;
273 for (
int fd = 0; fd <
MAX_FDS; fd++)
278 NS_LOG_DEBUG (
"Allocated fd=" << fd);
293 NS_LOG_DEBUG (
"UtilsAdvanceTime current thread wait 1µs.");
304 time_t realnow = time (0);
305 std::ostringstream oss;
306 oss << ((long)now.GetSeconds ());
307 std::string sec = oss.str ();
308 uint32_t indent = 10;
309 std::string padding =
"";
310 if (sec.length () < indent)
312 padding = std::string (indent - sec.length (),
' ');
320 std::string ns = oss.str ();
321 if (ns.length () < indent)
323 padding = std::string (indent - ns.length (),
' ');
329 oss <<
"NS3 Time: " << sec <<
"s (" << ns <<
") , REAL Time: " << realnow;
333 std::list<std::string>
334 Split (std::string input, std::string sep)
336 std::list<std::string> retval;
337 std::string::size_type cur = 0, next;
340 next = input.find (sep, cur);
346 else if (next == std::string::npos)
348 if (input.size () != cur)
350 retval.push_back (input.substr (cur, input.size () - cur));
354 retval.push_back (input.substr (cur, next - cur));
361 FindExecFile (std::string root, std::string envPath, std::string fileName, uid_t uid, gid_t gid,
int *errNo)
364 std::string found =
"";
367 int idx = fileName.find (
'/', 0);
372 if (0 == ::stat (vFile.c_str (), &st))
379 if (0 == ::stat (fileName.c_str (), &st))
389 std::list<std::string> paths =
Split (envPath,
":");
390 for (std::list<std::string>::const_iterator i = paths.begin (); i != paths.end (); i++)
392 std::string test = root +
"/" + *i +
"/" + fileName;
393 if (0 == ::stat (test.c_str (), &st))
413 return ((gid != st->st_gid) && (uid != st->st_uid) && ((st->st_mode & (S_IROTH | S_IXOTH)) == (S_IROTH | S_IXOTH)))
414 || ((gid == st->st_gid) && (uid != st->st_uid) && ((st->st_mode & (S_IRGRP | S_IXGRP)) == (S_IRGRP | S_IXGRP)))
415 || ((uid == st->st_uid) && ((st->st_mode & (S_IRUSR | S_IXUSR)) == (S_IRUSR | S_IXUSR)));
434 std::map<int,FileUsage *>::iterator it = p->
openFiles.find (fd);
438 return !opened || (!(*it).second->IsClosed ());
445 std::map<int,FileUsage *>::iterator it = current->
process->
openFiles.find (fd);
460 char direc[PATH_MAX + 1];
462 sprintf (proc,
"/proc/self/fd/%d", fd);
464 memset (direc, 0, PATH_MAX + 1);
466 ssize_t r = readlink (proc, direc,
sizeof(direc) - 1);
470 return std::string (direc);
472 return std::string (
"");
476 std::ostringstream &shellName, std::ostringstream &optionalArg)
478 int fd = open (fileName.c_str (), O_RDONLY);
486 ssize_t lg = read (fd, firstLine, 127);
488 if ((lg <= 2) || (
'#' != firstLine [0]) || (
'!' != firstLine [1]))
494 while (
' ' == firstLine [crsr])
502 ssize_t startShellName = crsr;
504 while (firstLine [crsr] && (
' ' != firstLine [crsr]) && (
'\n' != firstLine [crsr]))
512 ssize_t endShellName = crsr;
513 ssize_t lShellName = endShellName - startShellName;
518 shellName << std::string (firstLine + startShellName, lShellName);
523 ssize_t startOpt = crsr;
524 while (firstLine[crsr]&&(
'\n' != firstLine [crsr]))
532 ssize_t lOpt = crsr - startOpt;
537 optionalArg << std::string (firstLine + startOpt, lOpt);
543 int namelen = strlen (name);
545 for (cur = array; cur != 0 && *cur != 0; cur++)
547 char *equal = strchr (*cur,
'=');
552 if (strncmp (*cur, name, namelen) != 0)
563 static std::string pwd =
"";
564 if (pwd.length () > 0)
568 char *thePwd = get_current_dir_name ();
569 int fd = open (thePwd, O_RDONLY);