1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use std::io::Error;
use libc::{self, c_char, c_int, uid_t, size_t, passwd};
#[no_mangle]
pub extern "C" fn u_getuid_ocall() -> uid_t {
unsafe { libc::getuid()}
}
#[no_mangle]
pub extern "C" fn u_environ_ocall() -> *const *const c_char {
extern { static environ: *const *const c_char; }
unsafe { environ }
}
#[no_mangle]
pub extern "C" fn u_getenv_ocall(name: *const c_char) -> *const c_char {
unsafe { libc::getenv(name) }
}
#[no_mangle]
pub extern "C" fn u_setenv_ocall(error: *mut c_int,
name: *const c_char,
value: *const c_char,
overwrite: c_int) -> c_int {
let mut errno = 0;
let ret = unsafe { libc::setenv(name, value, overwrite) };
if ret < 0 {
errno = Error::last_os_error().raw_os_error().unwrap_or(0);
}
if !error.is_null() {
unsafe { *error = errno; }
}
ret
}
#[no_mangle]
pub extern "C" fn u_unsetenv_ocall(error: *mut c_int, name: *const c_char) -> c_int {
let mut errno = 0;
let ret = unsafe { libc::unsetenv(name) };
if ret < 0 {
errno = Error::last_os_error().raw_os_error().unwrap_or(0);
}
if !error.is_null() {
unsafe { *error = errno; }
}
ret
}
#[no_mangle]
pub extern "C" fn u_getcwd_ocall(error: *mut c_int, buf: *mut c_char, size: size_t) -> *mut c_char {
let mut errno = 0;
let ret = unsafe { libc::getcwd(buf, size) };
if ret.is_null() {
errno = Error::last_os_error().raw_os_error().unwrap_or(0);
}
if !error.is_null() {
unsafe { *error = errno; }
}
ret
}
#[no_mangle]
pub extern "C" fn u_chdir_ocall(error: *mut c_int, dir: *const c_char) -> c_int {
let mut errno = 0;
let ret = unsafe { libc::chdir(dir) };
if ret < 0 {
errno = Error::last_os_error().raw_os_error().unwrap_or(0);
}
if !error.is_null() {
unsafe { *error = errno; }
}
ret
}
#[no_mangle]
pub extern "C" fn u_getpwuid_r_ocall(uid: uid_t,
pwd: *mut passwd,
buf: *mut c_char,
buflen: size_t,
passwd_result: *mut *mut passwd) -> c_int {
let ret = unsafe { libc::getpwuid_r(uid, pwd, buf, buflen, passwd_result) };
if ret == 0 {
let pwd_ret = unsafe { *passwd_result };
if !pwd_ret.is_null() {
unsafe {
let mut temp_pwd = &mut *pwd;
let p = -1_isize as usize;
if !temp_pwd.pw_name.is_null() {
temp_pwd.pw_name = usize::checked_sub(temp_pwd.pw_name as _, buf as _).unwrap_or(p) as *mut c_char;
} else {
temp_pwd.pw_name = p as *mut c_char;
}
if !temp_pwd.pw_passwd.is_null() {
temp_pwd.pw_passwd = usize::checked_sub(temp_pwd.pw_passwd as _, buf as _).unwrap_or(p) as *mut c_char;
} else {
temp_pwd.pw_passwd = p as *mut c_char;
}
if !temp_pwd.pw_gecos.is_null() {
temp_pwd.pw_gecos = usize::checked_sub(temp_pwd.pw_gecos as _, buf as _).unwrap_or(p) as *mut c_char;
} else {
temp_pwd.pw_gecos = p as *mut c_char;
}
if !temp_pwd.pw_dir.is_null() {
temp_pwd.pw_dir = usize::checked_sub(temp_pwd.pw_dir as _, buf as _).unwrap_or(p) as *mut c_char;
} else {
temp_pwd.pw_dir = p as *mut c_char;
}
if !temp_pwd.pw_shell.is_null() {
temp_pwd.pw_shell = usize::checked_sub(temp_pwd.pw_shell as _, buf as _).unwrap_or(p) as *mut c_char;
} else {
temp_pwd.pw_shell = p as *mut c_char;
}
}
}
}
ret
}