// @(#)root/unix:$Id$
// Author: Fons Rademakers   15/09/95

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TUnixSystem
#define ROOT_TUnixSystem


//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TUnixSystem                                                          //
//                                                                      //
// Class providing an interface to the UNIX Operating System.           //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TSystem
#include "TSystem.h"
#endif
#ifndef ROOT_TSysEvtHandler
#include "TSysEvtHandler.h"
#endif
#ifndef ROOT_TTimer
#include "TTimer.h"
#endif

typedef void (*SigHandler_t)(ESignals);


class TUnixSystem : public TSystem {

protected:
   const char    *FindDynamicLibrary(TString &lib, Bool_t quiet = kFALSE);
   const char    *GetLinkedLibraries();

   // static functions providing semi-low level interface to raw Unix
   static int          UnixMakedir(const char *name);
   static void        *UnixOpendir(const char *name);
   static const char  *UnixGetdirentry(void *dir);
   static const char  *UnixHomedirectory(const char *user = 0);
   static Long64_t     UnixNow();
   static int          UnixWaitchild();
   static int          UnixSetitimer(Long_t ms);
   static int          UnixSelect(Int_t nfds, TFdSet *readready, TFdSet *writeready,
                                  Long_t timeout);
   static void         UnixSignal(ESignals sig, SigHandler_t h);
   static const char  *UnixSigname(ESignals sig);
   static void         UnixSigAlarmInterruptsSyscalls(Bool_t set);
   static void         UnixResetSignal(ESignals sig);
   static void         UnixResetSignals();
   static void         UnixIgnoreSignal(ESignals sig, Bool_t ignore);
   static int          UnixFilestat(const char *path, FileStat_t &buf);
   static int          UnixFSstat(const char *path, Long_t *id, Long_t *bsize,
                                  Long_t *blocks, Long_t *bfree);
   static int          UnixTcpConnect(const char *hostname, int port, int tcpwindowsize);
   static int          UnixUdpConnect(const char *hostname, int port);
   static int          UnixUnixConnect(int port);
   static int          UnixUnixConnect(const char *path);
   static int          UnixTcpService(int port, Bool_t reuse, int backlog,
                                      int tcpwindowsize);
   static int          UnixUdpService(int port, int backlog);
   static int          UnixUnixService(int port, int backlog);
   static int          UnixUnixService(const char *sockpath, int backlog);
   static int          UnixRecv(int sock, void *buf, int len, int flag);
   static int          UnixSend(int sock, const void *buf, int len, int flag);

public:
   TUnixSystem();
   virtual ~TUnixSystem();

   //---- Misc -------------------------------------------------
   Bool_t            Init();
   void              SetProgname(const char *name);
   void              SetDisplay();
   const char       *GetError();
   const char       *HostName();

   //---- EventLoop --------------------------------------------
   void              DispatchOneEvent(Bool_t pendingOnly = kFALSE);
   Int_t             Select(TList *active, Long_t timeout);
   Int_t             Select(TFileHandler *fh, Long_t timeout);

   //---- Handling of system events ----------------------------
   void              CheckChilds();
   Bool_t            CheckSignals(Bool_t sync);
   Bool_t            CheckDescriptors();
   void              DispatchSignals(ESignals sig);
   void              AddSignalHandler(TSignalHandler *sh);
   TSignalHandler   *RemoveSignalHandler(TSignalHandler *sh);
   void              ResetSignal(ESignals sig, Bool_t reset = kTRUE);
   void              ResetSignals();
   void              IgnoreSignal(ESignals sig, Bool_t ignore = kTRUE);
   void              SigAlarmInterruptsSyscalls(Bool_t set);
   void              AddFileHandler(TFileHandler *fh);
   TFileHandler     *RemoveFileHandler(TFileHandler *fh);

   //---- Floating Point Exceptions Control --------------------
   Int_t             GetFPEMask();
   Int_t             SetFPEMask(Int_t mask = kDefaultMask);

   //---- Time & Date ------------------------------------------
   TTime             Now();
   void              AddTimer(TTimer *ti);
   TTimer           *RemoveTimer(TTimer *ti);
   void              ResetTimer(TTimer *ti);
   Bool_t            DispatchTimers(Bool_t mode);
   void              Sleep(UInt_t milliSec);

