Add bin and edit workflow
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 9s

This commit is contained in:
2026-09-16 13:11:16 -06:00
parent c8ac4fcae5
commit 4cee170d66
17576 changed files with 895740 additions and 2 deletions
@@ -0,0 +1,8 @@
require 'ffi'
module Foo
extend FFI::Library
ffi_lib FFI::Library::LIBC
attach_function :getlogin, [ ], :string
end
puts "getlogin=#{Foo.getlogin}"
@@ -0,0 +1,8 @@
require 'ffi'
module Foo
extend FFI::Library
ffi_lib FFI::Library::LIBC
attach_function :getpid, [ ], :int
end
puts "My pid=#{Foo.getpid}"
@@ -0,0 +1,18 @@
require 'ffi'
require 'rbconfig'
class Timeval < FFI::Struct
layout tv_sec: :ulong, tv_usec: :ulong
end
module LibC
extend FFI::Library
if FFI::Platform.windows?
ffi_lib RbConfig::CONFIG["LIBRUBY_SO"]
else
ffi_lib FFI::Library::LIBC
end
attach_function :gettimeofday, [ :pointer, :pointer ], :int
end
t = Timeval.new
LibC.gettimeofday(t.pointer, nil)
puts "t.tv_sec=#{t[:tv_sec]} t.tv_usec=#{t[:tv_usec]}"
@@ -0,0 +1,8 @@
require 'ffi'
module Foo
extend FFI::Library
ffi_lib FFI::Library::LIBC
attach_function("cputs", "puts", [ :string ], :int)
end
Foo.cputs("Hello, World via libc puts using FFI on MRI ruby")
@@ -0,0 +1,19 @@
# See the ffi wiki for how to use FFI with Ractors:
# https://github.com/ffi/ffi/wiki/Ractors
require 'ffi'
class Ractor
# compat with Ruby-3.4 and older
alias value take unless method_defined? :value
end
module Foo
extend FFI::Library
ffi_lib FFI::Library::LIBC
attach_function("cputs", "puts", [ :string ], :int)
freeze
end
Ractor.new do
Foo.cputs("Hello, World via libc puts using FFI in a Ractor")
end.value
@@ -0,0 +1,60 @@
require 'ffi'
module Inotify
extend FFI::Library
ffi_lib FFI::Library::LIBC
class Event < FFI::Struct
layout \
:wd, :int,
:mask, :uint,
:cookie, :uint,
:len, :uint
end
attach_function :init, :inotify_init, [ ], :int
attach_function :add_watch, :inotify_add_watch, [ :int, :string, :uint ], :int
attach_function :rm_watch, :inotify_rm_watch, [ :int, :uint ], :int
attach_function :read, [ :int, :buffer_out, :uint ], :int
IN_ACCESS=0x00000001
IN_MODIFY=0x00000002
IN_ATTRIB=0x00000004
IN_CLOSE_WRITE=0x00000008
IN_CLOSE_NOWRITE=0x00000010
IN_CLOSE=(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
IN_OPEN=0x00000020
IN_MOVED_FROM=0x00000040
IN_MOVED_TO=0x00000080
IN_MOVE= (IN_MOVED_FROM | IN_MOVED_TO)
IN_CREATE=0x00000100
IN_DELETE=0x00000200
IN_DELETE_SELF=0x00000400
IN_MOVE_SELF=0x00000800
# Events sent by the kernel.
IN_UNMOUNT=0x00002000
IN_Q_OVERFLOW=0x00004000
IN_IGNORED=0x00008000
IN_ONLYDIR=0x01000000
IN_DONT_FOLLOW=0x02000000
IN_MASK_ADD=0x20000000
IN_ISDIR=0x40000000
IN_ONESHOT=0x80000000
IN_ALL_EVENTS=(IN_ACCESS | IN_MODIFY | IN_ATTRIB | IN_CLOSE_WRITE \
| IN_CLOSE_NOWRITE | IN_OPEN | IN_MOVED_FROM \
| IN_MOVED_TO | IN_CREATE | IN_DELETE \
| IN_DELETE_SELF | IN_MOVE_SELF)
end
if $0 == __FILE__
fd = Inotify.init
puts "fd=#{fd}"
wd = Inotify.add_watch(fd, "/tmp/", Inotify::IN_ALL_EVENTS)
fp = FFI::IO.for_fd(fd)
puts "wfp=#{fp}"
while true
buf = FFI::Buffer.alloc_out(Inotify::Event.size + 4096, 1, false)
ev = Inotify::Event.new buf
ready = IO.select([ fp ], nil, nil, nil)
n = Inotify.read(fd, buf, buf.total)
puts "Read #{n} bytes from inotify fd"
puts "event.wd=#{ev[:wd]} mask=#{ev[:mask]} len=#{ev[:len]} name=#{ev[:len] > 0 ? buf.get_string(16) : 'unknown'}"
end
end
@@ -0,0 +1,75 @@
require 'ffi'
module PTY
private
module LibC
extend FFI::Library
ffi_lib FFI::Library::LIBC
attach_function :forkpty, [ :buffer_out, :buffer_out, :buffer_in, :buffer_in ], :int
attach_function :openpty, [ :buffer_out, :buffer_out, :buffer_out, :buffer_in, :buffer_in ], :int
attach_function :login_tty, [ :int ], :int
attach_function :close, [ :int ], :int
attach_function :strerror, [ :int ], :string
attach_function :fork, [], :int
attach_function :execv, [ :string, :buffer_in ], :int
attach_function :execvp, [ :string, :buffer_in ], :int
attach_function :dup2, [ :int, :int ], :int
attach_function :dup, [ :int ], :int
end
Buffer = FFI::Buffer
def self.build_args(args)
cmd = args.shift
cmd_args = args.map do |arg|
MemoryPointer.from_string(arg)
end
exec_args = MemoryPointer.new(:pointer, 1 + cmd_args.length + 1)
exec_cmd = MemoryPointer.from_string(cmd)
exec_args[0].put_pointer(0, exec_cmd)
cmd_args.each_with_index do |arg, i|
exec_args[i + 1].put_pointer(0, arg)
end
[ cmd, exec_args ]
end
public
def self.getpty(*args)
mfdp = Buffer.new :int
name = Buffer.new 1024
#
# All the execv setup is done in the parent, since doing anything other than
# execv in the child after fork is really flakey
#
exec_cmd, exec_args = build_args(args)
pid = LibC.forkpty(mfdp, name, nil, nil)
raise "forkpty failed: #{LibC.strerror(FFI.errno)}" if pid < 0
if pid == 0
LibC.execvp(exec_cmd, exec_args)
exit 1
end
masterfd = mfdp.get_int(0)
rfp = FFI::IO.for_fd(masterfd, "r")
wfp = FFI::IO.for_fd(LibC.dup(masterfd), "w")
if block_given?
yield rfp, wfp, pid
rfp.close unless rfp.closed?
wfp.close unless wfp.closed?
else
[ rfp, wfp, pid ]
end
end
def self.spawn(*args, &block)
self.getpty("/bin/sh", "-c", args[0], &block)
end
end
module LibC
extend FFI::Library
attach_function :close, [ :int ], :int
attach_function :write, [ :int, :buffer_in, :ulong ], :long
attach_function :read, [ :int, :buffer_out, :ulong ], :long
end
PTY.getpty("/bin/ls", "-alR", "/") { |rfd, wfd, pid|
#PTY.spawn("ls -laR /") { |rfd, wfd, pid|
puts "child pid=#{pid}"
while !rfd.eof? && (buf = rfd.gets)
puts "child: '#{buf.strip}'"
end
}
@@ -0,0 +1,20 @@
require 'ffi'
module LibC
extend FFI::Library
ffi_lib FFI::Library::LIBC
callback :qsort_cmp, [ :pointer, :pointer ], :int
attach_function :qsort, [ :pointer, :ulong, :ulong, :qsort_cmp ], :int
end
p = FFI::MemoryPointer.new(:int, 2)
p.put_array_of_int32(0, [ 2, 1 ])
puts "ptr=#{p.inspect}"
puts "Before qsort #{p.get_array_of_int32(0, 2).join(', ')}"
LibC.qsort(p, 2, 4) do |p1, p2|
i1 = p1.get_int32(0)
i2 = p2.get_int32(0)
puts "In block: comparing #{i1} and #{i2}"
i1 < i2 ? -1 : i1 > i2 ? 1 : 0
end
puts "After qsort #{p.get_array_of_int32(0, 2).join(', ')}"
@@ -0,0 +1,36 @@
# See the ffi wiki for how to use FFI with Ractors:
# https://github.com/ffi/ffi/wiki/Ractors
require 'ffi'
class Ractor
# compat with Ruby-3.4 and older
alias value take unless method_defined? :value
end
module LibC
extend FFI::Library
ffi_lib FFI::Library::LIBC
callback :qsort_cmp, [ :pointer, :pointer ], :int
attach_function :qsort, [ :pointer, :ulong, :ulong, :qsort_cmp ], :int
freeze # Freeze the module variables, so that it can be shared across ractors.
end
p = FFI::MemoryPointer.new(:int, 3)
p.put_array_of_int32(0, [ 2, 3, 1 ]) # Write some unsorted data into the memory
# Ractor.make_shareable(p) # freeze the pointer to be shared between ractors instead of copied
puts "main -ptr=#{p.inspect}"
res = Ractor.new(p) do |p|
puts "ractor-ptr=#{p.inspect}"
puts "Before qsort #{p.get_array_of_int32(0, 3).join(', ')}"
LibC.qsort(p, 3, 4) do |p1, p2|
i1 = p1.get_int32(0)
i2 = p2.get_int32(0)
puts "In block: comparing #{i1} and #{i2}"
i1 < i2 ? -1 : i1 > i2 ? 1 : 0
end
puts "After qsort #{p.get_array_of_int32(0, 3).join(', ')}"
end.value
puts "After ractor termination #{p.get_array_of_int32(0, 3).join(', ')}"