Module Posix_unistd

POSIX unistd.h bindings with OCaml-friendly API

This module provides comprehensive bindings to POSIX unistd.h functions, with a user-friendly OCaml interface. Functions are wrapped to use standard OCaml types where appropriate and integrate with the Unix module.

Most functions return option types: Some value on success, None on error (with errno set appropriately via Errno_unix).

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 fd buf ofs len reads up to len bytes from fd into buf starting at offset ofs. Returns Some n where n is the number of bytes read, or None on error.

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

write fd buf ofs len writes up to len bytes from buf starting at offset ofs to fd. Returns Some n where n is the number of bytes written, or None on error.

Positioned I/O

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

pread fd buf ofs len offset reads up to len bytes from fd at file offset offset into buf starting at ofs. Does not change the file offset. Returns Some n or None on error.

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

pwrite fd buf ofs len offset writes up to len bytes to fd at file offset offset from buf starting at ofs. Does not change the file offset. Returns Some n or None on error.

File Descriptor Operations

val close : Unix.file_descr -> unit

close fd closes the file descriptor fd.

val dup : Unix.file_descr -> Unix.file_descr

dup fd duplicates the file descriptor fd.

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

dup2 fd1 fd2 duplicates fd1 to fd2, closing fd2 first if necessary.

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

pipe () creates a pipe and returns (read_end, write_end).

Data Synchronization

val fsync : Unix.file_descr -> unit

fsync fd synchronizes file data and metadata to disk.

val fdatasync : Unix.file_descr -> unit

fdatasync fd synchronizes file data (but not necessarily metadata) to disk.

val sync : unit -> unit

sync () schedules writes of all modified buffer cache blocks to disk.

File Operations

link ~target ~link_name creates a hard link.

symlink ~target ~link_name creates a symbolic link.

readlink path reads the target of a symbolic link.

unlink path removes a file.

val rmdir : string -> unit

rmdir path removes an empty directory.

Directory Operations

val chdir : string -> unit

chdir path changes the current working directory.

val fchdir : Unix.file_descr -> unit

fchdir fd changes the current working directory to the directory referenced by fd.

val getcwd : unit -> string

getcwd () returns the current working directory.

File Positioning

type seek_command =
  1. | Seek_set
  2. | Seek_cur
  3. | Seek_end
    (*

    File positioning commands for lseek.

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

lseek fd offset whence repositions the file offset.

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

access path perms checks whether the calling process can access the file path with the specified permissions.

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

chown path uid gid changes file ownership.

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

fchown fd uid gid changes file ownership using a file descriptor.

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

lchown path uid gid changes ownership of a symbolic link itself.

val truncate : string -> int -> unit

truncate path length truncates a file to specified length.

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

ftruncate fd length truncates a file to specified length using a file descriptor.

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

lockf fd cmd size applies, tests, or removes a POSIX lock on a section of a file.

Process Operations

val fork : unit -> int

fork () creates a new process. Returns Some 0 in the child, Some pid in the parent where pid is the child's process ID.

val getpid : unit -> int

getpid () returns the process ID of the calling process.

val getppid : unit -> int

getppid () returns the parent process ID.

val getpgid : int -> int

getpgid pid gets the process group ID of the specified process.

val setpgid : int -> int -> unit

setpgid pid pgid sets the process group ID.

val getpgrp : unit -> int

getpgrp () returns the process group ID of the calling process.

val setpgrp : unit -> unit

setpgrp () sets the process group ID to the process ID.

val setsid : unit -> int

setsid () creates a new session and returns the session ID.

val getsid : int -> int

getsid pid gets the session ID of the specified process.

User and Group IDs

val getuid : unit -> int

getuid () returns the real user ID.

val geteuid : unit -> int

geteuid () returns the effective user ID.

val getgid : unit -> int

getgid () returns the real group ID.

val getegid : unit -> int

getegid () returns the effective group ID.

val setuid : int -> unit

setuid uid sets the user ID.

val seteuid : int -> unit

seteuid uid sets the effective user ID.

val setgid : int -> unit

setgid gid sets the group ID.

val setegid : int -> unit

setegid gid sets the effective group ID.

val setreuid : int -> int -> unit

setreuid ruid euid sets real and effective user IDs.

val setregid : int -> int -> unit

setregid rgid egid sets real and effective group IDs.

Group Membership

val getgroups : unit -> int list

getgroups () returns the list of supplementary group IDs.

val setgroups : int list -> unit

setgroups groups sets the supplementary group IDs.

System Configuration

val sysconf : int -> int

sysconf name gets system configuration value. Use sc_* constants.

val pathconf : string -> int -> int

pathconf path name gets path configuration value. Use pc_* constants.

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

fpathconf fd name gets path configuration value for an open file.

val confstr : int -> string

confstr name gets configuration string value. Use cs_* constants.

Process Priority

val nice : int -> int

nice incr adjusts process priority by incr. Returns new priority.

Sleep Operations

val sleep : int -> int

sleep seconds sleeps for specified number of seconds. Returns 0 on completion, or number of seconds remaining if interrupted.

val usleep : int -> unit

usleep microseconds sleeps for specified number of microseconds.

Signal and Timer

val pause : unit -> unit

pause () waits until a signal is caught. Always returns None.

val alarm : int -> int

alarm seconds sets an alarm clock. Returns seconds remaining from a previous alarm, or 0 if no previous alarm.

Terminal Operations

val isatty : Unix.file_descr -> bool

isatty fd tests whether fd refers to a terminal.

val ttyname : Unix.file_descr -> string

ttyname fd returns the name of the terminal device.

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

ttyname_r ?len fd thread-safe version of ttyname.

val ctermid : unit -> string

ctermid () returns the path to the controlling terminal.

val tcgetpgrp : Unix.file_descr -> int

tcgetpgrp fd returns the process group ID of the foreground process group associated with the terminal.

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

tcsetpgrp fd pgrp sets the foreground process group ID associated with the terminal to pgrp.

System Information

val getpagesize : unit -> int

getpagesize () returns the system page size in bytes.

val gethostid : unit -> int64

gethostid () returns the unique identifier of the current host.

val gethostname : unit -> string

gethostname () returns the host name.

val sethostname : string -> unit

sethostname name sets the host name.

Login Information

val getlogin : unit -> string

getlogin () returns the login name of the user.

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

getlogin_r () thread-safe version of getlogin.

Program Execution

val execv : string -> string list -> unit

execv path args executes a program with specified arguments. Only returns on error (by raising an exception).

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

execve path args env executes a program with specified arguments and environment. Only returns on error (by raising an exception).

val execvp : string -> string list -> unit

execvp file args executes a program using PATH to find the executable. Only returns on error (by raising an exception).

Process Termination

val _exit : int -> unit

_exit status terminates the calling process immediately without cleanup. Use Stdlib.exit for normal termination.