Module posix

Functions

__index (name) Lazy load of available submodules.
abort () Abort the program immediately.
errno (n) Describe an error code/and or read errno
getopt (arg, shortopts, longopts) Parse command-line options.
isgraph (character) Check for any printable character except space.
isprint (character) Check for any printable character including space.
set_errno (n) Set errno.

File descriptors

close (fd) Close an open file descriptor.
creat (file, mode) Create a file.
dup (fd) Duplicate an open file descriptor.
dup2 (oldfd, newfd) Duplicate one open file descriptor to another, closing the new one if necessary.
fcntl (fd, cmd, arg) Manipulate file descriptor.
fdatasync (fd) synchronize a file's in-core state with storage device without metadata
fileno (file) File descriptor corresponding to a Lua file object.
fsync (fd) synchronize a file's in-core state with storage device
lseek (fd, offset, whence) reposition read/write file offset
mkfifo (path) Make a FIFO pipe.
nice (inc) change process priority
open (path, oflags, mode) Open a file.
pipe () Creates a pipe.
poll (list, timeout) Wait for events on multiple file descriptors.
read (fd, count) Read bytes from a file.
rpoll (file, timeout) Wait for some event on a file descriptor.
write (fd, buf) Write bytes to a file.

File system

access (path, mode) Check real user's permissions for a file.
basename (path) File part of path.
chdir (path) Set the working directory.
chmod (path, mode) Change the mode of the path.
chown (path, uid, gid) Change ownership of a file.
dir (path) Contents of directory.
dirname (path) Directory name of path.
fadvise (file) Instruct kernel on appropriate cache behaviour for a file or file segment.
files (path) Iterator over all files in this directory.
fnmatch (pat, name, flags) Match a filename against a shell pattern.
getcwd () Current working directory for this process.
glob (pat) Find all files in this directory matching a shell pattern.
link (target, link, soft) Create a link.
mkdir (path) Make a directory.
mkdtemp (templ) Create a unique temporary directory.
mkstemp (templ) Create a unique temporary file.
pathconf (path, ...) Get a value for a configuration option for a filename.
readlink (path) Read value of a symbolic link.
realpath (path) Find canonicalized absolute pathname.
rmdir (path) Remove a directory.
stat (path, ...) Information about an existing file path.
statvfs (path, ...) Get file system statistics.
sync () commit buffer cache to disk
umask (mode) Set file mode creation mask.
unlink (path) Unlink a file.
utime (path, mtime, atime) Change file last access and modification times.

Host management

hostid () Get host id.
sysconf (...) Get configuration information at runtime.
uname (optional) Return information about this machine.

Message queues

msgget (key, flags, mode) Get a message queue identifier
msgrcv (id, size, type, flags) Receive message from a message queue
msgsnd (id, type, message, flags) Send message to a message queue

Process management

_exit (return) Terminate the calling process.
exec (path, ..., t) Execute a program without using the shell.
execp (path, ..., t) Execute a program using the shell.
fork () Fork this program.
getenv (name) Get value of environment variable, or all variables.
getpid (...) Get process identifiers.
getrlimit (resource) Get resource limits for this process.
kill (pid, sig) Send a signal to the given process.
killpg (pgrp, sig) Send a signal to the given process group.
nanosleep (seconds, nanoseconds) Sleep with nanosecond precision.
raise (nsig) Raise a signal on this process.
setenv (name, value, over) Set an environment variable for this process.
setpid (what, id, gid) Set the uid, euid, gid, egid, sid or pid & gid.
setrlimit (resource, softlimit, hardlimit) Set a resource limit for subsequent child processes.
sleep (seconds) Sleep for a number of seconds.
times (...) Get the current process times.
wait (pid) Wait for the given process.

Signal handling

signal (signum, handler, flags) Install a signal handler for this signal number.

Socket management