   //---- Processes --------------------------------------------
   Int_t             Exec(const char *shellcmd);
   FILE             *OpenPipe(const char *shellcmd, const char *mode);
   int               ClosePipe(FILE *pipe);
   void              Exit(int code, Bool_t mode = kTRUE);
   void              Abort(int code = 0);
   int               GetPid();
   void              StackTrace();

   //---- Directories ------------------------------------------
   int               MakeDirectory(const char *name);
   void             *OpenDirectory(const char *name);
   void              FreeDirectory(void *dirp);
   const char       *GetDirEntry(void *dirp);
   Bool_t            ChangeDirectory(const char *path);
   const char       *WorkingDirectory();
   const char       *HomeDirectory(const char *userName = 0);
   const char       *TempDirectory() const;
   FILE             *TempFileName(TString &base, const char *dir = 0);

   //---- Paths & Files ----------------------------------------
   const char       *PrependPathName(const char *dir, TString& name);
   Bool_t            ExpandPathName(TString &patbuf);
   char             *ExpandPathName(const char *path);
   Bool_t            AccessPathName(const char *path, EAccessMode mode = kFileExists);
   Bool_t            IsPathLocal(const char *path);
   int               CopyFile(const char *from, const char *to, Bool_t overwrite = kFALSE);
   int               Rename(const char *from, const char *to);
   int               Link(const char *from, const char *to);
   int               Symlink(const char *from, const char *to);
   int               Unlink(const char *name);
   int               GetPathInfo(const char *path, FileStat_t &buf);
   int               GetFsInfo(const char *path, Long_t *id, Long_t *bsize,
                               Long_t *blocks, Long_t *bfree);
   int               Chmod(const char *file, UInt_t mode);
   int               Umask(Int_t mask);
   int               Utime(const char *file, Long_t modtime, Long_t actime);
   const char       *FindFile(const char *search, TString& file, EAccessMode mode = kFileExists);

   //---- Users & Groups ---------------------------------------
   Int_t             GetUid(const char *user = 0);
   Int_t             GetGid(const char *group = 0);
   Int_t             GetEffectiveUid();
   Int_t             GetEffectiveGid();
   UserGroup_t      *GetUserInfo(Int_t uid);
   UserGroup_t      *GetUserInfo(const char *user = 0);
   UserGroup_t      *GetGroupInfo(Int_t gid);
   UserGroup_t      *GetGroupInfo(const char *group = 0);

   //---- Environment Manipulation -----------------------------
   const char       *Getenv(const char *name);
   void              Setenv(const char *name, const char *value);
   void              Unsetenv(const char *name);

   //---- System Logging ---------------------------------------
   void              Openlog(const char *name, Int_t options, ELogFacility facility);
   void              Syslog(ELogLevel level, const char *mess);
   void              Closelog();

   //---- Standard Output redirection --------------------------
   Int_t             RedirectOutput(const char *name, const char *mode = "a",
                                    RedirectHandle_t *h = 0);

   //---- Dynamic Loading --------------------------------------
   void              AddDynamicPath(const char *lib);
   const char       *GetDynamicPath();
   void              SetDynamicPath(const char *lib);
   Func_t            DynFindSymbol(const char *module, const char *entry);
   int               Load(const char *module, const char *entry = "", Bool_t system = kFALSE);
   void              Unload(const char *module);
   void              ListSymbols(const char *module, const char *re = "");
   void              ListLibraries(const char *regexp = "");

