ocaml-base-compiler
  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

Interface to the Unix system.

Note: all the functions of this module (except error_message and handle_unix_error) are liable to raise the Unix_error exception whenever the underlying system call signals an error.

Error report

type error =
  1. | E2BIG
    (*

    Argument list too long

    *)
  2. | EACCES
    (*

    Permission denied

    *)
  3. | EAGAIN
    (*

    Resource temporarily unavailable; try again

    *)
  4. | EBADF
    (*

    Bad file descriptor

    *)
  5. | EBUSY
    (*

    Resource unavailable

    *)
  6. | ECHILD
    (*

    No child process

    *)
  7. | EDEADLK
    (*

    Resource deadlock would occur

    *)
  8. | EDOM
    (*

    Domain error for math functions, etc.

    *)
  9. | EEXIST
    (*

    File exists

    *)
  10. | EFAULT
    (*

    Bad address

    *)
  11. | EFBIG
    (*

    File too large

    *)
  12. | EINTR
    (*

    Function interrupted by signal

    *)
  13. | EINVAL
    (*

    Invalid argument

    *)
  14. | EIO
    (*

    Hardware I/O error

    *)
  15. | EISDIR
    (*

    Is a directory

    *)
  16. | EMFILE
    (*

    Too many open files by the process

    *)
  17. | ENAMETOOLONG
    (*

    Filename too long

    *)
  18. | ENFILE
    (*

    Too many open files in the system

    *)
  19. | ENODEV
    (*

    No such device

    *)
  20. | ENOENT
    (*

    No such file or directory

    *)
  21. | ENOEXEC
    (*

    Not an executable file

    *)
  22. | ENOLCK
    (*

    No locks available

    *)
  23. | ENOMEM
    (*

    Not enough memory

    *)
  24. | ENOSPC
    (*

    No space left on device

    *)
  25. | ENOSYS
    (*

    Function not supported

    *)
  26. | ENOTDIR
    (*

    Not a directory

    *)
  27. | ENOTEMPTY
    (*

    Directory not empty

    *)
  28. | ENOTTY
    (*

    Inappropriate I/O control operation

    *)
  29. | ENXIO
    (*

    No such device or address

    *)
  30. | EPERM
    (*

    Operation not permitted

    *)
  31. | EPIPE
    (*

    Broken pipe

    *)
  32. | ERANGE
    (*

    Result too large

    *)
  33. | EROFS
    (*

    Read-only file system

    *)
  34. | ESPIPE
    (*

    Invalid seek e.g. on a pipe

    *)
  35. | ESRCH
    (*

    No such process

    *)
  36. | EXDEV
    (*

    Invalid link

    *)
  37. | EWOULDBLOCK
    (*

    Operation would block

    *)
  38. | EINPROGRESS
    (*

    Operation now in progress

    *)
  39. | EALREADY
    (*

    Operation already in progress

    *)
  40. | ENOTSOCK
    (*

    Socket operation on non-socket

    *)
  41. | EDESTADDRREQ
    (*

    Destination address required

    *)
  42. | EMSGSIZE
    (*

    Message too long

    *)
  43. | EPROTOTYPE
    (*

    Protocol wrong type for socket

    *)
  44. | ENOPROTOOPT
    (*

    Protocol not available

    *)
  45. | EPROTONOSUPPORT
    (*

    Protocol not supported

    *)
  46. | ESOCKTNOSUPPORT
    (*

    Socket type not supported

    *)
  47. | EOPNOTSUPP
    (*

    Operation not supported on socket

    *)
  48. | EPFNOSUPPORT
    (*

    Protocol family not supported

    *)
  49. | EAFNOSUPPORT
    (*

    Address family not supported by protocol family

    *)
  50. | EADDRINUSE
    (*

    Address already in use

    *)
  51. | EADDRNOTAVAIL
    (*

    Can't assign requested address

    *)
  52. | ENETDOWN
    (*

    Network is down

    *)
  53. | ENETUNREACH
    (*

    Network is unreachable

    *)
  54. | ENETRESET
    (*

    Network dropped connection on reset

    *)
  55. | ECONNABORTED
    (*

    Software caused connection abort

    *)
  56. | ECONNRESET
    (*

    Connection reset by peer

    *)
  57. | ENOBUFS
    (*

    No buffer space available

    *)
  58. | EISCONN
    (*

    Socket is already connected

    *)
  59. | ENOTCONN
    (*

    Socket is not connected

    *)
  60. | ESHUTDOWN
    (*

    Can't send after socket shutdown

    *)
  61. | ETOOMANYREFS
    (*

    Too many references: can't splice

    *)
  62. | ETIMEDOUT
    (*

    Connection timed out

    *)
  63. | ECONNREFUSED
    (*

    Connection refused

    *)
  64. | EHOSTDOWN
    (*

    Host is down

    *)
  65. | EHOSTUNREACH
    (*

    No route to host

    *)
  66. | ELOOP
    (*

    Too many levels of symbolic links

    *)
  67. | EOVERFLOW
    (*

    File size or position not representable

    *)
  68. | EUNKNOWNERR of int
    (*

    Unknown error

    *)