accept (fd) Accept a connection on a socket.
bind (fd, sockaddr)
connect (fd, sockaddr) Initiate a connection on a socket.
getaddrinfo (host, service[, hints]) Network address and service translation.
listen (fd, backlog)
recv (fd, count) Receive a message from a socket.
recvfrom (fd, count) Receive a message from a socket.
send (fd, buffer) Send a message from a socket.
sendto (fd, buffer, address) Send a message from a socket.
setsockopt (fd, level, name, value1[, value2]) Get and set options on sockets.
shutdown (fd, how) Shut down part of a full-duplex connection.
socket (domain, type, options) Create an endpoint for communication.
socketpair (domain, type, options) Create a pair of connected sockets.

System log

closelog () Close system log.
openlog (ident, option, facility) Open the system logger.
setlogmask (bitwise) Set log priority mask.
syslog (priority, message) Write to the system logger.

Terminal handling

ctermid () Name of controlling terminal.
grantpt (file) Grant access to a slave pseudoterminal
isatty (fd) Test whether a file descriptor refers to a terminal.
openpt (oflags) Open a pseudoterminal.
openpty (term, win) Open a pseudo-terminal.
ptsname (file) Get the name of a slave pseudo-terminal
tcdrain (fd) Wait for all written output to reach the terminal.
tcflow (fd, action) Suspend transmission or receipt of data.
tcflush (fd, action) Discard any data already written but not yet sent to the terminal.
tcgetattr (fd) Get termios state.
tcsendbreak (fd, duration) Send a stream of zero valued bits.
tcsetattr (fd, actions, termios) Set termios state.
ttyname (fd) Name of a terminal device.
unlockpt (file) Unlock a pseudoterminal master/slave pair

Time and date

clock_getres (name) Find the precision of a clock.
clock_gettime (name) Read a clock.
gettimeofday () Get time of day.
gmtime (t) Convert UTC time in seconds to table.
localtime (t) Convert time in seconds to table.
mktime (tm) Convert a time table into a time value.
strftime (tm) Write a time out according to a format.
strptime (s, format) Parse a date string.
time () Get current time.

User management

crypt (string, salt) Encrypt a password.
getgroup (group) Information about a group.
getgroups () Get list of supplementary group IDs.
getpasswd (user, ...) Get the password entry for a user.


Functions

__index (name)
Lazy load of available submodules.

Parameters:

Returns:

    the submodule that was loaded to satisfy the missing name
abort ()
Abort the program immediately.

See also:

errno (n)
Describe an error code/and or read errno

Parameters:

  • n int optional error code (default: current value of errno )

Returns:

  1. description
  2. error code

See also:

getopt (arg, shortopts, longopts)
Parse command-line options.

Parameters:

  • arg command line arguments
  • shortopts string e.g 'ho:v' (colon means 'receives argument')
  • longopts e.g. {{'help','none',2},...}

See also:

Usage:

    for ret, longindex, optind, optarg in posix.getopt (arg, shortopts[, longopts[, opterr[, optind]]]) do ... end
isgraph (character)
Check for any printable character except space.

Parameters:

Returns:

    true if character is in the class

See also:

isprint (character)
Check for any printable character including space.

Parameters:

Returns:

    true if character is in the class

See also:

set_errno (n)
Set errno.

Parameters:

  • n int error code

See also:

File descriptors

close (fd)
Close an open file descriptor.

Parameters:

  • fd int

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed.

See also:

creat (file, mode)
Create a file.

Parameters:

  • file name of file to create
  • mode permissions with which to create file

Returns:

    file descriptor, or -1 on error
dup (fd)
Duplicate an open file descriptor.

Parameters:

  • fd int

Returns:

  1. file descriptor on success, nil otherwise
  2. error message if failed.

See also:

dup2 (oldfd, newfd)
Duplicate one open file descriptor to another, closing the new one if necessary.

Parameters:

  • oldfd int
  • newfd int

Returns:

  1. new file descriptor on success, nil otherwise
  2. error message if failed.

See also:

fcntl (fd, cmd, arg)
Manipulate file descriptor.

Parameters:

  • fd int file descriptor to act on
  • cmd int operation to perform
  • arg optional (default 0). Type and meaning of this param depends on cmd. Currently it expects arg to be a table for file lock related cmd and a number for all the rest. With file lock cmd the table should contain fields for flock structure (see example). When function returns the fields of the table get updated with corresponding values from flock structure (to comply with semantics of F_GETLK).

