Skip to content

Commit

Permalink
[#45267] Update ioctl magic numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
GPlaczek committed Jul 19, 2023
1 parent fb10025 commit b8df96e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
8 changes: 2 additions & 6 deletions c_lib/wasi_ext_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,20 +248,16 @@ int wasi_ext_kill(int pid, int sig) {
return -err;
}

int wasi_ext_ioctl(int fd, unsigned long cmd, void *arg, size_t arg_size) {
char *ptr;
asprintf(&ptr, "%p", arg);

int wasi_ext_ioctl(int fd, unsigned int cmd, void *arg) {
JsonNode *root = json_mkobject();
json_append_member(root, "fd", json_mknumber(fd));
json_append_member(root, "cmd", json_mknumber(cmd));

char *serialized = json_stringify(0, root, " ");
json_delete(root);

int err = __syscall("ioctl", serialized, arg, arg_size);
int err = __syscall("ioctl", serialized, arg, 0);

free(ptr);
free(serialized);

return -err;
Expand Down
22 changes: 16 additions & 6 deletions c_lib/wasi_ext_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,23 @@
#ifndef c_bindings_wasi_ext_lib_h_INCLUDED
#define c_bindings_wasi_ext_lib_h_INCLUDED

#define _IOC_NONE 0U
#define _IOC_WRITE 1U
#define _IOC_READ 2U

#define _IOC(rw, maj, func, size) (rw << 30 | size << 16 | maj << 8 | func)
#define _IO(maj, func) _IOC(_IOC_NONE, maj, func, 0)
#define _IOW(maj, func, size) _IOC(_IOC_WRITE, maj, func, size)
#define _IOR(maj, func, size) _IOC(_IOC_READ, maj, func, size)
#define _IOWR(maj, func, size) _IOC(_IOC_WRITE | _IOC_READ, maj, func, size)

#include <stdlib.h>

// Ioctl magic numbers
const unsigned int TIOCGWINSZ = _IOR(1, 0, 8);
const unsigned int TIOCSRAW = _IOW(1, 1, 4);
const unsigned int TIOCSECHO = _IOW(1, 2, 4);

enum RedirectType { READ, WRITE, APPEND };

struct Redirect {
Expand All @@ -20,11 +35,6 @@ struct Env {
const char *val;
};

// Ioctl magic numbers
const uint64_t GET_SCREEN_SIZE = 0;
const uint64_t SET_RAW = 1;
const uint64_t SET_ECHO = 2;

#ifdef HTERM
typedef uint32_t WasiEvents;
const size_t WASI_EVENTS_NUM = 2;
Expand All @@ -49,6 +59,6 @@ int wasi_ext_spawn(const char *, const char *const *, size_t,
const struct Env *, size_t, int, const struct Redirect *,
size_t n_redirects, int *);
int wasi_ext_kill(int, int);
int wasi_ext_ioctl(int, unsigned long, void *, size_t);
int wasi_ext_ioctl(int, unsigned int, void *);

#endif
23 changes: 9 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use std::collections::HashMap;
use std::convert::AsRef;
use std::convert::From;
use std::env;
use std::ffi::{c_ulong, c_void, CString};
use std::ffi::{c_uint, c_void, CString};
use std::fs;
use std::mem;
use std::os::wasi::ffi::OsStrExt;
use std::os::wasi::prelude::RawFd;
use std::path::Path;
Expand All @@ -22,7 +21,8 @@ mod wasi_ext_lib_generated;

#[cfg(feature = "hterm")]
pub use wasi_ext_lib_generated::{
WasiEvents, WASI_EVENTS_MASK_SIZE, WASI_EVENTS_NUM, WASI_EVENT_SIGINT, WASI_EVENT_WINCH,
WasiEvents, TIOCGWINSZ, TIOCSECHO, TIOCSRAW, WASI_EVENTS_MASK_SIZE, WASI_EVENTS_NUM,
WASI_EVENT_SIGINT, WASI_EVENT_WINCH,
};

pub use wasi::SIGNAL_KILL;
Expand All @@ -36,11 +36,11 @@ pub enum Redirect<'a> {
Append((wasi::Fd, &'a str)),
}

#[repr(u64)]
#[repr(u32)]
pub enum IoctlNum {
GetScreenSize = wasi_ext_lib_generated::GET_SCREEN_SIZE,
SetRaw = wasi_ext_lib_generated::SET_RAW,
SetEcho = wasi_ext_lib_generated::SET_ECHO,
GetScreenSize = wasi_ext_lib_generated::TIOCGWINSZ,
SetRaw = wasi_ext_lib_generated::TIOCSRAW,
SetEcho = wasi_ext_lib_generated::TIOCSECHO,
}

enum CStringRedirect {
Expand Down Expand Up @@ -276,17 +276,12 @@ pub fn ioctl<T>(fd: RawFd, command: IoctlNum, arg: Option<&mut T>) -> Result<(),
let result = if let Some(arg) = arg {
unsafe {
let arg_ptr: *mut c_void = arg as *mut T as *mut c_void;
wasi_ext_lib_generated::wasi_ext_ioctl(
fd,
command as c_ulong,
arg_ptr,
mem::size_of::<T>(),
)
wasi_ext_lib_generated::wasi_ext_ioctl(fd, command as c_uint, arg_ptr)
}
} else {
unsafe {
let null_ptr = ptr::null_mut::<T>() as *mut c_void;
wasi_ext_lib_generated::wasi_ext_ioctl(fd, command as c_ulong, null_ptr, 0)
wasi_ext_lib_generated::wasi_ext_ioctl(fd, command as c_uint, null_ptr)
}
};

Expand Down

0 comments on commit b8df96e

Please sign in to comment.