Skip to content

feat: reimplement a few more missing functional macros #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions nginx-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ rust-version.workspace = true

[dependencies]

[target.'cfg(not(windows))'.dependencies]
errno = { version = "0.3", default-features = false }

[build-dependencies]
bindgen = "0.71"
cc = "1.2.0"
Expand Down
87 changes: 87 additions & 0 deletions nginx-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,93 @@ impl ngx_module_t {
}
}

/// Returns the error code of the last failed operation (`errno`).
#[inline]
pub fn ngx_errno() -> ngx_err_t {
// SAFETY: GetLastError takes no arguments and reads a thread-local variable
#[cfg(windows)]
let err = unsafe { GetLastError() };

#[cfg(not(windows))]
let err = errno::errno().0;

err as ngx_err_t
}

/// Sets the error code (`errno`).
#[inline]
pub fn ngx_set_errno(err: ngx_err_t) {
#[cfg(windows)]
// SAFETY: SetLastError takes one argument by value and updates a thread-local variable
unsafe {
SetLastError(err as _)
}
#[cfg(not(windows))]
errno::set_errno(errno::Errno(err as _))
}

/// Returns the error code of the last failed sockets operation.
#[inline]
pub fn ngx_socket_errno() -> ngx_err_t {
// SAFETY: WSAGetLastError takes no arguments and reads a thread-local variable
#[cfg(windows)]
let err = unsafe { WSAGetLastError() };

#[cfg(not(windows))]
let err = errno::errno().0;

err as ngx_err_t
}

/// Sets the error code of the sockets operation.
#[inline]
pub fn ngx_set_socket_errno(err: ngx_err_t) {
#[cfg(windows)]
// SAFETY: WSaSetLastError takes one argument by value and updates a thread-local variable
unsafe {
WSASetLastError(err as _)
}
#[cfg(not(windows))]
errno::set_errno(errno::Errno(err as _))
}

/// Returns a non cryptograhpically-secure pseudo-random integer.
#[inline]
pub fn ngx_random() -> core::ffi::c_long {
#[cfg(windows)]
unsafe {
// Emulate random() as Microsoft CRT does not provide it.
// rand() should be thread-safe in the multi-threaded CRT we link to, but will not be seeded
// outside of the main thread.
let x: u32 = ((rand() as u32) << 16) ^ ((rand() as u32) << 8) ^ (rand() as u32);
(0x7fffffff & x) as _
}
#[cfg(not(windows))]
unsafe {
random()
}
}

/// Returns cached timestamp in seconds, updated at the start of the event loop iteration.
///
/// Can be stale when accessing from threads, see [ngx_time_update].
#[inline]
pub fn ngx_time() -> time_t {
// SAFETY: ngx_cached_time is initialized before any module code can run
unsafe { (*ngx_cached_time).sec }
}

/// Returns cached time, updated at the start of the event loop iteration.
///
/// Can be stale when accessing from threads, see [ngx_time_update].
/// A cached reference to the ngx_timeofday() result is guaranteed to remain unmodified for the next
/// NGX_TIME_SLOTS seconds.
#[inline]
pub fn ngx_timeofday() -> &'static ngx_time_t {
// SAFETY: ngx_cached_time is initialized before any module code can run
unsafe { &*ngx_cached_time }
}

/// Add a key-value pair to an nginx table entry (`ngx_table_elt_t`) in the given nginx memory pool.
///
/// # Arguments
Expand Down
Loading