LuaPosix

来自 阿尔派 Linux

这是 Lua Posix 函数的列表。包含有用的代码片段。安装 lua posix。要获取此列表,请执行

require "posix"
for a in pairs(posix) do 
  print(a)
end

访问

检查真实用户对文件的权限

概要

posix.access(pathname, mode)

描述

access() 检查调用进程是否可以访问文件 pathname。如果 pathname 是符号链接,则会对其进行解引用。

mode 指定要执行的可访问性检查,可以是字符串 "f",也可以是 "r""w""x" 中的一个或多个。"f" 测试文件是否存在。"r""w""x" 分别测试文件是否存在并授予读取、写入和执行权限。

返回值

成功时(所有请求的权限都已授予),返回零。如果出错(模式中至少有一个字符请求的权限被拒绝,或者发生其他错误),则返回 nil、errorstr 和 errno。

示例

status, errstr, errno = posix.access("/etc/passwd", "rw")
if not posix.access("/foo", "f") then
  print("/foo does not exist")
end

参考

https://man7.org/linux/man-pages/man2/access.2.html

chdir

更改工作目录

概要

posix.chdir(path)

返回值

成功时,返回零。如果出错,则返回 nil、错误字符串和 errno。

示例

status, errstr, errno = posix.chdir("/tmp")
if posix.chdir("/tmp") then
  print("Changed current working dir to:", posix.getpwd())
fi

chmod

更改文件的权限

概要

posix.chmod(path, mode)

描述

更改由 path 给出的文件的模式。

模式以以下格式之一指定

  • "rwxrwxrwx" (即 "rw-rw-w--")
  • "ugoa+-=rwx" (即 "u+w")
  • "+-=rwx" (即 "+w")

返回值

成功时,返回零。如果出错,则返回 nil、errstr 和 errno。

示例

posix.chmod("/tmp", "a+rwx")
status, errstr = posix.chmod(file, "-w")
if not status then
  print(errstr)
fi

chown

输入

  • 路径 - 文件或目录的位置
  • UID - 用户 ID 号
  • GID - 组 ID 号

输出

  • 字符串 - 0 表示成功,nil 表示失败
