2
$Id: unixsysc.inc,v 1.3 2002/09/07 16:01:26 peter Exp $
3
This file is part of the Free Pascal run time library.
4
Copyright (c) 1999-2000 by Michael Van Canneyt,
5
member of the Free Pascal development team.
7
See the file COPYING.FPC, included in this distribution,
8
for details about the copyright.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY;without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
**********************************************************************}
19
Function cFork:longint;cdecl; external name 'fork1'; // fork1 is better here then fork
20
Procedure cExecve(path:pchar;args:ppchar;ep:ppchar); cdecl; external name 'execve';
21
Function cWaitPid(Pid:longint;Status:pointer;Options:Longint):Longint; cdecl; external name 'waitpid';
22
Function cGetTimeOfDay(var tv:timeval;var tz:timezone):integer;cdecl;external name 'gettimeofday';
23
Function cNice(n:LongInt):LongInt; cdecl; external name 'nice';
24
Function cGetPid:LongInt;cdecl; external name 'getpid';
25
Function cGetPPid:LongInt;cdecl; external name 'getppid';
26
Function cGetUid:Longint;cdecl; external name 'getuid';
27
Function cGetEUid:Longint;cdecl; external name 'geteuid';
28
Function cGetGid:Longint;cdecl; external name 'getgid';
29
Function cGetEGid:Longint;cdecl; external name 'getgid';
30
Function cSetUid(aUID:longint):longint;cdecl; external name 'setuid';
31
Function cSetGid(aGID:longint):longint;cdecl; external name 'setuid';
32
function cSetreUid(aRealUID,aEffUid:Longint):Longint; cdecl; external name 'setreuid';
33
function cSetreGid(aRealGID,aEffGid:Longint):Longint; cdecl; external name 'setreuid';
34
Function cfTruncate(fd,size:longint):Longint;cdecl; external name 'ftruncate';
35
Function cfSync (fd : Longint) : Longint; cdecl; external name 'fsync';
36
Function cChmod(path:pathstr;Newmode:longint):Longint; cdecl; external name 'chmod';
37
Function cChown(path:pathstr;NewUid,NewGid:longint):Longint;cdecl; external name 'chown';
38
Function cUmask(Mask:Longint):Longint;cdecl;external name 'umask';
39
//Function cFlock (fd,mode : longint) : longint; cdecl; external name 'flock';
40
Function cDup(oldfile:longint):longint;cdecl;external name 'dup';
41
Function cDup2(oldfile,newfile:longint):longint;cdecl;external name 'dup2';
42
Function cGetPriority(Which,Who:LongInt):LongInt;cdecl; external name 'getpriority';
43
Function cSetPriority(Which:LongInt;Who:LongInt;What:LongInt):LongInt;cdecl; external name 'setpriority';
44
Function cFcntl(Fd:longint;Cmd:LongInt):LongInt;cdecl;external name 'fcntl';
45
Function cFcntlArg(Fd:longint;Cmd:LongInt; arg:LongInt):LongInt;cdecl;external name 'fcntl';
46
Function cAccess(Path:pCHar; mode:LongInt):LongInt; cdecl; external name 'access';
47
Function cPipe(var pip:tpipe):LongInt; cdecl; external name 'pipe';
48
Function cUtime(path:pchar; var utim:utimebuf):LongInt; cdecl; external name 'utime';
49
Function cSelect(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:PTimeVal):LongInt;cdecl; external name 'select';
50
Function cKill(Pid:longint;Sig:longint):LongINt;cdecl; external name 'kill';
51
Function cIOCtl(Handle,Ndx: Longint;Data: Pointer):LongInt; cdecl; external name 'ioctl';
52
Function cAlarm(Sec : Longint) : longint;cdecl; external name 'alarm';
53
Function cmknod(Vers:LongInt;pathname:pchar;mode,dev:longint):longint; cdecl; external name '_xmknod';
54
Function clStat(Vers:LongInt; Filename:pchar;var Buffer: Stat):longint; cdecl; external name '_lxstat';
55
Function cfStatfs(fd:LongInt; var Info:StatFs):LongInt; cdecl; external name 'fstatvfs';
56
Function cStatfs(Filename:pchar;var Buffer: StatFs):longint; cdecl; external name 'statvfs';
57
function cMUnMap(p:pointer;size:longint):integer;cdecl;external name 'munmap';
58
function cNanoSleep(const req : timespec;var rem : timespec) : longint; cdecl;external name 'nanosleep';
59
function cPause:longint; cdecl; external name 'pause';
60
function cSigProcMask(How:longint;SSet,OldSSet:PSigSet):longint; cdecl; external name 'sigprocmask';
61
function cSigPending(var s:SigSet):integer;cdecl; external name 'sigpending';
62
function cSigSuspend(s:PSigSet):longint;cdecl;external name 'sigsuspend';
63
function _cSignal(Signum:longint;Handler:Longint):SignalHandler; cdecl; external name 'signal';
64
function cSysInfo(cmd:longint; buff:pchar; len:longint):longint; cdecl; external name 'sysinfo';
65
{$LinkLib rt} // nanosleep
67
procedure libcerrorfix(fl:boolean); inline;
72
Linuxerror:=libcerrno;
83
Function Fork:longint;
85
This function issues the 'fork' System call. the program is duplicated in memory
86
and Execution continues in parent and child process.
87
In the parent process, fork returns the PID of the child. In the child process,
89
A negative value indicates that an error has occurred, the error is returned in
98
{ Solaris has no clone, there thread funktion (libthread), like thr_create, but they haven't
99
the same options with flags and return a TID istead of a PID.
100
If one is interestet he might look to a Hack for lxrun which is contributed as diff.
101
Allthough the lxrun-hack dos not work at all, it shows what to take care of }
103
function clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
105
begin // Quick Hack, never tested, but should work if func does not believe on the Stack
106
if (pointer(func)=nil) or (sp=nil) then
108
LinuxError:=Sys_EInval;
113
if (pid=0) then begin //Child
121
Procedure Execve(path:pchar;args:ppchar;ep:ppchar);
123
Replaces the current program by the program specified in path,
124
arguments in args are passed to Execve.
125
environment specified in ep is passed on.
128
cExecve(path,args,ep);
129
{ This only gets set when the call fails, otherwise we don't get here ! }
130
Linuxerror:=libcerrno;
135
Procedure Execve(path:pathstr;args:ppchar;ep:ppchar);
137
Replaces the current program by the program specified in path,
138
arguments in args are passed to Execve.
139
environment specified in ep is passed on.
142
path:=path+#0; cExecve(@path[1],args,ep);
143
{ This only gets set when the call fails, otherwise we don't get here ! }
144
Linuxerror:=libcerrno;
149
Procedure ExitProcess(val:longint);external name '_exit'; // not 'exit' ('exit' close the shared handle)
152
Function WaitPid(Pid:longint;Status:pointer;Options:Longint):Longint;
154
Waits until a child with PID Pid exits, or returns if it is exited already.
155
Any resources used by the child are freed.
156
The exit status is reported in the adress referred to by Status. It should
160
WaitPid:=cWaitPid(Pid,Status,Options); { =>PID, -1+errno=eintr: Signal, -1+errno, 0=Ok }
161
libcerrorfix(WaitPid=-1));
166
Procedure GetTimeOfDay(var tv:timeval);
168
Get the number of seconds since 00:00, January 1 1970, GMT
169
the time NOT corrected any way
174
r:=cGetTimeOfDay(tv,tz);
178
Function GetTimeOfDay: longint;
180
Get the number of seconds since 00:00, January 1 1970, GMT
181
the time NOT corrected any way
186
libcerrorfix(cGetTimeOfDay(tv,tz)=-1);
187
GetTimeOfDay:=tv.sec;
191
Function GetPriority(Which,Who:Integer):integer;
193
Get Priority of process, process group, or user.
194
Which : selects what kind of priority is used.
195
can be one of the following predefined Constants :
199
Who : depending on which, this is , respectively :
203
Errors are reported in linuxerror _only_. (priority can be negative)
207
if (which<prio_process) or (which>prio_user) then
209
{ We can save an interrupt here }
211
linuxerror:=Sys_einval;
216
GetPriority:=cGetPriority(Which,Who);
217
libcerrorfix(getpriority=-1);
222
Procedure SetPriority(Which:Integer;Who:Integer;What:Integer);
224
Set Priority of process, process group, or user.
225
Which : selects what kind of priority is used.
226
can be one of the following predefined Constants :
230
Who : depending on value of which, this is, respectively :
234
what : A number between -20 and 20. -20 is most favorable, 20 least.
240
if ((which<prio_process) or (which>prio_user)) or ((what<-20) or (what>20)) then
242
linuxerror:=Sys_einval { We can save an interrupt here }
247
r:=cSetPriority(Which,Who,What);
252
Procedure Nice(N:integer);
254
Set process priority. A positive N means a lower priority.
255
A negative N decreases priority.
258
libcerrorfix(cNice(n)=-1);
262
Function GetPid:LongInt;
268
libcerrorfix(GetPID=-1);
271
Function GetPPid:LongInt;
273
Get Process ID of parent process.
277
libcerrorfix(GetPPID=-1);
281
Function GetUid:Longint;
287
libcerrorfix (GetUid=-1);
291
Function GetEUid:Longint;
293
Get _effective_ User ID.
297
libcerrorfix(GetEUid=-1);
301
Function GetGid:Longint;
307
libcerrorfix(GetGid=-1);
310
Function GetEGid:Longint;
312
Get _effective_ Group ID.
316
libcerrorfix (GetEGid=-1);
319
// Set the real userid/groupid (uid/gid from calling process)
320
function SetUid(aUID:Longint):Boolean;
322
SetUid:=(cSetUid(aUid)=0);
323
libcerrorfix( not(SetUid));
326
function SetGid(aGID:Longint):Boolean;
328
SetGid:=(cSetGid(aGid)=0);
329
libcerrorfix( not(SetGid));
332
// Set the real and effective userid/groupid (like setuid/setgid bit in file permissions)
333
function SetreUid(aRealUID,aEffUid:Longint):Boolean;
335
SetreUid:=(cSetreUid(aRealUID,aEffUID)=0);
336
libcerrorfix( not(SetreUid));
339
function SetreUid(aUID:Longint):Boolean;
341
SetreUid:=SetreUid(aUID,aUID);
344
function SetreGid(aRealGid,aEffGid:Longint):Boolean; overload;
346
SetreGid:=(cSetreUid(aRealGID,aEffGID)=0);
347
libcerrorfix(not(SetreGid));
350
function SetreGid(aGid:Longint):Boolean;overload;
352
SetreGid:=SetreGid(aGID,aGID);
355
Function fdTruncate(fd,size:longint):boolean;
357
fdTruncate:=cfTruncate(fd,size)<>-1;
358
libcerrorfix(not fdTruncate);
361
Function fdFlush (fd : Longint) : Boolean;
363
fdFlush:=cfSync(fd)<>-1;
364
libcerrorfix( not fdFlush);
367
Function Fcntl(Fd:longint;Cmd:integer):integer;
369
Read or manipulate a file.(See also fcntl (2) )
370
Possible values for Cmd are :
371
F_GetFd,F_GetFl,F_GetOwn F_DUPFd, F_Dup2FD...
372
Errors are reported in Linuxerror;
373
If Cmd is different from the allowed values, linuxerror=Sys_eninval.
376
// the retun is not compatible to the linux-definition (returning 0 on -1 (err)), but 0 may be a valid return
377
if (cmd in [F_GetFd,F_GetFl,F_GetOwn, {solaris:} F_DupFd]) then
379
Fcntl:=cFcntl(fd,Cmd);
380
libcerrorfix(Fcntl=-1);
384
linuxerror:=Sys_einval;
390
Procedure Fcntl(Fd:longint;Cmd:Integer;Arg:Longint);
392
Read or manipulate a file. (See also fcntl (2) )
393
Possible values for Cmd are :
394
F_setFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkW,F_SetOwn;
395
Errors are reported in Linuxerror;
396
If Cmd is different from the allowed values, linuxerror=Sys_eninval.
397
F_DupFD is not allowed, due to the structure of Files in Pascal.
401
if (cmd in [F_SetFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkw,F_SetOwn {Solaris:} ,F_Dup2Fd,F_FreeSp{,F_GetLk64,F_SetLk64,F_SetLkw64}]) then
403
libcerrorfix( cFcntlArg(fd,Cmd,Arg)=-1);
406
linuxerror:=Sys_einval;
412
Function Chmod(path:pathstr;Newmode:longint):Boolean;
414
Changes the permissions of a file.
417
Chmod:=cChmod(path,NewMode)=0;
418
libcerrorfix( not Chmod);
422
Function Chown(path:pathstr;NewUid,NewGid:longint):boolean;
424
Change the owner and group of a file.
425
A user can only change the group to a group of which he is a member.
426
The super-user can change uid and gid of any file.
429
Chown:=cChown(path,NewUid,NewGid)=0;
430
libcerrorfix(not Chown);
434
Function Utime(path:pathstr;utim:utimebuf):boolean;
437
UTime:=cUtime(@Path[1],utim)=0;
438
libcerrorfix( not UTime);
442
Function Flock (fd,mode : longint) : boolean;
445
// FLock:=cFLock(fd,mode)=0;
446
libcerrorfix(not FLock);
451
Function Fstat(Fd:Longint;var Info:stat):Boolean;
453
Get all information on a file descriptor, and return it in info.
456
FStat:=Sys_fstat(fd,Info)=0;
457
libcerrorfix( not FStat);
461
Function Lstat(Filename: PathStr;var Info:stat):Boolean;
463
Get all information on a link (the link itself), and return it in info.
466
FileName:=FileName+#0;
467
LStat:=clstat(STAT_VERS,@FileName[1],Info)=0;
468
libcerrorfix( not LStat);
473
Function FSStat(Path:Pathstr;Var Info:statfs):Boolean;
475
Get all information on a fileSystem, and return it in Info.
476
Path is the name of a file/directory on the fileSystem you wish to
481
FSStat:=cstatfs(@path[1],Info)=0;
482
libcerrorfix(not FSStat);
487
Function FSStat(Fd:Longint;Var Info:statfs):Boolean;
489
Get all information on a fileSystem, and return it in Info.
490
Fd is the file descriptor of a file/directory on the fileSystem
491
you wish to investigate.
494
FSStat:=cfstatfs(fd,Info)=0;
495
libcerrorfix( not FSStat);
500
Function Link(OldPath,NewPath:pathstr):boolean;
502
Proceduces a hard link from new to old.
503
In effect, new will be the same file as old.
506
OldPath:=OldPath+#0; NewPath:=NewPath+#0;
507
Link:=Sys_SymLink(@OldPath[1],@NewPath[1])<>-1;
508
libcerrorfix( not Link);
512
Function Umask(Mask:Integer):integer;
514
Sets file creation mask to (Mask and 0777 (octal) ), and returns the
519
libcerrorfix (Umask=-1);
523
Function Access(Path:Pathstr ;mode:integer):boolean;
525
Test users access rights on the specified file.
526
Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
527
R,W,X stand for read,write and Execute access, simultaneously.
528
F_OK checks whether the test would be allowed on the file.
529
i.e. It checks the search permissions in all directory components
531
The test is done with the real user-ID, instead of the effective.
532
If access is denied, or an error occurred, false is returned.
533
If access is granted, true is returned.
534
Errors other than no access,are reported in linuxerror.
538
Access:=cAccess(@Path[1],mode)=0;
539
libcerrorfix( not Access);
542
Function Dup(oldfile:longint;var newfile:longint):Boolean;
544
Copies the filedescriptor oldfile to newfile
547
NewFile:=cDup(OldFile);
549
libcerrorfix( not Dup);
552
Function Dup2(oldfile,newfile:longint):Boolean;
554
Copies the filedescriptor oldfile to newfile
557
Dup2:=cDup2(OldFile,NewFile)<>-1;
558
libcerrorfix( not Dup2);
562
Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:PTimeVal):longint;
564
Select checks whether the file descriptor sets in readfs/writefs/exceptfs
568
SelectArray : Array[1..5] of longint;
570
Select:=cSelect(N,readfds,writefds,exceptfds,TimeOut);
571
libcerrorfix( Select=-1);
574
Function AssignPipe(var pipe_in,pipe_out:longint):boolean;
576
Sets up a pair of file variables, which act as a pipe. The first one can
577
be read from, the second one can be written to.
578
If the operation was unsuccesful, linuxerror is set.
583
AssignPipe:=cPipe(pip)=0;
584
if AssignPipe then begin
592
LinuxError:=libcErrNo;
597
Function PClose(Var F:text) :longint;
601
Sys_Close(Textrec(F).Handle);
602
{ closed our side, Now wait for the other - this appears to be needed ?? }
603
pl:=@(textrec(f).userdata[2]);
604
pclose:=WaitProcess(pl^);
608
Function PClose(Var F:file) : longint;
612
Sys_Close(Filerec(F).Handle);
613
{ closed our side, Now wait for the other - this appears to be needed ?? }
614
pl:=@(filerec(f).userdata[2]);
615
pclose:=WaitProcess(pl^);
619
Function Sysinfo(var Info:TSysinfo):Boolean; // ToDO
622
(Mvdv:Linux specific, not implemented under FreeBSD too.
623
Under FreeBSD I will simply implement a sysctl unit)
628
(* regs.reg2:=longint(@info);
629
Sysinfo:=SysCall(SysCall_nr_Sysinfo,regs)=0;*)
630
WriteLN('SysInfo not supported yet ');
634
Function mkFifo(pathname:string;mode:longint):boolean;
636
pathname:=pathname+#0;
637
mode:=mode or STAT_IFIFO;
638
mkFifo:=cmknod(_MKNOD_VER,@pathname[1],mode,0)=0;
639
libcerrorfix(not mkFifo);
643
Function Uname(var unamerec:utsname):Boolean; // ToDo
651
cSysInfo(SI_SYSNAME,@unamerec.sysname,SizeOf(unamerec.sysname));
652
cSysInfo(SI_HOSTNAME,@unamerec.nodename,SizeOf(unamerec.nodename));
653
cSysInfo(SI_RELEASE,@unamerec.release,SizeOf(unamerec.release));
654
cSysInfo(SI_VERSION,@unamerec.version,SizeOf(unamerec.version));
655
cSysInfo(SI_MACHINE,@unamerec.machine,SizeOf(unamerec.machine));
656
cSysInfo(SI_SRPC_DOMAIN,@unamerec.domainname,SizeOf(unamerec.domainname));
661
Function Kill(Pid:longint;Sig:longint):integer;
663
Send signal 'sig' to a process, or a group of processes.
664
If Pid > 0 then the signal is sent to pid
665
pid=-1 to all processes except process 1
666
pid < -1 to process group -pid
667
Return value is zero, except for case three, where the return value
668
is the number of processes to which the signal was sent.
671
Kill:=cKill(PID,Sig);
672
libcerrorfix( Kill=-1);
673
if kill<0 then Kill:=0; // from the linux source
678
Procedure SigProcMask(How:longint;SSet,OldSSet:PSigSet); //ToDo
680
Change the list of currently blocked signals.
681
How determines which signals will be blocked :
682
SigBlock : Add SSet to the current list of blocked signals
683
SigUnBlock : Remove the signals in SSet from the list of blocked signals.
684
SigSetMask : Set the list of blocked signals to SSet
685
if OldSSet is non-null, the old set will be saved there.
688
libcerrorfix( cSigProcMask(How,SSet,OldSSet)=0);
692
Function SigPending:SigSet;
694
Allows examination of pending signals. The signal mask of pending
695
signals is set in SSet
700
libcerrorfix(cSigPending(dummy)=0);
705
Procedure SigSuspend(Mask:Sigset); // ToDo
707
Set the signal mask with Mask, and suspend the program until a signal
711
libcerrorfix (cSigSuspend(@Mask)=0);
714
Function Signal(Signum:longint;Handler:SignalHandler):SignalHandler;
716
Install a new handler for signal Signum.
717
The old signal handler is returned.
718
This call does, in fact, the same as SigAction.
724
r:=longint(_cSignal(Signum,longint(Handler)));
727
LinuxError:=libcerrno;
732
Signal:=signalhandler(r);
740
Function Alarm(Sec : Longint) : longint;
743
LinuxError:=0; // no error
751
Function NanoSleep(const req : timespec;var rem : timespec) : longint;
753
NanoSleep:=cNanoSleep(req,rem);
754
Libcerrorfix( NanoSleep=-1);
758
Function IOCtl(Handle,Ndx: Longint;Data: Pointer):boolean;
760
Interface to Unix ioctl call.
761
Performs various operations on the filedescriptor Handle.
762
Ndx describes the operation to perform.
763
Data points to data needed for the Ndx function. The structure of this
764
data is function-dependent.
767
IOCtl:=cIOCtl(Handle,Ndx,Data)<>-1;
768
libcerrorfix( not IOCtl);
771
function MUnMap (P : Pointer; Size : Longint) : Boolean;
773
MUnMap:=cMUnMap(p,size)=0;
774
libcerrorfix( not MUnMap);
777
{--------------------------------
779
--------------------------------}
781
// all of them has to be checked for soalris
782
Function IOperm (From,Num : Cardinal; Value : Longint) : boolean;
784
Set permissions on NUM ports starting with port FROM to VALUE
785
this works ONLY as root.
794
IOPerm:=Syscall(Syscall_nr_ioperm,sr)=0;
796
WriteLN('IOperm not suppoted yet');
799
Function IoPL(Level : longint) : Boolean;
805
IOPL:=Syscall(Syscall_nr_iopl,sr)=0;
807
WriteLN('IoPL not suppoted yet');
811
$Log: unixsysc.inc,v $
812
Revision 1.3 2002/09/07 16:01:26 peter
813
* old logs removed and tabs fixed