Module Posix_unistd

POSIX unistd.h bindings.

This module provides OCaml bindings to POSIX functions defined in unistd.h.

It includes functions for file I/O, process control, user/group IDs, directory operations, and system configuration.

Example

  (* Read from a file descriptor *)
  let buf = Bytes.create 1024 in
  let n = Posix_unistd.read fd buf 0 1024 in
  Printf.printf "Read %d bytes\n" n;

  (* Get system configuration *)
  let max_open = Posix_unistd.sysconf sc_open_max in
  Printf.printf "Max open files: %d\n" max_open

Constants

Name limits

val host_name_max : int
val login_name_max : int

System configuration (sysconf) constants

val sc_arg_max : int

Commonly available sysconf names

val sc_child_max : int
val sc_clk_tck : int
val sc_open_max : int
val sc_pagesize : int
val sc_page_size : int
val sc_nprocessors_onln : int
val sc_nprocessors_conf : int
val sc_phys_pages : int
val sc_stream_max : int
val sc_tzname_max : int
val sc_version : int
val sc_atexit_max : int
val sc_login_name_max : int
val sc_tty_name_max : int
val sc_host_name_max : int
val sc_line_max : int
val sc_getgr_r_size_max : int
val sc_getpw_r_size_max : int
val sc_ngroups_max : int
val sc_re_dup_max : int
val sc_symloop_max : int
val sc_job_control : int

POSIX options

val sc_saved_ids : int
val sc_fsync : int
val sc_mapped_files : int
val sc_memlock : int
val sc_memlock_range : int
val sc_memory_protection : int
val sc_priority_scheduling : int
val sc_synchronized_io : int
val sc_timers : int
val sc_asynchronous_io : int
val sc_prioritized_io : int
val sc_realtime_signals : int
val sc_semaphores : int
val sc_shared_memory_objects : int
val sc_message_passing : int
val sc_threads : int
val sc_thread_safe_functions : int
val sc_thread_attr_stackaddr : int
val sc_thread_attr_stacksize : int
val sc_thread_priority_scheduling : int
val sc_thread_prio_inherit : int
val sc_thread_prio_protect : int
val sc_thread_process_shared : int
val sc_2_version : int

POSIX.2 constants

val sc_2_c_bind : int
val sc_2_c_dev : int
val sc_bc_base_max : int
val sc_bc_dim_max : int
val sc_bc_scale_max : int
val sc_bc_string_max : int
val sc_coll_weights_max : int
val sc_expr_nest_max : int
val sc_xopen_version : int

X/Open constants

val sc_xopen_crypt : int
val sc_xopen_enh_i18n : int
val sc_xopen_shm : int
val sc_xopen_unix : int

Path configuration (pathconf) constants

val pc_max_canon : int
val pc_max_input : int
val pc_name_max : int
val pc_path_max : int
val pc_pipe_buf : int
val pc_no_trunc : int
val pc_vdisable : int
val pc_chown_restricted : int
val pc_async_io : int
val pc_prio_io : int
val pc_sync_io : int
val pc_filesizebits : int

File locking (lockf) commands

val f_ulock : int
val f_lock : int
val f_tlock : int
val f_test : int

Configuration strings (confstr) constants

val cs_path : int

File positioning (lseek) constants

val seek_set : int
val seek_cur : int
val seek_end : int

File access mode flags

val r_ok : int

Read permission

val w_ok : int

Write permission

val x_ok : int

Execute permission

val f_ok : int

File exists

Standard file descriptors

val stdin_fileno : int
val stdout_fileno : int
val stderr_fileno : int

Basic I/O Operations

val read : Unix.file_descr -> bytes -> int -> int -> int

Read from a file descriptor.

See read(2).

  • returns

    Number of bytes read.

  • raises Unix.Unix_error

    on failure.

val write : Unix.file_descr -> bytes -> int -> int -> int

Write to a file descriptor.

See write(2).

  • returns

    Number of bytes written.

  • raises Unix.Unix_error

    on failure.

