diff --git a/basis/Posix.sml b/basis/Posix.sml index 813f70a7..489fb27a 100644 --- a/basis/Posix.sml +++ b/basis/Posix.sml @@ -1,1712 +1,1712 @@ (* Title: Standard Basis Library: Posix structure and signature. - Copyright David Matthews 2000, 2016-17, 2019 + Copyright David Matthews 2000, 2016-17, 2019-2020 This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *) signature POSIX_ERROR = sig type syserror = OS.syserror (* G&R 2004 has an error *) val toWord : syserror -> SysWord.word val fromWord : SysWord.word -> syserror val errorMsg : syserror -> string val errorName : syserror -> string val syserror : string -> syserror option val acces : syserror val again : syserror val badf : syserror val badmsg : syserror val busy : syserror val canceled (* sic *) : syserror val child : syserror val deadlk : syserror val dom : syserror val exist : syserror val fault : syserror val fbig : syserror val inprogress : syserror val intr : syserror val inval : syserror val io : syserror val isdir : syserror val loop : syserror val mfile : syserror val mlink : syserror val msgsize : syserror val nametoolong : syserror val nfile : syserror val nodev : syserror val noent : syserror val noexec : syserror val nolck : syserror val nomem : syserror val nospc : syserror val nosys : syserror val notdir : syserror val notempty : syserror val notsup : syserror val notty : syserror val nxio : syserror val perm : syserror val pipe : syserror val range : syserror val rofs : syserror val spipe : syserror val srch : syserror val toobig : syserror val xdev : syserror end; signature POSIX_SIGNAL = sig eqtype signal val toWord : signal -> SysWord.word val fromWord : SysWord.word -> signal val abrt : signal val alrm : signal val bus : signal val fpe : signal val hup : signal val ill : signal val int : signal val kill : signal val pipe : signal val quit : signal val segv : signal val term : signal val usr1 : signal val usr2 : signal val chld : signal val cont : signal val stop : signal val tstp : signal val ttin : signal val ttou : signal end; signature POSIX_PROCESS = sig eqtype signal eqtype pid val wordToPid : SysWord.word -> pid val pidToWord : pid -> SysWord.word val fork : unit -> pid option val exec : string * string list -> 'a val exece : string * string list * string list -> 'a val execp : string * string list -> 'a datatype waitpid_arg = W_ANY_CHILD | W_CHILD of pid | W_SAME_GROUP | W_GROUP of pid datatype exit_status = W_EXITED | W_EXITSTATUS of Word8.word | W_SIGNALED (* sic *) of signal | W_STOPPED of signal val fromStatus : OS.Process.status -> exit_status structure W: sig include BIT_FLAGS val untraced : flags end val wait : unit -> pid * exit_status val waitpid : waitpid_arg * W.flags list -> pid * exit_status val waitpid_nh : waitpid_arg * W.flags list -> (pid * exit_status) option val exit : Word8.word -> 'a datatype killpid_arg = K_PROC of pid | K_SAME_GROUP | K_GROUP of pid val kill : killpid_arg * signal -> unit val alarm : Time.time -> Time.time val pause : unit -> unit (* QUESTION: Why does sleep return a Time.time ? Is it intended to be the time remaining? Assume so. *) val sleep : Time.time -> Time.time end; signature POSIX_PROC_ENV = sig eqtype pid eqtype uid eqtype gid eqtype file_desc val uidToWord : uid -> SysWord.word val wordToUid : SysWord.word -> uid val gidToWord : gid -> SysWord.word val wordToGid : SysWord.word -> gid val getpid : unit -> pid val getppid : unit -> pid val getuid : unit -> uid val geteuid : unit -> uid val getgid : unit -> gid val getegid : unit -> gid val setuid : uid -> unit val setgid : gid -> unit val getgroups : unit -> gid list val getlogin : unit -> string val getpgrp : unit -> pid val setsid : unit -> pid val setpgid : {pid : pid option, pgid : pid option} -> unit val uname : unit -> (string * string) list val time : unit -> Time.time val times : unit -> { elapsed : Time.time, utime : Time.time, stime : Time.time, cutime : Time.time, cstime : Time.time } val getenv : string -> string option val environ : unit -> string list val ctermid : unit -> string val ttyname : file_desc -> string val isatty : file_desc -> bool val sysconf : string -> SysWord.word end; signature POSIX_FILE_SYS = sig eqtype uid eqtype gid eqtype file_desc val fdToWord : file_desc -> SysWord.word val wordToFD : SysWord.word -> file_desc val fdToIOD : file_desc -> OS.IO.iodesc val iodToFD : OS.IO.iodesc -> file_desc option type dirstream val opendir : string -> dirstream val readdir : dirstream -> string option val rewinddir : dirstream -> unit val closedir : dirstream -> unit val chdir : string -> unit val getcwd : unit -> string val stdin : file_desc val stdout : file_desc val stderr : file_desc structure S : sig eqtype mode include BIT_FLAGS where type flags = mode val irwxu : mode val irusr : mode val iwusr : mode val ixusr : mode val irwxg : mode val irgrp : mode val iwgrp : mode val ixgrp : mode val irwxo : mode val iroth : mode val iwoth : mode val ixoth : mode val isuid : mode val isgid : mode end structure O: sig include BIT_FLAGS val append : flags val excl : flags val noctty : flags val nonblock : flags val sync : flags val trunc : flags end datatype open_mode = O_RDONLY | O_WRONLY | O_RDWR val openf : string * open_mode * O.flags -> file_desc val createf : string * open_mode * O.flags * S.mode -> file_desc val creat : string * S.mode -> file_desc val umask : S.mode -> S.mode val link : {old : string, new : string} -> unit val mkdir : string * S.mode -> unit val mkfifo : string * S.mode -> unit val unlink : string -> unit val rmdir : string -> unit val rename : {old : string, new : string} -> unit val symlink : {old : string, new : string} -> unit val readlink : string -> string eqtype dev val wordToDev : SysWord.word -> dev val devToWord : dev -> SysWord.word eqtype ino val wordToIno : SysWord.word -> ino val inoToWord : ino -> SysWord.word structure ST: sig type stat val isDir : stat -> bool val isChr : stat -> bool val isBlk : stat -> bool val isReg : stat -> bool val isFIFO : stat -> bool val isLink : stat -> bool val isSock : stat -> bool val mode : stat -> S.mode val ino : stat -> ino val dev : stat -> dev val nlink : stat -> int val uid : stat -> uid val gid : stat -> gid val size : stat -> Position.int val atime : stat -> Time.time val mtime : stat -> Time.time val ctime : stat -> Time.time end val stat : string -> ST.stat val lstat : string -> ST.stat val fstat : file_desc -> ST.stat datatype access_mode = A_READ | A_WRITE | A_EXEC val access : string * access_mode list -> bool val chmod : string * S.mode -> unit val fchmod : file_desc * S.mode -> unit val chown : string * uid * gid -> unit val fchown : file_desc * uid * gid -> unit val utime : string * {actime : Time.time, modtime : Time.time} option -> unit val ftruncate : file_desc * Position.int -> unit val pathconf : string * string -> SysWord.word option val fpathconf : file_desc * string -> SysWord.word option end; signature POSIX_IO = sig eqtype file_desc eqtype pid val pipe: unit -> {infd : file_desc, outfd : file_desc} val dup: file_desc -> file_desc val dup2: {old : file_desc, new : file_desc} -> unit val close: file_desc -> unit val readVec : file_desc * int -> Word8Vector.vector val readArr: file_desc * Word8ArraySlice.slice -> int val writeVec: file_desc * Word8VectorSlice.slice -> int val writeArr: file_desc * Word8ArraySlice.slice -> int datatype whence = SEEK_SET | SEEK_CUR | SEEK_END structure FD: sig include BIT_FLAGS val cloexec: flags end structure O: sig include BIT_FLAGS val append : flags val nonblock : flags val sync : flags end datatype open_mode = O_RDONLY | O_WRONLY | O_RDWR val dupfd : {old : file_desc, base : file_desc} -> file_desc val getfd : file_desc -> FD.flags val setfd : file_desc * FD.flags -> unit val getfl : file_desc -> O.flags * open_mode val setfl : file_desc * O.flags -> unit val lseek : file_desc * Position.int * whence -> Position.int val fsync : file_desc -> unit datatype lock_type = F_RDLCK | F_WRLCK | F_UNLCK structure FLock: sig type flock val flock : { ltype : lock_type, whence : whence, start : Position.int, len : Position.int, pid : pid option } -> flock val ltype : flock -> lock_type val whence : flock -> whence val start : flock -> Position.int val len : flock -> Position.int val pid : flock -> pid option end val getlk : file_desc * FLock.flock -> FLock.flock val setlk : file_desc * FLock.flock -> FLock.flock val setlkw : file_desc * FLock.flock -> FLock.flock val mkBinReader: { fd : file_desc, name : string, initBlkMode : bool } -> BinPrimIO.reader val mkTextReader: { fd : file_desc, name : string, initBlkMode : bool } -> TextPrimIO.reader val mkBinWriter: { fd : file_desc, name : string, appendMode : bool, initBlkMode : bool, chunkSize : int } -> BinPrimIO.writer val mkTextWriter: { fd : file_desc, name : string, appendMode : bool, initBlkMode : bool, chunkSize : int } -> TextPrimIO.writer end; signature POSIX_SYS_DB = sig eqtype uid eqtype gid structure Passwd : sig type passwd val name : passwd -> string val uid : passwd -> uid val gid : passwd -> gid val home : passwd -> string val shell : passwd -> string end structure Group : sig type group val name : group -> string val gid : group -> gid val members : group -> string list end val getgrgid : gid -> Group.group val getgrnam : string -> Group.group val getpwuid : uid -> Passwd.passwd val getpwnam : string -> Passwd.passwd end; signature POSIX_TTY = sig eqtype pid eqtype file_desc structure V : sig val eof : int val eol : int val erase : int val intr : int val kill : int val min : int val quit : int val susp : int val time : int val start : int val stop : int val nccs : int type cc val cc : (int * char) list -> cc val update : cc * (int * char) list -> cc val sub : cc * int -> char end structure I : sig include BIT_FLAGS val brkint : flags val icrnl : flags val ignbrk : flags val igncr : flags val ignpar : flags val inlcr : flags val inpck : flags val istrip : flags val ixoff : flags val ixon : flags val parmrk : flags end structure O : sig include BIT_FLAGS val opost : flags end structure C : sig include BIT_FLAGS val clocal : flags val cread : flags val cs5 : flags val cs6 : flags val cs7 : flags val cs8 : flags val csize : flags val cstopb : flags val hupcl : flags val parenb : flags val parodd : flags end structure L : sig include BIT_FLAGS val echo : flags val echoe : flags val echok : flags val echonl : flags val icanon : flags val iexten : flags val isig : flags val noflsh : flags val tostop : flags end eqtype speed val compareSpeed : speed * speed -> order val speedToWord : speed -> SysWord.word val wordToSpeed : SysWord.word -> speed val b0 : speed val b50 : speed val b75 : speed val b110 : speed val b134 : speed val b150 : speed val b200 : speed val b300 : speed val b600 : speed val b1200 : speed val b1800 : speed val b2400 : speed val b4800 : speed val b9600 : speed val b19200 : speed val b38400 : speed type termios val termios : { iflag : I.flags, oflag : O.flags, cflag : C.flags, lflag : L.flags, cc : V.cc, ispeed : speed, ospeed : speed } -> termios val fieldsOf : termios -> { iflag : I.flags, oflag : O.flags, cflag : C.flags, lflag : L.flags, cc : V.cc, ispeed : speed, ospeed : speed } val getiflag : termios -> I.flags val getoflag : termios -> O.flags val getcflag : termios -> C.flags val getlflag : termios -> L.flags val getcc : termios -> V.cc structure CF : sig val getospeed : termios -> speed val setospeed : termios * speed -> termios val getispeed : termios -> speed val setispeed : termios * speed -> termios end structure TC : sig eqtype set_action val sanow : set_action val sadrain : set_action val saflush : set_action eqtype flow_action val ooff : flow_action val oon : flow_action val ioff : flow_action val ion : flow_action eqtype queue_sel val iflush : queue_sel val oflush : queue_sel val ioflush : queue_sel val getattr : file_desc -> termios val setattr : file_desc * set_action * termios -> unit val sendbreak : file_desc * int -> unit val drain : file_desc -> unit val flush : file_desc * queue_sel -> unit val flow : file_desc * flow_action -> unit end val getpgrp : file_desc -> pid val setpgrp : file_desc * pid -> unit end; signature POSIX = sig structure Error : POSIX_ERROR structure Signal : POSIX_SIGNAL structure Process : POSIX_PROCESS where type signal = Signal.signal structure ProcEnv : POSIX_PROC_ENV where type pid = Process.pid structure FileSys : POSIX_FILE_SYS where type file_desc = ProcEnv.file_desc where type uid = ProcEnv.uid where type gid = ProcEnv.gid structure IO : POSIX_IO where type pid = Process.pid where type file_desc = ProcEnv.file_desc where type open_mode = FileSys.open_mode structure SysDB : POSIX_SYS_DB where type uid = ProcEnv.uid where type gid = ProcEnv.gid structure TTY : POSIX_TTY where type pid = Process.pid where type file_desc = ProcEnv.file_desc end; structure Posix :> sig include POSIX (* I'm not sure if it's legal to use where type with a datatype. The alternative is to copy the whole of the signature and use datatype replication. *) where type FileSys.access_mode = OS.FileSys.access_mode sharing type Process.pid = ProcEnv.pid = IO.pid = TTY.pid sharing type ProcEnv.uid = FileSys.uid = SysDB.uid sharing type ProcEnv.gid = FileSys.gid = SysDB.gid sharing type ProcEnv.file_desc = FileSys.file_desc = IO.file_desc = TTY.file_desc end (* Posix.Signal.signal is made the same as int so that we can pass the values directly to our (non-standard) Signal.signal function. Since there isn't a standard way of handling signals this is the best we can do. *) where type Signal.signal = int where type FileSys.dirstream = OS.FileSys.dirstream = struct local val processEnvGeneralCall = RunCall.rtsCallFull2 "PolyProcessEnvGeneral" and osSpecificGeneralCall = RunCall.rtsCallFull2 "PolyOSSpecificGeneral" and timingGeneralCall = RunCall.rtsCallFull2 "PolyTimingGeneral" in fun processEnvGeneral(code: int, arg:'a):'b = RunCall.unsafeCast(processEnvGeneralCall(RunCall.unsafeCast(code, arg))) and osSpecificGeneral(code: int, arg:'a):'b = RunCall.unsafeCast(osSpecificGeneralCall(RunCall.unsafeCast(code, arg))) and timingGeneral(code: int, arg:'a):'b = RunCall.unsafeCast(timingGeneralCall(RunCall.unsafeCast(code, arg))) end fun getConst i : SysWord.word = osSpecificGeneral (4, i) structure BitFlags = (* This structure is used as the basis of all the BIT_FLAGS structures. *) struct type flags = SysWord.word fun toWord f = f fun fromWord f = f val flags = List.foldl (fn (a, b) => SysWord.orb(a,b)) 0w0 fun allSet (fl1, fl2) = SysWord.andb(fl1, fl2) = fl1 fun anySet (fl1, fl2) = SysWord.andb(fl1, fl2) <> 0w0 fun clear (fl1, fl2) = SysWord.andb(SysWord.notb fl1, fl2) end structure Error = struct type syserror = OS.syserror (* Implemented as a SysWord.word value. *) val errorMsg = OS.errorMsg fun toWord (s: syserror): SysWord.word = RunCall.unsafeCast s and fromWord (w: SysWord.word) : syserror = RunCall.unsafeCast w val toobig = fromWord(getConst 0) and acces = fromWord(getConst 1) and again = fromWord(getConst 2) and badf = fromWord(getConst 3) and badmsg = fromWord(getConst 4) and busy = fromWord(getConst 5) and canceled (* sic *) = fromWord(getConst 6) and child = fromWord(getConst 7) and deadlk = fromWord(getConst 8) and dom = fromWord(getConst 9) and exist = fromWord(getConst 10) and fault = fromWord(getConst 11) and fbig = fromWord(getConst 12) and inprogress = fromWord(getConst 13) and intr = fromWord(getConst 14) and inval = fromWord(getConst 15) and io = fromWord(getConst 16) and isdir = fromWord(getConst 17) and loop = fromWord(getConst 18) and mfile = fromWord(getConst 19) and mlink = fromWord(getConst 20) and msgsize = fromWord(getConst 21) and nametoolong = fromWord(getConst 22) and nfile = fromWord(getConst 23) and nodev = fromWord(getConst 24) and noent = fromWord(getConst 25) and noexec = fromWord(getConst 26) and nolck = fromWord(getConst 27) and nomem = fromWord(getConst 28) and nospc = fromWord(getConst 29) and nosys = fromWord(getConst 30) and notdir = fromWord(getConst 31) and notempty = fromWord(getConst 32) and notsup = fromWord(getConst 33) and notty = fromWord(getConst 34) and nxio = fromWord(getConst 35) and perm = fromWord(getConst 36) and pipe = fromWord(getConst 37) and range = fromWord(getConst 38) and rofs = fromWord(getConst 39) and spipe = fromWord(getConst 40) and srch = fromWord(getConst 41) and xdev = fromWord(getConst 42) val errNames = [ (acces, "acces"), (again, "again"), (badf, "badf"), (badmsg, "badmsg"), (busy, "busy"), (canceled, "canceled"), (child, "child"), (deadlk, "deadlk"), (dom, "dom"), (exist, "exist"), (fault, "fault"), (fbig, "fbig"), (inprogress, "inprogress"), (intr, "intr"), (inval, "inval"), (io, "io"), (isdir, "isdir"), (loop, "loop"), (mfile, "mfile"), (mlink, "mlink"), (msgsize, "msgsize"), (nametoolong, "nametoolong"), (nfile, "nfile"), (nodev, "nodev"), (noent, "noent"), (noexec, "noexec"), (nolck, "nolck"), (nomem, "nomem"), (nospc, "nospc"), (nosys, "nosys"), (notdir, "notdir"), (notempty, "notempty"), (notsup, "notsup"), (notty, "notty"), (nxio, "nxio"), (perm, "perm"), (pipe, "pipe"), (range, "range"), (rofs, "rofs"), (spipe, "spipe"), (srch, "srch"), (toobig, "toobig"), (xdev, "xdev") ] (* These are defined to return the names above. *) fun errorName n = case List.find (fn (e, _) => e = n) errNames of SOME(_, s) => s | NONE => OS.errorName n fun syserror s = case List.find (fn (_, t) => s = t) errNames of SOME(e, _) => SOME e | NONE => OS.syserror s end; structure Signal = struct type signal = int val toWord = SysWord.fromInt and fromWord = SysWord.toInt (* These signal values are probably defined to correspond to particular numbers but there's no harm in getting them from the RTS. *) val abrt = fromWord(getConst 43) and alrm = fromWord(getConst 44) and bus = fromWord(getConst 45) and fpe = fromWord(getConst 46) and hup = fromWord(getConst 47) and ill = fromWord(getConst 48) and int = fromWord(getConst 49) and kill = fromWord(getConst 50) and pipe = fromWord(getConst 51) and quit = fromWord(getConst 52) and segv = fromWord(getConst 53) and term = fromWord(getConst 54) and usr1 = fromWord(getConst 55) and usr2 = fromWord(getConst 56) and chld = fromWord(getConst 57) and cont = fromWord(getConst 58) and stop = fromWord(getConst 59) and tstp = fromWord(getConst 60) and ttin = fromWord(getConst 61) and ttou = fromWord(getConst 62) end; structure Process = struct type signal = Signal.signal type pid = int val pidToWord = SysWord.fromInt and wordToPid = SysWord.toInt datatype waitpid_arg = W_ANY_CHILD | W_CHILD of pid | W_SAME_GROUP | W_GROUP of pid datatype exit_status = W_EXITED | W_EXITSTATUS of Word8.word | W_SIGNALED of signal | W_STOPPED of signal datatype killpid_arg = K_PROC of pid | K_SAME_GROUP | K_GROUP of pid structure W = struct open BitFlags val untraced = getConst 133 val nohang = getConst 134 (* Not exported. *) val all = flags [ untraced, nohang] val intersect = List.foldl (fn (a, b) => SysWord.andb(a, b)) all end local val doCall = osSpecificGeneral in fun fork () = case doCall(5, ()) of 0 => NONE (* Parent *) | n => SOME n (* Child *) end local val doCall = osSpecificGeneral in (* Map the pid argument to positive, zero or negative. *) fun kill (K_PROC pid, si) = doCall(6,(pid, si)) | kill (K_SAME_GROUP, si) = doCall(6, (0, si)) | kill (K_GROUP pid, si) = doCall(6, (~pid, si)) end local val doCall = osSpecificGeneral in (* The format of a result may well be sufficiently fixed that we could decode it without calling the RTS. It's probably worth the small cost to make maintenance easier. *) fun fromStatus (stat: OS.Process.status): exit_status = case (doCall(15, stat)) of (1, 0) => W_EXITED | (1, n) => W_EXITSTATUS(Word8.fromInt n) | (2, n) => W_SIGNALED n | (3, n) => W_STOPPED n | _ => raise Fail "Unknown result status" end local val doCall = osSpecificGeneral fun doWait(kind: int, pid: pid, flags: W.flags list) = let val (pid, status) = doCall(14, (kind, pid, SysWord.toInt(W.flags flags))) in (pid, fromStatus status) end in fun waitpid(W_ANY_CHILD, flags) = doWait(0, 0, flags) | waitpid(W_CHILD pid, flags) = doWait(1, pid, flags) | waitpid(W_SAME_GROUP, flags) = doWait(2, 0, flags) | waitpid(W_GROUP pid, flags) = doWait(3, pid, flags) fun wait() = waitpid(W_ANY_CHILD, []) fun waitpid_nh(wpa, flags) = let val (pid, status) = waitpid(wpa, W.nohang :: flags) in if pid = 0 then NONE else SOME(pid, status) end end fun exec(p, args) = osSpecificGeneral(17, (p, args)) and exece(p, args, env) = osSpecificGeneral(18, (p, args, env)) and execp(p, args) = osSpecificGeneral(19, (p, args)) (* The definition of "exit" is obviously designed to allow OS.Process.exit to be defined in terms of it. In particular it doesn't execute the functions registered with atExit. This should use Terminate rather than Finish so that C atExit routines aren't executed either. *) local val doExit: Word8.word -> unit = RunCall.rtsCallFull1 "PolyTerminate" in fun exit w = ( doExit w; raise Bind (* Never executed but gives the correct result type.*) ) end local val doCall = osSpecificGeneral fun toAbsolute t = if t < Time.zeroTime then raise OS.SysErr("Invalid time", NONE) else t + Time.now() (* Because of rounding we may get a negative time. In that case we return zero. *) fun endTime t = let val now = Time.now() in if t > now then t-now else Time.zeroTime end in (* This previously used absolute times. Now uses relative. *) fun alarm t = doCall(20, t) fun sleep t = let val finish = toAbsolute t in (* We need to pass in the absolute time here. That's because the process scheduler retries the function until a signal occurs or the time expires. *) (* The result is zero if it returns successfully. If an exception is raised we return the remaining time. We assume that this only happens because the process is interrupted. We don't handle the Interrupt exception, though. *) (doCall(22, finish); Time.zeroTime) handle OS.SysErr _ => endTime finish end end local val doCall = osSpecificGeneral in fun pause() = doCall(21, ()) end end; structure ProcEnv = struct type pid = Process.pid and file_desc = OS.IO.iodesc type uid = int and gid = int val uidToWord = SysWord.fromInt and wordToUid = SysWord.toInt and gidToWord = SysWord.fromInt and wordToGid = SysWord.toInt local val doCall = osSpecificGeneral in fun getpid () = doCall(7, ()) and getppid () = doCall(8, ()) and getuid () = doCall(9, ()) and geteuid () = doCall(10, ()) and getgid () = doCall(11, ()) and getegid () = doCall(12, ()) and getpgrp () = doCall(13, ()) and setsid () = doCall(27, ()) end val getenv = OS.Process.getEnv fun environ() = processEnvGeneral(21, ()) local val doCall = osSpecificGeneral in fun setuid(u: uid) = doCall(23, u) and setgid(g: gid) = doCall(24, g) end local val doCall = osSpecificGeneral in fun getgroups() = doCall(25, ()) end local val doCall = osSpecificGeneral in fun getlogin() = doCall(26, ()) and ctermid() = doCall(30, ()) end local val doCall = osSpecificGeneral in (* In each case NONE as an argument is taken as 0. *) fun setpgid{pid, pgid} = doCall(28, (getOpt(pid, 0), getOpt(pgid, 0))) end local val doCall = osSpecificGeneral in fun uname() = doCall(29, ()) end val time = Time.now fun times() = let (* Apart from the child times all these could be obtained by calling the Timer functions. *) val doCall: int*unit -> Time.time = timingGeneral fun getUserTime() = doCall(7, ()) and getSysTime() = doCall(8, ()) and getRealTime() = doCall(10, ()) and getChildUserTime() = doCall(11, ()) and getChildSysTime() = doCall(12, ()) in { elapsed=getRealTime(), utime=getUserTime(), stime=getSysTime(), cutime=getChildUserTime(), cstime=getChildSysTime()} end local val doCall = osSpecificGeneral in fun ttyname(f: file_desc) = doCall(31, f) end local val doCall = osSpecificGeneral in fun isatty(f: file_desc) = doCall(32, f) end local val doCall = osSpecificGeneral in fun sysconf(s: string) = SysWord.fromInt(doCall(33, s)) end end; structure FileSys = struct type uid = ProcEnv.uid and gid = ProcEnv.gid type file_desc = OS.IO.iodesc type dirstream = OS.FileSys.dirstream datatype open_mode = O_RDONLY | O_WRONLY | O_RDWR structure O = struct open BitFlags val append = getConst 66 and excl = getConst 67 and noctty = getConst 68 and nonblock = getConst 69 and sync = getConst 70 and trunc = getConst 71 val all = flags [append, excl, noctty, nonblock, sync, trunc] val intersect = List.foldl (fn (a, b) => SysWord.andb(a, b)) all end local val doIo: int*file_desc*unit -> int = RunCall.rtsCallFull3 "PolyBasicIOGeneral" in fun fdToWord (f: file_desc) = SysWord.fromInt(doIo(30, f, ())) end - local - val doIo: int*unit*int -> file_desc = RunCall.rtsCallFull3 "PolyBasicIOGeneral" - in - fun wordToFD(s: SysWord.word): file_desc = - doIo(31, (), SysWord.toInt s) - end (* file_desc and OS.IO.iodesc are the same. *) fun fdToIOD i = i and iodToFD i = SOME i val opendir = OS.FileSys.openDir and readdir = OS.FileSys.readDir and rewinddir = OS.FileSys.rewindDir and closedir = OS.FileSys.closeDir and chdir = OS.FileSys.chDir and getcwd = OS.FileSys.getDir and unlink = OS.FileSys.remove and rmdir = OS.FileSys.rmDir and rename = OS.FileSys.rename and readlink = OS.FileSys.readLink local val persistentFD: int -> file_desc = RunCall.rtsCallFull1 "PolyPosixCreatePersistentFD" in - val stdin : file_desc = persistentFD 0 - and stdout : file_desc = persistentFD 1 - and stderr : file_desc = persistentFD 2 + (* Use persistent file descriptors here. i.e. don't reset them to "invalid" if they are + read into a new session. We always want that for 0, 1 and 2 but it's not clear whether + that is correct for other file descriptors. Since this is a low-level function + assume that the caller understands the issues. *) + val wordToFD = persistentFD o SysWord.toInt end + val stdin = wordToFD 0w0 (* Must be persistent. *) + and stdout = wordToFD 0w1 + and stderr = wordToFD 0w2 + structure S = struct open BitFlags type mode = flags val irusr : mode = getConst 145 and iwusr : mode = getConst 146 and ixusr : mode = getConst 147 val irwxu : mode = flags[irusr, iwusr, ixusr] val irgrp : mode = getConst 148 and iwgrp : mode = getConst 149 and ixgrp : mode = getConst 150 val irwxg : mode = flags[irgrp, iwgrp, ixgrp] val iroth : mode = getConst 151 and iwoth : mode = getConst 152 and ixoth : mode = getConst 153 val irwxo : mode = flags[iroth, iwoth, ixoth] val isuid : mode = getConst 154 val isgid : mode = getConst 155 val all = flags [irwxu, irwxg, irwxo, isuid, isgid] val intersect = List.foldl (fn (a, b) => SysWord.andb(a, b)) all end local val o_rdonly = getConst 63 and o_wronly = getConst 64 and o_rdwr = getConst 65 fun toBits O_RDONLY = o_rdonly | toBits O_WRONLY = o_wronly | toBits O_RDWR = o_rdwr val doIo = RunCall.rtsCallFull3 "PolyBasicIOGeneral" in fun openf(name, mode, flags) = let val bits = SysWord.orb(flags, toBits mode) in doIo(70, 0, (name, SysWord.toInt bits, 0)) end and createf(name, mode, flags, smode) = let val bits = SysWord.orb(flags, toBits mode) in doIo(71, 0, (name, SysWord.toInt bits, SysWord.toInt smode)) end end fun creat(s, m) = createf(s, O_WRONLY, O.trunc, m) local val doCall = osSpecificGeneral in fun umask m = SysWord.fromInt(doCall(50, SysWord.toInt m)) end local val doCall = osSpecificGeneral in fun link{old, new} = doCall(51, (old, new)) and symlink{old, new} = doCall(54, (old, new)) end local val doCall = osSpecificGeneral in fun mkdir(name, mode) = doCall(52, (name, SysWord.toInt mode)) and mkfifo(name, mode) = doCall(53, (name, SysWord.toInt mode)) and chmod(name, mode) = doCall(59, (name, SysWord.toInt mode)) end type dev = LargeInt.int and ino = LargeInt.int val wordToDev = SysWord.toLargeInt and devToWord = SysWord.fromLargeInt and wordToIno = SysWord.toLargeInt and inoToWord = SysWord.fromLargeInt structure ST = struct type stat = { mode: S.mode, kind: int, ino: ino, dev: dev, nlink: int, uid: uid, gid: gid, size: Position.int, atime: Time.time, mtime: Time.time, ctime: Time.time } (* The "kind" information is encoded by "stat" *) fun isDir({ kind, ...} : stat) = kind = 1 and isChr({ kind, ...} : stat) = kind = 2 and isBlk({ kind, ...} : stat) = kind = 3 and isReg({ kind, ...} : stat) = kind = 0 and isFIFO({ kind, ...} : stat) = kind = 4 and isLink({ kind, ...} : stat) = kind = 5 and isSock({ kind, ...} : stat) = kind = 6 val mode : stat -> S.mode = #mode and ino : stat -> ino = #ino val dev : stat -> dev = #dev val nlink : stat -> int = #nlink val uid : stat -> uid = #uid val gid : stat -> gid = #gid val size : stat -> Position.int = #size val atime : stat -> Time.time = #atime val mtime : stat -> Time.time = #mtime val ctime : stat -> Time.time = #ctime end local val doCall1 = osSpecificGeneral val doCall2 = osSpecificGeneral fun convStat(mode, kind, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = { mode = SysWord.fromInt mode, kind = kind, ino = ino, dev = dev, nlink = nlink, uid = uid, gid = gid, size = size, atime = atime, mtime = mtime, ctime = ctime } in fun stat name = convStat(doCall1(55, name)) and lstat name = convStat(doCall1(56, name)) and fstat f = convStat(doCall2(57, f)) end datatype access_mode = datatype OS.FileSys.access_mode local val doCall = osSpecificGeneral val rOK = getConst 156 and wOK = getConst 157 and eOK = getConst 158 and fOK = getConst 159 fun abit A_READ = rOK | abit A_WRITE = wOK | abit A_EXEC = eOK val abits = List.foldl (fn (a, b) => SysWord.orb(abit a,b)) 0w0 in (* If the bits are nil it tests for existence of the file. *) fun access(name, []) = doCall(58, (name, SysWord.toInt(fOK))) | access(name, al) = doCall(58, (name, SysWord.toInt(abits al))) end local val doCall = osSpecificGeneral in fun fchmod(fd, mode) = doCall(60, (fd, SysWord.toInt mode)) end local val doCall = osSpecificGeneral in fun chown(name, uid, gid) = doCall(61, (name, uid, gid)) end local val doCall = osSpecificGeneral in fun fchown(fd, uid, gid) = doCall(62, (fd, uid, gid)) end local val doCall1 = osSpecificGeneral and doCall2 = osSpecificGeneral in fun utime (name, NONE) = doCall1(64, name) | utime (name, SOME{actime, modtime}) = doCall2(63, (name, actime, modtime)) end local val doCall = osSpecificGeneral in fun ftruncate(fd, size) = doCall(65, (fd, size)) end local val doCall = osSpecificGeneral in fun pathconf(name, var) = let val res = doCall(66, (name, var)) in if res < 0 then NONE else SOME(SysWord.fromInt res) end end local val doCall = osSpecificGeneral in fun fpathconf(fd, var) = let val res = doCall(67, (fd, var)) in if res < 0 then NONE else SOME(SysWord.fromInt res) end end end; structure IO = struct type file_desc = OS.IO.iodesc and pid = Process.pid structure FD = struct open BitFlags val cloexec: flags = getConst 132 val all = flags [cloexec] val intersect = List.foldl (fn (a, b) => SysWord.andb(a, b)) all end (* Posix.IO.O seems to be a cut-down version of Posix.FileSys.O. It seems to me that one structure would suffice. *) structure O = FileSys.O datatype open_mode = datatype FileSys.open_mode local val doIo = RunCall.rtsCallFull3 "PolyBasicIOGeneral" in fun close (strm: file_desc): unit = doIo(7, strm, 0) end local val doIo = RunCall.rtsCallFull3 "PolyBasicIOGeneral" in fun readVec (strm: file_desc, len: int): Word8Vector.vector = doIo(26, strm, len) end local val doCall = osSpecificGeneral in fun pipe() = let val (inf, outf) = doCall(110, ()) in { infd=inf, outfd=outf } end end local val doCall = osSpecificGeneral in fun dup fd = doCall(111, fd) end local val doCall = osSpecificGeneral in fun dup2{old, new} = doCall(112, (old, new)) end local val doCall = osSpecificGeneral in fun dupfd{old, base} = doCall(113, (old, base)) end local val doCall = osSpecificGeneral val o_rdonly = getConst 63 and o_wronly = getConst 64 and o_accmode = getConst 166 (* Access mode mask. *) in fun getfd fd = SysWord.fromInt(doCall(114, fd)) and getfl fd = let val res = SysWord.fromInt(doCall(116, fd)) (* Separate out the mode bits. *) val flgs = SysWord.andb(res, SysWord.notb o_accmode) val mode = SysWord.andb(res, o_accmode) val omode = if mode = o_rdonly then O_RDONLY else if mode = o_wronly then O_WRONLY else O_RDWR in (flgs, omode) end end local val doCall = osSpecificGeneral in fun setfd(fd, flags) = doCall(115, (fd, SysWord.toInt flags)) and setfl(fd, flags) = doCall(117, (fd, SysWord.toInt flags)) end datatype whence = SEEK_SET | SEEK_CUR | SEEK_END local val seekSet = SysWord.toInt(getConst 160) and seekCur = SysWord.toInt(getConst 161) and seekEnd = SysWord.toInt(getConst 162) in (* Convert the datatype to the corresponding int. *) fun seekWhence SEEK_SET = seekSet | seekWhence SEEK_CUR = seekCur | seekWhence SEEK_END = seekEnd fun whenceSeek s = if s = seekSet then SEEK_SET else if s = seekCur then SEEK_CUR else SEEK_END end local val doCall = osSpecificGeneral in fun lseek(fd, pos, whence) = doCall(118, (fd, pos, seekWhence whence)) end local val doCall = osSpecificGeneral in fun fsync fd = doCall(119, fd) end datatype lock_type = F_RDLCK | F_WRLCK | F_UNLCK structure FLock = struct val fRdlck = SysWord.toInt(getConst 163) and fWrlck = SysWord.toInt(getConst 164) and fUnlck = SysWord.toInt(getConst 165) type flock = int (* lock type *) * int (* whence *) * Position.int (* start *) * Position.int (* len *) * pid fun flock{ltype, whence, start, len, pid} = let val lt = case ltype of F_RDLCK => fRdlck | F_WRLCK => fWrlck | F_UNLCK => fUnlck in (lt, seekWhence whence, start, len, getOpt(pid, ~1)) end fun ltype (lt, _, _, _, _) = if lt = fRdlck then F_RDLCK else if lt = fWrlck then F_WRLCK else F_UNLCK fun whence (fl: flock) = whenceSeek(#2 fl) val start : flock -> Position.int = #3 val len : flock -> Position.int = #4 fun pid (_, _, _, _, pid) = if pid < 0 then NONE else SOME pid end local val doCall = osSpecificGeneral in fun getlk(fd, (t, w, s, l, p)) = doCall(120, (fd, t, w, s, l, p)) (* Note: the return type of setlk and setlkw is Flock.lock not unit. I assume they simply return their argument. *) and setlk(fd, (t, w, s, l, p)) = doCall(121, (fd, t, w, s, l, p)) and setlkw(fd, (t, w, s, l, p)) = doCall(122, (fd, t, w, s, l, p)) end val readArr = LibraryIOSupport.readBinArray and writeVec = LibraryIOSupport.writeBinVec and writeArr = LibraryIOSupport.writeBinArray val mkTextReader = LibraryIOSupport.wrapInFileDescr and mkTextWriter = LibraryIOSupport.wrapOutFileDescr val mkBinReader = LibraryIOSupport.wrapBinInFileDescr and mkBinWriter = LibraryIOSupport.wrapBinOutFileDescr end; structure SysDB = struct type uid = ProcEnv.uid and gid = ProcEnv.gid structure Passwd = struct type passwd = string * uid * gid * string * string val name: passwd->string = #1 and uid: passwd->uid = #2 and gid: passwd->gid = #3 and home: passwd->string = #4 and shell: passwd->string = #5 end structure Group = struct type group = string * gid * string list val name: group->string = #1 and gid: group->gid = #2 and members: group->string list = #3 end local val doCall = osSpecificGeneral in fun getpwnam (s: string): Passwd.passwd = doCall(100, s) end local val doCall = osSpecificGeneral in fun getpwuid (u: uid): Passwd.passwd = doCall(101, u) end local val doCall = osSpecificGeneral in fun getgrnam (s: string): Group.group = doCall(102, s) end local val doCall = osSpecificGeneral in fun getgrgid (g: gid): Group.group = doCall(103, g) end end; structure TTY = struct type pid = Process.pid and file_desc = OS.IO.iodesc structure V = struct val eof = SysWord.toInt(getConst 72) and eol = SysWord.toInt(getConst 73) and erase = SysWord.toInt(getConst 74) and intr = SysWord.toInt(getConst 75) and kill = SysWord.toInt(getConst 76) and min = SysWord.toInt(getConst 77) and quit = SysWord.toInt(getConst 78) and susp = SysWord.toInt(getConst 79) and time = SysWord.toInt(getConst 80) and start = SysWord.toInt(getConst 81) and stop = SysWord.toInt(getConst 82) and nccs = SysWord.toInt(getConst 83) type cc = string fun cc l = (* Generate a string using the values given and defaulting the rest to NULL. *) let fun find [] _ = #"\000" | find ((n, c)::l) i = if i = n then c else find l i in CharVector.tabulate(nccs, find l) end (* Question: What order does this take? E.g. What is the result of update(cc, [(eof, #"a"), (eof, #"b")]) ? Assume that earlier entries take precedence. That also affects the processing of exceptions. *) fun update(cc, l) = let fun find [] i = String.sub(cc, i) | find ((n, c)::l) i = if i = n then c else find l i in CharVector.tabulate(nccs, find l) end val sub = String.sub end structure I = struct open BitFlags val brkint = getConst 84 and icrnl = getConst 85 and ignbrk = getConst 86 and igncr = getConst 87 and ignpar = getConst 88 and inlcr = getConst 89 and inpck = getConst 90 and istrip = getConst 91 and ixoff = getConst 92 and ixon = getConst 93 and parmrk = getConst 94 val all = flags [brkint, icrnl, ignbrk, igncr, ignpar, inlcr, inpck, istrip, ixoff, ixon, parmrk] val intersect = List.foldl (fn (a, b) => SysWord.andb(a, b)) all end structure O = struct open BitFlags val opost = getConst 95 val all = flags [opost] val intersect = List.foldl (fn (a, b) => SysWord.andb(a, b)) all end structure C = struct open BitFlags val clocal = getConst 96 and cread = getConst 97 and cs5 = getConst 98 and cs6 = getConst 99 and cs7 = getConst 100 and cs8 = getConst 101 and csize = getConst 102 and cstopb = getConst 103 and hupcl = getConst 104 and parenb = getConst 105 and parodd = getConst 106 val all = flags [clocal, cread, cs5, cs6, cs7, cs8, csize, cstopb, hupcl, parenb, parodd] val intersect = List.foldl (fn (a, b) => SysWord.andb(a, b)) all end structure L = struct open BitFlags val echo = getConst 107 and echoe = getConst 108 and echok = getConst 109 and echonl = getConst 110 and icanon = getConst 111 and iexten = getConst 112 and isig = getConst 113 and noflsh = getConst 114 and tostop = getConst 115 val all = flags [echo, echoe, echok, echonl, icanon, iexten, isig, noflsh, tostop] val intersect = List.foldl (fn (a, b) => SysWord.andb(a, b)) all end type speed = int (* compareSpeed is supposed to compare by the baud rate, not by the encoding. Provided the encoding maintains the ordering then that's fine. Maybe we should have an RTS call. *) val compareSpeed : speed * speed -> order = Int.compare and speedToWord : speed -> SysWord.word = SysWord.fromInt and wordToSpeed : SysWord.word -> speed = SysWord.toInt val b0 : speed = SysWord.toInt(getConst 116) and b50 : speed = SysWord.toInt(getConst 117) and b75 : speed = SysWord.toInt(getConst 118) and b110 : speed = SysWord.toInt(getConst 119) and b134 : speed = SysWord.toInt(getConst 120) and b150 : speed = SysWord.toInt(getConst 121) and b200 : speed = SysWord.toInt(getConst 122) and b300 : speed = SysWord.toInt(getConst 123) and b600 : speed = SysWord.toInt(getConst 124) and b1200 : speed = SysWord.toInt(getConst 125) and b1800 : speed = SysWord.toInt(getConst 126) and b2400 : speed = SysWord.toInt(getConst 127) and b4800 : speed = SysWord.toInt(getConst 128) and b9600 : speed = SysWord.toInt(getConst 129) and b19200 : speed = SysWord.toInt(getConst 130) and b38400 : speed = SysWord.toInt(getConst 131) type termios = { iflag : I.flags, oflag : O.flags, cflag : C.flags, lflag : L.flags, cc : V.cc, ispeed : speed, ospeed : speed } fun termios t = t and fieldsOf t = t val getiflag : termios -> I.flags = #iflag and getoflag : termios -> O.flags = #oflag and getcflag : termios -> C.flags = #cflag and getlflag : termios -> L.flags = #lflag and getcc : termios -> V.cc = #cc structure CF = struct val getospeed : termios -> speed = #ospeed and getispeed : termios -> speed = #ispeed fun setospeed ({ iflag, oflag, cflag, lflag, cc, ispeed, ... }, speed) = { iflag=iflag, oflag=oflag, cflag=cflag, lflag=lflag, cc=cc, ispeed = ispeed, ospeed = speed } fun setispeed ({ iflag, oflag, cflag, lflag, cc, ospeed, ... }, speed) = { iflag=iflag, oflag=oflag, cflag=cflag, lflag=lflag, cc=cc, ispeed = speed, ospeed = ospeed } end structure TC = struct type set_action = int val sanow : set_action = SysWord.toInt(getConst 135) val sadrain : set_action = SysWord.toInt(getConst 136) val saflush : set_action = SysWord.toInt(getConst 137) type flow_action = int val ooff : flow_action = SysWord.toInt(getConst 138) val oon : flow_action = SysWord.toInt(getConst 139) val ioff : flow_action = SysWord.toInt(getConst 140) val ion : flow_action = SysWord.toInt(getConst 141) type queue_sel = int val iflush : queue_sel = SysWord.toInt(getConst 142) val oflush : queue_sel = SysWord.toInt(getConst 143) val ioflush : queue_sel = SysWord.toInt(getConst 144) local val doCall = osSpecificGeneral in fun getattr f = let val (iflag, oflag, cflag, lflag, cc, ispeed, ospeed) = doCall(150, f) in { iflag=SysWord.fromInt iflag, oflag=SysWord.fromInt oflag, cflag=SysWord.fromInt cflag, lflag=SysWord.fromInt lflag, cc=cc, ispeed = ispeed, ospeed = ospeed } end end local val doCall = osSpecificGeneral in fun setattr (f, sa, {iflag, oflag, cflag, lflag, cc, ispeed, ospeed}) = doCall(151, (f, sa, SysWord.toInt iflag, SysWord.toInt oflag, SysWord.toInt cflag, SysWord.toInt lflag, cc, ispeed, ospeed)) end local val doCall = osSpecificGeneral in fun sendbreak (f, d) = doCall(152, (f, d)) end local val doCall = osSpecificGeneral in fun drain f = doCall(153, f) end local val doCall = osSpecificGeneral in fun flush (f, qs) = doCall(154, (f, qs)) end local val doCall = osSpecificGeneral in fun flow (f, fa) = doCall(155, (f, fa)) end end local val doCall = osSpecificGeneral in fun getpgrp (f: file_desc): pid = doCall(156, f) end local val doCall = osSpecificGeneral in fun setpgrp (f: file_desc, p: pid): unit = doCall(157, (f,p)) end end end; local (* Install the pretty printers for pid, uid, gid. Don't install one for signal because it's now the same as int. *) fun ppid _ _ x = PolyML.PrettyString(Int.toString(SysWord.toInt(Posix.Process.pidToWord x))) and puid _ _ x = PolyML.PrettyString(Int.toString(SysWord.toInt(Posix.ProcEnv.uidToWord x))) and pgid _ _ x = PolyML.PrettyString(Int.toString(SysWord.toInt(Posix.ProcEnv.gidToWord x))) in val () = PolyML.addPrettyPrinter ppid val () = PolyML.addPrettyPrinter puid val () = PolyML.addPrettyPrinter pgid end; diff --git a/libpolyml/basicio.cpp b/libpolyml/basicio.cpp index 09db46b6..f9e39b21 100644 --- a/libpolyml/basicio.cpp +++ b/libpolyml/basicio.cpp @@ -1,1151 +1,1151 @@ /* Title: Basic IO. - Copyright (c) 2000, 2015-2019 David C. J. Matthews + Copyright (c) 2000, 2015-2020 David C. J. Matthews Portions of this code are derived from the original stream io package copyright CUTS 1983-2000. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ /* This module replaces the old stream IO based on stdio. It works at a lower level with the buffering being done in ML. Sockets are generally dealt with in network.c but it is convenient to use the same table for them particularly since it simplifies the implementation of "poll". Directory operations are also included in here. DCJM May 2000. */ #ifdef HAVE_CONFIG_H #include "config.h" #elif defined(_WIN32) #include "winconfig.h" #else #error "No configuration file" #endif #ifdef HAVE_FCNTL_H #include #endif #ifdef HAVE_SYS_TYPES_H #include #endif #ifdef HAVE_SYS_STAT_H #include #endif #ifdef HAVE_ASSERT_H #include #define ASSERT(x) assert(x) #else #define ASSERT(x) 0 #endif #ifdef HAVE_ERRNO_H #include #endif #ifdef HAVE_SIGNAL_H #include #endif #ifdef HAVE_STDLIB_H #include #endif #ifdef HAVE_ALLOCA_H #include #endif #ifdef HAVE_IO_H #include #endif #ifdef HAVE_SYS_PARAM_H #include #endif #ifdef HAVE_SYS_IOCTL_H #include #endif #ifdef HAVE_SYS_TIME_H #include #endif #ifdef HAVE_UNISTD_H #include #endif #ifdef HAVE_POLL_H #include #endif #ifdef HAVE_STRING_H #include #endif #ifdef HAVE_SYS_SELECT_H #include #endif #ifdef HAVE_MALLOC_H #include #endif #ifdef HAVE_DIRECT_H #include #endif #ifdef HAVE_STDIO_H #include #endif #include #ifndef INFTIM #define INFTIM (-1) #endif #ifdef HAVE_DIRENT_H # include # define NAMLEN(dirent) strlen((dirent)->d_name) #else # define dirent direct # define NAMLEN(dirent) (dirent)->d_namlen # if HAVE_SYS_NDIR_H # include # endif # if HAVE_SYS_DIR_H # include # endif # if HAVE_NDIR_H # include # endif #endif #include "globals.h" #include "basicio.h" #include "sys.h" #include "gc.h" #include "run_time.h" #include "machine_dep.h" #include "arb.h" #include "processes.h" #include "diagnostics.h" #include "io_internal.h" #include "scanaddrs.h" #include "polystring.h" #include "mpoly.h" #include "save_vec.h" #include "rts_module.h" #include "locking.h" #include "rtsentry.h" #include "timing.h" #define TOOMANYFILES EMFILE #define NOMEMORY ENOMEM #define STREAMCLOSED EBADF #define FILEDOESNOTEXIST ENOENT #define ERRORNUMBER errno #ifndef O_ACCMODE #define O_ACCMODE (O_RDONLY|O_RDWR|O_WRONLY) #endif #define SAVE(x) taskData->saveVec.push(x) #ifdef _MSC_VER // Don't tell me about ISO C++ changes. #pragma warning(disable:4996) #endif extern "C" { POLYEXTERNALSYMBOL POLYUNSIGNED PolyChDir(PolyObject *threadId, PolyWord arg); POLYEXTERNALSYMBOL POLYUNSIGNED PolyBasicIOGeneral(PolyObject *threadId, PolyWord code, PolyWord strm, PolyWord arg); POLYEXTERNALSYMBOL POLYUNSIGNED PolyPosixCreatePersistentFD(PolyObject *threadId, PolyWord fd); } static bool isAvailable(TaskData *taskData, int ioDesc) { #ifdef __CYGWIN__ static struct timeval poll = {0,1}; #else static struct timeval poll = {0,0}; #endif fd_set read_fds; int selRes; FD_ZERO(&read_fds); FD_SET(ioDesc, &read_fds); /* If there is something there we can return. */ selRes = select(FD_SETSIZE, &read_fds, NULL, NULL, &poll); if (selRes > 0) return true; /* Something waiting. */ else if (selRes < 0 && errno != EINTR) // Maybe another thread closed descr raise_syscall(taskData, "select error", ERRORNUMBER); else return false; } // The strm argument is a volatile word containing the descriptor. // Volatiles are set to zero on entry to indicate a closed descriptor. // Zero is a valid descriptor but -1 is not so we add 1 when storing and // subtract 1 when loading. // N.B. There are also persistent descriptors created with PolyPosixCreatePersistentFD Handle wrapFileDescriptor(TaskData *taskData, int fd) { return MakeVolatileWord(taskData, fd+1); } // Return a file descriptor or -1 if it is invalid. int getStreamFileDescriptorWithoutCheck(PolyWord strm) { return *(intptr_t*)(strm.AsObjPtr()) -1; } // Most of the time we want to raise an exception if the file descriptor // has been closed although this could be left to the system call. int getStreamFileDescriptor(TaskData *taskData, PolyWord strm) { int descr = getStreamFileDescriptorWithoutCheck(strm); if (descr == -1) raise_syscall(taskData, "Stream is closed", STREAMCLOSED); return descr; } /* Open a file in the required mode. */ static Handle open_file(TaskData *taskData, Handle filename, int mode, int access, int isPosix) { while (true) // Repeat only with certain kinds of errors { TempString cFileName(filename->Word()); // Get file name if (cFileName == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); int stream = open(cFileName, mode, access); if (stream >= 0) { if (! isPosix) { /* Set the close-on-exec flag. We don't set this if we are being called from one of the low level functions in the Posix structure. I assume that if someone is using those functions they know what they're doing and would expect the behaviour to be close to that of the underlying function. */ fcntl(stream, F_SETFD, 1); } return wrapFileDescriptor(taskData, stream); } switch (errno) { case EINTR: // Just try the call. Is it possible to block here indefinitely? continue; default: raise_syscall(taskData, "Cannot open", ERRORNUMBER); /*NOTREACHED*/ return 0; } } } /* Close the stream unless it is stdin or stdout or already closed. */ static Handle close_file(TaskData *taskData, Handle stream) { int descr = getStreamFileDescriptorWithoutCheck(stream->Word()); // Don't close it if it's already closed or any of the standard streams if (descr > 2) { close(descr); *(intptr_t*)(stream->WordP()) = 0; // Mark as closed } return Make_fixed_precision(taskData, 0); } static void waitForAvailableInput(TaskData *taskData, Handle stream) { int fd = getStreamFileDescriptor(taskData, stream->Word()); while (!isAvailable(taskData, fd)) { WaitInputFD waiter(fd); processes->ThreadPauseForIO(taskData, &waiter); } } /* Read into an array. */ // We can't combine readArray and readString because we mustn't compute the // destination of the data in readArray until after any GC. static Handle readArray(TaskData *taskData, Handle stream, Handle args, bool/*isText*/) { /* The isText argument is ignored in both Unix and Windows but is provided for future use. Windows remembers the mode used when the file was opened to determine whether to translate CRLF into LF. */ // We should check for interrupts even if we're not going to block. processes->TestAnyEvents(taskData); while (1) // Loop if interrupted. { // First test to see if we have input available. // These tests may result in a GC if another thread is running. // First test to see if we have input available. // These tests may result in a GC if another thread is running. waitForAvailableInput(taskData, stream); // We can now try to read without blocking. // Actually there's a race here in the unlikely situation that there // are multiple threads sharing the same low-level reader. They could // both detect that input is available but only one may succeed in // reading without blocking. This doesn't apply where the threads use // the higher-level IO interfaces in ML which have their own mutexes. int fd = getStreamFileDescriptor(taskData, stream->Word()); byte *base = DEREFHANDLE(args)->Get(0).AsObjPtr()->AsBytePtr(); POLYUNSIGNED offset = getPolyUnsigned(taskData, DEREFWORDHANDLE(args)->Get(1)); size_t length = getPolyUnsigned(taskData, DEREFWORDHANDLE(args)->Get(2)); ssize_t haveRead = read(fd, base + offset, length); if (haveRead >= 0) return Make_fixed_precision(taskData, haveRead); // Success. // If it failed because it was interrupted keep trying otherwise it's an error. if (errno != EINTR) raise_syscall(taskData, "Error while reading", ERRORNUMBER); } } /* Return input as a string. We don't actually need both readArray and readString but it's useful to have both to reduce unnecessary garbage. The IO library will construct one from the other but the higher levels choose the appropriate function depending on need. */ static Handle readString(TaskData *taskData, Handle stream, Handle args, bool/*isText*/) { size_t length = getPolyUnsigned(taskData, DEREFWORD(args)); // We should check for interrupts even if we're not going to block. processes->TestAnyEvents(taskData); while (1) // Loop if interrupted. { // First test to see if we have input available. // These tests may result in a GC if another thread is running. waitForAvailableInput(taskData, stream); // We can now try to read without blocking. int fd = getStreamFileDescriptor(taskData, stream->Word()); // We previously allocated the buffer on the stack but that caused // problems with multi-threading at least on Mac OS X because of // stack exhaustion. We limit the space to 100k. */ if (length > 102400) length = 102400; byte *buff = (byte*)malloc(length); if (buff == 0) raise_syscall(taskData, "Unable to allocate buffer", NOMEMORY); ssize_t haveRead = read(fd, buff, length); if (haveRead >= 0) { Handle result = SAVE(C_string_to_Poly(taskData, (char*)buff, haveRead)); free(buff); return result; } free(buff); // If it failed because it was interrupted keep trying otherwise it's an error. if (errno != EINTR) raise_syscall(taskData, "Error while reading", ERRORNUMBER); } } static Handle writeArray(TaskData *taskData, Handle stream, Handle args, bool/*isText*/) { /* The isText argument is ignored in both Unix and Windows but is provided for future use. Windows remembers the mode used when the file was opened to determine whether to translate LF into CRLF. */ PolyWord base = DEREFWORDHANDLE(args)->Get(0); POLYUNSIGNED offset = getPolyUnsigned(taskData, DEREFWORDHANDLE(args)->Get(1)); size_t length = getPolyUnsigned(taskData, DEREFWORDHANDLE(args)->Get(2)); int fd = getStreamFileDescriptor(taskData, stream->Word()); /* We don't actually handle cases of blocking on output. */ byte *toWrite = base.AsObjPtr()->AsBytePtr(); ssize_t haveWritten = write(fd, toWrite+offset, length); if (haveWritten < 0) raise_syscall(taskData, "Error while writing", ERRORNUMBER); return Make_fixed_precision(taskData, haveWritten); } // Test whether we can write without blocking. Returns false if it will block, // true if it will not. static bool canOutput(TaskData *taskData, Handle stream) { int fd = getStreamFileDescriptor(taskData, stream->Word()); /* Unix - use "select" to find out if output is possible. */ #ifdef __CYGWIN__ static struct timeval poll = {0,1}; #else static struct timeval poll = {0,0}; #endif fd_set read_fds, write_fds, except_fds; int sel; FD_ZERO(&read_fds); FD_ZERO(&write_fds); FD_ZERO(&except_fds); FD_SET(fd, &write_fds); sel = select(FD_SETSIZE,&read_fds,&write_fds,&except_fds,&poll); if (sel < 0 && errno != EINTR) raise_syscall(taskData, "select failed", ERRORNUMBER); return sel > 0; } static long seekStream(TaskData *taskData, int fd, long pos, int origin) { long lpos = lseek(fd, pos, origin); if (lpos < 0) raise_syscall(taskData, "Position error", ERRORNUMBER); return lpos; } /* Return the number of bytes available on the device. Works only for files since it is meaningless for other devices. */ static Handle bytesAvailable(TaskData *taskData, Handle stream) { int fd = getStreamFileDescriptor(taskData, stream->Word()); /* Remember our original position, seek to the end, then seek back. */ long original = seekStream(taskData, fd, 0L, SEEK_CUR); long endOfStream = seekStream(taskData, fd, 0L, SEEK_END); if (seekStream(taskData, fd, original, SEEK_SET) != original) raise_syscall(taskData, "Position error", ERRORNUMBER); return Make_fixed_precision(taskData, endOfStream-original); } static Handle fileKind(TaskData *taskData, Handle stream) { int fd = getStreamFileDescriptor(taskData, stream->Word()); struct stat statBuff; if (fstat(fd, &statBuff) < 0) raise_syscall(taskData, "Stat failed", ERRORNUMBER); switch (statBuff.st_mode & S_IFMT) { case S_IFIFO: return Make_fixed_precision(taskData, FILEKIND_PIPE); case S_IFCHR: case S_IFBLK: if (isatty(fd)) return Make_fixed_precision(taskData, FILEKIND_TTY); else return Make_fixed_precision(taskData, FILEKIND_DEV); case S_IFDIR: return Make_fixed_precision(taskData, FILEKIND_DIR); case S_IFREG: return Make_fixed_precision(taskData, FILEKIND_FILE); case S_IFLNK: return Make_fixed_precision(taskData, FILEKIND_LINK); case S_IFSOCK: return Make_fixed_precision(taskData, FILEKIND_SKT); default: return Make_fixed_precision(taskData, -1); } } /* Find out what polling options, if any, are allowed on this file descriptor. We assume that polling is allowed on all descriptors, either for reading or writing depending on how the stream was opened. */ Handle pollTest(TaskData *taskData, Handle stream) { // How do we test this? Assume all of them. return Make_fixed_precision(taskData, POLL_BIT_IN|POLL_BIT_OUT|POLL_BIT_PRI); } // Do the polling. Takes a vector of io descriptors, a vector of bits to test // and a time to wait and returns a vector of results. class WaitPoll: public Waiter{ public: WaitPoll(POLYUNSIGNED nDesc, struct pollfd *fds, unsigned maxMillisecs); virtual void Wait(unsigned maxMillisecs); int PollResult(void) { return pollResult; } int PollError(void) { return errorResult; } private: int pollResult; int errorResult; unsigned maxTime; struct pollfd *fdVec; POLYUNSIGNED nDescr; }; WaitPoll::WaitPoll(POLYUNSIGNED nDesc, struct pollfd *fds, unsigned maxMillisecs) { maxTime = maxMillisecs; pollResult = 0; errorResult = 0; nDescr = nDesc; fdVec = fds; } void WaitPoll::Wait(unsigned maxMillisecs) { if (nDescr == 0) pollResult = 0; else { if (maxTime < maxMillisecs) maxMillisecs = maxTime; pollResult = poll(fdVec, nDescr, maxMillisecs); if (pollResult < 0) errorResult = ERRORNUMBER; } } static Handle pollDescriptors(TaskData *taskData, Handle args, int blockType) { Handle hSave = taskData->saveVec.mark(); PolyObject *strmVec = DEREFHANDLE(args)->Get(0).AsObjPtr(); PolyObject *bitVec = DEREFHANDLE(args)->Get(1).AsObjPtr(); POLYUNSIGNED nDesc = strmVec->Length(); ASSERT(nDesc == bitVec->Length()); while (1) // Until timeout or we get a result. { struct pollfd * fds = 0; unsigned maxMillisecs = 1000; // Set the wait time. This code is almost the same as selectCall in network.cpp. switch (blockType) { case 0: { struct timeval tvTime, tvNow; /* We have a value in microseconds. We need to split it into seconds and microseconds. */ Handle hTime = SAVE(DEREFWORDHANDLE(args)->Get(2)); Handle hMillion = Make_arbitrary_precision(taskData, 1000000); tvTime.tv_sec = get_C_ulong(taskData, DEREFWORD(div_longc(taskData, hMillion, hTime))); tvTime.tv_usec = get_C_ulong(taskData, DEREFWORD(rem_longc(taskData, hMillion, hTime))); // If the timeout time is earlier than the current time we just poll // otherwise we block for up to a second. if (gettimeofday(&tvNow, NULL) != 0) raise_syscall(taskData, "gettimeofday failed", errno); if (tvNow.tv_sec > tvTime.tv_sec || (tvNow.tv_sec == tvTime.tv_sec && tvNow.tv_usec >= tvTime.tv_usec)) maxMillisecs = 0; else { subTimevals(&tvTime, &tvNow); if (tvTime.tv_sec >= 1) maxMillisecs = 1000; // Don't overflow if it's very long else maxMillisecs = tvTime.tv_usec / 1000; } break; } case 1: /* Block until one of the descriptors is ready. */ maxMillisecs = 1000; // Max 1 second break; case 2: // Just a simple poll maxMillisecs = 0; break; } if (nDesc > 0) fds = (struct pollfd *)alloca(nDesc * sizeof(struct pollfd)); /* Set up the request vector. */ for (unsigned i = 0; i < nDesc; i++) { fds[i].fd = getStreamFileDescriptor(taskData, strmVec->Get(i)); POLYUNSIGNED bits = UNTAGGED(bitVec->Get(i)); fds[i].events = 0; if (bits & POLL_BIT_IN) fds[i].events |= POLLIN; /* | POLLRDNORM??*/ if (bits & POLL_BIT_OUT) fds[i].events |= POLLOUT; if (bits & POLL_BIT_PRI) fds[i].events |= POLLPRI; fds[i].revents = 0; } // Poll the descriptors. WaitPoll pollWait(nDesc, fds, maxMillisecs); processes->ThreadPauseForIO(taskData, &pollWait); if (pollWait.PollResult() < 0) raise_syscall(taskData, "poll failed", pollWait.PollError()); else if (pollWait.PollResult() > 0 || maxMillisecs == 0) { // There was a result or the time expired or it was just a poll. // Construct the result vectors. Handle resVec = alloc_and_save(taskData, nDesc); for (unsigned i = 0; i < nDesc; i++) { int res = 0; if (fds[i].revents & POLLIN) res = POLL_BIT_IN; if (fds[i].revents & POLLOUT) res = POLL_BIT_OUT; if (fds[i].revents & POLLPRI) res = POLL_BIT_PRI; DEREFWORDHANDLE(resVec)->Set(i, TAGGED(res)); } return resVec; } // else try again. taskData->saveVec.reset(hSave); } } // Directory functions. static Handle openDirectory(TaskData *taskData, Handle dirname) { TempString dirName(dirname->Word()); if (dirName == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); while (1) { DIR *dirp = opendir(dirName); if (dirp != NULL) return MakeVolatileWord(taskData, dirp); switch (errno) { case EINTR: continue; // Just retry the call. default: raise_syscall(taskData, "opendir failed", ERRORNUMBER); } } } /* Return the next entry from the directory, ignoring current and parent arcs ("." and ".." in Windows and Unix) */ Handle readDirectory(TaskData *taskData, Handle stream) { DIR *pDir = *(DIR**)(stream->WordP()); // In a Volatile if (pDir == 0) raise_syscall(taskData, "Stream is closed", STREAMCLOSED); while (1) { struct dirent *dp = readdir(pDir); int len; if (dp == NULL) return taskData->saveVec.push(EmptyString(taskData)); len = NAMLEN(dp); if (!((len == 1 && strncmp(dp->d_name, ".", 1) == 0) || (len == 2 && strncmp(dp->d_name, "..", 2) == 0))) return SAVE(C_string_to_Poly(taskData, dp->d_name, len)); } } Handle rewindDirectory(TaskData *taskData, Handle stream, Handle dirname) { DIR *pDir = *(DIR**)(stream->WordP()); // In a Volatile if (pDir == 0) raise_syscall(taskData, "Stream is closed", STREAMCLOSED); rewinddir(pDir); return Make_fixed_precision(taskData, 0); } static Handle closeDirectory(TaskData *taskData, Handle stream) { DIR *pDir = *(DIR**)(stream->WordP()); // In a SysWord if (pDir != 0) { closedir(pDir); *((DIR**)stream->WordP()) = 0; // Clear this - no longer valid } return Make_fixed_precision(taskData, 0); } /* change_dirc - this is called directly and not via the dispatch function. */ static Handle change_dirc(TaskData *taskData, Handle name) /* Change working directory. */ { TempString cDirName(name->Word()); if (cDirName == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); if (chdir(cDirName) != 0) raise_syscall(taskData, "chdir failed", ERRORNUMBER); return SAVE(TAGGED(0)); } // External call POLYUNSIGNED PolyChDir(PolyObject *threadId, PolyWord arg) { TaskData *taskData = TaskData::FindTaskForId(threadId); ASSERT(taskData != 0); taskData->PreRTSCall(); Handle reset = taskData->saveVec.mark(); Handle pushedArg = taskData->saveVec.push(arg); try { (void)change_dirc(taskData, pushedArg); } catch (...) { } // If an ML exception is raised taskData->saveVec.reset(reset); // Ensure the save vec is reset taskData->PostRTSCall(); return TAGGED(0).AsUnsigned(); // Result is unit } /* Test for a directory. */ Handle isDir(TaskData *taskData, Handle name) { TempString cDirName(name->Word()); if (cDirName == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); struct stat fbuff; if (stat(cDirName, &fbuff) != 0) raise_syscall(taskData, "stat failed", ERRORNUMBER); if ((fbuff.st_mode & S_IFMT) == S_IFDIR) return Make_fixed_precision(taskData, 1); else return Make_fixed_precision(taskData, 0); } /* Get absolute canonical path name. */ Handle fullPath(TaskData *taskData, Handle filename) { TempString cFileName; /* Special case of an empty string. */ if (PolyStringLength(filename->Word()) == 0) cFileName = strdup("."); else cFileName = Poly_string_to_C_alloc(filename->Word()); if (cFileName == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); TempCString resBuf(realpath(cFileName, NULL)); if (resBuf == NULL) raise_syscall(taskData, "realpath failed", ERRORNUMBER); /* Some versions of Unix don't check the final component of a file. To be consistent try doing a "stat" of the resulting string to check it exists. */ struct stat fbuff; if (stat(resBuf, &fbuff) != 0) raise_syscall(taskData, "stat failed", ERRORNUMBER); return(SAVE(C_string_to_Poly(taskData, resBuf))); } /* Get file modification time. This returns the value in the time units and from the base date used by timing.c. c.f. filedatec */ Handle modTime(TaskData *taskData, Handle filename) { TempString cFileName(filename->Word()); if (cFileName == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); struct stat fbuff; if (stat(cFileName, &fbuff) != 0) raise_syscall(taskData, "stat failed", ERRORNUMBER); /* Convert to microseconds. */ return Make_arb_from_pair_scaled(taskData, STAT_SECS(&fbuff,m), STAT_USECS(&fbuff,m), 1000000); } /* Get file size. */ Handle fileSize(TaskData *taskData, Handle filename) { TempString cFileName(filename->Word()); if (cFileName == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); struct stat fbuff; if (stat(cFileName, &fbuff) != 0) raise_syscall(taskData, "stat failed", ERRORNUMBER); return Make_arbitrary_precision(taskData, fbuff.st_size); } /* Set file modification and access times. */ Handle setTime(TaskData *taskData, Handle fileName, Handle fileTime) { TempString cFileName(fileName->Word()); if (cFileName == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); struct timeval times[2]; /* We have a value in microseconds. We need to split it into seconds and microseconds. */ Handle hTime = fileTime; Handle hMillion = Make_arbitrary_precision(taskData, 1000000); /* N.B. Arguments to div_longc and rem_longc are in reverse order. */ unsigned secs = get_C_ulong(taskData, DEREFWORD(div_longc(taskData, hMillion, hTime))); unsigned usecs = get_C_ulong(taskData, DEREFWORD(rem_longc(taskData, hMillion, hTime))); times[0].tv_sec = times[1].tv_sec = secs; times[0].tv_usec = times[1].tv_usec = usecs; if (utimes(cFileName, times) != 0) raise_syscall(taskData, "utimes failed", ERRORNUMBER); return Make_fixed_precision(taskData, 0); } /* Rename a file. */ Handle renameFile(TaskData *taskData, Handle oldFileName, Handle newFileName) { TempString oldName(oldFileName->Word()), newName(newFileName->Word()); if (oldName == 0 || newName == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); if (rename(oldName, newName) != 0) raise_syscall(taskData, "rename failed", ERRORNUMBER); return Make_fixed_precision(taskData, 0); } /* Access right requests passed in from ML. */ #define FILE_ACCESS_READ 1 #define FILE_ACCESS_WRITE 2 #define FILE_ACCESS_EXECUTE 4 /* Get access rights to a file. */ Handle fileAccess(TaskData *taskData, Handle name, Handle rights) { TempString fileName(name->Word()); if (fileName == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); int rts = get_C_int(taskData, DEREFWORD(rights)); int mode = 0; if (rts & FILE_ACCESS_READ) mode |= R_OK; if (rts & FILE_ACCESS_WRITE) mode |= W_OK; if (rts & FILE_ACCESS_EXECUTE) mode |= X_OK; if (mode == 0) mode = F_OK; /* Return true if access is allowed, otherwise false for any other error. */ if (access(fileName, mode) == 0) return Make_fixed_precision(taskData, 1); else return Make_fixed_precision(taskData, 0); } /* IO_dispatchc. Called from assembly code module. */ static Handle IO_dispatch_c(TaskData *taskData, Handle args, Handle strm, Handle code) { unsigned c = get_C_unsigned(taskData, DEREFWORD(code)); switch (c) { case 0: /* Return standard input */ return wrapFileDescriptor(taskData, 0); case 1: /* Return standard output */ return wrapFileDescriptor(taskData, 1); case 2: /* Return standard error */ return wrapFileDescriptor(taskData, 2); case 3: /* Open file for text input. */ case 4: /* Open file for binary input. */ return open_file(taskData, args, O_RDONLY, 0666, 0); case 5: /* Open file for text output. */ case 6: /* Open file for binary output. */ return open_file(taskData, args, O_WRONLY | O_CREAT | O_TRUNC, 0666, 0); case 7: /* Close file */ return close_file(taskData, strm); case 8: /* Read text into an array. */ return readArray(taskData, strm, args, true); case 9: /* Read binary into an array. */ return readArray(taskData, strm, args, false); case 10: /* Get text as a string. */ return readString(taskData, strm, args, true); case 11: /* Write from memory into a text file. */ return writeArray(taskData, strm, args, true); case 12: /* Write from memory into a binary file. */ return writeArray(taskData, strm, args, false); case 13: /* Open text file for appending. */ /* The IO library definition leaves it open whether this should use "append mode" or not. */ case 14: /* Open binary file for appending. */ return open_file(taskData, args, O_WRONLY | O_CREAT | O_APPEND, 0666, 0); case 15: /* Return recommended buffer size. */ // This is a guess but 4k seems reasonable. return Make_fixed_precision(taskData, 4096); case 16: /* See if we can get some input. */ { int fd = getStreamFileDescriptor(taskData, strm->Word()); return Make_fixed_precision(taskData, isAvailable(taskData, fd) ? 1 : 0); } case 17: /* Return the number of bytes available. */ return bytesAvailable(taskData, strm); case 18: /* Get position on stream. */ { /* Get the current position in the stream. This is used to test for the availability of random access so it should raise an exception if setFilePos or endFilePos would fail. */ int fd = getStreamFileDescriptor(taskData, strm->Word()); long pos = seekStream(taskData, fd, 0L, SEEK_CUR); return Make_arbitrary_precision(taskData, pos); } case 19: /* Seek to position on stream. */ { long position = (long)get_C_long(taskData, DEREFWORD(args)); int fd = getStreamFileDescriptor(taskData, strm->Word()); (void)seekStream(taskData, fd, position, SEEK_SET); return Make_arbitrary_precision(taskData, 0); } case 20: /* Return position at end of stream. */ { int fd = getStreamFileDescriptor(taskData, strm->Word()); /* Remember our original position, seek to the end, then seek back. */ long original = seekStream(taskData, fd, 0L, SEEK_CUR); long endOfStream = seekStream(taskData, fd, 0L, SEEK_END); if (seekStream(taskData, fd, original, SEEK_SET) != original) raise_syscall(taskData, "Position error", ERRORNUMBER); return Make_arbitrary_precision(taskData, endOfStream); } case 21: /* Get the kind of device underlying the stream. */ return fileKind(taskData, strm); case 22: /* Return the polling options allowed on this descriptor. */ return pollTest(taskData, strm); case 23: /* Poll the descriptor, waiting forever. */ return pollDescriptors(taskData, args, 1); case 24: /* Poll the descriptor, waiting for the time requested. */ return pollDescriptors(taskData, args, 0); case 25: /* Poll the descriptor, returning immediately.*/ return pollDescriptors(taskData, args, 2); case 26: /* Get binary as a vector. */ return readString(taskData, strm, args, false); case 27: /* Block until input is available. */ // We should check for interrupts even if we're not going to block. processes->TestAnyEvents(taskData); waitForAvailableInput(taskData, strm); return Make_fixed_precision(taskData, 0); case 28: /* Test whether output is possible. */ return Make_fixed_precision(taskData, canOutput(taskData, strm) ? 1:0); case 29: /* Block until output is possible. */ // We should check for interrupts even if we're not going to block. processes->TestAnyEvents(taskData); while (true) { if (canOutput(taskData, strm)) return Make_fixed_precision(taskData, 0); // Use the default waiter for the moment since we don't have // one to test for output. processes->ThreadPauseForIO(taskData, Waiter::defaultWaiter); } /* Functions added for Posix structure. */ case 30: /* Return underlying file descriptor. */ /* This is now also used internally to test for stdIn, stdOut and stdErr. */ { int fd = getStreamFileDescriptor(taskData, strm->Word()); return Make_fixed_precision(taskData, fd); } - case 31: /* Make an entry for a given descriptor. */ + case 31: /* Make an entry for a given descriptor. No longer used - previously used for Posix.FileSys.wordToFD. */ { int ioDesc = get_C_int(taskData, DEREFWORD(args)); return wrapFileDescriptor(taskData, ioDesc); } /* Directory functions. */ case 50: /* Open a directory. */ return openDirectory(taskData, args); case 51: /* Read a directory entry. */ return readDirectory(taskData, strm); case 52: /* Close the directory */ return closeDirectory(taskData, strm); case 53: /* Rewind the directory. */ return rewindDirectory(taskData, strm, args); case 54: /* Get current working directory. */ { size_t size = 4096; TempString string_buffer((char *)malloc(size * sizeof(char))); if (string_buffer == NULL) raise_syscall(taskData, "Insufficient memory", NOMEMORY); char *cwd; while ((cwd = getcwd(string_buffer, size)) == NULL && errno == ERANGE) { if (size > std::numeric_limits::max() / 2) raise_fail(taskData, "getcwd needs too large a buffer"); size *= 2; char *new_buf = (char *)realloc(string_buffer, size * sizeof(char)); if (new_buf == NULL) raise_syscall(taskData, "Insufficient memory", NOMEMORY); string_buffer = new_buf; } if (cwd == NULL) raise_syscall(taskData, "getcwd failed", ERRORNUMBER); return SAVE(C_string_to_Poly(taskData, cwd)); } case 55: /* Create a new directory. */ { TempString dirName(args->Word()); if (dirName == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); if (mkdir(dirName, 0777) != 0) raise_syscall(taskData, "mkdir failed", ERRORNUMBER); return Make_fixed_precision(taskData, 0); } case 56: /* Delete a directory. */ { TempString dirName(args->Word()); if (dirName == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); if (rmdir(dirName) != 0) raise_syscall(taskData, "rmdir failed", ERRORNUMBER); return Make_fixed_precision(taskData, 0); } case 57: /* Test for directory. */ return isDir(taskData, args); case 58: /* Test for symbolic link. */ { TempString fileName(args->Word()); if (fileName == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); struct stat fbuff; if (lstat(fileName, &fbuff) != 0) raise_syscall(taskData, "stat failed", ERRORNUMBER); return Make_fixed_precision(taskData, ((fbuff.st_mode & S_IFMT) == S_IFLNK) ? 1 : 0); } case 59: /* Read a symbolic link. */ { int nLen; TempString linkName(args->Word()); if (linkName == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); size_t size = 4096; TempString resBuf((char *)malloc(size * sizeof(char))); if (resBuf == NULL) raise_syscall(taskData, "Insufficient memory", NOMEMORY); // nLen is signed, so cast size to ssize_t to perform signed // comparison, avoiding an infinite loop when nLen is -1. while ((nLen = readlink(linkName, resBuf, size)) >= (ssize_t) size) { size *= 2; if (size > std::numeric_limits::max()) raise_fail(taskData, "readlink needs too large a buffer"); char *newBuf = (char *)realloc(resBuf, size * sizeof(char)); if (newBuf == NULL) raise_syscall(taskData, "Insufficient memory", NOMEMORY); resBuf = newBuf; } if (nLen < 0) raise_syscall(taskData, "readlink failed", ERRORNUMBER); return(SAVE(C_string_to_Poly(taskData, resBuf, nLen))); } case 60: /* Return the full absolute path name. */ return fullPath(taskData, args); case 61: /* Modification time. */ return modTime(taskData, args); case 62: /* File size. */ return fileSize(taskData, args); case 63: /* Set file time. */ return setTime(taskData, strm, args); case 64: /* Delete a file. */ { TempString fileName(args->Word()); if (fileName == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); if (unlink(fileName) != 0) raise_syscall(taskData, "unlink failed", ERRORNUMBER); return Make_fixed_precision(taskData, 0); } case 65: /* rename a file. */ return renameFile(taskData, strm, args); case 66: /* Get access rights. */ return fileAccess(taskData, strm, args); case 67: /* Return a temporary file name. */ { const char *template_subdir = "/MLTEMPXXXXXX"; #ifdef P_tmpdir TempString buff((char *)malloc(strlen(P_tmpdir) + strlen(template_subdir) + 1)); if (buff == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); strcpy(buff, P_tmpdir); #else const char *tmpdir = "/tmp"; TempString buff((char *)malloc(strlen(tmpdir) + strlen(template_subdir) + 1)); if (buff == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); strcpy(buff, tmpdir); #endif strcat(buff, template_subdir); #if (defined(HAVE_MKSTEMP) && ! defined(UNICODE)) // mkstemp is present in the Mingw64 headers but only as ANSI not Unicode. // Set the umask to mask out access by anyone else. // mkstemp generally does this anyway. mode_t oldMask = umask(0077); int fd = mkstemp(buff); int wasError = ERRORNUMBER; (void)umask(oldMask); if (fd != -1) close(fd); else raise_syscall(taskData, "mkstemp failed", wasError); #else if (mktemp(buff) == 0) raise_syscall(taskData, "mktemp failed", ERRORNUMBER); int fd = open(buff, O_RDWR | O_CREAT | O_EXCL, 00600); if (fd != -1) close(fd); else raise_syscall(taskData, "Temporary file creation failed", ERRORNUMBER); #endif Handle res = SAVE(C_string_to_Poly(taskData, buff)); return res; } case 68: /* Get the file id. */ { struct stat fbuff; TempString fileName(args->Word()); if (fileName == 0) raise_syscall(taskData, "Insufficient memory", NOMEMORY); if (stat(fileName, &fbuff) != 0) raise_syscall(taskData, "stat failed", ERRORNUMBER); /* Assume that inodes are always non-negative. */ return Make_arbitrary_precision(taskData, fbuff.st_ino); } case 69: // Return an index for a token. It is used in OS.IO.hash. // This is supposed to be well distributed for any 2^n but simply return // the stream number. return Make_fixed_precision(taskData, getStreamFileDescriptor(taskData, strm->Word())); case 70: /* Posix.FileSys.openf - open a file with given mode. */ { Handle name = taskData->saveVec.push(DEREFWORDHANDLE(args)->Get(0)); int mode = get_C_int(taskData, DEREFWORDHANDLE(args)->Get(1)); return open_file(taskData, name, mode, 0666, 1); } case 71: /* Posix.FileSys.createf - create a file with given mode and access. */ { Handle name = taskData->saveVec.push(DEREFWORDHANDLE(args)->Get(0)); int mode = get_C_int(taskData, DEREFWORDHANDLE(args)->Get(1)); int access = get_C_int(taskData, DEREFWORDHANDLE(args)->Get(2)); return open_file(taskData, name, mode|O_CREAT, access, 1); } default: { char msg[100]; sprintf(msg, "Unknown io function: %d", c); raise_exception_string(taskData, EXC_Fail, msg); return 0; } } } // General interface to IO. Ideally the various cases will be made into // separate functions. POLYUNSIGNED PolyBasicIOGeneral(PolyObject *threadId, PolyWord code, PolyWord strm, PolyWord arg) { TaskData *taskData = TaskData::FindTaskForId(threadId); ASSERT(taskData != 0); taskData->PreRTSCall(); Handle reset = taskData->saveVec.mark(); Handle pushedCode = taskData->saveVec.push(code); Handle pushedStrm = taskData->saveVec.push(strm); Handle pushedArg = taskData->saveVec.push(arg); Handle result = 0; try { result = IO_dispatch_c(taskData, pushedArg, pushedStrm, pushedCode); } catch (KillException &) { processes->ThreadExit(taskData); // TestAnyEvents may test for kill } catch (...) { } // If an ML exception is raised taskData->saveVec.reset(reset); taskData->PostRTSCall(); if (result == 0) return TAGGED(0).AsUnsigned(); else return result->Word().AsUnsigned(); } // Create a persistent file descriptor value for Posix.FileSys.stdin etc. POLYEXTERNALSYMBOL POLYUNSIGNED PolyPosixCreatePersistentFD(PolyObject *threadId, PolyWord fd) { TaskData *taskData = TaskData::FindTaskForId(threadId); ASSERT(taskData != 0); taskData->PreRTSCall(); Handle reset = taskData->saveVec.mark(); Handle result = 0; try { result = alloc_and_save(taskData, WORDS(SIZEOF_VOIDP), F_BYTE_OBJ | F_MUTABLE_BIT | F_NO_OVERWRITE); *(POLYSIGNED*)(result->Word().AsCodePtr()) = fd.UnTagged() + 1; } catch (...) { } // If an ML exception is raised - could have run out of memory taskData->saveVec.reset(reset); taskData->PostRTSCall(); if (result == 0) return TAGGED(0).AsUnsigned(); else return result->Word().AsUnsigned(); } struct _entrypts basicIOEPT[] = { { "PolyChDir", (polyRTSFunction)&PolyChDir}, { "PolyBasicIOGeneral", (polyRTSFunction)&PolyBasicIOGeneral}, { "PolyPosixCreatePersistentFD", (polyRTSFunction)&PolyPosixCreatePersistentFD}, { NULL, NULL} // End of list. };