bar = require "posix"
print(bar.chown("/etc/passwd",100,200)
--above will give back nil. Since we printed it will also give back the system errors.

clock_getres

待办

clock_gettime

待办

crypt

密码和数据加密

概要

posix.crypt(string, salt)

返回值

成功时,返回零。如果出错,则返回 nil、错误字符串和 errno。


ctermid

输入

  • 无 - 显示终端 ID

输出

  • 带有终端 ID 的字符串
bar = require "posix"
print(bar.ctermid())

dir

读取目录

概要

posix.dir(path)

描述

读取目录 path,并返回包含所有文件的表。如果省略 path,则读取当前工作目录。

返回值

posix.dir() 成功时返回包含所有文件名的表。如果出错,则返回 nil、错误字符串和 errno。

示例

files, errstr, errno = posix.dir("/var/log")
if files then
  for a,b in ipairs(files) do
    print(b)
  end
else
  print(errstr)
end

dup

复制文件描述符

概要

posix.dup(oldfd[, newfd])

描述

posix.dup() 创建文件描述符 oldfd 的副本。

newfd 将是 oldfd 的副本,如有必要,首先关闭 newfd

如果省略 newfd,则最低编号的未使用描述符将用于新描述符。

返回值

posix.dup() 返回新描述符,如果发生错误,则返回 nil、错误字符串和 errno。

errno

显示错误信息

输入

输出

  • 成功或错误消息
bar = require "posix"
a,b = bar.dir("/var/foo"), bar.errno()
print(a,b)
-- a will be nil or 0, b will be No such file or directory or Success

exec

执行文件

概要

posix.exec(path,[args])

描述

exec() 将当前进程映像替换为新的进程映像。

返回值

成功时,exec() 不会返回。如果出错,则返回 nil、errstr 和 errno。

execp

使用 PATH 环境变量执行文件

概要

posix.execp(path,[args])

描述

execp() 将当前进程映像替换为新的进程映像。如果 path 不包含斜杠 (/) 字符,则将在 PATH 环境变量中搜索该文件。

返回值

成功时,execp() 不会返回。如果出错,则返回 nil、errstr 和 errno。


mkfifo

输入

  • 路径 - 在何处创建 fifo

输出

  • 0 表示成功,nil 表示失败
bar = require "posix"
print(bar.mkfifo("/tmp/bobo"))
--Returns 0 for success, nil for failure

files

返回一个迭代器函数,该函数循环遍历给定目录中的每个文件

输入

  • 路径 - 目录

输出

require "posix"
for name in posix.files("/etc") do
    print (name)
end

fork

输入

输出 返回:错误时返回 -1,子进程返回 0,父进程返回子进程的 pid。

require("posix")

print ("parent: my pid is: " .. posix.getpid("pid")) 

local pid = posix.fork () 

if ( pid == -1 ) then
        print ("parent: The fork failed.")
elseif ( pid == 0 ) then
        print ("child: Hello World! I am pid: " .. posix.getpid("pid") )
        print ("child: I'll sleep for 1 second ... ")
        posix.sleep(1)
        print ("child: Good bye");
else
        print ("parent: While the child sleeps, I'm still running.")
        print ("parent: waiting for child (pid:" .. pid .. ") to die...")
        posix.wait(pid)
        print ("parent: child died, but I'm still alive.")
        print ("parent: Good bye")
end
parent: my pid is: 11050
child: Hello World! I am pid: 11051
child: I'll sleep for 1 second ... 
parent: While the child sleeps, I'm still running.
parent: waiting for child (pid:11051) to die...
child: Good bye
parent: child died, but I'm still alive.
parent: Good bye

gmtime

待办

getcwd

获取当前工作目录 输入

输出

  • 字符串 - 内容为当前工作目录
require "posix"
print(posix.getcwd())

getenv

输入

输出

  • 表 - 当前环境变量设置
bar = require "posix"
bobo = bar.getenv()
for a,b in pairs(bobo) do print(b) end
-- Varible - Value

getgroup

输入

  • GID 或组名

输出

  • 表 - 内容包含组名、gid 和用户
bar = require "posix"
bobo = bar.getgroup(1000)
for a,b in pairs(bobo) do print(a,b) end
--if you use pairs then bobo will print also the name and gid or the group
--if you use ipairs then just the group members
1 user1
2 user2
name wheel
gid 1

getlogin

获取用户名

概要

posix.getlogin()

描述

getlogin() 返回一个字符串,其中包含在进程的控制终端上登录的用户的名称;如果无法确定此信息,则返回 nil。

示例

print(posix.getlogin())

getrlimit

待办

getpasswd

获取密码文件条目

概要

posix.getpasswd(user, field)

描述

getpasswd() 查询本地 /etc/passwd 数据库。user 可以是 uid 或用户名。field 是以下字符串之一:"uid""name""gid""password""gecos""dir""shell"

返回值

返回 userfield 值。如果省略 field,则返回包含所有字段的表。

示例

for a,b in pairs(posix.getpasswd("root")) do
  print(a,b)
end

输出

uid     0
name    root
gid     0
passwd  x
gecos   root
dir     /root
shell   /bin/bash
print(posix.getpasswd("root", "shell"))

输出

/bin/sh

getprocessid

输入

  • 选择器 - 可以是 [egid,euid,gid,uid,pgrp,pid,ppid,NULL]

输出

  • 与当前进程和选择器匹配的数字
bar = require "posix"
print(bar.getprocessid("gid"))
18456 
--it just printed the current process id for the script or interactive lua.


这里有版本差异吗?AL 1.7.7 将其称为 "getpid" Nangel

gettimeofday

待办

glob

查找与模式匹配的路径名

概要

posix.glob(pattern)

描述

glob() 函数根据 shell 使用的规则搜索与 pattern 匹配的所有路径名。

返回值

成功完成时,glob() 返回包含文件名的表。如果出错,glob() 返回 nil、errstr 和 errno。

示例

for i,j in pairs(posix.glob("/proc/[0-9]*/exe")) do
  local f = posix.readlink(j)
  if f then
    print(f)
  end
end

kill

输入

  • PID - 进程标识符
  • 信号 - 要发送到进程

输出

bar = require "posix"
--kill your current process
bobo = bar.getprocessid("pid")
bar.kill(bobo,9)
---Signals looks to be the number signals accepted in Unix

link

为文件创建新名称

概要

posix.dup(oldpath, newpath[, symbolic])

描述

为现有文件创建新名称。如果 symbolic 为 true,则新名称将是符号链接(软链接),否则将是硬链接。

返回值

成功时,返回零。如果出错,link() 返回 nil、errstr 和 errno。

示例

require "posix"
source="/etc/passwd"
dest="testfile"
status, errstr = posix.link(source, dest, true)
if status == nil then
  io.stderr:write(dest..": "..errstr.."\n")
end


localtime

待办

mkdir

创建目录

概要

posix.dup(pathname)

描述

mkdir() 尝试创建名为 pathname 的目录。

返回值

glob() 成功时返回 0,如果出错,则返回 nil、errstr 和 errno。

示例

require "posix"
newdir = "/home/user/bobo"
status, errstr = posix.mkdir(newdir)
if status == nil then
        io.stderr:write(newdir..": "..errstr.."\n")
end


pathconf

获取文件的配置值

概要

posix.pathconf(path, name)

描述

posix.pathconf() 获取文件名 path 的配置选项 name 的值。将 name 设置为以下字符串之一将返回以下配置选项

  • link_max
    • 返回文件的最大链接数。如果 path 指的是目录,则该值适用于整个目录。
  • max_canon
    • 返回格式化输入行的最大长度,其中 path 必须指向终端。
  • max_input
    • 返回输入行的最大长度,其中 path 必须指向终端。
  • name_max
    • 返回进程允许在目录 path 中创建的文件名的最大长度。
  • path_max
    • path 是当前工作目录时,返回相对路径名的最大长度。
  • pipe_buf
    • 返回管道缓冲区的大小,其中 path 必须指向 FIFO。
  • chown_restricted
    • 如果不能在此文件上使用 chown(2) 调用,则返回非零值。如果 path 指的是目录,则这适用于该目录中的所有文件。
  • no_trunc
    • 如果访问长度超过 name_max 的文件名会生成错误,则返回非零值。
  • vdisable
    • 如果可以禁用特殊字符处理,则返回非零值,其中 path 必须指向终端。

返回值

如果存在限制,则返回该限制。如果省略 name,则返回所有限制的表。

示例

for i,j in pairs(posix.pathconf("/dev/tty" )) do
  print(i, j)
end

将输出

pipe_buf        4096
link_max        127
path_max        4096
max_canon       255
chown_restricted        1
no_trunc        1
max_input       255
name_max        255
vdisable        0

参考

https://man7.org/linux/man-pages/man3/pathconf.3.html

pipe

待办

示例

require "posix"
rd, wr = posix.pipe()
wr:write("hello, world\n")
wr:close()
print(rd:read("*all"))
rd:close()

putenv

输入

  • 字符串 -

输出

  • 0 表示成功,nil 表示失败
require "posix"
posix.putenv("DISPLAY=localhost:0")

readlink

输入

  • 路径 - 链接的路径

输出

  • 0 表示成功,nil 表示失败
require "posix"
posix.readlink("/etc/rc.d/postfix")
--0 if sucess, nil if failed

rmdir

输入

  • 路径 - 目录的路径

输出

  • 0 表示成功
  • nil 表示失败
require "posix"
posix.rmdir("/home/testdir")
-- 0 for success, nil for failure

setgid

输入

    • 名称或 GID

输出

  • 0 表示成功
  • nil 表示失败
require "posix"
posix.setgid("1000")

setlogmask

待办

setrlimit

待办

setuid

输入

  • 用户
    • 名称或 UID

输出

  • 0 表示成功,nil 表示失败
require "posix"
posix.setuid("1000")
-- 0 for sucess, nil for failure

sleep

输入

输出

  • 0 表示成功,nil 表示失败
require "posix"
posix.sleep(5)
-- will sleep for 5 seconds, usually will not fail

stat

输入

  • 路径 - 文件或目录的位置
  • 选择器 -
    • dev
    • type
    • ctime
    • nlink
    • atime
    • uid
    • mtime
    • gid
    • mode
    • ino
    • size

输出

  • 如果未设置模式,则将输出为包含上述每个设置的表
  • 如果设置了模式,则将给出带有值的字符串
require "posix"
bar = posix.stat("/etc/")
for a,b in pairs(bar) do print(a,b) end
dev     769
type    directory
ctime   1194624026
nlink   113
atime   1194866712
uid     0
mtime   1194624026
gid     0
mode    rwxr-xr-x
ino     4
size    6208

sysconf

输入

  • 如果给出任何内容,则接受选择器
    • tzname_max
    • clk_tck
    • stream_max
    • ngroups_max
    • child_max
    • open_max
    • saved_ids
    • job_control
    • version
    • arg_max

输出

  • 表 - 如果没有选择器
  • 字符串 - 如果有 1 个选择器
require "posix"
bar = posix.stat()
for a,b in pairs(bar) do print(a,b) end
tzname_max      6
clk_tck 100
stream_max      16
ngroups_max     65536
child_max       999
open_max        1024
saved_ids       1
job_control     1
version 200112
arg_max 131072

strftime

待办

time

待办

times

输入

    • utime
    • cstime
    • elapsed
    • cutime
    • stime

输出

  • 表 - 如果未指定,则在表中输出所有值
  • 字符串 - 如果指定了上述其中一个,则字符串输出
require "posix"
bar = posix.times()
for a,b in pairs(bar) do print(a,b) end
--Maybe takes function calls to see how long they run

ttyname

输入

输出

  • 字符串 - 我在哪个 tty 上
require "posix"
print(posix.ttyname())

umask

输入

    • 或模式,要将 umask 更改为什么

输出

  • 字符串 - 当前 umask 设置为什么
    • 或模式,要更改为什么
require "posix"
print(posix.umask)
-- rwxr-xr-x
--or change it
posix.umask("a=rwx")
print(posix.umask())
-- rwxrwxrwx

uname

获取有关当前内核的名称和信息

概要

posix.access([format])

描述

uname() 返回字符串 format,其中以 '%' 开头的指令替换为指定的系统信息。如果未指定 format 或为 nil,则将使用字符串 "%s %n %r %v %m"。

格式字符串指令为

%s - kernel name
%n - network node hostname
%r - kernel release
%v - kernel version
%m - machine hardware name

返回值

包含格式化系统信息的字符串。

示例

print("Kernel version is: "..posix.uname("%v"))

参考

https://man7.org/linux/man-pages/man2/uname.2.html

utime

输入

  • 路径 - 文件或目录的位置
    • atime - 访问时间
    • mtime - 修改时间

输出

  • 0 表示成功
  • nil 表示失败
require "posix"
posix.utime("/var/log/test",atime,mtime)

wait

输入

  • PID - 进程 ID

输出

  • 0 表示成功
  • nil 表示失败
require "posix"
posix.wait(10)


如果您在 linux 下使用此功能,还可以获取

setenv

输入

  • 名称 - 变量名称
  • 值 - 变量名称应设置为的值
    • 覆盖 - 这是否覆盖已设置的变量

输出

  • 0 表示成功,nil 表示失败
require "posix"
posix.setenv("BAR","YES")

unsetenv

输入

  • 名称 - 要取消设置的变量的名称

输出

  • 0 表示成功,nil 表示失败
require "posix"
posix.unsetenv("BAR")