Positioned I/O

val pread : Unix.file_descr -> bytes -> int -> int -> int -> int

Read from a file descriptor at a given offset without changing the file offset.

See pread(2).

  • returns

    Number of bytes read.

  • raises Unix.Unix_error

    on failure.

val pwrite : Unix.file_descr -> bytes -> int -> int -> int -> int

Write to a file descriptor at a given offset without changing the file offset.

See pwrite(2).

  • returns

    Number of bytes written.

  • raises Unix.Unix_error

    on failure.

File Descriptor Operations

val close : Unix.file_descr -> unit

Close a file descriptor. See close(2).

val dup : Unix.file_descr -> Unix.file_descr

Duplicate a file descriptor. See dup(2).

val dup2 : Unix.file_descr -> Unix.file_descr -> Unix.file_descr

Duplicate a file descriptor to a specified descriptor. See dup2(2).

val pipe : unit -> Unix.file_descr * Unix.file_descr

Create a pipe. See pipe(2).

  • returns

    (read_end, write_end).

Data Synchronization

val fsync : Unix.file_descr -> unit

Synchronize file data and metadata to disk. See fsync(2).

val fdatasync : Unix.file_descr -> unit

Synchronize file data (but not necessarily metadata) to disk. See fdatasync(2).

val sync : unit -> unit

Schedule writes of all modified buffer cache blocks to disk. See sync(2).

File Operations

Create a hard link. See link(2).

Create a symbolic link. See symlink(2).

Read the target of a symbolic link. See readlink(2).

Remove a file. See unlink(2).

val rmdir : string -> unit

Remove an empty directory. See rmdir(2).

Directory Operations

val chdir : string -> unit

Change the current working directory. See chdir(2).

val fchdir : Unix.file_descr -> unit

Change the current working directory to a directory referenced by a file descriptor. See fchdir(2).

val getcwd : unit -> string

Get the current working directory. See getcwd(3).

File Positioning

type seek_command =
  1. | Seek_set
    (*

    Set offset to the given value

    *)
  2. | Seek_cur
    (*

    Set offset relative to current position

    *)
  3. | Seek_end
    (*

    Set offset relative to end of file

    *)

File positioning commands for lseek.

val lseek : Unix.file_descr -> int -> seek_command -> int

Reposition the file offset. See lseek(2).

File Permissions and Ownership

type access_permission = [
  1. | `Read
  2. | `Write
  3. | `Execute
  4. | `Exists
]

Access permission flags for access.

val access : string -> access_permission list -> bool

Check file accessibility. See access(2).

val chown : string -> int -> int -> unit

Change file ownership. See chown(2).

val fchown : Unix.file_descr -> int -> int -> unit

Change file ownership using a file descriptor. See fchown(2).

val lchown : string -> int -> int -> unit

Change ownership of a symbolic link itself. See lchown(2).

val truncate : string -> int -> unit

Truncate a file to a specified length. See truncate(2).

val ftruncate : Unix.file_descr -> int -> unit

Truncate a file to a specified length using a file descriptor. See ftruncate(2).

File Locking

type lock_command = [
  1. | `Unlock
  2. | `Lock
  3. | `Test_lock
  4. | `Try_lock
]

File locking commands for lockf.

val lockf : Unix.file_descr -> lock_command -> int -> unit

Apply, test, or remove a POSIX lock on a section of a file. See lockf(3).

Process Operations

val fork : unit -> int

Create a new process. See fork(2).

  • returns

    0 in the child, child's PID in the parent.

val getpid : unit -> int

Get the process ID of the calling process. See getpid(2).

val getppid : unit -> int

Get the parent process ID. See getppid(2).

val getpgid : int -> int

Get the process group ID of a process. See getpgid(2).

val setpgid : int -> int -> unit

Set the process group ID. See setpgid(2).

val getpgrp : unit -> int

Get the process group ID of the calling process. See getpgrp(2).

val setpgrp : unit -> unit

