ns-3 Direct Code Execution
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
unix-file-fd.cc
Go to the documentation of this file.
1 #include "unix-file-fd.h"
2 #include "process.h"
3 #include "dce-manager.h"
4 #include "utils.h"
5 #include "ns3/log.h"
6 #include "ns3/assert.h"
7 #include <unistd.h>
8 #include <errno.h>
9 #include <sys/mman.h>
10 #include <fcntl.h>
11 #include "dce-node-context.h"
12 #include "poll.h"
13 
14 NS_LOG_COMPONENT_DEFINE ("UnixFileFd");
15 
16 namespace ns3 {
17 
19  : m_realFd (realFd)
20 {
21 }
23 {
24  NS_LOG_FUNCTION (this << m_realFd);
25  m_realFd = -1;
26 }
27 int
29 {
30  return m_realFd;
31 }
32 ssize_t
33 UnixFileFdBase::Write (const void *buf, size_t count)
34 {
35  Thread *current = Current ();
36  NS_LOG_FUNCTION (this << current << buf << count);
37  NS_ASSERT (current != 0);
38  ssize_t result = ::write (m_realFd, buf, count);
39  if (result == -1)
40  {
41  current->err = errno;
42  }
43  return result;
44 }
45 ssize_t
46 UnixFileFdBase::Read (void *buf, size_t count)
47 {
48  Thread *current = Current ();
49  NS_LOG_FUNCTION (this << current << buf << count);
50  NS_ASSERT (current != 0);
51  ssize_t result = ::read (m_realFd, buf, count);
52  if (result == -1)
53  {
54  current->err = errno;
55  }
56  return result;
57 }
58 
59 ssize_t
60 UnixFileFdBase::Recvmsg (struct msghdr *msg, int flags)
61 {
62  Thread *current = Current ();
63  NS_LOG_FUNCTION (this << current);
64  NS_ASSERT (current != 0);
65  current->err = ENOTSOCK;
66  return -1;
67 }
68 ssize_t
69 UnixFileFdBase::Sendmsg (const struct msghdr *msg, int flags)
70 {
71  Thread *current = Current ();
72  NS_LOG_FUNCTION (this << current);
73  NS_ASSERT (current != 0);
74  current->err = ENOTSOCK;
75  return -1;
76 }
77 bool
79 {
80  return ::isatty (m_realFd);
81 }
82 int
83 UnixFileFdBase::Setsockopt (int level, int optname,
84  const void *optval, socklen_t optlen)
85 {
86  Thread *current = Current ();
87  NS_LOG_FUNCTION (this << current);
88  NS_ASSERT (current != 0);
89  current->err = ENOTSOCK;
90  return -1;
91 }
92 int
93 UnixFileFdBase::Getsockopt (int level, int optname,
94  void *optval, socklen_t *optlen)
95 {
96  Thread *current = Current ();
97  NS_LOG_FUNCTION (this << current);
98  NS_ASSERT (current != 0);
99  current->err = ENOTSOCK;
100  return -1;
101 }
102 int
103 UnixFileFdBase::Getsockname (struct sockaddr *name, socklen_t *namelen)
104 {
105  Thread *current = Current ();
106  NS_LOG_FUNCTION (this << current << name << *namelen);
107  NS_ASSERT (current != 0);
108  current->err = ENOTSOCK;
109  return -1;
110 }
111 int
112 UnixFileFdBase::Getpeername (struct sockaddr *name, socklen_t *namelen)
113 {
114  Thread *current = Current ();
115  NS_LOG_FUNCTION (this << current << name << *namelen);
116  NS_ASSERT (current != 0);
117  current->err = ENOTSOCK;
118  return -1;
119 }
120 
121 int
122 UnixFileFdBase::Ioctl (int request, char *argp)
123 {
124  Thread *current = Current ();
125  NS_LOG_FUNCTION (this << current);
126  NS_ASSERT (current != 0);
127  current->err = EINVAL;
128  return -1;
129 }
130 int
131 UnixFileFdBase::Bind (const struct sockaddr *my_addr, socklen_t addrlen)
132 {
133  Thread *current = Current ();
134  NS_LOG_FUNCTION (this << current);
135  NS_ASSERT (current != 0);
136  current->err = ENOTSOCK;
137  return -1;
138 }
139 int
140 UnixFileFdBase::Connect (const struct sockaddr *my_addr, socklen_t addrlen)
141 {
142  Thread *current = Current ();
143  NS_LOG_FUNCTION (this << current);
144  NS_ASSERT (current != 0);
145  current->err = ENOTSOCK;
146  return -1;
147 }
148 int
150 {
151  Thread *current = Current ();
152  NS_LOG_FUNCTION (this << current);
153  NS_ASSERT (current != 0);
154  current->err = ENOTSOCK;
155  return -1;
156 }
157 int
158 UnixFileFdBase::Accept (struct sockaddr *my_addr, socklen_t *addrlen)
159 {
160  Thread *current = Current ();
161  NS_LOG_FUNCTION (this << current << my_addr << addrlen);
162  NS_ASSERT (current != 0);
163  current->err = ENOTSOCK;
164  return -1;
165 }
166 int
168 {
169  Thread *current = Current ();
170  NS_LOG_FUNCTION (this << current << how);
171  NS_ASSERT (current != 0);
172  current->err = ENOTSOCK;
173  return -1;
174 }
175 void *
176 UnixFileFdBase::Mmap (void *start, size_t length, int prot, int flags,
177  off64_t offset)
178 {
179  Thread *current = Current ();
180  NS_LOG_FUNCTION (this << current);
181  NS_ASSERT (current != 0);
182 
183  void *retval = ::mmap (start, length, prot, flags, m_realFd, offset);
184  if (retval == 0)
185  {
186  current->err = errno;
187  }
188  return retval;
189 }
190 off64_t
191 UnixFileFdBase::Lseek (off64_t offset, int whence)
192 {
193  Thread *current = Current ();
194  NS_LOG_FUNCTION (this << current << offset << whence);
195  NS_ASSERT (current != 0);
196  off64_t retval = ::lseek64 (m_realFd, offset, whence);
197  if (retval == -1)
198  {
199  current->err = errno;
200  }
201  return retval;
202 }
203 int
204 UnixFileFdBase::Fxstat (int ver, struct ::stat *buf)
205 {
206  Thread *current = Current ();
207  NS_LOG_FUNCTION (this << current << buf);
208  NS_ASSERT (current != 0);
209  int retval = ::__fxstat (ver, m_realFd, buf);
210  if (retval == -1)
211  {
212  current->err = errno;
213  }
214  return retval;
215 }
216 int
217 UnixFileFdBase::Fxstat64 (int ver, struct ::stat64 *buf)
218 {
219  Thread *current = Current ();
220  NS_LOG_FUNCTION (this << current << buf);
221  NS_ASSERT (current != 0);
222  int retval = ::__fxstat64 (ver, m_realFd, buf);
223  if (retval == -1)
224  {
225  current->err = errno;
226  }
227  return retval;
228 }
229 int
230 UnixFileFdBase::Fcntl (int cmd, unsigned long arg)
231 {
232  NS_LOG_FUNCTION (this << Current () << cmd << arg);
233  NS_ASSERT (Current () != 0);
234  Thread *current = Current ();
235  int retval = ::fcntl (m_realFd, cmd, arg);
236  if (retval == -1)
237  {
238  current->err = errno;
239  }
240  return retval;
241 }
242 int
244  const struct itimerspec *new_value,
245  struct itimerspec *old_value)
246 {
247  NS_LOG_FUNCTION (this << Current () << flags << new_value << old_value);
248  NS_ASSERT (Current () != 0);
249  Thread *current = Current ();
250  current->err = EINVAL;
251  return -1;
252 }
253 int
254 UnixFileFdBase::Gettime (struct itimerspec *cur_value) const
255 {
256  NS_LOG_FUNCTION (this << Current () << cur_value);
257  NS_ASSERT (Current () != 0);
258  Thread *current = Current ();
259  current->err = EINVAL;
260  return -1;
261 }
262 
263 
264 bool
266 {
267  NS_LOG_FUNCTION (this << " fd:" << m_realFd);
268  // Must do a real select
269  fd_set readFd;
270  struct timeval timeOut;
271  int ret = 0;
272 
273  timeOut.tv_sec = 0;
274  timeOut.tv_usec = 0;
275  FD_ZERO (&readFd);
276  FD_SET (m_realFd, &readFd);
277 
278  ret = select (1 + m_realFd, &readFd, NULL, NULL, &timeOut);
279 
280  return (ret == 1) && (FD_ISSET (m_realFd, &readFd));
281 }
282 bool
284 {
285  NS_LOG_FUNCTION (this << " fd:" << m_realFd);
286  // Must do a real select
287  fd_set writeFd;
288  struct timeval timeOut;
289  int ret = 0;
290 
291  timeOut.tv_sec = 0;
292  timeOut.tv_usec = 0;
293  FD_ZERO (&writeFd);
294  FD_SET (m_realFd, &writeFd);
295 
296  ret = select (1 + m_realFd, &writeFd, NULL, NULL, &timeOut);
297 
298  return (ret == 1) && (FD_ISSET (m_realFd, &writeFd));
299 }
300 bool
302 {
303  return false;
304 }
305 
306 int
308 {
309  Thread *current = Current ();
310  NS_ASSERT (current != 0);
311  NS_LOG_FUNCTION (this << current << length);
312 
313  int retval = ::ftruncate (m_realFd, length);
314  if (retval == -1)
315  {
316  current->err = errno;
317  }
318  return retval;
319 }
320 int
322 {
323  return m_realFd;
324 }
325 
327  : UnixFileFdBase (realFd)
328 {
329 }
331 {
332  if (PeekRealFd () != -1)
333  {
334  ::close (PeekRealFd ());
335  }
336 }
337 int
339 {
340  Thread *current = Current ();
341  NS_LOG_FUNCTION (this << current);
342  NS_ASSERT (current != 0);
343  int result = ::close (PeekRealFd ());
344  if (result == -1)
345  {
346  current->err = errno;
347  }
348  // caller is responsible for removing this fd from process
349  // list of fds and deleting this class instance.
350  return result;
351 }
352 
353 int
355 {
356  int ret = 0;
357 
358  if (CanRecv ())
359  {
360  ret |= POLLIN;
361  }
362  if (CanSend ())
363  {
364  ret |= POLLOUT;
365  }
366  if (HangupReceived ())
367  {
368  ret |= POLLHUP;
369  }
370 
371  if (ptable)
372  {
373  ptable->PollWait (this);
374  }
375 
376  return ret;
377 }
378 
379 UnixFileFdLight::UnixFileFdLight (std::string path) : m_path (path),
380  UnixFileFdBase (-1)
381 {
382 
383 }
384 
386 {
387  m_path = "";
388 }
389 
390 ssize_t
391 UnixFileFdLight::Write (const void *buf, size_t count)
392 {
393  FILE *f = fopen (m_path.c_str (), "a");
394 
395  if (!f)
396  {
397  Current ()->err = errno;
398  return -1;
399  }
400 
401  ssize_t res = fwrite (buf, count, 1, f);
402 
403  fclose (f);
404 
405  return res;
406 }
407 
408 int
410 {
411  return 0;
412 }
413 
414 bool
416 {
417  return true;
418 }
419 
421  : UnixFileFdBase (realFd)
422 {
423 }
425 {
426 }
427 int
429 {
430  return 0;
431 }
432 
433 UnixRandomFd::UnixRandomFd (std::string devPath) : m_devPath (devPath),
434  UnixFileFdBase (-1)
435 {
436 }
437 
439 {
440 
441 }
442 
443 ssize_t
444 UnixRandomFd::Read (void *buf, size_t count)
445 {
446  Ptr<DceNodeContext> nodeContext = DceNodeContext::GetNodeContext ();
447  NS_ASSERT (0 != nodeContext);
448 
449  return nodeContext->RandomRead (buf, count);
450 }
451 
452 bool
454 {
455  return true;
456 }
457 
458 int
460 {
461  return 0;
462 }
463 bool
465 {
466  return true;
467 }
468 ssize_t
469 UnixRandomFd::Write (const void *buf, size_t count)
470 {
471  Thread *current = Current ();
472  NS_ASSERT (current != 0);
473  current->err = EBADF;
474 
475  return -1;
476 }
477 int
478 UnixRandomFd::Fxstat (int ver, struct ::stat *buf)
479 {
480  Thread *current = Current ();
481  NS_LOG_FUNCTION (this << current << buf);
482  int tmpFd = open (m_devPath.c_str (), O_RDONLY, 0);
483 
484  if (tmpFd < 0)
485  {
486  current->err = errno;
487  return -1;
488  }
489 
490  NS_ASSERT (current != 0);
491  int retval = ::__fxstat (ver, tmpFd, buf);
492  if (retval == -1)
493  {
494  current->err = errno;
495  }
496  close (tmpFd);
497  return retval;
498 }
499 int
500 UnixRandomFd::Fxstat64 (int ver, struct ::stat64 *buf)
501 {
502  Thread *current = Current ();
503  NS_LOG_FUNCTION (this << current << buf);
504  int tmpFd = open (m_devPath.c_str (), O_RDONLY, 0);
505 
506  if (tmpFd < 0)
507  {
508  current->err = errno;
509  return -1;
510  }
511 
512  NS_ASSERT (current != 0);
513  int retval = ::__fxstat64 (ver, tmpFd, buf);
514  if (retval == -1)
515  {
516  current->err = errno;
517  }
518  close (tmpFd);
519  return retval;
520 }
521 } // namespace ns3