Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
utils Namespace Reference

Classes

class  CArrayBuilder
 
class  CallbackGuard
 Callback guard. More...
 
class  CallbackWrapper
 Wraps callback and callback tracker into single object. More...
 
class  Channel
 Channel is implemented with a pair of anonymous sockets. More...
 
class  CountingMap
 Structure used to count elements. More...
 
class  EventFD
 
struct  UtilsException
 Base class for exceptions in utils. More...
 
struct  EventFDException
 
struct  ProvisionExistsException
 
class  ScopedGlibLoop
 Glib loop controller. More...
 
class  Glib
 Miscellaneous helpers for the Glib library. More...
 
class  Inotify
 Inotify monitors a directory and when a specified file or folder is created or deleted it calls a corresponding handler. More...
 
class  Latch
 A synchronization aid that allows one thread to wait until an operation being performed in other thread completes. More...
 
class  SameThreadGuard
 Same thread guard. More...
 
class  ScopedGError
 
class  SignalFD
 SignalFD takes control over handling signals sent to the thread. More...
 
class  ValueLatch
 
class  Worker
 A queue with tasks executed in a dedicated thread. More...
 

Typedefs

typedef CArrayBuilder< const
char * > 
CStringArrayBuilder
 
typedef std::function< void()> VoidCallback
 

Enumerations

enum  Color : unsigned int {
  Color::DEFAULT = 0, Color::BLACK = 30, Color::RED = 31, Color::GREEN = 32,
  Color::YELLOW = 33, Color::BLUE = 34, Color::MAGENTA = 35, Color::CYAN = 36,
  Color::WHITE = 37
}
 
enum  Attributes : unsigned int { Attributes::DEFAULT = 0, Attributes::BOLD = 1 }
 
enum  RunLevel : int { RUNLEVEL_POWEROFF = 0, RUNLEVEL_REBOOT = 6 }
 

Functions

template<class Callback >
CallbackWrapper< Callback > * createCallbackWrapper (const Callback &callback, const CallbackGuard::Tracker &tracker)
 Creates callback wrapper. More...
 
template<class Callback >
void deleteCallbackWrapper (void *pointer)
 Deletes callback wrapper. More...
 
template<class Callback >
const Callback & getCallbackFromPointer (const void *pointer)
 Recovers callback from wrapper pointer. More...
 
std::string getConsoleEscapeSequence (Attributes attr, Color color)
 
bool daemonize ()
 
bool setSuppGroups (const std::vector< std::string > &groups)
 Set supplementary groups to the current process. More...
 
bool dropRoot (uid_t uid, gid_t gid, const std::vector< unsigned int > &caps)
 Set effective and permitted capabilities on the current process and drop root privileges. More...
 
bool launchAsRoot (const std::function< bool()> &func)
 Launch func as root user. More...
 
bool joinToNs (int nsPid, int ns)
 Join to namespace. More...
 
int passNamespacedFd (int nsPid, int ns, const std::function< int()> &fdFactory)
 Pass file descriptor from namespace of some process. More...
 
std::string getSystemErrorMessage ()
 Return string describing error number it is wrapper for strerror_r. More...
 
std::string getSystemErrorMessage (int err)
 
bool waitPid (pid_t pid, int &status)
 Wait until child processes ends. More...
 
int open (const std::string &path, int flags, mode_t mode=-1)
 Open a file. More...
 
void close (int fd)
 Close the file descriptor. More...
 
void shutdown (int fd)
 Shut down part of a full-duplex connection. More...
 
int ioctl (int fd, unsigned long request, void *argp)
 Operation on a special file. More...
 
int dup2 (int olfFD, int newFD, bool closeOnExec=false)
 Duplicate one file desciptor onto another. More...
 
void write (int fd, const void *bufferPtr, const size_t size, int timeoutMS=5000)
 Write to a file descriptor, throw on error. More...
 
void read (int fd, void *bufferPtr, const size_t size, int timeoutMS=5000)
 Read from a file descriptor, throw on error. More...
 
unsigned int getMaxFDNumber ()
 
void setMaxFDNumber (unsigned int limit)
 Set the software and hardware limit of file descriptors for this process. More...
 
