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