Module Linux_ext

Interface to Linux-specific system calls.

sysinfo

module Sysinfo : sig ... end

Filesystem functions

val sendfile : (?pos:int -> ?len:int -> fd:Core_unix.File_descr.t -> Core_unix.File_descr.t -> int) Core.Or_error.t @@ portable

sendfile ?pos ?len ~fd sock sends mmap-able data from file descriptor fd to socket sock using offset pos and length len. Returns the number of characters actually written.

NOTE: If the returned value is unequal to what was requested (= the initial size of the data by default), the system call may have been interrupted by a signal, the source file may have been truncated during operation, or a timeout may have occurred on the socket during sending. It is currently impossible to find out which of these events actually happened. Calling sendfile several times on the same descriptor that only partially accepted data due to a timeout will eventually lead to the Unix error EAGAIN.

Raises Unix_error on Unix-errors.

module Bound_to_interface : sig ... end

Type for status of SO_BINDTODEVICE socket option. The socket may either restrict the traffic to a given (by name, e.g. "eth0") interface, or do no restriction at all.

Non-portable TCP functionality

type tcp_bool_option =
  1. | TCP_CORK
    (*

    (Since Linux 2.2) If set, don’t send out partial frames. All queued partial frames are sent when the option is cleared again. This is useful for prepending headers before calling sendfile(2), or for throughput optimization. As currently implemented, there is a 200ms ceiling on the time for which output is corked by TCP_CORK. If this ceiling is reached, queued data is automatically transmitted.

    This option should not be used in code intended to be portable.

    *)
  2. | TCP_QUICKACK
    (*

    (Since Linux 2.4.4) Quick ack solves an unfortunate interaction between the delayed acks and the Nagle algorithm (TCP_NODELAY). On fast LANs, the Linux TCP stack quickly reaches a CWND (congestion window) of 1 (Linux interprets this as "1 unacknowledged packet", BSD/Windows and others consider it "1 unacknowledged segment of data").

    If Linux determines a connection to be bidirectional, it will delay sending acks, hoping to bundle them with other outgoing data. This can lead to serious connection stalls on, say, a TCP market data connection with one second heartbeats. TCP_QUICKACK can be used to prevent entering this delayed ack state.

    This option should not be used in code intended to be portable.

    *)
val sexp_of_tcp_bool_option : tcp_bool_option -> Sexplib0.Sexp.t
val tcp_bool_option_of_sexp : Sexplib0.Sexp.t -> tcp_bool_option
val bin_shape_tcp_bool_option : Core.Bin_prot.Shape.t
val bin_size_tcp_bool_option : tcp_bool_option Core.Bin_prot.Size.sizer
val bin_write_tcp_bool_option : tcp_bool_option Core.Bin_prot.Write.writer
val bin_writer_tcp_bool_option : tcp_bool_option Core.Bin_prot.Type_class.writer
val bin_read_tcp_bool_option : tcp_bool_option Core.Bin_prot.Read.reader
val __bin_read_tcp_bool_option__ : tcp_bool_option Core.Bin_prot.Read.vtag_reader
val bin_reader_tcp_bool_option : tcp_bool_option Core.Bin_prot.Type_class.reader
type tcp_string_option =
  1. | TCP_CONGESTION
    (*

    (Since Linux 2.6.13) Get or set the congestion-control algorithm for this socket.

    The algorithm "reno" is always permitted; other algorithms may be available, depending on kernel configuration and loaded modules (see /proc/sys/net/ipv4/tcp_allowed_congestion_control; add more using modprobe).

    "man 7 tcp" states that getsockopt(... TCP_CONGESTION ...) can return the empty string to indicate "uses the default congestion algorithm", but this does not seem to be necessarily true; sometimes in that situation it will just return the name of the default congestion algorithm.

    *)