Set the process group ID to the process ID. See setpgrp(3).

val setsid : unit -> int

Create a new session. See setsid(2).

  • returns

    The session ID.

val getsid : int -> int

Get the session ID of a process. See getsid(2).

User and Group IDs

val getuid : unit -> int

Get the real user ID. See getuid(2).

val geteuid : unit -> int

Get the effective user ID. See geteuid(2).

val getgid : unit -> int

Get the real group ID. See getgid(2).

val getegid : unit -> int

Get the effective group ID. See getegid(2).

val setuid : int -> unit

Set the user ID. See setuid(2).

val seteuid : int -> unit

Set the effective user ID. See seteuid(2).

val setgid : int -> unit

Set the group ID. See setgid(2).

val setegid : int -> unit

Set the effective group ID. See setegid(2).

val setreuid : int -> int -> unit

Set real and effective user IDs. See setreuid(2).

val setregid : int -> int -> unit

Set real and effective group IDs. See setregid(2).

Group Membership

val getgroups : unit -> int list

Get the list of supplementary group IDs. See getgroups(2).

val setgroups : int list -> unit

Set the supplementary group IDs. See setgroups(2).

System Configuration

val sysconf : int -> int

Get system configuration value. See sysconf(3). Use sc_* constants.

val pathconf : string -> int -> int

Get path configuration value. See pathconf(3). Use pc_* constants.

val fpathconf : Unix.file_descr -> int -> int

Get path configuration value for an open file. See fpathconf(3).

val confstr : int -> string

Get configuration string value. See confstr(3). Use cs_* constants.

Process Priority

val nice : int -> int

Adjust process priority. See nice(2).

  • returns

    The new priority.

Sleep Operations

val sleep : int -> int

Sleep for a number of seconds. See sleep(3).

  • returns

    0 on completion, or seconds remaining if interrupted.

val usleep : int -> unit

Sleep for a number of microseconds. See usleep(3).

Signal and Timer

val pause : unit -> unit

Wait until a signal is caught. See pause(2).

val alarm : int -> int

Set an alarm clock. See alarm(2).

  • returns

    Seconds remaining from previous alarm, or 0 if none.

Terminal Operations

val isatty : Unix.file_descr -> bool

Test whether a file descriptor refers to a terminal. See isatty(3).

val ttyname : Unix.file_descr -> string

Get the name of a terminal device. See ttyname(3).

val ttyname_r : ?len:int -> Unix.file_descr -> string

Thread-safe version of ttyname. See ttyname_r(3).

val ctermid : unit -> string

Get the pathname of the controlling terminal. See ctermid(3).

val tcgetpgrp : Unix.file_descr -> int

Get the foreground process group ID associated with a terminal. See tcgetpgrp(3).

val tcsetpgrp : Unix.file_descr -> int -> unit

Set the foreground process group ID associated with a terminal. See tcsetpgrp(3).

System Information

val getpagesize : unit -> int

Get the system page size in bytes. See getpagesize(2).

val gethostid : unit -> int64

Get the unique identifier of the current host. See gethostid(3).

val gethostname : unit -> string

Get the host name. See gethostname(2).

val sethostname : string -> unit

Set the host name. See sethostname(2).

Login Information

val getlogin : unit -> string

Get the login name of the user. See getlogin(3).

val getlogin_r : ?len:int -> unit -> string

Thread-safe version of getlogin. See getlogin_r(3).

Program Execution

val execv : string -> string list -> unit

Execute a program with specified arguments. See execv(2). Only returns on error (by raising an exception).

val execve : string -> string list -> string list -> unit

Execute a program with specified arguments and environment. See execve(2). Only returns on error (by raising an exception).

val execvp : string -> string list -> unit

Execute a program using PATH to find the executable. See execvp(2). Only returns on error (by raising an exception).

Process Termination

val _exit : int -> unit

Terminate the calling process immediately without cleanup. See _exit(2). Use Stdlib.exit for normal termination.