libqi-api  release-2.5.3-2016-11-18
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
os.hpp
Go to the documentation of this file.
1 #pragma once
2 /*
3  * Copyright (c) 2012, 2013 Aldebaran Robotics. All rights reserved.
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the COPYING file.
6  */
7 
8 #ifndef _QI_OS_HPP_
9 # define _QI_OS_HPP_
10 
11 # include <cstdio>
12 # include <string>
13 # include <map>
14 # include <vector>
15 # include <qi/api.hpp>
16 # include <qi/types.hpp>
17 # include <qi/path.hpp>
18 # include <qi/clock.hpp>
19 
20 # include <boost/lexical_cast.hpp>
21 
22 #ifdef _WIN32
23 #ifndef SIGKILL
24 #define SIGKILL 9
25 #endif
26 #endif
27 
28 struct stat;
29 
30 namespace qi {
31 
45  namespace os {
62  QI_API FILE* fopen(const char *filename, const char *mode);
79  QI_API int stat(const char *filename, struct stat *pstat);
84  QI_API int checkdbg();
89  QI_API std::string home();
105  QI_API std::string mktmpdir(const char *prefix = "");
113  QI_API std::string tmp();
117  QI_API void symlink(const qi::Path& source, const qi::Path& destination);
122  QI_API std::string gethostname();
133  QI_API int isatty(int fd = 1);
134 
138  QI_API bool fnmatch(const std::string &pattern, const std::string &string);
139 
140  // lib C
144  QI_API char* strdup(const char *src);
152  QI_API int snprintf(char *str, size_t size, const char *format, ...);
153 
167  QI_API std::string getenv(const char *var);
168 
177  QI_API std::string pathsep();
178 
192  QI_API int setenv(const char *var, const char *value);
201  QI_API std::string timezone();
202 
203  // time
221  QI_API void sleep(unsigned int seconds);
236  QI_API void msleep(unsigned int milliseconds);
240  struct QI_API timeval {
243 
244  timeval() : tv_sec(0), tv_usec(0) {}
245  timeval(int64_t sec, int64_t usec) : tv_sec(sec), tv_usec(usec) {}
246  explicit timeval(int64_t usec);
247  explicit timeval(const qi::Duration &);
248  explicit timeval(const qi::SystemClockTimePoint &);
249  };
250 
277  QI_API std::pair<int64_t, int64_t> cputime();
278 
285  const qi::os::timeval &rhs);
292  long us);
299  const qi::os::timeval &rhs);
306  long us);
307 
327  QI_API void *dlopen(const char *filename, int flag = -1);
346  QI_API int dlclose(void *handle);
363  QI_API void *dlsym(void *handle, const char *symbol);
386  QI_API const char *dlerror();
387 
388  // process management
401  QI_API int spawnvp(char *const argv[]);
414  QI_API int spawnlp(const char* argv, ...);
428  QI_API int system(const char *command);
432  QI_API int getpid();
436  QI_API int gettid();
458  QI_API int waitpid(int pid, int* status);
478  QI_API int kill(int pid, int sig);
479 
487  QI_API bool isProcessRunning(int pid, const std::string& fileName = std::string());
488 
505  QI_API unsigned short findAvailablePort(unsigned short port);
515  QI_API std::map<std::string, std::vector<std::string> > hostIPAddrs(bool ipv6Addr = false);
516 
525  QI_API void setCurrentThreadName(const std::string &name);
531  QI_API std::string currentThreadName();
532 
533 
540  public:
541  ScopedThreadName(const std::string& newName) {
542  _oldName = currentThreadName();
543  setCurrentThreadName(newName);
544  };
546  setCurrentThreadName(_oldName);
547  }
548  private:
549  std::string _oldName;
550  };
551 
552 
566  QI_API bool setCurrentThreadCPUAffinity(const std::vector<int> &cpus);
571  QI_API long numberOfCPUs();
580  QI_API std::string getMachineId();
586  QI_API std::string generateUuid();
593  QI_API size_t memoryUsage(unsigned int pid);
594 
595 
604  template <typename T>
605  inline T getEnvParam(const char* name, T defaultVal)
606  {
607  std::string sval = qi::os::getenv(name);
608  if (sval.empty())
609  return defaultVal;
610  else
611  return boost::lexical_cast<T>(sval);
612  }
613 
614  }
615 }
616 
617 #endif // _QI_OS_HPP_
qi::os::timeval operator-(const qi::os::timeval &lhs, const qi::os::timeval &rhs)
Substract two timeval together.
std::string getenv(const char *var)
Get an environment variable.
int gettid()
Get the thread identifier.
int64_t int64_t
Definition: types.hpp:61
timeval(int64_t sec, int64_t usec)
Definition: os.hpp:245
int spawnlp(const char *argv,...)
Create and execute a new process.
#define QI_API
Definition: api.hpp:33
Set the current thread name and restore it after use.
Definition: os.hpp:539
int stat(const char *filename, struct stat *pstat)
Get file status.
int setenv(const char *var, const char *value)
Change or add an environment variable.
bool isProcessRunning(int pid, const std::string &fileName=std::string())
Check whether a process is running, given its name and pid.
int kill(int pid, int sig)
Send a signal to a process.
std::string generateUuid()
Generate a universally unique identifier.
T getEnvParam(const char *name, T defaultVal)
Returns the value of the environment variableif set, the defaultVal otherwise.
Definition: os.hpp:605
std::string timezone()
Return the timezone.
void msleep(unsigned int milliseconds)
Sleep for the specified number of milliseconds.
int spawnvp(char *const argv[])
Create and execute a new process.
std::pair< int64_t, int64_t > cputime()
Return CPU time used by the calling thread as a pair (userTime, systemTime) in microseconds.
void symlink(const qi::Path &source, const qi::Path &destination)
Create a symlink from source to destination.
int checkdbg()
Check if the current process is running into a debugger.
dll import/export and compiler message
const char * dlerror()
Returns a human readable string of the error code.
std::string home()
Return path to the current user's HOME.
QI_API_DEPRECATED int gettimeofday(qi::os::timeval *tp)
The gettimeofday() function shall obtain the current time.
std::string currentThreadName()
returns the current thread name as a std::string
void * dlsym(void *handle, const char *symbol)
Get the address where the symbol is loaded into memory.
NanoSeconds Duration
Definition: clock.hpp:32
ScopedThreadName(const std::string &newName)
Definition: os.hpp:541
void sleep(unsigned int seconds)
Sleep for the specified number of seconds.
std::string mktmpdir(const char *prefix="")
Return a writable temporary directory.
std::map< std::string, std::vector< std::string > > hostIPAddrs(bool ipv6Addr=false)
Find all network adapters and corresponding IPs.
bool fnmatch(const std::string &pattern, const std::string &string)
Implement POSIX compliant fnmatch.
SystemClock::time_point SystemClockTimePoint
System clock time point.
Definition: clock.hpp:213
bool setCurrentThreadCPUAffinity(const std::vector< int > &cpus)
Set the CPU affinity for the current thread.
qi::int64_t tv_sec
seconds
Definition: os.hpp:241
std::string tmp()
Return the system's temporary directory.
std::string gethostname()
Get the system's hostname.
long numberOfCPUs()
Get the number of CPUs on the local machin.
qi::int64_t tv_usec
microseconds
Definition: os.hpp:242
The Path class allow handling filesystem path in a cross-platform maner. <includename>qi/path.hpp</includename> The class assume that all string are encoded in UTF-8 if not specified otherwise.
Definition: path.hpp:34
int isatty(int fd=1)
Test if descriptor represents a terminal.
std::string getMachineId()
Returns an unique uuid for the machine.
qi::int64_t ustime()
Elapsed time since program started in microseconds.
struct similar to POSIX timeval
Definition: os.hpp:240
int dlclose(void *handle)
Decrements the reference count on the dynamic library.
std::string pathsep()
Get the path separator.
int waitpid(int pid, int *status)
Wait for process to change state.
size_t memoryUsage(unsigned int pid)
Get the memory usage of a process in kB.
qi::os::timeval operator+(const qi::os::timeval &lhs, const qi::os::timeval &rhs)
Add two timeval together.
FILE * fopen(const char *filename, const char *mode)
Open a file and returns and handle on it.
void setCurrentThreadName(const std::string &name)
Set the current thread name to the string in parameter.
int system(const char *command)
Execute a shell command.
#define QI_API_DEPRECATED
Compiler flags to mark a function as deprecated. It will generate a compiler warning.
Definition: macro.hpp:39
int snprintf(char *str, size_t size, const char *format,...)
Implement POSIX compliant snprintf.
void * dlopen(const char *filename, int flag=-1)
Load a dynamic library.
int getpid()
Get the process identifier.
char * strdup(const char *src)
Implement POSIX compliant strdup.
unsigned short findAvailablePort(unsigned short port)
Find the first available port starting at port number in parameter.