package posix-unistd

  1. Overview
  2. Docs

doc/posix-unistd/Posix_unistd/index.html

Module Posix_unistdSource

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

Sourceval host_name_max : int
Sourceval login_name_max : int

System configuration (sysconf) constants

Sourceval sc_arg_max : int

Commonly available sysconf names

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

POSIX options

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

POSIX.2 constants

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

X/Open constants

Sourceval sc_xopen_crypt : int
Sourceval sc_xopen_enh_i18n : int
Sourceval sc_xopen_shm : int
Sourceval sc_xopen_unix : int

Path configuration (pathconf) constants

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

File locking (lockf) commands

Sourceval f_ulock : int
Sourceval f_lock : int
Sourceval f_tlock : int
Sourceval f_test : int

Configuration strings (confstr) constants

Sourceval cs_path : int

File positioning (lseek) constants

Sourceval seek_set : int
Sourceval seek_cur : int
Sourceval seek_end : int

File access mode flags

Sourceval r_ok : int

Read permission

Sourceval w_ok : int

Write permission

Sourceval x_ok : int

Execute permission

Sourceval f_ok : int

File exists

Standard file descriptors

Sourceval stdin_fileno : int
Sourceval stdout_fileno : int
Sourceval stderr_fileno : int

Basic I/O Operations

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

Read from a file descriptor.

See read(2).

  • returns

    Number of bytes read.

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

Write to a file descriptor.

See write(2).

  • returns

    Number of bytes written.

Positioned I/O

Sourceval 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.

Sourceval 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.

File Descriptor Operations

Sourceval close : Unix.file_descr -> unit

Close a file descriptor. See close(2).

Duplicate a file descriptor. See dup(2).

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

Create a pipe. See pipe(2).

  • returns

    (read_end, write_end).

Data Synchronization

Sourceval fsync : Unix.file_descr -> unit

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

Sourceval fdatasync : Unix.file_descr -> unit

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

Sourceval 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).

Sourceval rmdir : string -> unit

Remove an empty directory. See rmdir(2).

Directory Operations

Sourceval chdir : string -> unit

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

Sourceval fchdir : Unix.file_descr -> unit

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

Sourceval getcwd : unit -> string

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

File Positioning

Sourcetype 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.

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

Reposition the file offset. See lseek(2).

File Permissions and Ownership

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

Access permission flags for access.

Sourceval access : string -> access_permission list -> bool

Check file accessibility. See access(2).

Sourceval chown : string -> int -> int -> unit

Change file ownership. See chown(2).

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

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

Sourceval lchown : string -> int -> int -> unit

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

Sourceval truncate : string -> int -> unit

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

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

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

File Locking

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

File locking commands for lockf.

Sourceval 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

Sourceval fork : unit -> int

Create a new process. See fork(2).

  • returns

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

Sourceval getpid : unit -> int

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

Sourceval getppid : unit -> int

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

Sourceval getpgid : int -> int

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

Sourceval setpgid : int -> int -> unit

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

Sourceval getpgrp : unit -> int

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

Sourceval setpgrp : unit -> unit

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

Sourceval setsid : unit -> int

Create a new session. See setsid(2).

  • returns

    The session ID.

Sourceval getsid : int -> int

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

User and Group IDs

Sourceval getuid : unit -> int

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

Sourceval geteuid : unit -> int

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

Sourceval getgid : unit -> int

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

Sourceval getegid : unit -> int

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

Sourceval setuid : int -> unit

Set the user ID. See setuid(2).

Sourceval seteuid : int -> unit

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

Sourceval setgid : int -> unit

Set the group ID. See setgid(2).

Sourceval setegid : int -> unit

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

Sourceval setreuid : int -> int -> unit

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

Sourceval setregid : int -> int -> unit

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

Group Membership

Sourceval getgroups : unit -> int list

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

Sourceval setgroups : int list -> unit

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

System Configuration

Sourceval sysconf : int -> int

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

Sourceval pathconf : string -> int -> int

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

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

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

Sourceval confstr : int -> string

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

Process Priority

Sourceval nice : int -> int

Adjust process priority. See nice(2).

  • returns

    The new priority.

Sleep Operations

Sourceval sleep : int -> int

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

  • returns

    0 on completion, or seconds remaining if interrupted.

Sourceval usleep : int -> unit

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

Signal and Timer

Sourceval pause : unit -> unit

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

Sourceval alarm : int -> int

Set an alarm clock. See alarm(2).

  • returns

    Seconds remaining from previous alarm, or 0 if none.

Terminal Operations

Sourceval isatty : Unix.file_descr -> bool

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

Sourceval ttyname : Unix.file_descr -> string

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

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

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

Sourceval ctermid : unit -> string

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

Sourceval tcgetpgrp : Unix.file_descr -> int

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

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

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

System Information

Sourceval getpagesize : unit -> int

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

Sourceval gethostid : unit -> int64

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

Sourceval gethostname : unit -> string

Get the host name. See gethostname(2).

Sourceval sethostname : string -> unit

Set the host name. See sethostname(2).

Login Information

Sourceval getlogin : unit -> string

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

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

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

Program Execution

Sourceval execv : string -> string list -> unit

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

Sourceval 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).

Sourceval 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

Sourceval _exit : int -> unit

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