unsigned int getFDNumber ()
 
int fdRecv (int socket, const unsigned int timeoutMS=5000)
 Receive fd via Unix Domain socket. More...
 
bool fdSend (int socket, int fd, const unsigned int timeoutMS=5000)
 Send Socket via Unix Domain socket. More...
 
void setCloseOnExec (int fd, bool closeOnExec)
 Set or remove CLOEXEC on a file descriptor. More...
 
void setNonBlocking (int fd, bool nonBlocking)
 Set or remove NONBLOCK on a file descriptor. More...
 
void waitForFile (const std::string &file, const unsigned int timeoutMs)
 
std::string readFileStream (const std::string &path)
 Reads the content of file stream (no seek); Throws exception on error. More...
 
bool readFileStream (const std::string &path, std::string &result)
 Reads the content of file stream (no seek) More...
 
std::string readFileContent (const std::string &path)
 Reads the content of a file (performs seek); Throws exception on error. More...
 
bool readFileContent (const std::string &path, std::string &content)
 Reads the content of a file. More...
 
bool saveFileContent (const std::string &path, const std::string &content)
 Save the content to the file. More...
 
bool readFirstLineOfFile (const std::string &path, std::string &ret)
 Read a line from file Its goal is to read a kernel config files (eg. More...
 
bool removeFile (const std::string &path)
 Remove file. More...
 
bool isCharDevice (const std::string &path)
 Checks if a char device exists. More...
 
void assertIsDir (const std::string &path)
 Checks if a path exists and points to a directory. More...
 
void assertIsAbsolute (const std::string &path)
 Checks if path is absolute. More...
 
bool mountRun (const std::string &path)
 Mounts run as a tmpfs on a given path. More...
 
bool mount (const std::string &source, const std::string &target, const std::string &filesystemtype, unsigned long mountflags, const std::string &data)
 Creates mount point. More...
 
bool umount (const std::string &path)
 Umounts a filesystem. More...
 
bool isMountPoint (const std::string &path, bool &result)
 Check if given path is a mount point. More...
 
bool hasSameMountPoint (const std::string &path1, const std::string &path2, bool &result)
 Checks whether the given paths are under the same mount point. More...
 
bool moveFile (const std::string &src, const std::string &dst)
 Moves the file either by rename if under the same mount point or by copy&delete if under a different one. More...
 
bool copyDirContents (const std::string &src, const std::string &dst)
 Recursively copy contents of src dir to dst dir. More...
 
bool createDir (const std::string &path, uid_t uid, uid_t gid, boost::filesystem::perms mode)
 Creates a directory with specific UID, GID and permissions set. More...
 
bool createDirs (const std::string &path, mode_t mode)
 Recursively creates a directory with specific permissions set. More...
 
bool createEmptyDir (const std::string &path)
 Creates an empty directory, ready to serve as mount point. More...
 
bool createFile (const std::string &path, int flags, mode_t mode)
 Creates an empty file. More...
 
bool createFifo (const std::string &path, mode_t mode)
 Creates an FIFO special file. More...
 
bool copyFile (const std::string &src, const std::string &dest)
 Copy an file. More...
 
bool createLink (const std::string &src, const std::string &dest)
 Create hard link. More...
 
bool listDir (const std::string &path, std::vector< std::string > &files)
 List all (including '. More...
 
void executeInGlibThread (const VoidCallback &callback, const CallbackGuard &guard)
 Executes a callback in glib thread (adds an iddle event to glib) More...
 
bool getFreeLoopDevice (std::string &ret)
 Returns string with first free loop device. More...
 
bool mountImage (const std::string &image, const std::string &loopdev, const std::string &path)
 Mount an ext4 image from file on a given path by using a loop device. More...
 
bool umountImage (const std::string &path, const std::string &loopdev)
 Umounts previously mounted image. More...
 
bool copyImageContents (const std::string &img, const std::string &dst)
 Mounts an image and copies its contents to dst directory. More...
 
bool setRunLevel (RunLevel runLevel)
 
template<class T >
void make_clean (T &value)
 
template<class T >
make_clean ()
 
template<class... Paths>
std::string createFilePath (const Paths &...paths)
 
std::string dirName (std::string path)
 
std::string getAbsolutePath (const std::string &path, const std::string &base)
 
std::ostream & operator<< (std::ostream &os, const ScopedGError &e)
 
::sigset_t getSignalMask ()
 
bool isSignalPending (const int sigNum)
 
bool waitForSignal (const int sigNum, int timeoutMs)
 
bool isSignalBlocked (const int sigNum)
 
void signalBlock (const int sigNum)
 
void signalBlockAllExcept (const std::initializer_list< int > &signals)
 
void signalUnblock (const int sigNum)
 
std::vector< std::pair< int,
struct::sigaction > > 
signalIgnore (const std::initializer_list< int > &signals)
 
struct::sigaction signalSet (const int sigNum, const struct::sigaction *sigAct)
 
void sendSignal (const pid_t pid, const int sigNum)
 
template<class Predicate >
bool spinWaitFor (int timeoutMs, Predicate pred)
 
std::string toHexString (const void *data, unsigned len)
 Convert binary bytes array to hex string representation. More...
 
std::vector< std::string > split (const std::string &str, const std::string &delim)
 
bool beginsWith (std::string const &value, std::string const &part)
 
bool endsWith (std::string const &value, std::string const &part)
 
template<typename T >
std::string join (const std::vector< T > &vec, const char *delim)
 
std::string getTypeName (const std::type_info &ti)
 
template<class T >
std::string getTypeName (const T &t)
 
bool activateVT (const int &vt)
 
bool executeAndWait (const std::function< void()> &func, int &status)
 Execute function in child process. More...
 
bool executeAndWait (const std::function< void()> &func)
 
bool executeAndWait (const char *fname, const char *const *argv, int &status)
 
bool executeAndWait (const char *fname, const char *const *argv)
 Execute binary. More...
 

Variables

const int ERROR_MESSAGE_BUFFER_CAPACITY = 256
 

Typedef Documentation

typedef std::function<void()> utils::VoidCallback

Enumeration Type Documentation

enum utils::Attributes : unsigned int
strong
Enumerator
DEFAULT 
BOLD 
enum utils::Color : unsigned int
strong
Enumerator
DEFAULT 
BLACK 
RED 
GREEN 
YELLOW 
BLUE 
MAGENTA 
CYAN 
WHITE 
enum utils::RunLevel : int
Enumerator
RUNLEVEL_POWEROFF 
RUNLEVEL_REBOOT 

Function Documentation

bool utils::activateVT ( const int &  vt)
void utils::assertIsAbsolute ( const std::string &  path)

Checks if path is absolute.

void utils::assertIsDir ( const std::string &  path)

Checks if a path exists and points to a directory.

bool utils::beginsWith ( std::string const &  value,
std::string const &  part 
)
inline
void utils::close ( int  fd)

Close the file descriptor.

bool utils::copyDirContents ( const std::string &  src,
const std::string &  dst 
)

Recursively copy contents of src dir to dst dir.

bool utils::copyFile ( const std::string &  src,
const std::string &  dest 
)

Copy an file.

bool utils::copyImageContents ( const std::string &  img,
const std::string &  dst 
)

Mounts an image and copies its contents to dst directory.

template<class Callback >
CallbackWrapper<Callback>* utils::createCallbackWrapper ( const Callback &  callback,
const CallbackGuard::Tracker &  tracker 
)

Creates callback wrapper.

Useful for C callback api.

bool utils::createDir ( const std::string &  path,
uid_t  uid,
uid_t  gid,
boost::filesystem::perms  mode 
)

Creates a directory with specific UID, GID and permissions set.

bool utils::createDirs ( const std::string &  path,
mode_t  mode 
)

Recursively creates a directory with specific permissions set.

bool utils::createEmptyDir ( const std::string &  path)

Creates an empty directory, ready to serve as mount point.

Succeeds either if path did not exist and was created successfully, or if already existing dir under the same path is empty and is not a mount point.

bool utils::createFifo ( const std::string &  path,
mode_t  mode 
)

Creates an FIFO special file.

bool utils::createFile ( const std::string &  path,
int  flags,
mode_t  mode 
)

Creates an empty file.

template<class... Paths>
std::string utils::createFilePath ( const Paths &...  paths)
bool utils::createLink ( const std::string &  src,
const std::string &  dest 
)

Create hard link.

bool utils::daemonize ( )

Close all open standard file descriptors

template<class Callback >
void utils::deleteCallbackWrapper ( void *  pointer)

Deletes callback wrapper.

Useful for C callback api.

std::string utils::dirName ( std::string  path)
inline
bool utils::dropRoot ( uid_t  uid,
gid_t  gid,
const std::vector< unsigned int > &  caps 
)

Set effective and permitted capabilities on the current process and drop root privileges.

int utils::dup2 ( int  oldFD,
int  newFD,
bool  closeOnExec 
)

Duplicate one file desciptor onto another.

bool utils::endsWith ( std::string const &  value,
std::string const &  part 
)
inline
bool utils::executeAndWait ( const std::function< void()> &  func,
int &  status 
)

Execute function in child process.

bool utils::executeAndWait ( const std::function< void()> &  func)
bool utils::executeAndWait ( const char *  fname,
const char *const *  argv,
int &  status 
)
bool utils::executeAndWait ( const char *  fname,
const char *const *  argv 
)

Execute binary.

void utils::executeInGlibThread ( const VoidCallback &  callback,
const CallbackGuard &  guard 
)

Executes a callback in glib thread (adds an iddle event to glib)

int utils::fdRecv ( int  socket,
const unsigned int  timeoutMS 
)

Receive fd via Unix Domain socket.

bool utils::fdSend ( int  socket,
int  fd,
const unsigned int  timeoutMS 
)

Send Socket via Unix Domain socket.

std::string utils::getAbsolutePath ( const std::string &  path,
const std::string &  base 
)
inline
template<class Callback >
const Callback& utils::getCallbackFromPointer ( const void *  pointer)

Recovers callback from wrapper pointer.

Useful for C callback api.

std::string utils::getConsoleEscapeSequence ( Attributes  attr,
Color  color 
)
unsigned int utils::getFDNumber ( )
Returns
number of opened file descriptors by this process
bool utils::getFreeLoopDevice ( std::string &  ret)

Returns string with first free loop device.

unsigned int utils::getMaxFDNumber ( )
Returns
the max number of file descriptors for this process.
sigset_t utils::getSignalMask ( )
std::string utils::getSystemErrorMessage ( )

Return string describing error number it is wrapper for strerror_r.

std::string utils::getSystemErrorMessage ( int  err)
std::string utils::getTypeName ( const std::type_info &  ti)
template<class T >
std::string utils::getTypeName ( const T &  t)
bool utils::hasSameMountPoint ( const std::string &  path1,
const std::string &  path2,
bool &  result 
)

Checks whether the given paths are under the same mount point.

int utils::ioctl ( int  fd,
unsigned long  request,
void *  argp 
)

Operation on a special file.

bool utils::isCharDevice ( const std::string &  path)

Checks if a char device exists.

bool utils::isMountPoint ( const std::string &  path,
bool &  result 
)

Check if given path is a mount point.

bool utils::isSignalBlocked ( const int  sigNum)
bool utils::isSignalPending ( const int  sigNum)
template<typename T >
std::string utils::join ( const std::vector< T > &  vec,
const char *  delim 
)
bool utils::joinToNs ( int  nsPid,
int  ns 
)

Join to namespace.

bool utils::launchAsRoot ( const std::function< bool()> &  func)

Launch func as root user.

This function forks, sets UID 0 to child process and calls func.

bool utils::listDir ( const std::string &  path,
std::vector< std::string > &  files 
)

List all (including '.

' and '..' entries) dir entries

template<class T >
void utils::make_clean ( T &  value)
template<class T >
T utils::make_clean ( )
bool utils::mount ( const std::string &  source,
const std::string &  target,
const std::string &  filesystemtype,
unsigned long  mountflags,
const std::string &  data 
)

Creates mount point.

bool utils::mountImage ( const std::string &  image,
const std::string &  path,
const std::string &  loopdev 
)

Mount an ext4 image from file on a given path by using a loop device.

bool utils::mountRun ( const std::string &  path)

Mounts run as a tmpfs on a given path.

bool utils::moveFile ( const std::string &  src,
const std::string &  dst 
)

Moves the file either by rename if under the same mount point or by copy&delete if under a different one.

The destination has to be a full path including file name.

int utils::open ( const std::string &  path,
int  flags,
mode_t  mode 
)

Open a file.

std::ostream& utils::operator<< ( std::ostream &  os,
const ScopedGError &  e 
)
Parameters
osthe output stream
eerror to write out
int utils::passNamespacedFd ( int  nsPid,
int  ns,
const std::function< int()> &  fdFactory 
)

Pass file descriptor from namespace of some process.

void utils::read ( int  fd,
void *  bufferPtr,
const size_t  size,
int  timeoutMS = 5000 
)

Read from a file descriptor, throw on error.

Parameters
fdfile descriptor
bufferPtrpointer to the data buffer
sizesize of the data to read
timeoutMStimeout in milliseconds
std::string utils::readFileContent ( const std::string &  path)

Reads the content of a file (performs seek); Throws exception on error.

bool utils::readFileContent ( const std::string &  path,
std::string &  result 
)

Reads the content of a file.

std::string utils::readFileStream ( const std::string &  path)

Reads the content of file stream (no seek); Throws exception on error.

bool utils::readFileStream ( const std::string &  path,
std::string &  result 
)

Reads the content of file stream (no seek)

bool utils::readFirstLineOfFile ( const std::string &  path,
std::string &  ret 
)

Read a line from file Its goal is to read a kernel config files (eg.

from /proc, /sys/)

bool utils::removeFile ( const std::string &  path)

Remove file.

bool utils::saveFileContent ( const std::string &  path,
const std::string &  content 
)

Save the content to the file.

void utils::sendSignal ( const pid_t  pid,
const int  sigNum 
)
void utils::setCloseOnExec ( int  fd,
bool  closeOnExec 
)

Set or remove CLOEXEC on a file descriptor.

void utils::setMaxFDNumber ( unsigned int  limit)

Set the software and hardware limit of file descriptors for this process.

Parameters
limitlimit of file descriptors
void utils::setNonBlocking ( int  fd,
bool  nonBlocking 
)

Set or remove NONBLOCK on a file descriptor.

bool utils::setRunLevel ( RunLevel  runLevel)
bool utils::setSuppGroups ( const std::vector< std::string > &  groups)

Set supplementary groups to the current process.

void utils::shutdown ( int  fd)

Shut down part of a full-duplex connection.

void utils::signalBlock ( const int  sigNum)
void utils::signalBlockAllExcept ( const std::initializer_list< int > &  signals)
std::vector< std::pair< int, struct::sigaction > > utils::signalIgnore ( const std::initializer_list< int > &  signals)
struct::sigaction utils::signalSet ( const int  sigNum,
const struct::sigaction *  sigAct 
)
void utils::signalUnblock ( const int  sigNum)
template<class Predicate >
bool utils::spinWaitFor ( int  timeoutMs,
Predicate  pred 
)
std::vector< std::string > utils::split ( const std::string &  str,
const std::string &  delim 
)
std::string utils::toHexString ( const void *  data,
unsigned  len 
)

Convert binary bytes array to hex string representation.

bool utils::umount ( const std::string &  path)

Umounts a filesystem.

bool utils::umountImage ( const std::string &  path,
const std::string &  loopdev 
)

Umounts previously mounted image.

This call will also free loop device used to mount specified path.

void utils::waitForFile ( const std::string &  file,
const unsigned int  timeoutMs 
)
bool utils::waitForSignal ( const int  sigNum,
int  timeoutMs 
)
bool utils::waitPid ( pid_t  pid,
int &  status 
)

Wait until child processes ends.

void utils::write ( int  fd,
const void *  bufferPtr,
const size_t  size,
int  timeoutMS = 5000 
)

Write to a file descriptor, throw on error.

Parameters
fdfile descriptor
bufferPtrpointer to the data buffer
sizesize of data to write
timeoutMStimeout in milliseconds

Variable Documentation

const int utils::ERROR_MESSAGE_BUFFER_CAPACITY = 256