   //---- RPC --------------------------------------------------
   TInetAddress      GetHostByName(const char *server);
   TInetAddress      GetPeerName(int sock);
   TInetAddress      GetSockName(int sock);
   int               GetServiceByName(const char *service);
   char             *GetServiceByPort(int port);
   int               ConnectService(const char *server, int port, int tcpwindowsize, const char *protocol = "tcp");
   int               OpenConnection(const char *server, int port, int tcpwindowsize = -1, const char *protocol = "tcp");
   int               AnnounceTcpService(int port, Bool_t reuse, int backlog, int tcpwindowsize = -1);
   int               AnnounceUdpService(int port, int backlog);
   int               AnnounceUnixService(int port, int backlog);
   int               AnnounceUnixService(const char *sockpath, int backlog);
   int               AcceptConnection(int sock);
   void              CloseConnection(int sock, Bool_t force = kFALSE);
   int               RecvRaw(int sock, void *buffer, int length, int flag);
   int               SendRaw(int sock, const void *buffer, int length, int flag);
   int               RecvBuf(int sock, void *buffer, int length);
   int               SendBuf(int sock, const void *buffer, int length);
   int               SetSockOpt(int sock, int option, int val);
   int               GetSockOpt(int sock, int option, int *val);

   //---- System, CPU and Memory info
   int               GetSysInfo(SysInfo_t *info) const;
   int               GetCpuInfo(CpuInfo_t *info, Int_t sampleTime = 1000) const;
   int               GetMemInfo(MemInfo_t *info) const;
   int               GetProcInfo(ProcInfo_t *info) const;

   ClassDef(TUnixSystem,0)  //Interface to Unix OS services
};

