-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathutils.cc
583 lines (546 loc) · 14 KB
/
utils.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#include "utils.h"
#include "dce-manager.h"
#include "unix-fd.h"
#include "process.h"
#include "task-manager.h"
#include "ns3/node.h"
#include "ns3/log.h"
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <list>
#include <fcntl.h>
#include <unistd.h>
#include "file-usage.h"
#include "ns3/global-value.h"
#include "ns3/uinteger.h"
NS_LOG_COMPONENT_DEFINE ("DceProcessUtils");
namespace ns3 {
GlobalValue g_timeBase = GlobalValue ("SimulationTimeBase",
"The timebase for this simulation",
// January 1st, 2010, 00:00:00
UintegerValue (1262304000),
MakeUintegerChecker<uint32_t> ());
// unsigned long secondsSinceEpochOnFridayApril042008 = 1207284276;
// return secondsSinceEpochOnFridayApril042008;
uint32_t UtilsGetNodeId (void)
{
if (gDisposingThreadContext)
{
return gDisposingThreadContext->process->nodeId;
}
return Simulator::GetContext ();
}
static std::string UtilsGetRealFilePath (uint32_t node)
{
std::ostringstream oss;
oss << "files-" << node;
return oss.str ();
}
static std::string UtilsGetRealFilePath (void)
{
return UtilsGetRealFilePath (UtilsGetNodeId ());
}
std::string UtilsGetAbsRealFilePath (uint32_t node, std::string path)
{
NS_LOG_FUNCTION (Current () << path);
std::string nodeDir = UtilsGetRealFilePath (node);
UtilsEnsureDirectoryExists (nodeDir);
return nodeDir + path;
}
std::string
UtilsGetRealFilePath (std::string path)
{
NS_LOG_FUNCTION (Current () << path);
std::string nodeDir = UtilsGetRealFilePath ();
UtilsEnsureDirectoryExists (nodeDir);
return nodeDir + UtilsGetVirtualFilePath (path);
}
void
UtilsEnsureAllDirectoriesExist (std::string realPath)
{
int idx = 0;
while ((idx = realPath.find ('/', idx)) >= 0)
{
UtilsEnsureDirectoryExists (realPath.substr (0, idx + 1));
idx++;
}
}
void UtilsEnsureDirectoryExists (std::string realPath)
{
::DIR *dir = ::opendir (realPath.c_str ());
if (dir != 0)
{
::closedir (dir);
}
else if (errno == ENOENT)
{
int status = ::mkdir (realPath.c_str (), S_IRWXU | S_IRWXG);
if (status == -1)
{
NS_FATAL_ERROR ("Could not create directory " << realPath <<
": " << strerror (errno));
}
}
}
std::string UtilsGetVirtualFilePath (std::string path)
{
NS_LOG_FUNCTION (Current () << path);
std::string::size_type slash = path.find ("/");
if (slash == 0)
{
// path relative to root of node.
return path;
}
else
{
NS_ASSERT (Current () != 0);
Thread *current = Current ();
// path relative to cwd in node
return current->process->cwd + "/" + path;
}
}
Thread *gDisposingThreadContext = 0;
Thread * Current (void)
{
if (gDisposingThreadContext)
{
return gDisposingThreadContext;
}
TaskManager *manager = TaskManager::Current ();
if (manager == 0)
{
return 0;
}
return (struct Thread *)manager->CurrentTask ()->GetContext ();
}
bool HasPendingSignal (void)
{
bool isPending = sigisemptyset (&(Current ()->process->pendingSignals)) == 0;
return isPending;
}
struct timeval UtilsTimeToTimeval (Time time)
{
struct timeval tv;
int64_t m = time.GetMicroSeconds ();
tv.tv_sec = m / 1000000L;
tv.tv_usec = m % 1000000L;
return tv;
}
struct timespec UtilsTimeToTimespec (Time time)
{
struct timespec tv;
int64_t n = time.GetNanoSeconds ();
tv.tv_sec = (time_t)(n / 1000000000L);
tv.tv_nsec = n % 1000000000;
return tv;
}
std::string
UtilsGenerateIfNameFromIndex(uint32_t i) {
std::stringstream ss;
ss << "ns3-device " << i;
return ss.str();
}
Time UtilsSimulationTimeToTime (Time time)
{
UintegerValue uintegerValue;
GlobalValue::GetValueByName ("SimulationTimeBase", uintegerValue);
return time + Seconds (uintegerValue.Get ());
}
Time UtilsTimeToSimulationTime (Time time)
{
UintegerValue uintegerValue;
GlobalValue::GetValueByName ("SimulationTimeBase", uintegerValue);
return time - Seconds (uintegerValue.Get ());
}
Time UtilsTimevalToTime (struct timeval tv)
{
Time time = Seconds (tv.tv_sec);
time += MicroSeconds (tv.tv_usec);
return time;
}
Time UtilsTimevalToTime (const struct timeval *tv)
{
if (tv == 0)
{
return Seconds (0.0);
}
return UtilsTimevalToTime (*tv);
}
Time UtilsTimespecToTime (struct timespec tm)
{
Time time = Seconds (tm.tv_sec);
time += NanoSeconds (tm.tv_nsec);
return time;
}
void
UtilsSendSignal (Process *process, int signum)
{
sigaddset (&process->pendingSignals, signum);
for (std::vector<Thread *>::iterator i = process->threads.begin ();
i != process->threads.end (); ++i)
{
Thread *thread = *i;
if (sigismember (&thread->signalMask, signum) == 0)
{
// signal not blocked by thread.
if (thread->task->IsBlocked ())
{
process->manager->Wakeup (thread);
return;
}
}
}
// Could not find any candidate thread to receive signal.
// signal pending until a thread unblocks it.
}
void UtilsDoSignal (void)
{
Thread *current = Current ();
if (!current)
{
return;
}
// we try to check if we
// have pending signals and we deliver them if we have any.
for (std::vector<SignalHandler>::iterator i = current->process->signalHandlers.begin ();
i != current->process->signalHandlers.end (); ++i)
{
if (sigismember (¤t->signalMask, i->signal) == 1
&& i->signal != SIGKILL
&& i->signal != SIGSTOP)
{
// don't deliver signals which are masked
// ignore the signal mask for SIGKILL and SIGSTOP
// though.
continue;
}
if (sigismember (¤t->pendingSignals, i->signal) == 1)
{
NS_LOG_DEBUG ("deliver signal=" << i->signal);
// the signal is pending so, we try to deliver it.
if (i->flags & SA_SIGINFO)
{
siginfo_t info;
ucontext_t ctx;
i->sigaction (i->signal, &info, &ctx);
}
else
{
i->handler (i->signal);
}
sigdelset (¤t->pendingSignals, i->signal);
}
if (sigismember (¤t->process->pendingSignals, i->signal) == 1)
{
NS_LOG_DEBUG ("deliver signal=" << i->signal);
// the signal is pending so, we try to deliver it.
if (i->flags & SA_SIGINFO)
{
siginfo_t info;
ucontext_t ctx;
i->sigaction (i->signal, &info, &ctx);
}
else
{
i->handler (i->signal);
}
sigdelset (¤t->process->pendingSignals, i->signal);
}
}
}
int UtilsAllocateFd (void)
{
Thread *current = Current ();
NS_LOG_FUNCTION (current);
NS_ASSERT (current != 0);
std::map<int,FileUsage *>::iterator end = current->process->openFiles.end ();
std::map<int,FileUsage *>::iterator it = end;
for (int fd = 0; fd < MAX_FDS; fd++)
{
it = current->process->openFiles.find (fd);
if (it == end)
{
NS_LOG_DEBUG ("Allocated fd=" << fd);
return fd;
}
}
return -1;
}
// Little hack to advance time when detecting a possible infinite loop.
void UtilsAdvanceTime (Thread *current)
{
Time now = Now ();
if (now == current->lastTime)
{
// NS_LOG_DEBUG ("UtilsAdvanceTime current thread wait 1ms.");
//current->process->manager->Wait (Time (MilliSeconds (1)));
NS_LOG_DEBUG ("UtilsAdvanceTime current thread wait 1µs.");
current->process->manager->Wait (Time (MicroSeconds (1)));
}
current->lastTime = Now ();
}
std::string
GetTimeStamp ()
{
Time now = Now ();
time_t realnow = time (0);
std::ostringstream oss;
oss << ((long)now.GetSeconds ());
std::string sec = oss.str ();
uint32_t indent = 10;
std::string padding = "";
if (sec.length () < indent)
{
padding = std::string (indent - sec.length (), ' ');
}
sec = padding + sec;
padding = "";
oss.str ("");
oss.clear ();
oss << now;
indent = 25;
std::string ns = oss.str ();
if (ns.length () < indent)
{
padding = std::string (indent - ns.length (), ' ');
}
ns = padding + ns;
padding = "";
oss.str ("");
oss.clear ();
oss << "NS3 Time: " << sec << "s (" << ns << ") , REAL Time: " << realnow;
return oss.str ();
}
std::list<std::string>
Split (std::string input, std::string sep)
{
std::list<std::string> retval;
std::string::size_type cur = 0, next;
while (true)
{
next = input.find (sep, cur);
if (next == cur)
{
cur++;
continue;
}
else if (next == std::string::npos)
{
if (input.size () != cur)
{
retval.push_back (input.substr (cur, input.size () - cur));
}
break;
}
retval.push_back (input.substr (cur, next - cur));
cur = next + 1;
}
return retval;
}
std::string
FindExecFile (std::string root, std::string envPath, std::string fileName, uid_t uid, gid_t gid, int *errNo)
{
struct stat st;
std::string found = "";
*errNo = ENOENT;
int idx = fileName.find ('/', 0);
if (idx >= 0) // fileName contain a '/'
{
std::string vFile = UtilsGetRealFilePath (fileName);
if (0 == ::stat (vFile.c_str (), &st))
{
if (((uid) && CheckExeMode (&st, uid, gid)) || (!uid))
{
return vFile;
}
}
if (0 == ::stat (fileName.c_str (), &st))
{
if (((uid) && CheckExeMode (&st, uid, gid)) || (!uid))
{
return fileName;
}
}
}
else
{
std::list<std::string> paths = Split (envPath, ":");
for (std::list<std::string>::const_iterator i = paths.begin (); i != paths.end (); i++)
{
std::string test = root + "/" + *i + "/" + fileName;
if (0 == ::stat (test.c_str (), &st))
{
if (((uid) && CheckExeMode (&st, uid, gid)) || (!uid))
{
found = test;
break;
}
else
{
*errNo = EACCES;
}
}
}
}
return found;
}
bool
CheckExeMode (struct stat *st, uid_t uid, gid_t gid)
{
return ((gid != st->st_gid) && (uid != st->st_uid) && ((st->st_mode & (S_IROTH | S_IXOTH)) == (S_IROTH | S_IXOTH)))
|| ((gid == st->st_gid) && (uid != st->st_uid) && ((st->st_mode & (S_IRGRP | S_IXGRP)) == (S_IRGRP | S_IXGRP)))
|| ((uid == st->st_uid) && ((st->st_mode & (S_IRUSR | S_IXUSR)) == (S_IRUSR | S_IXUSR)));
}
void
FdDecUsage (int fd)
{
Thread *current = Current ();
FileUsage *fu = current->process->openFiles[fd];
if (fu && fu->DecUsage ())
{
current->process->openFiles.erase (fd);
delete fu;
fu = 0;
}
}
bool
CheckFdExists (Process* const p, int const fd, bool const opened)
{
std::map<int,FileUsage *>::iterator it = p->openFiles.find (fd);
if (it != p->openFiles.end ())
{
return !opened || (!(*it).second->IsClosed ());
}
return false;
}
int getRealFd (int fd, Thread *current)
{
std::map<int,FileUsage *>::iterator it = current->process->openFiles.find (fd);
if (current->process->openFiles.end () == it)
{
return -1;
}
FileUsage *fu = it->second;
if (fu->IsClosed ())
{
return -1;
}
return fu->GetFile ()->GetRealFd ();
}
std::string PathOfFd (int fd)
{
char proc[50];
char direc[PATH_MAX + 1];
sprintf (proc, "/proc/self/fd/%d", fd);
memset (direc, 0, PATH_MAX + 1);
ssize_t r = readlink (proc, direc, sizeof(direc) - 1);
if (r >= 0)
{
return std::string (direc);
}
return std::string ("");
}
bool
CheckShellScript (std::string fileName,
std::ostringstream &shellName, std::ostringstream &optionalArg)
{
int fd = open (fileName.c_str (), O_RDONLY);
if (fd < 0)
{
return false;
}
//A maximum line length of 127 characters is allowed for the first line in a #! executable shell script.
char firstLine[128];
ssize_t lg = read (fd, firstLine, 127);
close (fd);
if ((lg <= 2) || ('#' != firstLine [0]) || ('!' != firstLine [1]))
{
return false;
}
ssize_t crsr = 2;
firstLine [lg] = 0;
while (' ' == firstLine [crsr])
{
crsr++;
if (crsr >= lg)
{
return false;
}
}
ssize_t startShellName = crsr;
crsr++;
while (firstLine [crsr] && (' ' != firstLine [crsr]) && ('\n' != firstLine [crsr]))
{
crsr++;
if (crsr >= lg)
{
break;
}
}
ssize_t endShellName = crsr;
ssize_t lShellName = endShellName - startShellName;
if (lShellName <= 0)
{
return false;
}
shellName << std::string (firstLine + startShellName, lShellName);
if (crsr >= lg)
{
return true;
}
ssize_t startOpt = crsr;
while (firstLine[crsr]&&('\n' != firstLine [crsr]))
{
crsr++;
if (crsr >= lg)
{
break;
}
}
ssize_t lOpt = crsr - startOpt;
if (lOpt <= 0)
{
return true;
}
optionalArg << std::string (firstLine + startOpt, lOpt);
return true;
}
char * seek_env (const char *name, char **array)
{
int namelen = strlen (name);
char **cur;
for (cur = array; cur != 0 && *cur != 0; cur++)
{
char *equal = strchr (*cur, '=');
if (equal == 0)
{
continue;
}
if (strncmp (*cur, name, namelen) != 0)
{
continue;
}
return equal + 1;
}
return 0;
}
std::string UtilsGetCurrentDirName (void)
{
// DCE and NS3 never change the cwd.
static std::string pwd = "";
if (pwd.length () > 0)
{
return pwd;
}
char *thePwd = get_current_dir_name ();
int fd = open (thePwd, O_RDONLY);
pwd = PathOfFd (fd);
close (fd);
free (thePwd);
return pwd;
}
} // namespace ns3