The type of error codes. Errors defined in the POSIX standard and additional errors from UNIX98 and BSD. All other errors are mapped to EUNKNOWNERR.

exception Unix_error of error * string * string

Raised by the system calls below when an error is encountered. The first component is the error code; the second component is the function name; the third component is the string parameter to the function, if it has one, or the empty string otherwise.

val error_message : error -> string

Return a string describing the given error code.

val handle_unix_error : ('a -> 'b) -> 'a -> 'b

handle_unix_error f x applies f to x and returns the result. If the exception Unix_error is raised, it prints a message describing the error and exits with code 2.

Access to the process environment

val environment : unit -> string array

Return the process environment, as an array of strings with the format ``variable=value''. The returned array is empty if the process has special privileges.

val unsafe_environment : unit -> string array

Return the process environment, as an array of strings with the format ``variable=value''. Unlike environment, this function returns a populated array even if the process has special privileges. See the documentation for unsafe_getenv for more details.

  • since 4.06.0
val getenv : string -> string

Return the value associated to a variable in the process environment, unless the process has special privileges.

  • raises Not_found

    if the variable is unbound or the process has special privileges.

    (This function is identical to Sys.getenv.

val unsafe_getenv : string -> string

Return the value associated to a variable in the process environment.

Unlike getenv, this function returns the value even if the process has special privileges. It is considered unsafe because the programmer of a setuid or setgid program must be careful to avoid using maliciously crafted environment variables in the search path for executables, the locations for temporary files or logs, and the like.

  • raises Not_found

    if the variable is unbound.

  • since 4.06.0
val putenv : string -> string -> unit

Unix.putenv name value sets the value associated to a variable in the process environment. name is the name of the environment variable, and value its new associated value.

Process handling

type process_status =
  1. | WEXITED of int
    (*

    The process terminated normally by exit; the argument is the return code.

    *)
  2. | WSIGNALED of int
    (*

    The process was killed by a signal; the argument is the signal number.

    *)
  3. | WSTOPPED of int
    (*

    The process was stopped by a signal; the argument is the signal number.

    *)

The termination status of a process. See module Sys for the definitions of the standard signal numbers. Note that they are not the numbers used by the OS.

type wait_flag =
  1. | WNOHANG
    (*

    Do not block if no child has died yet, but immediately return with a pid equal to 0.

    *)
  2. | WUNTRACED
    (*

    Report also the children that receive stop signals.

    *)

Flags for Unix.waitpid.

val execv : string -> string array -> 'a

execv prog args execute the program in file prog, with the arguments args, and the current process environment. These execv* functions never return: on success, the current program is replaced by the new one.

  • raises Unix.Unix_error

    on failure.

val execve : string -> string array -> string array -> 'a

Same as Unix.execv, except that the third argument provides the environment to the program executed.

val execvp : string -> string array -> 'a

Same as Unix.execv, except that the program is searched in the path.

val execvpe : string -> string array -> string array -> 'a

Same as Unix.execve, except that the program is searched in the path.

val fork : unit -> int

Fork a new process. The returned integer is 0 for the child process, the pid of the child process for the parent process.

On Windows: not implemented, use create_process or threads.

val wait : unit -> int * process_status

Wait until one of the children processes die, and return its pid and termination status.

On Windows: Not implemented, use waitpid.

val waitpid : wait_flag list -> int -> int * process_status

Same as Unix.wait, but waits for the child process whose pid is given. A pid of -1 means wait for any child. A pid of 0 means wait for any child in the same process group as the current process. Negative pid arguments represent process groups. The list of options indicates whether waitpid should return immediately without waiting, and whether it should report s