Returns:

  1. integer return value depending on cmd, or nil on error
  2. error message if failed

See also:

fdatasync (fd)
synchronize a file's in-core state with storage device without metadata

Parameters:

  • fd int

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed.

See also:

fileno (file)
File descriptor corresponding to a Lua file object.

Parameters:

  • file Lua file object

Returns:

  1. handle on success, nil otherwise
  2. error message if failed.
fsync (fd)
synchronize a file's in-core state with storage device

Parameters:

  • fd int

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed.

See also:

lseek (fd, offset, whence)
reposition read/write file offset

Parameters:

  • fd int
  • offset int
  • whence int one of SEEK_SET, SEEK_CUR or SEEK_END

Returns:

  1. new offset on success, nil otherwise
  2. error message if failed.

See also:

mkfifo (path)
Make a FIFO pipe.

Parameters:

Returns:

  1. handle on success, nil otherwise
  2. error message if failed.

See also:

nice (inc)
change process priority

Parameters:

  • inc int adds inc to the nice value for the calling process

Returns:

  1. new nice value on success, nil otherwise
  2. error message if failed.

See also:

open (path, oflags, mode)
Open a file.

Parameters:

  • path string
  • oflags int bitwise OR of the values O_RDONLY, O_WRONLY, O_RDWR, O_APPEND, O_CREAT, O_DSYNC, O_EXCL, O_NOCTTY, O_NONBLOCK, O_RSYNC, O_SYNC, O_TRUNC (all in the library's namespace)
  • mode string (used with O_CREAT; see chmod for format)

Returns:

  1. file descriptor on success, nil otherwise
  2. error message if failed.

See also:

pipe ()
Creates a pipe.

Returns:

  1. fd read end
  2. fd write end

See also:

poll (list, timeout)
Wait for events on multiple file descriptors.

Parameters:

  • list of file descriptors
  • timeout int optional (default -1)

Returns:

  1. return code, nil otherwise
  2. error message if failed.

See also:

read (fd, count)
Read bytes from a file.

Parameters:

  • fd int the file descriptor
  • count int number of bytes to read

Returns:

  1. string with at most count bytes, or nil on error
  2. error message if failed

See also:

rpoll (file, timeout)
Wait for some event on a file descriptor. Adapted from http://lua-users.org/lists/lua-l/2007-11/msg00346.html

Parameters:

  • file int descriptor
  • timeout int

Returns:

  1. return code, nil otherwise
  2. error message if failed.

See also:

write (fd, buf)
Write bytes to a file.

Parameters:

  • fd int the file descriptor
  • buf string containing bytes

Returns:

  1. number of bytes written on success, nil otherwise
  2. error message if failed.

See also:

File system

access (path, mode)
Check real user's permissions for a file.

Parameters:

  • path string
  • mode string optional, can contain 'r','w','x' and 'f' (default 'f')

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed.

See also:

Usage:

    status, errstr, errno = posix.access("/etc/passwd", "rw")
basename (path)
File part of path.

Parameters:

Returns:

    file part

See also:

chdir (path)
Set the working directory.

Parameters:

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed.

See also:

Usage:

    status, errstr, errno = posix.chdir("/var/tmp")
chmod (path, mode)
Change the mode of the path. Modes are specified in one of the following formats: "rwxrwxrwx" (e.g. "rw-rw-w--") "ugoa+-=rwx" (e.g. "u+w") "+-=rwx" (e.g. "+w")

Parameters:

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed.

See also:

Usage:

    posix.chmod('bin/dof','+x')
chown (path, uid, gid)
Change ownership of a file.

Parameters:

  • path string existing file path
  • uid (string or number)
  • gid (string or number)

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed.

See also:

Usage:

    print(posix.chown("/etc/passwd",100,200)) -- will fail for a normal user, and hence print an error
dir (path)
Contents of directory.

Parameters:

  • path string optional (default .)

Returns:

    contents as table

See also:

dirname (path)
Directory name of path.

Parameters:

Returns:

    directory part

See also:

fadvise (file)
Instruct kernel on appropriate cache behaviour for a file or file segment.

Parameters:

  • file Lua file object

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed.
files (path)
Iterator over all files in this directory.

Parameters:

  • path string optional (default .)

Returns:

    an iterator
fnmatch (pat, name, flags)
Match a filename against a shell pattern.

Parameters:

  • pat string shell pattern
  • name string filename
  • flags int optional (default 0)

Returns:

    true or false

Raises:

error if fnmatch failed

See also:

getcwd ()
Current working directory for this process.

Returns:

    path

See also:

glob (pat)
Find all files in this directory matching a shell pattern.

Parameters:

Returns:

    table of matching filenames

See also:

link (target, link, soft)
Create a link.

Parameters:

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed.

See also:

mkdir (path)
Make a directory.

Parameters:

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed.

See also:

mkdtemp (templ)
Create a unique temporary directory.

Parameters:

  • templ string pattern that ends in six 'X' characters

Returns:

  1. path on success, nil otherwise
  2. error message if failed

See also:

mkstemp (templ)
Create a unique temporary file.

Parameters:

  • templ string pattern that ends in "XXXXXX"

Returns:

  1. file descriptor, nil otherwise
  2. name on success, error otherwise

See also:

Usage:

    posix.mkstemp 'wooXXXXXX'
pathconf (path, ...)
Get a value for a configuration option for a filename.

Parameters:

  • path string optional (default ".")
  • ... string field names, each one of "LINK_MAX", "MAX_CANON", "NAME_MAX", "PIPE_BUF", "CHOWN_RESTRICTED", "NO_TRUNC", "VDISABLE"

Returns:

    ... values, or table of all fields if no option given

See also:

Usage:

    for a, b in pairs(posix.pathconf("/dev/tty")) do print(a, b) end
readlink (path)
Read value of a symbolic link.

Parameters:

Returns:

  1. link target on success, error otherwise
  2. error message if failed

See also:

realpath (path)
Find canonicalized absolute pathname.

Parameters:

Returns:

  1. canonicalized absolute path, or nil on error
  2. error message if failed

See also:

rmdir (path)
Remove a directory.

Parameters:

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed.

See also:

stat (path, ...)
Information about an existing file path.

Parameters:

  • path string file path
  • ... string field names, each one of "mode", "ino", "dev", "nlink", "uid", "gid", "size", "atime", "mtime", "ctime", "type"

Returns:

    ... values, or table of all fields if no option given

See also:

Usage:

    for a, b in pairs(posix.stat("/etc/")) do print(a, b) end
statvfs (path, ...)
Get file system statistics.

Parameters:

  • path string any path within the mounted file system
  • ... string field names, each one of "bsize", "frsize", "blocks", "bfree", "bavail", "files", "ffree", "favail", "fsid", "flag", "namemax"

Returns:

    ... values, or table of all fields if no option given

See also:

sync ()
commit buffer cache to disk

See also:

umask (mode)
Set file mode creation mask.

Parameters:

  • mode string optional file creation mask string (default current mask; see chmod for format)

Returns:

    previous umask

See also:

unlink (path)
Unlink a file.

Parameters:

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed.

See also:

utime (path, mtime, atime)
Change file last access and modification times.

Parameters:

  • path string existing file path
  • mtime int optional modification time (default current time)
  • atime int optional access time (default current time)

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed.

See also:

Host management

hostid ()
Get host id.

Returns:

    host id

See also:

sysconf (...)
Get configuration information at runtime.

Parameters:

  • ... string field names, each one of "ARG_MAX", "CHILDMAX", "CLK_TCK", "NGROUPSMAX", "STREAM_MAX", "TZNAME_MAX", "OPEN_MAX", "JOB_CONTROL", "VERSION"

Returns:

    ... values, or table of all fields no option

See also:

uname (optional)
Return information about this machine.

Parameters:

  • optional string , contains zero or more of: %m machine name %n node name %r release %s sys name %v version

    (default return all information available)

Returns:

  1. information string on success, nil otherwise
  2. error message if failed

See also:

Message queues

msgget (key, flags, mode)
Get a message queue identifier

Parameters:

  • key int
  • flags int (optional, default - 0)
  • mode int (optional, default - 0777)

Returns:

  1. message queue identifier on success
  2. nil and error message if failed

See also:

msgrcv (id, size, type, flags)
Receive message from a message queue

Parameters:

  • id int
    • message queue identifier returned by msgget
  • size int
    • maximum message size
  • type int
    • message type (optional, default - 0)
  • flags int (optional, default - 0)

Returns:

  1. message type and message text on success
  2. nil, nil and error message if failed

See also:

msgsnd (id, type, message, flags)
Send message to a message queue

Parameters:

  • id int
    • message queue identifier returned by msgget
  • type int
    • message type
  • message string
  • flags int (optional, default - 0)

Returns:

  1. 0 on success
  2. nil and error message if failed

See also:

Process management

_exit (return)
Terminate the calling process.

Parameters:

  • return int status

See also:

exec (path, ..., t)
Execute a program without using the shell.

Parameters:

  • path string
  • ... any arguments, or
  • t table of arguments (can include index 0)

Returns:

  1. return code, nil otherwise
  2. error message if failed.

See also:

execp (path, ..., t)
Execute a program using the shell.

Parameters:

  • path string
  • ... any arguments, or
  • t table of arguments (can include index 0)

Returns:

  1. return code, nil otherwise
  2. error message if failed.

See also:

fork ()
Fork this program.

Returns:

  1. return code, nil otherwise
  2. error message if failed.

See also:

getenv (name)
Get value of environment variable, or all variables.

Parameters:

Returns:

    value if name given, otherwise a name-indexed table of values.

See also:

Usage:

    for a,b in pairs(posix.getenv()) do print(a, b) end
getpid (...)
Get process identifiers.

Parameters:

  • ... string types, each one of "egid", "euid", "gid", "uid", "pgrp", "pid", "ppid"

Returns:

    ... values, or table of all ids if no option given

Usage:

    posix.getpid 'pid' -- PID of current process
getrlimit (resource)
Get resource limits for this process.

Parameters:

  • resource string one of "core", "cpu", "data", "fsize", "nofile", "stack", "as"

Returns:

  1. softlimit, or nil if error
  2. hardlimit, or message on error
kill (pid, sig)
Send a signal to the given process.

Parameters:

  • pid int process id
  • sig int optional (default SIGTERM)

Returns:

  1. return code, nil otherwise
  2. error message if failed.

See also:

killpg (pgrp, sig)
Send a signal to the given process group.

Parameters:

  • pgrp int group id
  • sig int optional (default SIGTERM)

Returns:

  1. return code, nil otherwise
  2. error message if failed.

See also:

nanosleep (seconds, nanoseconds)
Sleep with nanosecond precision.

Parameters:

  • seconds int
  • nanoseconds int

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed, or the remaining time as two results tv_sec, tv_nsec if return value is EINTR.

See also:

raise (nsig)
Raise a signal on this process.

Parameters:

  • nsig int

Returns:

    integer error code

See also:

setenv (name, value, over)
Set an environment variable for this process. (Child processes will inherit this)

Parameters:

  • name string
  • value string (maybe nil, meaning 'unset')
  • over non-nil prevents overwriting a variable

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed.

See also:

setpid (what, id, gid)
Set the uid, euid, gid, egid, sid or pid & gid.

Parameters:

  • what string one of 'u', 'U', 'g', 'G', 's', 'p' (upper-case means "effective")
  • id (uid, gid or pid for every value of what except 's')
  • gid (only for what value 'p')

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed

See also:

setrlimit (resource, softlimit, hardlimit)
Set a resource limit for subsequent child processes.

Parameters:

  • resource string one of "core", "cpu", "data", "fsize", "nofile", "stack", "as"
  • softlimit optional (default keep current limit)
  • hardlimit optional (default keep current limit)

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed.

See also:

Usage:

    posix.setrlimit("nofile", 1000, 2000)
sleep (seconds)
Sleep for a number of seconds.

Parameters:

  • seconds int

Returns:

    code

See also:

times (...)
Get the current process times.

Parameters:

  • ... string field names, each one of "utime", "stime", "cutime", "cstime", "elapsed"

Returns:

    ... times, or table of all times if no option given

See also:

wait (pid)
Wait for the given process.

Parameters:

  • pid int optional (default -1 (any child process))

Returns:

  1. pid of terminated child, nil on error
  2. how child ended ("exited", "killed" or "stopped"), or error message on error.
  3. status value (computed with WEXITSTATUS, WTERMSIG or WSTOPSIG as appropriate), or nothing on error.

See also:

Signal handling

signal (signum, handler, flags)
Install a signal handler for this signal number. N.B. Although this is the same API as signal(2), it uses sigaction for guaranteed semantics.

Parameters:

  • signum int
  • handler function
  • flags optional the sa_flags element of struct sigaction

Returns:

    previous handler function

See also:

Socket management

accept (fd)
Accept a connection on a socket.

Parameters:

  • fd int socket descriptor

Returns:

  1. connection descriptor if successful, otherwise nil
  2. connection address if successful, otherwise error mesage

See also:

bind (fd, sockaddr)

Parameters:

  • fd int socket descriptor
  • sockaddr one of the entries from a successful getaddrinfo call

Returns:

  1. true if successful, otherwise nil
  2. error message if failed
  3. 0 if successful, otherwise nil
  4. error message if failed

See also:

connect (fd, sockaddr)
Initiate a connection on a socket.

Parameters:

  • fd int socket descriptor
  • sockaddr one of the entries from a successful getaddrinfo call

Returns:

  1. true if successful, otherwise nil
  2. error message if failed
  3. true if successful, otherwise nil
  4. error message if failed

See also:

getaddrinfo (host, service[, hints])
Network address and service translation.

Parameters:

  • host string name of a host
  • service string name of service
  • hints table with one or more fields from "family", "flags", "socktype", "protocol"

Returns:

  1. a list of tables with fields "socktype", "canonname" and "protocol" if successful, otherwise nil
  2. string error message if failed
  3. int error code if failed

See also:

Usage:

    res, errmsg, errcode = posix.getaddrinfo ("www.lua.org", "http",
     { family = posix.IF_INET, socktype = posix.SOCK_STREAM })
listen (fd, backlog)

Parameters:

  • fd int socket descriptor
  • backlog int maximum length for queue of pending connections

Returns:

  1. 0 if successful, otherwise nil
  2. error message if failed

See also:

recv (fd, count)
Receive a message from a socket.

Parameters:

  • fd int socket descriptor
  • count int number of bytes to receive

Returns:

  1. received bytes if successful, otherwise nil
  2. error message if failed

See also:

recvfrom (fd, count)
Receive a message from a socket.

Parameters:

  • fd int socket descriptor
  • count int number of bytes to receive

Returns:

  1. received bytes if successful, otherwise nil
  2. address of message source if successful, otherwise error message

See also:

send (fd, buffer)
Send a message from a socket.

Parameters:

  • fd int socket descriptor
  • buffer string message bytes to send

Returns:

  1. number of bytes sent if successful, otherwise nil
  2. error message if failed

See also:

sendto (fd, buffer, address)
Send a message from a socket.

Parameters:

  • fd int socket descriptor
  • buffer string message bytes to send
  • address desination address for message

Returns:

  1. number of bytes sent if successful, otherwise nil
  2. error message if failed

See also:

setsockopt (fd, level, name, value1[, value2])
Get and set options on sockets.

Parameters:

  • fd int socket descriptor
  • level int one of SOL_SOCKET, IPPROTO_IPV6, IPPROTO_TCP
  • name int option name, varies according to level value
  • value1 option value to set
  • value2 some option names need an additional value

Returns:

  1. 0 if successful, otherwise nil
  2. error message if failed

See also:

Usage:

    ok, errmsg = posix.setsockopt (sock, posix.SOL_SOCKET, posix.SO_SNDTIMEO, 1, 0)
shutdown (fd, how)
Shut down part of a full-duplex connection.

Parameters:

  • fd int socket descriptor
  • how int one of SHUT_RD, SHUT_WR or SHUT_RDWR

Returns:

  1. 0 if successful, otherwise nil
  2. error message if failed

See also:

Usage:

    ok, errmsg = posix.shutdown (sock, posix.SHUT_RDWR)
socket (domain, type, options)
Create an endpoint for communication.

Parameters:

  • domain int one of AF_INET, AF_INET6, AF_UNIX or AF_NETLINK
  • type int one of SOCK_STREAM, SOCK_DGRAM or SOCK_RAW
  • options int usually 0, but some socket types might imlement other protocols.

Returns:

  1. int socket descriptor if successful, nil otherwise
  2. string error message if failed

See also:

Usage:

    sockd = posix.socket (posix.AF_INET, posix.SOCK_STREAM, 0)
socketpair (domain, type, options)
Create a pair of connected sockets.

Parameters:

  • domain int one of AF_INET, AF_INET6, AF_UNIX or AF_NETLINK
  • type int one of SOCK_STREAM, SOCK_DGRAM or SOCK_RAW
  • options int usually 0, but some socket types might imlement other protocols.

Returns:

  1. descriptor of one end of the socket pair if successful, nil otherwise
  2. descriptor of the other end of the pair, or error message if failed

System log

closelog ()
Close system log.

See also:

openlog (ident, option, facility)
Open the system logger.

Parameters:

  • ident string all messages will start with this
  • option string optional, any combination of 'c' (directly to system console if an error sending), 'n' (no delay) or 'p' (show PID)
  • facility int optional (default LOG_USER)

See also:

setlogmask (bitwise)
Set log priority mask.

Parameters:

  • bitwise int OR of values from LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG

Returns:

  1. 0 on success, nil otherwise
  2. error message if failed.
syslog (priority, message)
Write to the system logger.

Parameters:

  • priority int one of these values: 1 Alert - immediate action 2 Critcal 3 Error 4 Warning 5 Notice 6 Informational 7 Debug
  • message string

See also:

Terminal handling

ctermid ()
Name of controlling terminal.

Returns:

    code

See also:

grantpt (file)
Grant access to a slave pseudoterminal

Parameters:

  • file descriptor returned by opening /dev/ptmx

Returns:

  1. 0 on success
  2. nil, error message if failed.

See also:

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

Parameters:

  • fd int file descriptor to test

Returns:

  1. 1 if fd is an open file descriptor referring to a terminal, or nil otherwise
  2. error message if failed

See also:

openpt (oflags)
Open a pseudoterminal.

Parameters:

  • oflags int bitwise OR of the values O_RDWR, and possibly O_NOCTTY (all in the library's namespace)

Returns:

  1. file descriptor on success, nil otherwise
  2. error message if failed.

See also:

openpty (term, win)
Open a pseudo-terminal. Based on the glibc function of the same name. FIXME: add support for term and win arguments.

Parameters:

  • term
  • win

Returns:

  1. master file descriptor, or nil on error
  2. slave file descriptor, or message on error
  3. file name, or nothing on error
ptsname (file)
Get the name of a slave pseudo-terminal

Parameters:

  • file descriptor returned by opening /dev/ptmx

Returns:

  1. path name of the slave terminal device
  2. nil, error message if failed.

See also:

tcdrain (fd)
Wait for all written output to reach the terminal.

Parameters:

  • fd int terminal descriptor

Returns:

  1. 0 if successful, otherwise nil
  2. error message if failed

See also:

tcflow (fd, action)
Suspend transmission or receipt of data.

Parameters:

  • fd int terminal descriptor
  • action int one of TCOOFF, TCOON, TCIOFF, TCION

Returns:

  1. 0 if successful, otherwise nil
  2. error message if failed

See also:

tcflush (fd, action)
Discard any data already written but not yet sent to the terminal.

Parameters:

  • fd int terminal descriptor
  • action int one of TCIFLUSH, TCOFLUSH, TCIOFLUSH

Returns:

  1. 0 if successful, otherwise nil
  2. error message if failed

See also:

tcgetattr (fd)
Get termios state.

Parameters:

  • fd int terminal descriptor

Returns:

  1. termios table with fields iflag, oflag, cflag, lflag and cc if successful, otherwise nil
  2. error message if failed

See also:

Usage:

    termios, errmsg = tcgetattr (fd)
tcsendbreak (fd, duration)
Send a stream of zero valued bits.

Parameters:

  • fd int terminal descriptor
  • duration int if non-zero, stream for some implementation defined time

Returns:

  1. 0 if successful, otherwise nil
  2. error message if failed

See also:

tcsetattr (fd, actions, termios)
Set termios state.

Parameters:

  • fd int terminal descriptor
  • actions int bitwise or of TCSANOW, TCSADRAIN, TCSAFLUSH and TSASOFT
  • termios a table with fields from iflag, oflag, cflag, lflag and cc, each formed by bor operations with various posix constants

Returns:

  1. 0 if successful, otherwise nil
  2. error message if failed

See also:

Usage:

    ok, errmsg = tcsetattr (fd, 0, { cc = { [posix.VTIME] = 0, [posix.VMIN] = 1 })
ttyname (fd)
Name of a terminal device.

Parameters:

  • fd int optional file descriptor (default 0)

Returns:

    string name

See also:

unlockpt (file)
Unlock a pseudoterminal master/slave pair

Parameters:

  • file descriptor returned by opening /dev/ptmx

Returns:

  1. 0 on success
  2. nil, error message if failed.

See also:

Time and date

clock_getres (name)
Find the precision of a clock.

Parameters:

  • name string of clock, one of "monotonic", "process_cputime_id", or "thread_cputime_id", or nil for realtime clock.

Returns:

  1. seconds, or nil on error
  2. nanoseconds, or message on error
clock_gettime (name)
Read a clock.

Parameters:

  • name string of clock, one of "monotonic", "process_cputime_id", or "thread_cputime_id", or nil for realtime clock.

Returns:

  1. seconds, or nil on error
  2. nanoseconds, or message on error
gettimeofday ()
Get time of day.

Returns:

    epoch table: contains fields "sec" (the number of seconds elapsed since the epoch), and "usec" (remainder in nanoseconds)

See also:

gmtime (t)
Convert UTC time in seconds to table.

Parameters:

  • t time in seconds since epoch (default now)

Returns:

    time table as in localtime
localtime (t)
Convert time in seconds to table.

Parameters:

  • t time in seconds since epoch (default now)

Returns:

    time table: contains "is_dst","yearday","hour","min","year","month", "sec","weekday","monthday", "day" (the same as "monthday")
mktime (tm)
Convert a time table into a time value.

Parameters:

Returns:

    time in seconds since epoch
strftime (tm)
Write a time out according to a format.

Parameters:

  • tm optional time table (e.g from localtime ; default current time)

See also:

strptime (s, format)
Parse a date string.

Parameters:

Returns:

  1. time table, like localtime
  2. next index of first character not parsed as part of the date

See also:

Usage:

    posix.strptime('20','%d').monthday == 20
time ()
Get current time.

Returns:

    time in seconds since epoch

User management

crypt (string, salt)
Encrypt a password. Not recommended for general encryption purposes.

Parameters:

  • string string
  • salt string two-character string from [a-zA-Z0-9./]

Returns:

    encrypted string

See also:

getgroup (group)
Information about a group.

Parameters:

  • group id or name

Returns:

    table {name=name,gid=gid,0=member0,1=member1,...}
getgroups ()
Get list of supplementary group IDs.

Returns:

    table of group IDs.

See also:

getpasswd (user, ...)
Get the password entry for a user.

Parameters:

  • user (name or id)
  • ... string field names, each one of "uid", "name", "gid", "passwd", "dir", "shell"

Returns:

    ... values, or table of all fields if no option given

Usage:

  • for a,b in pairs(posix.getpasswd("root")) do print(a,b) end
  • print(posix.getpasswd("root", "shell"))
generated by LDoc 1.4.2