libqi-api
2.8.7.4
|
OS abstraction layer. . More...
Classes | |
struct | timeval |
struct similar to POSIX timeval More... | |
class | ScopedThreadName |
Set the current thread name and restore it after use. More... | |
Functions | |
template<typename T > | |
T | getEnvDefault (const char *name, T defaultVal) |
FILE * | fopen (const char *filename, const char *mode) |
Open a file and returns and handle on it. More... | |
int | stat (const char *filename, struct stat *pstat) |
Get file status. More... | |
int | checkdbg () |
Check if the current process is running into a debugger. More... | |
std::string | home () |
Return path to the current user's HOME. More... | |
std::string | mktmpdir (const char *prefix="") |
Return a writable temporary directory. More... | |
std::string | tmp () |
Return the system's temporary directory. More... | |
void | symlink (const qi::Path &source, const qi::Path &destination) |
Create a symlink from source to destination. More... | |
std::string | gethostname () |
Get the system's hostname. More... | |
int | isatty (int fd=1) |
Test if descriptor represents a terminal. More... | |
bool | fnmatch (const std::string &pattern, const std::string &string) |
Implement POSIX compliant fnmatch. More... | |
char * | strdup (const char *src) |
Implement POSIX compliant strdup. More... | |
int | snprintf (char *str, size_t size, const char *format,...) |
Implement POSIX compliant snprintf. More... | |
std::string | getenv (const char *var) |
Get an environment variable. More... | |
std::string | pathsep () |
Get the path separator. More... | |
int | setenv (const char *var, const char *value) |
Change or add an environment variable. More... | |
int | unsetenv (const char *var) |
Remove an environment variable. More... | |
std::string | timezone () |
Return the timezone. More... | |
void | sleep (unsigned int seconds) |
Sleep for the specified number of seconds. More... | |
void | msleep (unsigned int milliseconds) |
Sleep for the specified number of milliseconds. More... | |
QI_API_DEPRECATED int | gettimeofday (qi::os::timeval *tp) |
The gettimeofday() function shall obtain the current time. More... | |
qi::int64_t | ustime () |
Elapsed time since program started in microseconds. More... | |
std::pair< int64_t, int64_t > | cputime () |
Return CPU time used by the calling thread as a pair (userTime, systemTime) in microseconds. More... | |
qi::os::timeval | operator+ (const qi::os::timeval &lhs, const qi::os::timeval &rhs) |
Add two timeval together. More... | |
qi::os::timeval | operator+ (const qi::os::timeval &lhs, long us) |
Add a delay to a timeval (in microsecond) More... | |
qi::os::timeval | operator- (const qi::os::timeval &lhs, const qi::os::timeval &rhs) |
Substract two timeval together. More... | |
qi::os::timeval | operator- (const qi::os::timeval &lhs, long us) |
Substract a delay to a timeval (in microsecond) More... | |
void * | dlopen (const char *filename, int flag=-1) |
Load a dynamic library. More... | |
int | dlclose (void *handle) |
Decrements the reference count on the dynamic library. More... | |
void * | dlsym (void *handle, const char *symbol) |
Get the address where the symbol is loaded into memory. More... | |
const char * | dlerror () |
Returns a human readable string of the error code. More... | |
int | spawnvp (char *const argv[]) |
Create and execute a new process. More... | |
int | spawnlp (const char *argv,...) |
Create and execute a new process. More... | |
int | system (const char *command) |
Execute a shell command. More... | |
int | getpid () |
Get the process identifier. More... | |
int | gettid () |
Get the thread identifier. More... | |
int | waitpid (int pid, int *status) |
Wait for process to change state. More... | |
int | kill (int pid, int sig) |
Send a signal to a process. More... | |
bool | isProcessRunning (int pid, const std::string &fileName=std::string()) |
Check whether a process is running, given its file name and pid. More... | |
unsigned short | findAvailablePort (unsigned short port) |
Find the first available port starting at port number in parameter. More... | |
std::map< std::string, std::vector< std::string > > | hostIPAddrs (bool ipv6Addr=false) |
Find all network adapters and corresponding IPs. More... | |
void | setCurrentThreadName (const std::string &name) |
Set the current thread name to the string in parameter. More... | |
std::string | currentThreadName () |
returns the current thread name as a std::string More... | |
bool | setCurrentThreadCPUAffinity (const std::vector< int > &cpus) |
Set the CPU affinity for the current thread. More... | |
long | numberOfCPUs () |
Get the number of CPUs on the local machin. More... | |
std::string | getMachineId () |
Returns an unique uuid for the machine. More... | |
const Uuid & | getMachineIdAsUuid () |
Same as getMachineId but return a uuid and not its string representation. More... | |
const Uuid & | getProcessUuid () |
Returns an unique uuid for the process. More... | |
std::string | generateUuid () |
Generate a universally unique identifier. More... | |
size_t | memoryUsage (unsigned int pid) |
Get the memory usage of a process in kB. More... | |
PtrUid | ptrUid (void *address) |
template<typename T > | |
T | getEnvParam (const char *name, T defaultVal) |
Returns the value of the environment variableif set, the defaultVal otherwise. More... | |
template<typename N > | |
std::string | to_string (N n) |
(Arithmetic or Enum) N More... | |
OS abstraction layer. .
* This module provides some functions following POSIX convention to manipulate * the operating system layer in a cross-platform way. * * .. note:: * * Every path taken in parameter *must* be encoded in UTF-8. Every path * returned is encoded in UTF-8. *
int qi::os::checkdbg | ( | ) |
Check if the current process is running into a debugger.
Return CPU time used by the calling thread as a pair (userTime, systemTime) in microseconds.
std::string qi::os::currentThreadName | ( | ) |
returns the current thread name as a std::string
int qi::os::dlclose | ( | void * | handle | ) |
Decrements the reference count on the dynamic library.
handle | The dynamic library handle. |
* Decrements the reference count on the dynamic library handle. If the * reference count drops to zero and no other loaded library uses symbols in * it, then the dynamic library is unloaded. * * If there is an error you can look which one with dlerror function provided in * this same module. * * .. seealso:: :cpp:func:`qi::os::dlerror()` for more details on the error. * * .. versionadded:: 1.12 *
const char* qi::os::dlerror | ( | ) |
Returns a human readable string of the error code.
.. warning:: On windows, return value may be modified by another function unrelated to qi::os::dlopen familly. This function does not ensure that error value is 0 at initialisation. You may reset error value before a call to any qi::os::dl{open, sym, close} functions with a first call to this function.
* Returns a human readable string describing the most recent error that * occurred from :cpp:func:`qi::os::dlopen(const char*, int)`, * :cpp:func:`qi::os::dlsym(void*, const char*)` or * :cpp:func:`qi::os::dlclose(void*)` since the last call to * :cpp:func:`qi::os::dlerror()`. * * .. versionadded:: 1.12 *
void* qi::os::dlopen | ( | const char * | filename, |
int | flag = -1 |
||
) |
Load a dynamic library.
filename | Name of the dynamic library. |
flag | Flags used to load the dynamic library. |
* Loads the dynamic library file named by the null-terminated string filename * and returns an opaque "handle" for the dynamic library. If filename is NULL, * then the returned handle is for the main program. * * No flag is supported on windows platform. Otherwise, see ``man 0p dlfcn.h`` * for more information on flags available. If not given, ``RTLD_NOW`` is used. * * .. seealso:: :cpp:func:`qi::os::dlerror()` for more details on the error. * * .. versionadded:: 1.12 *
void* qi::os::dlsym | ( | void * | handle, |
const char * | symbol | ||
) |
Get the address where the symbol is loaded into memory.
handle | Handle on a dynamic library returned by dlopen(). |
symbol | The null-terminated symbol name. |
* If the symbol is not found in the specified library or any of the libraries * that were automatically loaded by :cpp:func:`qi::os::dlopen()` when that * library was loaded, :cpp:func:`qi::os::dlsym()` returns 0. * * .. seealso:: :cpp:func:`qi::os::dlerror()` for more details on the error. * * .. versionadded:: 1.12 *
unsigned short qi::os::findAvailablePort | ( | unsigned short | port | ) |
Find the first available port starting at port number in parameter.
port | First port tested, then each port following it is tested one by one until one available is found. |
* .. warning:: * * This function is not thread safe and suffers from a race condition. You * should avoid calling it and call listen on port 0 instead. This will pick * a random port with no race condition. * * .. versionadded:: 1.14 *
bool qi::os::fnmatch | ( | const std::string & | pattern, |
const std::string & | string | ||
) |
Implement POSIX compliant fnmatch.
FILE* qi::os::fopen | ( | const char * | filename, |
const char * | mode | ||
) |
Open a file and returns and handle on it.
filename | Path to the file (in UTF-8). |
mode | The mode. |
* Nothing special under POSIX systems, it's only useful for Windows, * where files should be open using a widestring. * * Refer to ``man 3 fopen`` for more informations on POSIX systems, and to the * documentation of _wfopen on MSDN to understand the Windows behaviors. * * .. versionadded:: 1.12 *
std::string qi::os::generateUuid | ( | ) |
Generate a universally unique identifier.
std::string qi::os::getenv | ( | const char * | var | ) |
Get an environment variable.
var | The environment variable to search for. |
* Searches the environment list to find the environment variable var, and * returns a pointer to the corresponding value string. * * .. versionadded:: 1.12 *
T qi::os::getEnvDefault | ( | const char * | name, |
T | defaultVal | ||
) |
Definition at line 20 of file getenv.hpp.
|
inline |
Returns the value of the environment variableif set, the defaultVal otherwise.
name | Name of the environment variable |
defaultVal | DefaultVal to return if the environment variable isn't set. |
std::string qi::os::gethostname | ( | ) |
Get the system's hostname.
std::string qi::os::getMachineId | ( | ) |
Returns an unique uuid for the machine.
The UUID is stored on disk and will stay unchanged as long as it is not removed. All programs running on the same machine will have the same UUID returned. An empty string is returned on failure.
const Uuid& qi::os::getMachineIdAsUuid | ( | ) |
Same as getMachineId but return a uuid and not its string representation.
int qi::os::getpid | ( | ) |
Get the process identifier.
const Uuid& qi::os::getProcessUuid | ( | ) |
Returns an unique uuid for the process.
int qi::os::gettid | ( | ) |
Get the thread identifier.
QI_API_DEPRECATED int qi::os::gettimeofday | ( | qi::os::timeval * | tp | ) |
The gettimeofday() function shall obtain the current time.
tp | The timeval structure used to return the current time. |
The gettimeofday() function shall obtain the current time, expressed as seconds and microseconds since the Epoch, and store it in the timeval structure pointed to by tp. The resolution of the system clock is unspecified. This clock is subject to NTP adjustments.
std::string qi::os::home | ( | ) |
Return path to the current user's HOME.
std::map<std::string, std::vector<std::string> > qi::os::hostIPAddrs | ( | bool | ipv6Addr = false | ) |
Find all network adapters and corresponding IPs.
ipv6Addr | Look for IPv6 addresses instead of IPv4 ones. |
* .. versionadded:: 1.14 *
int qi::os::isatty | ( | int | fd = 1 | ) |
Test if descriptor represents a terminal.
fd | File descriptor. |
* .. versionadded:: 1.22 *
bool qi::os::isProcessRunning | ( | int | pid, |
const std::string & | fileName = std::string() |
||
) |
Check whether a process is running, given its file name and pid.
pid | The PID to check. |
fileName | The name of the process: the executable file name with no .exe or _d.exe extension. |
int qi::os::kill | ( | int | pid, |
int | sig | ||
) |
Send a signal to a process.
pid | PID to kill. |
sig | Signal to deliver to the process. |
* The kill() function shall send a signal to a process or a group of processes * specified by pid. * * Return value (rc): * * - rc = 0 means that everything went well. * - rc != 0 means that an error occurred. (For instance, no process * corresponding to the pid was found). * * .. versionadded:: 1.14 *
size_t qi::os::memoryUsage | ( | unsigned int | pid | ) |
Get the memory usage of a process in kB.
pid | PID of a process |
std::string qi::os::mktmpdir | ( | const char * | prefix = "" | ) |
Return a writable temporary directory.
prefix | Prefix of the tmp file (in UTF-8). |
* The caller is responsible for destroying the returned directory. This will * create a unique directory in the temporary directory returned by * :cpp:func:`qi::os::tmp()`. * * The specified prefix will be prepended to a uniquely generated name. * * .. versionadded:: 1.12.1 *
void qi::os::msleep | ( | unsigned int | milliseconds | ) |
Sleep for the specified number of milliseconds.
milliseconds | Number of milliseconds to sleep. |
* Under Linux/OSX it will not be disturbed by eventual signals sent to process. * Makes the calling thread sleep until millliseconds have elapsed or a signal * which is not ignored arrives. * * .. seealso:: :cpp:func:`qi::os::sleep(unsigned int)` * * .. versionadded:: 1.12 *
long qi::os::numberOfCPUs | ( | ) |
Get the number of CPUs on the local machin.
qi::os::timeval qi::os::operator+ | ( | const qi::os::timeval & | lhs, |
const qi::os::timeval & | rhs | ||
) |
Add two timeval together.
lhs | First timeval |
rhs | Second timeval |
qi::os::timeval qi::os::operator+ | ( | const qi::os::timeval & | lhs, |
long | us | ||
) |
Add a delay to a timeval (in microsecond)
lhs | Frist timeval |
us | Second timeval |
qi::os::timeval qi::os::operator- | ( | const qi::os::timeval & | lhs, |
const qi::os::timeval & | rhs | ||
) |
Substract two timeval together.
lhs | First timeval |
rhs | Second timeval |
qi::os::timeval qi::os::operator- | ( | const qi::os::timeval & | lhs, |
long | us | ||
) |
Substract a delay to a timeval (in microsecond)
lhs | Frist timeval |
us | Second timeval |
std::string qi::os::pathsep | ( | ) |
Get the path separator.
* It is the separator we can found in PATH env for example. *
PtrUid qi::os::ptrUid | ( | void * | address | ) |
bool qi::os::setCurrentThreadCPUAffinity | ( | const std::vector< int > & | cpus | ) |
Set the CPU affinity for the current thread.
cpus | a vector of CPU core ids |
* When CPU Affinity is set, the current thread will only be allowed * to run on the given set of cores. * * .. versionadded:: 1.22 *
void qi::os::setCurrentThreadName | ( | const std::string & | name | ) |
Set the current thread name to the string in parameter.
name | The new name of the current thread. |
Prefer using ScopedThreadName that will restore the thread name on exit.
int qi::os::setenv | ( | const char * | var, |
const char * | value | ||
) |
Change or add an environment variable.
var | The variable name. |
value | The value of the variable. |
errno
is set to indicate the cause of the error.* Adds the variable name to the environment with the value in argument if name * does not already exist. Else changes its value. * * .. versionadded:: 1.12 *
void qi::os::sleep | ( | unsigned int | seconds | ) |
Sleep for the specified number of seconds.
seconds | Number of seconds to sleep. |
* Under Linux/OS X it will not be disturbed by eventual signals. Makes the * calling thread sleep until seconds have elapsed or a signal arrives which * is not ignored. * * .. todo:: * Explain the behavior of signals on windows when sleeping. * * .. seealso:: :cpp:func:`qi::os::msleep(unsigned int)` * * .. versionadded:: 1.12 *
int qi::os::snprintf | ( | char * | str, |
size_t | size, | ||
const char * | format, | ||
... | |||
) |
Implement POSIX compliant snprintf.
* .. seealso:: http://en.cppreference.com/w/cpp/io/c/fprintf *
int qi::os::spawnlp | ( | const char * | argv, |
... | |||
) |
Create and execute a new process.
argv | Path to the file to be executed. |
... | The command line arguments of the new process as var args. |
* Creates and executes a new process. * * .. versionadded:: 1.12 *
int qi::os::spawnvp | ( | char *const | argv[] | ) |
Create and execute a new process.
argv | The command line arguments of the new process as an array (NULL terminated). |
* Creates and executes a new process. * * .. versionadded:: 1.12 *
int qi::os::stat | ( | const char * | filename, |
struct stat * | pstat | ||
) |
Get file status.
filename | Path to the file (in UTF-8). |
pstat | A pointer to a stat structure that will be filled by the function. |
* Stats the file pointed to by filename and fills in pstat. * You need to include <sys/stat.h> to get access to struct. * * .. todo:: * explain how to use stat on windows ! * * .. versionadded:: 1.12 *
char* qi::os::strdup | ( | const char * | src | ) |
Implement POSIX compliant strdup.
Create a symlink from source to destination.
int qi::os::system | ( | const char * | command | ) |
Execute a shell command.
command | Command to execute. |
* Executes a command by calling ``/bin/sh -c command``, and returns when the * command has completed. * * .. versionadded:: 1.12 *
std::string qi::os::timezone | ( | ) |
Return the timezone.
* .. versionadded:: 1.22 *
std::string qi::os::tmp | ( | ) |
Return the system's temporary directory.
The directory is writable and exists. The caller is responsible for destroying the temporary files it creates.
std::string qi::os::to_string | ( | N | n | ) |
int qi::os::unsetenv | ( | const char * | var | ) |
Remove an environment variable.
var | The variable name. If null, the behavior is unspecified. |
errno
is set to indicate the cause of the error.* Removes the variable name from the environment. If the variable did not already exist or was * unset, the environment is unchanged and the function returns with a success. *
getenv(var).empty()
qi::int64_t qi::os::ustime | ( | ) |
Elapsed time since program started in microseconds.
int qi::os::waitpid | ( | int | pid, |
int * | status | ||
) |
Wait for process to change state.
pid | Pid to wait. |
status | Status returned by the process. |
* Suspends execution of the calling process until a child specified by pid * argument changes of state. * * Return values (rc): * * - rc = 0 means that everything went well. * - rc > 0 means that an error occurred. (For instance, no process * corresponding to the pid was found). The exact value is an errno code. * - rc < 0 means that the child was killed by a signal. The value of the signal * is -rc. * * .. versionadded:: 1.12 *