#endif
 TUnixSystem.h:1
 TUnixSystem.h:2
 TUnixSystem.h:3
 TUnixSystem.h:4
 TUnixSystem.h:5
 TUnixSystem.h:6
 TUnixSystem.h:7
 TUnixSystem.h:8
 TUnixSystem.h:9
 TUnixSystem.h:10
 TUnixSystem.h:11
 TUnixSystem.h:12
 TUnixSystem.h:13
 TUnixSystem.h:14
 TUnixSystem.h:15
 TUnixSystem.h:16
 TUnixSystem.h:17
 TUnixSystem.h:18
 TUnixSystem.h:19
 TUnixSystem.h:20
 TUnixSystem.h:21
 TUnixSystem.h:22
 TUnixSystem.h:23
 TUnixSystem.h:24
 TUnixSystem.h:25
 TUnixSystem.h:26
 TUnixSystem.h:27
 TUnixSystem.h:28
 TUnixSystem.h:29
 TUnixSystem.h:30
 TUnixSystem.h:31
 TUnixSystem.h:32
 TUnixSystem.h:33
 TUnixSystem.h:34
 TUnixSystem.h:35
 TUnixSystem.h:36
 TUnixSystem.h:37
 TUnixSystem.h:38
 TUnixSystem.h:39
 TUnixSystem.h:40
 TUnixSystem.h:41
 TUnixSystem.h:42
 TUnixSystem.h:43
 TUnixSystem.h:44
 TUnixSystem.h:45
 TUnixSystem.h:46
 TUnixSystem.h:47
 TUnixSystem.h:48
 TUnixSystem.h:49
 TUnixSystem.h:50
 TUnixSystem.h:51
 TUnixSystem.h:52
 TUnixSystem.h:53
 TUnixSystem.h:54
 TUnixSystem.h:55
 TUnixSystem.h:56
 TUnixSystem.h:57
 TUnixSystem.h:58
 TUnixSystem.h:59
 TUnixSystem.h:60
 TUnixSystem.h:61
 TUnixSystem.h:62
 TUnixSystem.h:63
 TUnixSystem.h:64
 TUnixSystem.h:65
 TUnixSystem.h:66
 TUnixSystem.h:67
 TUnixSystem.h:68
 TUnixSystem.h:69
 TUnixSystem.h:70
 TUnixSystem.h:71
 TUnixSystem.h:72
 TUnixSystem.h:73
 TUnixSystem.h:74
 TUnixSystem.h:75
 TUnixSystem.h:76
 TUnixSystem.h:77
 TUnixSystem.h:78
 TUnixSystem.h:79
 TUnixSystem.h:80
 TUnixSystem.h:81
 TUnixSystem.h:82
 TUnixSystem.h:83
 TUnixSystem.h:84
 TUnixSystem.h:85
 TUnixSystem.h:86
 TUnixSystem.h:87
 TUnixSystem.h:88
 TUnixSystem.h:89
 TUnixSystem.h:90
 TUnixSystem.h:91
 TUnixSystem.h:92
 TUnixSystem.h:93
 TUnixSystem.h:94
 TUnixSystem.h:95
 TUnixSystem.h:96
 TUnixSystem.h:97
 TUnixSystem.h:98
 TUnixSystem.h:99
 TUnixSystem.h:100
 TUnixSystem.h:101
 TUnixSystem.h:102
 TUnixSystem.h:103
 TUnixSystem.h:104
 TUnixSystem.h:105
 TUnixSystem.h:106
 TUnixSystem.h:107
 TUnixSystem.h:108
 TUnixSystem.h:109
 TUnixSystem.h:110
 TUnixSystem.h:111
 TUnixSystem.h:112
 TUnixSystem.h:113
 TUnixSystem.h:114
 TUnixSystem.h:115
 TUnixSystem.h:116
 TUnixSystem.h:117
 TUnixSystem.h:118
 TUnixSystem.h:119
 TUnixSystem.h:120
 TUnixSystem.h:121
 TUnixSystem.h:122
 TUnixSystem.h:123
 TUnixSystem.h:124
 TUnixSystem.h:125
 TUnixSystem.h:126
 TUnixSystem.h:127
 TUnixSystem.h:128
 TUnixSystem.h:129
 TUnixSystem.h:130
 TUnixSystem.h:131
 TUnixSystem.h:132
 TUnixSystem.h:133
 TUnixSystem.h:134
 TUnixSystem.h:135
 TUnixSystem.h:136
 TUnixSystem.h:137
 TUnixSystem.h:138
 TUnixSystem.h:139
 TUnixSystem.h:140
 TUnixSystem.h:141
 TUnixSystem.h:142
 TUnixSystem.h:143
 TUnixSystem.h:144
 TUnixSystem.h:145
 TUnixSystem.h:146
 TUnixSystem.h:147
 TUnixSystem.h:148
 TUnixSystem.h:149
 TUnixSystem.h:150
 TUnixSystem.h:151
 TUnixSystem.h:152
 TUnixSystem.h:153
 TUnixSystem.h:154
 TUnixSystem.h:155
 TUnixSystem.h:156
 TUnixSystem.h:157
 TUnixSystem.h:158
 TUnixSystem.h:159
 TUnixSystem.h:160
 TUnixSystem.h:161
 TUnixSystem.h:162
 TUnixSystem.h:163
 TUnixSystem.h:164
 TUnixSystem.h:165
 TUnixSystem.h:166
 TUnixSystem.h:167
 TUnixSystem.h:168
 TUnixSystem.h:169
 TUnixSystem.h:170
 TUnixSystem.h:171
 TUnixSystem.h:172
 TUnixSystem.h:173
 TUnixSystem.h:174
 TUnixSystem.h:175
 TUnixSystem.h:176
 TUnixSystem.h:177
 TUnixSystem.h:178
 TUnixSystem.h:179
 TUnixSystem.h:180
 TUnixSystem.h:181
 TUnixSystem.h:182
 TUnixSystem.h:183
 TUnixSystem.h:184
 TUnixSystem.h:185
 TUnixSystem.h:186
 TUnixSystem.h:187
 TUnixSystem.h:188
 TUnixSystem.h:189
 TUnixSystem.h:190
 TUnixSystem.h:191
 TUnixSystem.h:192
 TUnixSystem.h:193
 TUnixSystem.h:194
 TUnixSystem.h:195
 TUnixSystem.h:196
 TUnixSystem.h:197
 TUnixSystem.h:198
 TUnixSystem.h:199
 TUnixSystem.h:200
 TUnixSystem.h:201
 TUnixSystem.h:202
 TUnixSystem.h:203
 TUnixSystem.h:204
 TUnixSystem.h:205
 TUnixSystem.h:206
 TUnixSystem.h:207
 TUnixSystem.h:208
 TUnixSystem.h:209
 TUnixSystem.h:210
 TUnixSystem.h:211
 TUnixSystem.h:212
 TUnixSystem.h:213
 TUnixSystem.h:214
 TUnixSystem.h:215
 TUnixSystem.h:216
 TUnixSystem.h:217
 TUnixSystem.h:218
 TUnixSystem.h:219