libcsdbg  1.28
C++ exception (and generic) stack trace debug library
sttybuf & csdbg::sttybuf::open ( bool  ctty)
virtual

Open the serial interface for output.

Parameters
[in]cttytrue to make the interface the process controlling terminal
Returns
*this
Exceptions
csdbg::exception
Note
If the interface is already open, it is re-opened with the new settings

Definition at line 250 of file sttybuf.cpp.

References csdbg::streambuf::close(), config(), csdbg::util::is_chardev(), csdbg::util::is_writable(), likely, m_devnode, csdbg::streambuf::m_handle, open(), and unlikely.

251 {
252  if ( unlikely(m_handle >= 0) )
253  close();
254 
255  /* Stat the device node path and make some preliminary checks */
256  fileinfo_t inf;
257  i32 retval = stat(m_devnode, &inf);
258  if ( unlikely(errno == ENOENT) )
259  throw exception("device node '%s' does not exist", m_devnode);
260 
261  else if ( unlikely(retval < 0) )
262  throw exception(
263  "failed to stat path '%s' (errno %d - %s)",
264  m_devnode,
265  errno,
266  strerror(errno)
267  );
268 
269  else if ( unlikely(!util::is_chardev(inf)) )
270  throw exception("'%s' is not a character device", m_devnode);
271 
272  else if ( unlikely(!util::is_writable(inf)) )
273  throw exception("serial interface '%s' is not writable", m_devnode);
274 
275  u32 flags = O_WRONLY;
276  if ( likely(!ctty) )
277  flags |= O_NOCTTY;
278 
279  /* Open the device node */
280  do {
281  m_handle = ::open(m_devnode, flags);
282  }
283  while ( unlikely(m_handle < 0 && errno == EINTR) );
284 
285  if ( unlikely(m_handle < 0) )
286  throw exception(
287  "failed to open serial interface '%s' (errno %d - %s)",
288  m_devnode,
289  errno,
290  strerror(errno)
291  );
292 
293  return config();
294 }
static bool is_writable(const fileinfo_t &)
Check if the process has write access to a file.
Definition: util.cpp:464
struct stat fileinfo_t
File metadata.
Definition: config.hpp:112
i32 m_handle
Stream handle (descriptor)
Definition: streambuf.hpp:43
#define likely(expr)
Offer a hint (positive) to the pipeline branch predictor.
Definition: config.hpp:344
virtual sttybuf & open()
Open the serial interface for output.
Definition: sttybuf.cpp:233
static bool is_chardev(const fileinfo_t &)
Check if a file is a character device node.
Definition: util.cpp:432
i8 * m_devnode
Device node file (devfs)
Definition: sttybuf.hpp:34
unsigned int u32
32-bit unsigned integer
Definition: config.hpp:102
int i32
32-bit signed integer
Definition: config.hpp:82
virtual sttybuf & config() const
Configure the serial interface.
Definition: sttybuf.cpp:22
#define unlikely(expr)
Offer a hint (negative) to the pipeline branch predictor.
Definition: config.hpp:349
virtual streambuf & close()
Close the stream.
Definition: streambuf.cpp:130

+ Here is the call graph for this function: