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:
- name string submodule name
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:
- description
- 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:
- character string to check
Returns:
-
true if character is in the class
See also:
- isprint (character)
-
Check for any printable character including space.
Parameters:
- character string to check
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:
- 0 on success, nil otherwise
- 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:
- file descriptor on success, nil otherwise
- 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:
- new file descriptor on success, nil otherwise
- 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 relatedcmd
and a number for all the rest. With file lockcmd
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 ofF_GETLK
).
Returns:
-
integer return value depending on
cmd
, or nil on error - error message if failed
See also:
- fdatasync (fd)
-
synchronize a file's in-core state with storage device without metadata
Parameters:
- fd int
Returns:
- 0 on success, nil otherwise
- error message if failed.
See also:
- fileno (file)
-
File descriptor corresponding to a Lua file object.
Parameters:
- file Lua file object
Returns:
- handle on success, nil otherwise
- error message if failed.
- fsync (fd)
-
synchronize a file's in-core state with storage device
Parameters:
- fd int
Returns:
- 0 on success, nil otherwise
- 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:
- new offset on success, nil otherwise
- error message if failed.
See also:
- mkfifo (path)
-
Make a FIFO pipe.
Parameters:
- path string
Returns:
- handle on success, nil otherwise
- 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:
- new nice value on success, nil otherwise
- 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:
- file descriptor on success, nil otherwise
- error message if failed.
See also:
- pipe ()
-
Creates a pipe.
Returns:
- fd read end
- 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:
- return code, nil otherwise
- 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:
-
string with at most
count
bytes, or nil on error - 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:
- return code, nil otherwise
- 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:
- number of bytes written on success, nil otherwise
- error message if failed.
See also:
File system
- access (path, mode)
-
Check real user's permissions for a file.
Parameters:
Returns:
- 0 on success, nil otherwise
- error message if failed.
See also:
Usage:
status, errstr, errno = posix.access("/etc/passwd", "rw")
- basename (path)
-
File part of path.
Parameters:
- path string
Returns:
-
file part
See also:
- chdir (path)
-
Set the working directory.
Parameters:
- path string
Returns:
- 0 on success, nil otherwise
- 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:
- 0 on success, nil otherwise
- 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:
- 0 on success, nil otherwise
- 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:
- path string
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:
- 0 on success, nil otherwise
- 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:
Returns:
-
true or false
Raises:
error if fnmatch failedSee 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:
- pat string shell pattern
Returns:
-
table of matching filenames
See also:
- link (target, link, soft)
-
Create a link.
Parameters:
Returns:
- 0 on success, nil otherwise
- error message if failed.
See also:
- mkdir (path)
-
Make a directory.
Parameters:
- path string
Returns:
- 0 on success, nil otherwise
- error message if failed.
See also:
- mkdtemp (templ)
-
Create a unique temporary directory.
Parameters:
- templ string pattern that ends in six 'X' characters
Returns:
- path on success, nil otherwise
- error message if failed
See also:
- mkstemp (templ)
-
Create a unique temporary file.
Parameters:
- templ string pattern that ends in "XXXXXX"
Returns:
- file descriptor, nil otherwise
- 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:
- path string
Returns:
- link target on success, error otherwise
- error message if failed
See also:
- realpath (path)
-
Find canonicalized absolute pathname.
Parameters:
- path string
Returns:
- canonicalized absolute path, or nil on error
- error message if failed
See also:
- rmdir (path)
-
Remove a directory.
Parameters:
- path string
Returns:
- 0 on success, nil otherwise
- 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:
- path string
Returns:
- 0 on success, nil otherwise
- 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:
- 0 on success, nil otherwise
- 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:
- information string on success, nil otherwise
- error message if failed
See also:
- optional
string
, contains zero or more of:
%m machine name
%n node name
%r release
%s sys name
%v version
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:
- message queue identifier on success
- 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:
- message type and message text on success
- nil, nil and error message if failed
See also:
- id
int
- 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:
- 0 on success
- nil and error message if failed
See also:
- id
int
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:
- return code, nil otherwise
- 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:
- return code, nil otherwise
- error message if failed.
See also:
- fork ()
-
Fork this program.
Returns:
- return code, nil otherwise
- error message if failed.
See also:
- getenv (name)
-
Get value of environment variable, or all variables.
Parameters:
- name string if nil, get all
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:
- softlimit, or nil if error
- 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:
- return code, nil otherwise
- 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:
- return code, nil otherwise
- error message if failed.
See also:
- nanosleep (seconds, nanoseconds)
-
Sleep with nanosecond precision.
Parameters:
- seconds int
- nanoseconds int
Returns:
- 0 on success, nil otherwise
-
error message if failed, or the remaining time as two
results
tv_sec
,tv_nsec
if return value isEINTR
.
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:
Returns:
- 0 on success, nil otherwise
- 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:
- 0 on success, nil otherwise
- 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:
- 0 on success, nil otherwise
- 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:
- pid of terminated child, nil on error
- how child ended ("exited", "killed" or "stopped"), or error message on error.
-
status value (computed with
WEXITSTATUS
,WTERMSIG
orWSTOPSIG
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 ofstruct sigaction
Returns:
-
previous handler function
See also:
Socket management
- accept (fd)
-
Accept a connection on a socket.
Parameters:
- fd int socket descriptor
Returns:
- connection descriptor if successful, otherwise nil
- 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:
- true if successful, otherwise nil
- error message if failed
- 0 if successful, otherwise nil
- 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:
- true if successful, otherwise nil
- error message if failed
- true if successful, otherwise nil
- 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:
- a list of tables with fields "socktype", "canonname" and "protocol" if successful, otherwise nil
- string error message if failed
- 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:
- 0 if successful, otherwise nil
- 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:
- received bytes if successful, otherwise nil
- 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:
- received bytes if successful, otherwise nil
- 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:
- number of bytes sent if successful, otherwise nil
- 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:
- number of bytes sent if successful, otherwise nil
- 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
name
s need an additional value
Returns:
- 0 if successful, otherwise nil
- 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:
- 0 if successful, otherwise nil
- 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:
- int socket descriptor if successful, nil otherwise
- 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:
- descriptor of one end of the socket pair if successful, nil otherwise
- 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:
- 0 on success, nil otherwise
- error message if failed.
- bitwise
int
OR of values from
- 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:
- 0 on success
- 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 if fd is an open file descriptor referring to a terminal, or nil otherwise
- error message if failed
See also:
- openpt (oflags)
-
Open a pseudoterminal.
Parameters:
- oflags
int
bitwise OR of the values
O_RDWR
, and possiblyO_NOCTTY
(all in the library's namespace)
Returns:
- file descriptor on success, nil otherwise
- error message if failed.
See also:
- oflags
int
bitwise OR of the values
- 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:
- master file descriptor, or nil on error
- slave file descriptor, or message on error
- 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:
- path name of the slave terminal device
- nil, error message if failed.
See also:
- tcdrain (fd)
-
Wait for all written output to reach the terminal.
Parameters:
- fd int terminal descriptor
Returns:
- 0 if successful, otherwise nil
- 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:
- 0 if successful, otherwise nil
- 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:
- 0 if successful, otherwise nil
- error message if failed
See also:
- tcgetattr (fd)
-
Get termios state.
Parameters:
- fd int terminal descriptor
Returns:
- termios table with fields iflag, oflag, cflag, lflag and cc if successful, otherwise nil
- 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:
- 0 if successful, otherwise nil
- 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:
- 0 if successful, otherwise nil
- 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:
- 0 on success
- 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:
- seconds, or nil on error
- 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:
- seconds, or nil on error
- 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:
- tm time table as in localtime
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:
- time table, like localtime
- 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:
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"))