val sexp_of_tcp_string_option : tcp_string_option -> Sexplib0.Sexp.t
val tcp_string_option_of_sexp : Sexplib0.Sexp.t -> tcp_string_option
val bin_shape_tcp_string_option : Core.Bin_prot.Shape.t
val bin_size_tcp_string_option : tcp_string_option Core.Bin_prot.Size.sizer
val bin_write_tcp_string_option : tcp_string_option Core.Bin_prot.Write.writer
val bin_writer_tcp_string_option : tcp_string_option Core.Bin_prot.Type_class.writer
val bin_read_tcp_string_option : tcp_string_option Core.Bin_prot.Read.reader
val __bin_read_tcp_string_option__ : tcp_string_option Core.Bin_prot.Read.vtag_reader
val bin_reader_tcp_string_option : tcp_string_option Core.Bin_prot.Type_class.reader
val bin_tcp_string_option : tcp_string_option Core.Bin_prot.Type_class.t
val gettcpopt_bool : (Core_unix.File_descr.t -> tcp_bool_option -> bool) Core.Or_error.t @@ portable

gettcpopt_bool sock opt Returns the current value of the boolean TCP socket option opt for socket sock.

val settcpopt_bool : (Core_unix.File_descr.t -> tcp_bool_option -> bool -> unit) Core.Or_error.t @@ portable

settcpopt_bool sock opt v sets the current value of the boolean TCP socket option opt for socket sock to value v.

val gettcpopt_string : (Core_unix.File_descr.t -> tcp_string_option -> string) Core.Or_error.t @@ portable

gettcpopt_string sock opt Returns the current value of the string TCP socket option opt for socket sock.

val settcpopt_string : (Core_unix.File_descr.t -> tcp_string_option -> string -> unit) Core.Or_error.t @@ portable

settcpopt_string sock opt v sets the current value of the string TCP socket option opt for socket sock to value v.

val send_nonblocking_no_sigpipe : (Core_unix.File_descr.t -> ?pos:int -> ?len:int -> Core.Bytes.t -> int option) Core.Or_error.t @@ portable

send_nonblocking_no_sigpipe sock ?pos ?len buf tries to do a nonblocking send on socket sock given buffer buf, offset pos and length len. Prevents SIGPIPE, i.e., raises a Unix-error in that case immediately. Returns Some bytes_written or None if the operation would have blocked.

Raises Invalid_argument if the designated buffer range is invalid. Raises Unix_error on Unix-errors.

val send_no_sigpipe : (Core_unix.File_descr.t -> ?pos:int -> ?len:int -> Core.Bytes.t -> int) Core.Or_error.t @@ portable

send_no_sigpipe sock ?pos ?len buf tries to do a blocking send on socket sock given buffer buf, offset pos and length len. Prevents SIGPIPE, i.e., raises a Unix-error in that case immediately. Returns the number of bytes written.

Raises Invalid_argument if the designated buffer range is invalid. Raises Unix_error on Unix-errors.

val sendmsg_nonblocking_no_sigpipe : (Core_unix.File_descr.t -> ?count:int -> string Core_unix.IOVec.t array -> int option) Core.Or_error.t @@ portable

sendmsg_nonblocking_no_sigpipe sock ?count iovecs tries to do a nonblocking send on socket sock using count I/O-vectors iovecs. Prevents SIGPIPE, i.e., raises a Unix-error in that case immediately. Returns Some bytes_written or None if the operation would have blocked.

Raises Invalid_argument if the designated ranges are invalid. Raises Unix_error on Unix-errors.

Non-portable socket functionality

module Peer_credentials : sig ... end
val peer_credentials : (Core_unix.File_descr.t -> Peer_credentials.t) Core.Or_error.t @@ portable

peer_credential fd takes a file descriptor of a unix socket. It returns the pid and real ids of the process on the other side, as described in man 7 socket entry for SO_PEERCRED. This is useful in particular in the presence of pid namespace, as the returned pid will be a pid in the current namespace, not the namespace of the other process.

