Logo ROOT  
Reference Guide
TUnixSystem.h
Go to the documentation of this file.
1 // @(#)root/unix:$Id$
2 // Author: Fons Rademakers 15/09/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TUnixSystem
13 #define ROOT_TUnixSystem
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TUnixSystem //
19 // //
20 // Class providing an interface to the UNIX Operating System. //
21 // //
22 //////////////////////////////////////////////////////////////////////////
23 
24 #include "TSystem.h"
25 #include "TSysEvtHandler.h"
26 #include "TTimer.h"
27 
29 
30 
31 class TUnixSystem : public TSystem {
32 
33 private:
34  void FillWithCwd(char *cwd) const;
35 
36 protected:
37  const char *GetLinkedLibraries() override;
38 
39  // static functions providing semi-low level interface to raw Unix
40  static int UnixMakedir(const char *name);
41  static void *UnixOpendir(const char *name);
42  static const char *UnixGetdirentry(void *dir);
43  static const char *UnixHomedirectory(const char *user = nullptr);
44  static const char *UnixHomedirectory(const char *user, char *path, char *mydir);
45  static Long64_t UnixNow();
46  static int UnixWaitchild();
47  static int UnixSetitimer(Long_t ms);
48  static int UnixSelect(Int_t nfds, TFdSet *readready, TFdSet *writeready,
49  Long_t timeout);
50  static void UnixSignal(ESignals sig, SigHandler_t h);
51  static const char *UnixSigname(ESignals sig);
52  static void UnixSigAlarmInterruptsSyscalls(Bool_t set);
53  static void UnixResetSignal(ESignals sig);
54  static void UnixResetSignals();
55  static void UnixIgnoreSignal(ESignals sig, Bool_t ignore);
56  static int UnixFilestat(const char *path, FileStat_t &buf);
57  static int UnixFSstat(const char *path, Long_t *id, Long_t *bsize,
58  Long_t *blocks, Long_t *bfree);
59  static int UnixTcpConnect(const char *hostname, int port, int tcpwindowsize);
60  static int UnixUdpConnect(const char *hostname, int port);
61  static int UnixUnixConnect(int port);
62  static int UnixUnixConnect(const char *path);
63  static int UnixTcpService(int port, Bool_t reuse, int backlog,
64  int tcpwindowsize);
65  static int UnixUdpService(int port, int backlog);
66  static int UnixUnixService(int port, int backlog);
67  static int UnixUnixService(const char *sockpath, int backlog);
68  static int UnixRecv(int sock, void *buf, int len, int flag);
69  static int UnixSend(int sock, const void *buf, int len, int flag);
70 
71 public:
72  TUnixSystem();
73  virtual ~TUnixSystem();
74 
75  //---- Misc -------------------------------------------------
76  Bool_t Init() override;
77  void SetProgname(const char *name) override;
78  void SetDisplay() override;
79  const char *GetError() override;
80  const char *HostName() override;
81 
82  //---- EventLoop --------------------------------------------
83  void DispatchOneEvent(Bool_t pendingOnly = kFALSE) override;
84  Int_t Select(TList *active, Long_t timeout) override;
85  Int_t Select(TFileHandler *fh, Long_t timeout) override;
86 
87  //---- Handling of system events ----------------------------
88  void CheckChilds();
91  void DispatchSignals(ESignals sig);
92  void AddSignalHandler(TSignalHandler *sh) override;
94  void ResetSignal(ESignals sig, Bool_t reset = kTRUE) override;
95  void ResetSignals() override;
96  void IgnoreSignal(ESignals sig, Bool_t ignore = kTRUE) override;
97  void SigAlarmInterruptsSyscalls(Bool_t set) override;
98  void AddFileHandler(TFileHandler *fh) override;
100 
101  //---- Floating Point Exceptions Control --------------------
102  Int_t GetFPEMask() override;
103  Int_t SetFPEMask(Int_t mask = kDefaultMask) override;
104 
105  //---- Time & Date ------------------------------------------
106  TTime Now() override;
107  void AddTimer(TTimer *ti) override;
108  TTimer *RemoveTimer(TTimer *ti) override;
109  void ResetTimer(TTimer *ti) override;
111  void Sleep(UInt_t milliSec) override;
112 
113  //---- Processes --------------------------------------------
114  Int_t Exec(const char *shellcmd) override;
115  FILE *OpenPipe(const char *shellcmd, const char *mode) override;
116  int ClosePipe(FILE *pipe) override;
117  void Exit(int code, Bool_t mode = kTRUE) override;
118  void Abort(int code = 0) override;
119  int GetPid() override;
120  void StackTrace() override;
121 
122  //---- Directories ------------------------------------------
123  int MakeDirectory(const char *name) override;
124  void *OpenDirectory(const char *name) override;
125  void FreeDirectory(void *dirp) override;
126  const char *GetDirEntry(void *dirp) override;
127  Bool_t ChangeDirectory(const char *path) override;
128  const char *WorkingDirectory() override;
129  std::string GetWorkingDirectory() const override;
130  const char *HomeDirectory(const char *userName = nullptr) override;
131  std::string GetHomeDirectory(const char *userName = nullptr) const override;
132  const char *TempDirectory() const override;
133  FILE *TempFileName(TString &base, const char *dir = nullptr) override;
134 
135  //---- Paths & Files ----------------------------------------
136  const char *PrependPathName(const char *dir, TString& name) override;
137  Bool_t ExpandPathName(TString &patbuf) override;
138  char *ExpandPathName(const char *path) override;
139  Bool_t AccessPathName(const char *path, EAccessMode mode = kFileExists) override;
140  Bool_t IsPathLocal(const char *path) override;
141  int CopyFile(const char *from, const char *to, Bool_t overwrite = kFALSE) override;
142  int Rename(const char *from, const char *to) override;
143  int Link(const char *from, const char *to) override;
144  int Symlink(const char *from, const char *to) override;
145  int Unlink(const char *name) override;
146  int GetPathInfo(const char *path, FileStat_t &buf) override;
147  int GetFsInfo(const char *path, Long_t *id, Long_t *bsize,
148  Long_t *blocks, Long_t *bfree) override;
149  int Chmod(const char *file, UInt_t mode) override;
150  int Umask(Int_t mask) override;
151  int Utime(const char *file, Long_t modtime, Long_t actime) override;
152  const char *FindFile(const char *search, TString& file, EAccessMode mode = kFileExists) override;
153 
154  //---- Users & Groups ---------------------------------------
155  Int_t GetUid(const char *user = nullptr) override;
156  Int_t GetGid(const char *group = nullptr) override;
157  Int_t GetEffectiveUid() override;
158  Int_t GetEffectiveGid() override;
159  UserGroup_t *GetUserInfo(Int_t uid) override;
160  UserGroup_t *GetUserInfo(const char *user = nullptr) override;
161  UserGroup_t *GetGroupInfo(Int_t gid) override;
162  UserGroup_t *GetGroupInfo(const char *group = nullptr) override;
163 
164  //---- Environment Manipulation -----------------------------
165  const char *Getenv(const char *name) override;
166  void Setenv(const char *name, const char *value) override;
167  void Unsetenv(const char *name) override;
168 
169  //---- System Logging ---------------------------------------
170  void Openlog(const char *name, Int_t options, ELogFacility facility) override;
171  void Syslog(ELogLevel level, const char *mess) override;
172  void Closelog() override;
173 
174  //---- Standard Output redirection --------------------------
175  Int_t RedirectOutput(const char *name, const char *mode = "a",
176  RedirectHandle_t *h = nullptr) override;
177 
178  //---- Dynamic Loading --------------------------------------
179  void AddDynamicPath(const char *lib) override;
180  const char *GetDynamicPath() override;
181  void SetDynamicPath(const char *lib) override;
182  const char *FindDynamicLibrary(TString &lib, Bool_t quiet = kFALSE) override;
183  Func_t DynFindSymbol(const char *module, const char *entry) override;
184  int Load(const char *module, const char *entry = "", Bool_t system = kFALSE) override;
185  void Unload(const char *module) override;
186  void ListSymbols(const char *module, const char *re = "") override;
187  void ListLibraries(const char *regexp = "") override;
188 
189  //---- RPC --------------------------------------------------
190  TInetAddress GetHostByName(const char *server) override;
191  TInetAddress GetPeerName(int sock) override;
192  TInetAddress GetSockName(int sock) override;
193  int GetServiceByName(const char *service) override;
194  char *GetServiceByPort(int port) override;
195  int ConnectService(const char *server, int port, int tcpwindowsize, const char *protocol = "tcp");
196  int OpenConnection(const char *server, int port, int tcpwindowsize = -1, const char *protocol = "tcp") override;
197  int AnnounceTcpService(int port, Bool_t reuse, int backlog, int tcpwindowsize = -1) override;
198  int AnnounceUdpService(int port, int backlog) override;
199  int AnnounceUnixService(int port, int backlog) override;
200  int AnnounceUnixService(const char *sockpath, int backlog) override;
201  int AcceptConnection(int sock) override;
202  void CloseConnection(int sock, Bool_t force = kFALSE) override;
203  int RecvRaw(int sock, void *buffer, int length, int flag) override;
204  int SendRaw(int sock, const void *buffer, int length, int flag) override;
205  int RecvBuf(int sock, void *buffer, int length) override;
206  int SendBuf(int sock, const void *buffer, int length) override;
207  int SetSockOpt(int sock, int option, int val) override;
208  int GetSockOpt(int sock, int option, int *val) override;
209 
210  //---- System, CPU and Memory info
211  int GetSysInfo(SysInfo_t *info) const override;
212  int GetCpuInfo(CpuInfo_t *info, Int_t sampleTime = 1000) const override;
213  int GetMemInfo(MemInfo_t *info) const override;
214  int GetProcInfo(ProcInfo_t *info) const override;
215 
216  ClassDefOverride(TUnixSystem,0) //Interface to Unix OS services
217 };
218 
219 #endif
void(* SigHandler_t)(ESignals)
Definition: TUnixSystem.h:28
int GetSysInfo(SysInfo_t *info) const override
Returns static system info, like OS type, CPU type, number of CPUs RAM size, etc into the SysInfo_t s...
Bool_t ExpandPathName(TString &patbuf) override
Expand a pathname getting rid of special shell characters like ~.
int AnnounceTcpService(int port, Bool_t reuse, int backlog, int tcpwindowsize=-1) override
Announce TCP/IP service.
const char * FindDynamicLibrary(TString &lib, Bool_t quiet=kFALSE) override
Returns the path of a shared library (searches for library in the shared library search path)...
long long Long64_t
Definition: RtypesCore.h:71
static const char * UnixHomedirectory(const char *user=nullptr)
Returns the user's home directory.
int AnnounceUdpService(int port, int backlog) override
Announce UDP service.
void Syslog(ELogLevel level, const char *mess) override
Send mess to syslog daemon.
int SetSockOpt(int sock, int option, int val) override
Set socket option.
const char * WorkingDirectory() override
Return working directory.
std::string GetWorkingDirectory() const override
Return working directory.
static void UnixResetSignal(ESignals sig)
Restore old signal handler for specified signal.
virtual ~TUnixSystem()
Reset to original state.
Bool_t ChangeDirectory(const char *path) override
Change directory. Returns kTRUE in case of success, kFALSE otherwise.
void StackTrace() override
Print a stack trace.
static int UnixFilestat(const char *path, FileStat_t &buf)
Get info about a file.
static int UnixTcpConnect(const char *hostname, int port, int tcpwindowsize)
Open a TCP/IP connection to server and connect to a service (i.e.
static int UnixRecv(int sock, void *buf, int len, int flag)
Receive exactly length bytes into buffer.
void Openlog(const char *name, Int_t options, ELogFacility facility) override
Open connection to system log daemon.
void AddDynamicPath(const char *lib) override
Add a new directory to the dynamic path.
Bool_t DispatchTimers(Bool_t mode)
Handle and dispatch timers.
const char * GetLinkedLibraries() override
Get list of shared libraries loaded at the start of the executable.
const char * GetDirEntry(void *dirp) override
Get next Unix file system directory entry. Returns 0 if no more entries.
This class represents an Internet Protocol (IP) address.
Definition: TInetAddress.h:36
void Abort(int code=0) override
Abort the application.
static Long64_t UnixNow()
Get current time in milliseconds since 0:00 Jan 1 1995.
Basic string class.
Definition: TString.h:131
Int_t Select(TList *active, Long_t timeout) override
Select on file descriptors.
int SendRaw(int sock, const void *buffer, int length, int flag) override
Send exactly length bytes from buffer.
Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists) override
Returns FALSE if one can access a file using the specified access mode.
int Unlink(const char *name) override
Unlink, i.e.
int Utime(const char *file, Long_t modtime, Long_t actime) override
Set a files modification and access times.
void Setenv(const char *name, const char *value) override
Set environment variable.
Int_t GetEffectiveUid() override
Returns the effective user id.
const char * HostName() override
Return the system's host name.
Int_t GetGid(const char *group=nullptr) override
Returns the group's id. If group = 0, returns current user's group.
TSignalHandler * RemoveSignalHandler(TSignalHandler *sh) override
Remove a signal handler from list of signal handlers.
Basic time type with millisecond precision.
Definition: TTime.h:27
static void UnixSigAlarmInterruptsSyscalls(Bool_t set)
When the argument is true the SIGALRM signal handler is set so that interrupted syscalls will not be ...
void FillWithCwd(char *cwd) const
Fill buffer with current working directory.
int ConnectService(const char *server, int port, int tcpwindowsize, const char *protocol="tcp")
Connect to service servicename on server servername.
void Unsetenv(const char *name) override
Unset environment variable.
void ListSymbols(const char *module, const char *re="") override
List symbols in a shared library.
static int UnixSelect(Int_t nfds, TFdSet *readready, TFdSet *writeready, Long_t timeout)
Wait for events on the file descriptors specified in the readready and writeready masks or for timeou...
int RecvRaw(int sock, void *buffer, int length, int flag) override
Receive exactly length bytes into buffer.
int MakeDirectory(const char *name) override
Make a Unix file system directory.
int Umask(Int_t mask) override
Set the process file creation mode mask.
TFileHandler * RemoveFileHandler(TFileHandler *fh) override
Remove a file handler from the list of file handlers.
TInetAddress GetHostByName(const char *server) override
Get Internet Protocol (IP) address of host.
void Unload(const char *module) override
Unload a shared library.
void IgnoreSignal(ESignals sig, Bool_t ignore=kTRUE) override
If ignore is true ignore the specified signal, else restore previous behaviour.
static int UnixSend(int sock, const void *buf, int len, int flag)
Send exactly length bytes from buffer.
TTimer * RemoveTimer(TTimer *ti) override
Remove timer from list of system timers.
Func_t DynFindSymbol(const char *module, const char *entry) override
dynamic linking of module
int Link(const char *from, const char *to) override
Create a link from file1 to file2.
static int UnixSetitimer(Long_t ms)
Set interval timer to time-out in ms milliseconds.
int GetFsInfo(const char *path, Long_t *id, Long_t *bsize, Long_t *blocks, Long_t *bfree) override
Get info about a file system: id, bsize, bfree, blocks.
static void UnixResetSignals()
Restore old signal handlers.
int RecvBuf(int sock, void *buffer, int length) override
Receive a buffer headed by a length indicator.
void * OpenDirectory(const char *name) override
Open a Unix file system directory. Returns 0 if directory does not exist.
const char * TempDirectory() const override
Return a user configured or systemwide directory to create temporary files in.
char * GetServiceByPort(int port) override
Get name of internet service.
void SetDynamicPath(const char *lib) override
Set the dynamic path to a new value.
Bool_t IsPathLocal(const char *path) override
Returns TRUE if the url in 'path' points to the local file system.
Int_t SetFPEMask(Int_t mask=kDefaultMask) override
Set which conditions trigger a floating point exception.
ELogLevel
Definition: TSystem.h:54
int GetProcInfo(ProcInfo_t *info) const override
Returns cpu and memory used by this process into the ProcInfo_t structure.
A doubly linked list.
Definition: TList.h:44
int GetPathInfo(const char *path, FileStat_t &buf) override
Get info about a file.
const char * HomeDirectory(const char *userName=nullptr) override
Return the user's home directory.
int Load(const char *module, const char *entry="", Bool_t system=kFALSE) override
Load a shared library.
void CloseConnection(int sock, Bool_t force=kFALSE) override
Close socket.
ESignals
int Chmod(const char *file, UInt_t mode) override
Set the file permission bits. Returns -1 in case or error, 0 otherwise.
UserGroup_t * GetGroupInfo(Int_t gid) override
Returns all group info in the UserGroup_t structure.
void Sleep(UInt_t milliSec) override
Sleep milliSec milliseconds.
static constexpr double ms
int ClosePipe(FILE *pipe) override
Close the pipe.
void ResetSignals() override
Reset signals handlers to previous behaviour.
Int_t GetEffectiveGid() override
Returns the effective group id.
unsigned int UInt_t
Definition: RtypesCore.h:44
int OpenConnection(const char *server, int port, int tcpwindowsize=-1, const char *protocol="tcp") override
Open a connection to a service on a server.
int SendBuf(int sock, const void *buffer, int length) override
Send a buffer headed by a length indicator.
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:51
Bool_t CheckDescriptors()
Check if there is activity on some file descriptors and call their Notify() member.
void ListLibraries(const char *regexp="") override
List all loaded shared libraries.
void SetProgname(const char *name) override
Set the application name (from command line, argv[0]) and copy it in gProgName.
static int UnixWaitchild()
Wait till child is finished.
void DispatchOneEvent(Bool_t pendingOnly=kFALSE) override
Dispatch a single event.
void FreeDirectory(void *dirp) override
Close a Unix file system directory.
static int UnixUnixService(int port, int backlog)
Open a socket, bind to it and start listening for Unix domain connections to it.
static const char * UnixSigname(ESignals sig)
Return the signal name associated with a signal.
static int UnixFSstat(const char *path, Long_t *id, Long_t *bsize, Long_t *blocks, Long_t *bfree)
Get info about a file system: id, bsize, bfree, blocks.
void ResetTimer(TTimer *ti) override
Reset a-sync timer.
#define h(i)
Definition: RSha256.hxx:106
TInetAddress GetPeerName(int sock) override
Get Internet Protocol (IP) address of remote host and port #.
const Bool_t kFALSE
Definition: RtypesCore.h:90
static void UnixSignal(ESignals sig, SigHandler_t h)
Set a signal handler for a signal.
void AddSignalHandler(TSignalHandler *sh) override
Add a signal handler to list of system signal handlers.
long Long_t
Definition: RtypesCore.h:52
static int UnixMakedir(const char *name)
Make a Unix file system directory.
std::string GetHomeDirectory(const char *userName=nullptr) const override
Return the user's home directory.
int AnnounceUnixService(int port, int backlog) override
Announce unix domain service on path "kServerPath/<port>".
static int UnixUdpService(int port, int backlog)
Open a socket, bind to it and start listening for UDP connections on the port.
Int_t RedirectOutput(const char *name, const char *mode="a", RedirectHandle_t *h=nullptr) override
Redirect standard output (stdout, stderr) to the specified file.
int GetPid() override
Get process id.
TTime Now() override
Get current time in milliseconds since 0:00 Jan 1 1995.
TInetAddress GetSockName(int sock) override
Get Internet Protocol (IP) address of host and port #.
static int UnixUnixConnect(int port)
Connect to a Unix domain socket.
void AddTimer(TTimer *ti) override
Add timer to list of system timers.
static int UnixTcpService(int port, Bool_t reuse, int backlog, int tcpwindowsize)
Open a socket, bind to it and start listening for TCP/IP connections on the port. ...
ELogFacility
Definition: TSystem.h:65
int Symlink(const char *from, const char *to) override
Create a symlink from file1 to file2.
FILE * OpenPipe(const char *shellcmd, const char *mode) override
Open a pipe.
void CheckChilds()
Check if children have finished.
static void UnixIgnoreSignal(ESignals sig, Bool_t ignore)
If ignore is true ignore the specified signal, else restore previous behaviour.
const char * GetDynamicPath() override
Return the dynamic path (used to find shared libraries).
const char * PrependPathName(const char *dir, TString &name) override
Concatenate a directory and a file name.
int GetServiceByName(const char *service) override
Get port # of internet service.
void Exit(int code, Bool_t mode=kTRUE) override
Exit the application.
const char * Getenv(const char *name) override
Get environment variable.
static void * UnixOpendir(const char *name)
Open a directory.
EAccessMode
Definition: TSystem.h:42
void Closelog() override
Close connection to system log daemon.
int GetCpuInfo(CpuInfo_t *info, Int_t sampleTime=1000) const override
Returns cpu load average and load info into the CpuInfo_t structure.
int Rename(const char *from, const char *to) override
Rename a file. Returns 0 when successful, -1 in case of failure.
typedef void((*Func_t)())
UserGroup_t * GetUserInfo(Int_t uid) override
Returns all user info in the UserGroup_t structure.
const char * FindFile(const char *search, TString &file, EAccessMode mode=kFileExists) override
Find location of file "wfil" in a search path.
int CopyFile(const char *from, const char *to, Bool_t overwrite=kFALSE) override
Copy a file.
Int_t Exec(const char *shellcmd) override
Execute a command.
Int_t GetUid(const char *user=nullptr) override
Returns the user&#39;s id. If user = 0, returns current user&#39;s id.
static const char * UnixGetdirentry(void *dir)
Returns the next directory entry.
Definition: file.py:1
void ResetSignal(ESignals sig, Bool_t reset=kTRUE) override
If reset is true reset the signal handler for the specified signal to the default handler...
int GetSockOpt(int sock, int option, int *val) override
Get socket option.
#define ClassDefOverride(name, id)
Definition: Rtypes.h:326
void SetDisplay() override
Set DISPLAY environment variable based on utmp entry. Only for UNIX.
Bool_t CheckSignals(Bool_t sync)
Check if some signals were raised and call their Notify() member.
static int UnixUdpConnect(const char *hostname, int port)
Creates a UDP socket connection Is called via the TSocket constructor.
Abstract base class defining a generic interface to the underlying Operating System.
Definition: TSystem.h:265
int AcceptConnection(int sock) override
Accept a connection.
const char * GetError() override
Return system error string.
void DispatchSignals(ESignals sig)
Handle and dispatch signals.
const Bool_t kTRUE
Definition: RtypesCore.h:89
Bool_t Init() override
Initialize Unix system interface.
char name[80]
Definition: TGX11.cxx:109
Int_t GetFPEMask() override
Return the bitmap of conditions that trigger a floating point exception.
void AddFileHandler(TFileHandler *fh) override
Add a file handler to the list of system file handlers.
FILE * TempFileName(TString &base, const char *dir=nullptr) override
Create a secure temporary file by appending a unique 6 letter string to base.
void SigAlarmInterruptsSyscalls(Bool_t set) override
When the argument is true the SIGALRM signal handler is set so that interrupted syscalls will not be ...
int GetMemInfo(MemInfo_t *info) const override
Returns ram and swap memory usage info into the MemInfo_t structure.