Raises Unix_error if something goes wrong (file descriptor doesn't satisfy the conditions above, no process on the other side of the socket, etc.).

Clock functions

module Clock : sig ... end

Eventfd functions

module Eventfd : sig ... end

Fallocate functions

module Fallocate : sig ... end

Timerfd functions

module Timerfd : sig ... end

Memfd functions

module Memfd : sig ... end

Parent death notifications

val pr_set_pdeathsig : (Core.Signal.t -> unit) Core.Or_error.t @@ portable

pr_set_pdeathsig s sets the signal s to be sent to the executing process when its parent dies. NOTE: the parent may have died before or while executing this system call. To make sure that you do not miss this event, you should call getppid to get the parent process id after this system call. If the parent has died, the returned parent PID will be 1, i.e., the init process will have adopted the child. You should then either send the signal to yourself using Unix.kill, or execute an appropriate handler.

val pr_get_pdeathsig : (unit -> Core.Signal.t) Core.Or_error.t @@ portable

pr_get_pdeathsig () gets the signal that will be sent to the currently executing process when its parent dies.

Task name

val pr_set_name_first16 : (string -> unit) Core.Or_error.t @@ portable

pr_set_name_first16 name sets the name of the executing thread to name. Only the first 16 bytes in name will be used; the rest is ignored.

val pr_get_name : (unit -> string) Core.Or_error.t @@ portable

pr_get_name () gets the name of the executing thread. The name is at most 16 bytes long.

Pathname resolution

val file_descr_realpath : (Core_unix.File_descr.t -> string) Core.Or_error.t @@ portable

file_descr_realpath fd returns the canonicalized absolute pathname of the file associated with file descriptor fd.

Raises Unix_error on errors.

val out_channel_realpath : (Core.Out_channel.t -> string) Core.Or_error.t @@ portable

out_channel_realpath oc returns the canonicalized absolute pathname of the file associated with output channel oc.

Raises Unix_error on errors.

val in_channel_realpath : (Core.In_channel.t -> string) Core.Or_error.t @@ portable

in_channel_realpath ic returns the canonicalized absolute pathname of the file associated with input channel ic.

Raises Unix_error on errors.

Affinity

val sched_setaffinity : (?pid:Core.Pid.t -> cpuset:int list -> unit -> unit) Core.Or_error.t @@ portable

Setting the CPU affinity causes a thread to only run on the cores chosen. You can find out how many cores a system has in /proc/cpuinfo. This can be useful in two ways: first, it limits a process to a core so that it won't interfere with processes on other cores. Second, you save time by not moving the process back and forth between CPUs, which sometimes invalidates their cache.

See man sched_setaffinity for details.

Note, in particular, that affinity is a "per-thread attribute that can be adjusted independently for each of the threads in a thread group", and so omitting ~pid (or specifying ?pid as None) "will set the attribute for the calling thread", and "passing the value returned from a call to getpid will set the attribute for the main thread of the thread group". (A thread group is what you might think of as a process.)

val sched_getaffinity : (?pid:Core.Pid.t -> unit -> int list) Core.Or_error.t @@ portable
val sched_setaffinity_this_thread : (cpuset:int list -> unit) Core.Or_error.t @@ portable

sched_setaffinity_this_thread is equivalent to sched_setaffinity ?pid:None, though happens to be implemented by using gettid rather than passing pid=0 to sched_setaffinity. It exists for historical reasons, and may be removed in the future.

val cores : (unit -> int) Core.Or_error.t @@ portable

cores () returns the number of cores on the machine. This may be different than the number of cores available to the calling process.

val cpu_list_of_string_exn : string -> int list @@ portable

Parse a kernel %*pbl-format CPU list (e.g. 1,2,3,10-15) into an int list.

val isolated_cpus : (unit -> int list) Core.Or_error.t @@ portable

isolated_cpus () returns the list of cores marked as isolated.

val online_cpus : (unit -> int list) Core.Or_error.t @@ portable

online_cpus () returns the list of cores online for scheduling.

val allowed_cpus : (?include_offline:bool -> ?pid:Core.Pid.t -> unit -> int list) Core.Or_error.t @@ portable

allowed_cpus ?include_offline ?pid () returns the list of cores allowed for scheduling for this particular PID. If no PID is specified, this checks the current PID. By default, this function automatically filters the returned list of cores to only include online cores. Set include_offline to true to also return CPU cores that the kernel is reporting as allowed, but are currently offline.

val cpus_local_to_nic : (ifname:string -> int list) Core.Or_error.t @@ portable

cpus_local_to_nic ~ifname returns the list of cores NUMA-local to ifname.

val get_terminal_size : ([ `Controlling | `Fd of Core_unix.File_descr.t ] -> int * int) Core.Or_error.t @@ portable

get_terminal_size term returns (rows, cols), the number of rows and columns of the controlling terminal (raises if no controlling terminal), or of the specified file descriptor (useful when writing to stdout, because stdout doesn't have to be the controlling terminal).

module Priority : sig ... end

Priority.t is what is usually referred to as the "nice" value of a process. It is also known as the "dynamic" priority. It is used with normal (as opposed to real-time) processes that have static priority zero. See Unix.Scheduler.set for setting the static priority.

The meaning of pids for get/setpriority is a bit weird. According to the POSIX standard the priority is per process (pid), however in Linux the priority is per thread (tid/LWP): man 2 setpriority says, in the "BUGS" section, that "According to POSIX, the nice value is a per-process setting. However, under the current Linux/NPTL implementation of POSIX threads, the nice value is a per-thread attribute ... portable applications should avoid relying on the Linux behavior".

As a result, if you omit ?pid or pass ?pid:None, only the current thread will be affected; passing ~pid:(getpid ()) will only affect the main thread of the current process.

val setpriority : (?pid:Core.Pid.t -> Priority.t -> unit) Core.Or_error.t @@ portable

Set the thread's priority in the Linux scheduler. Omitting the pid argument means that (only) the calling thread will be affected.

val getpriority : (?pid:Core.Pid.t -> unit -> Priority.t) Core.Or_error.t @@ portable

Get the thread's priority in the Linux scheduler. Omitting the pid argument means that the calling thread's priority will be retrieved.

val get_ipv4_address_for_interface : (string -> string) Core.Or_error.t @@ portable

get_ipv4_address_for_interface "eth0" returns the IP address assigned to eth0, or throws an exception if no IP address is configured.

val get_mac_address : (ifname:string -> string) Core.Or_error.t @@ portable

get_mac_address returns the mac address of ifname in the canonical form, e.g. "aa:bb:cc:12:34:56".

val bind_to_interface : (Core_unix.File_descr.t -> Bound_to_interface.t -> unit) Core.Or_error.t @@ portable

bind_to_interface fd (Only "eth0") restricts packets from being received/sent on the given file descriptor fd on any interface other than "eth0". Use bind_to_interface fd Any to allow traffic on any interface. The bindings are not cumulative; you may only select one interface, or Any.

Not to be confused with a traditional BSD sockets API bind() call, this Linux-specific socket option (SO_BINDTODEVICE) is used for applications on multi-homed machines with specific security concerns. For similar functionality when using multicast, see Core_unix.mcast_set_ifname.

get_bind_to_interface fd returns the current interface the socket is bound to. It uses getsockopt() with Linux-specific SO_BINDTODEVICE option. Empty string means it is not bound to any specific interface. See man 7 socket for more information.

val get_bind_to_interface : (Core_unix.File_descr.t -> Bound_to_interface.t) Core.Or_error.t @@ portable
val setfsuid : (uid:int -> int) Core.Or_error.t @@ portable

Set the user identity used for filesystem checks for this thread. Returns the previous user identity.

No error indications are returned even if the call fails. To check if it was successful, you can call getfsuid to check if the fsuid actually updated.

val setfsgid : (gid:int -> int) Core.Or_error.t @@ portable

Set the group identity used for filesystem checks for this thread. Returns the previous group identity.

No error indications are returned even if the call fails. To check if it was successful, you can call getfsgid to check if the fsgid actually updated.

val getfsuid : (unit -> int) Core.Or_error.t @@ portable

Get the user identity used for filesystem checks. This is a wrapper around setfsuid ~fsuid:(-1)

val getfsgid : (unit -> int) Core.Or_error.t @@ portable

Get the group identity used for filesystem checks. This is a wrapper around setfsgid ~fsgid:(-1)

module Epoll : Epoll.S
module Extended_file_attributes : sig ... end

Extended attributes are name:value pairs associated with inodes (files, directories, symlinks, etc). They are extensions to the normal attributes which are associated with all inodes in the system (i.e. the 'man 2 stat' data). A complete overview of extended attributes concepts can be found in 'man 5 attr'.