1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 * Higher-level interfaces to libc::* functions and operating system services.
14 * In general these take and return rust types, use rust idioms (enums,
15 * closures, vectors) rather than C idioms, and do more extensive safety
18 * This module is not meant to only contain 1:1 mappings to libc entries; any
19 * os-interface code that is reasonably useful and broadly applicable can go
20 * here. Including utility routines that merely build on other os code.
22 * We assume the general case is that users do not care, and do not want to
23 * be made to care, which operating system they are on. While they may want
24 * to special case various special cases -- and so we will not _hide_ the
25 * facts of which OS the user is on -- they should be given the opportunity
26 * to write OS-ignorant code by default.
29 #![allow(missing_doc)]
32 use container::Container;
35 use libc::{c_void, c_int};
38 use option::{Some, None, Option};
40 use path::{Path, GenericPath};
43 use result::{Err, Ok, Result};
44 use slice::{Vector, ImmutableVector, MutableVector, OwnedVector};
45 use str::{Str, StrSlice, StrAllocating};
48 use sync::atomics::{AtomicInt, INIT_ATOMIC_INT, SeqCst};
58 /// Delegates to the libc close() function, returning the same return value.
59 pub fn close(fd: int) -> int {
61 libc::close(fd as c_int) as int
65 pub static TMPBUF_SZ : uint = 1000u;
66 static BUF_BYTES : uint = 2048u;
68 /// Returns the current working directory.
70 pub fn getcwd() -> Path {
73 let mut buf = [0 as c_char, ..BUF_BYTES];
75 if libc::getcwd(buf.as_mut_ptr(), buf.len() as libc::size_t).is_null() {
78 Path::new(CString::new(buf.as_ptr(), false))
82 /// Returns the current working directory.
84 pub fn getcwd() -> Path {
86 use libc::GetCurrentDirectoryW;
89 let mut buf = [0 as u16, ..BUF_BYTES];
91 if libc::GetCurrentDirectoryW(buf.len() as DWORD, buf.as_mut_ptr()) == 0 as DWORD {
95 Path::new(str::from_utf16(str::truncate_utf16_at_nul(buf))
96 .expect("GetCurrentDirectoryW returned invalid UTF-16"))
101 use libc::types::os::arch::extra::DWORD;
103 use option::{None, Option, Expect};
106 use slice::{MutableVector, ImmutableVector};
108 use str::{StrSlice, StrAllocating};
112 pub fn fill_utf16_buf_and_decode(f: |*mut u16, DWORD| -> DWORD)
116 let mut n = TMPBUF_SZ as DWORD;
118 let mut done = false;
120 let mut buf = Vec::from_elem(n as uint, 0u16);
121 let k = f(buf.as_mut_ptr(), n);
122 if k == (0 as DWORD) {
125 libc::GetLastError() ==
126 libc::ERROR_INSUFFICIENT_BUFFER as DWORD {
134 let sub = buf.slice(0, k as uint);
135 // We want to explicitly catch the case when the
136 // closure returned invalid UTF-16, rather than
137 // set `res` to None and continue.
138 let s = str::from_utf16(sub)
139 .expect("fill_utf16_buf_and_decode: closure created invalid UTF-16");
140 res = option::Some(s)
147 pub fn as_utf16_p<T>(s: &str, f: |*u16| -> T) -> T {
148 as_mut_utf16_p(s, |t| { f(t as *u16) })
151 pub fn as_mut_utf16_p<T>(s: &str, f: |*mut u16| -> T) -> T {
152 let mut t = s.to_utf16();
153 // Null terminate before passing on.
160 Accessing environment variables is not generally threadsafe.
161 Serialize access through a global lock.
163 fn with_env_lock<T>(f: || -> T) -> T {
164 use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
166 static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
169 let _guard = lock.lock();
174 /// Returns a vector of (variable, value) pairs for all the environment
175 /// variables of the current process.
177 /// Invalid UTF-8 bytes are replaced with \uFFFD. See `str::from_utf8_lossy()`
179 pub fn env() -> Vec<(StrBuf,StrBuf)> {
180 env_as_bytes().move_iter().map(|(k,v)| {
181 let k = str::from_utf8_lossy(k.as_slice()).to_strbuf();
182 let v = str::from_utf8_lossy(v.as_slice()).to_strbuf();
187 /// Returns a vector of (variable, value) byte-vector pairs for all the
188 /// environment variables of the current process.
189 pub fn env_as_bytes() -> Vec<(Vec<u8>,Vec<u8>)> {
192 unsafe fn get_env_pairs() -> Vec<Vec<u8>> {
195 use libc::funcs::extra::kernel32::{
196 GetEnvironmentStringsW,
197 FreeEnvironmentStringsW
199 let ch = GetEnvironmentStringsW();
201 fail!("os::env() failure getting env string from OS: {}",
202 os::last_os_error());
204 // Here, we lossily decode the string as UTF16.
206 // The docs suggest that the result should be in Unicode, but
207 // Windows doesn't guarantee it's actually UTF16 -- it doesn't
208 // validate the environment string passed to CreateProcess nor
209 // SetEnvironmentVariable. Yet, it's unlikely that returning a
210 // raw u16 buffer would be of practical use since the result would
211 // be inherently platform-dependent and introduce additional
212 // complexity to this code.
214 // Using the non-Unicode version of GetEnvironmentStrings is even
215 // worse since the result is in an OEM code page. Characters that
216 // can't be encoded in the code page would be turned into question
218 let mut result = Vec::new();
220 while *ch.offset(i) != 0 {
221 let p = &*ch.offset(i);
222 let len = ptr::position(p, |c| *c == 0);
223 raw::buf_as_slice(p, len, |s| {
224 result.push(str::from_utf16_lossy(s).into_bytes());
228 FreeEnvironmentStringsW(ch);
232 unsafe fn get_env_pairs() -> Vec<Vec<u8>> {
236 fn rust_env_pairs() -> **c_char;
238 let environ = rust_env_pairs();
239 if environ as uint == 0 {
240 fail!("os::env() failure getting env string from OS: {}",
241 os::last_os_error());
243 let mut result = Vec::new();
244 ptr::array_each(environ, |e| {
246 Vec::from_slice(CString::new(e, false).as_bytes_no_nul());
247 result.push(env_pair);
252 fn env_convert(input: Vec<Vec<u8>>) -> Vec<(Vec<u8>, Vec<u8>)> {
253 let mut pairs = Vec::new();
254 for p in input.iter() {
255 let mut it = p.as_slice().splitn(1, |b| *b == '=' as u8);
256 let key = Vec::from_slice(it.next().unwrap());
257 let val = Vec::from_slice(it.next().unwrap_or(&[]));
258 pairs.push((key, val));
263 let unparsed_environ = get_env_pairs();
264 env_convert(unparsed_environ)
270 /// Fetches the environment variable `n` from the current process, returning
271 /// None if the variable isn't set.
273 /// Any invalid UTF-8 bytes in the value are replaced by \uFFFD. See
274 /// `str::from_utf8_lossy()` for details.
278 /// Fails if `n` has any interior NULs.
279 pub fn getenv(n: &str) -> Option<StrBuf> {
280 getenv_as_bytes(n).map(|v| str::from_utf8_lossy(v.as_slice()).to_strbuf())
284 /// Fetches the environment variable `n` byte vector from the current process,
285 /// returning None if the variable isn't set.
289 /// Fails if `n` has any interior NULs.
290 pub fn getenv_as_bytes(n: &str) -> Option<Vec<u8>> {
295 let s = n.with_c_str(|buf| libc::getenv(buf));
299 Some(Vec::from_slice(CString::new(s,
300 false).as_bytes_no_nul()))
307 /// Fetches the environment variable `n` from the current process, returning
308 /// None if the variable isn't set.
309 pub fn getenv(n: &str) -> Option<StrBuf> {
312 use os::win32::{as_utf16_p, fill_utf16_buf_and_decode};
314 fill_utf16_buf_and_decode(|buf, sz| {
315 libc::GetEnvironmentVariableW(u, buf, sz)
323 /// Fetches the environment variable `n` byte vector from the current process,
324 /// returning None if the variable isn't set.
325 pub fn getenv_as_bytes(n: &str) -> Option<Vec<u8>> {
326 getenv(n).map(|s| s.into_bytes())
331 /// Sets the environment variable `n` to the value `v` for the currently running
336 /// Fails if `n` or `v` have any interior NULs.
337 pub fn setenv(n: &str, v: &str) {
340 n.with_c_str(|nbuf| {
341 v.with_c_str(|vbuf| {
342 libc::funcs::posix01::unistd::setenv(nbuf, vbuf, 1);
351 /// Sets the environment variable `n` to the value `v` for the currently running
353 pub fn setenv(n: &str, v: &str) {
356 use os::win32::as_utf16_p;
357 as_utf16_p(n, |nbuf| {
358 as_utf16_p(v, |vbuf| {
359 libc::SetEnvironmentVariableW(nbuf, vbuf);
366 /// Remove a variable from the environment entirely
370 /// Fails (on unix) if `n` has any interior NULs.
371 pub fn unsetenv(n: &str) {
373 fn _unsetenv(n: &str) {
376 n.with_c_str(|nbuf| {
377 libc::funcs::posix01::unistd::unsetenv(nbuf);
383 fn _unsetenv(n: &str) {
386 use os::win32::as_utf16_p;
387 as_utf16_p(n, |nbuf| {
388 libc::SetEnvironmentVariableW(nbuf, ptr::null());
397 /// A low-level OS in-memory pipe.
399 /// A file descriptor representing the reading end of the pipe. Data written
400 /// on the `out` file descriptor can be read from this file descriptor.
402 /// A file descriptor representing the write end of the pipe. Data written
403 /// to this file descriptor can be read from the `input` file descriptor.
407 /// Creates a new low-level OS in-memory pipe.
409 pub fn pipe() -> Pipe {
411 let mut fds = Pipe {input: 0,
413 assert_eq!(libc::pipe(&mut fds.input), 0);
414 return Pipe {input: fds.input, out: fds.out};
418 /// Creates a new low-level OS in-memory pipe.
420 pub fn pipe() -> Pipe {
422 // Windows pipes work subtly differently than unix pipes, and their
423 // inheritance has to be handled in a different way that I do not
424 // fully understand. Here we explicitly make the pipe non-inheritable,
425 // which means to pass it to a subprocess they need to be duplicated
426 // first, as in std::run.
427 let mut fds = Pipe {input: 0,
429 let res = libc::pipe(&mut fds.input, 1024 as ::libc::c_uint,
430 (libc::O_BINARY | libc::O_NOINHERIT) as c_int);
432 assert!((fds.input != -1 && fds.input != 0 ));
433 assert!((fds.out != -1 && fds.input != 0));
434 return Pipe {input: fds.input, out: fds.out};
438 /// Returns the proper dll filename for the given basename of a file.
439 pub fn dll_filename(base: &str) -> StrBuf {
440 format_strbuf!("{}{}{}", consts::DLL_PREFIX, base, consts::DLL_SUFFIX)
443 /// Optionally returns the filesystem path of the current executable which is
444 /// running. If any failure occurs, None is returned.
445 pub fn self_exe_name() -> Option<Path> {
447 #[cfg(target_os = "freebsd")]
448 fn load_self() -> Option<Vec<u8>> {
450 use libc::funcs::bsd44::*;
451 use libc::consts::os::extra::*;
452 let mib = box [CTL_KERN as c_int,
454 KERN_PROC_PATHNAME as c_int, -1 as c_int];
455 let mut sz: libc::size_t = 0;
456 let err = sysctl(mib.as_ptr(), mib.len() as ::libc::c_uint,
457 ptr::mut_null(), &mut sz, ptr::null(),
459 if err != 0 { return None; }
460 if sz == 0 { return None; }
461 let mut v: Vec<u8> = Vec::with_capacity(sz as uint);
462 let err = sysctl(mib.as_ptr(), mib.len() as ::libc::c_uint,
463 v.as_mut_ptr() as *mut c_void, &mut sz, ptr::null(),
465 if err != 0 { return None; }
466 if sz == 0 { return None; }
467 v.set_len(sz as uint - 1); // chop off trailing NUL
472 #[cfg(target_os = "linux")]
473 #[cfg(target_os = "android")]
474 fn load_self() -> Option<Vec<u8>> {
477 match io::fs::readlink(&Path::new("/proc/self/exe")) {
478 Ok(path) => Some(path.into_vec()),
483 #[cfg(target_os = "macos")]
484 fn load_self() -> Option<Vec<u8>> {
486 use libc::funcs::extra::_NSGetExecutablePath;
488 _NSGetExecutablePath(ptr::mut_null(), &mut sz);
489 if sz == 0 { return None; }
490 let mut v: Vec<u8> = Vec::with_capacity(sz as uint);
491 let err = _NSGetExecutablePath(v.as_mut_ptr() as *mut i8, &mut sz);
492 if err != 0 { return None; }
493 v.set_len(sz as uint - 1); // chop off trailing NUL
499 fn load_self() -> Option<Vec<u8>> {
503 use os::win32::fill_utf16_buf_and_decode;
504 fill_utf16_buf_and_decode(|buf, sz| {
505 libc::GetModuleFileNameW(0u as libc::DWORD, buf, sz)
506 }).map(|s| s.into_strbuf().into_bytes())
510 load_self().and_then(Path::new_opt)
513 /// Optionally returns the filesystem path to the current executable which is
514 /// running. Like self_exe_name() but without the binary's name.
515 /// If any failure occurs, None is returned.
516 pub fn self_exe_path() -> Option<Path> {
517 self_exe_name().map(|mut p| { p.pop(); p })
521 * Returns the path to the user's home directory, if known.
523 * On Unix, returns the value of the 'HOME' environment variable if it is set
524 * and not equal to the empty string.
526 * On Windows, returns the value of the 'HOME' environment variable if it is
527 * set and not equal to the empty string. Otherwise, returns the value of the
528 * 'USERPROFILE' environment variable if it is set and not equal to the empty
531 * Otherwise, homedir returns option::none.
533 pub fn homedir() -> Option<Path> {
534 // FIXME (#7188): getenv needs a Vec<u8> variant
535 return match getenv("HOME") {
536 Some(ref p) if !p.is_empty() => Path::new_opt(p.as_slice()),
541 fn secondary() -> Option<Path> {
546 fn secondary() -> Option<Path> {
547 getenv("USERPROFILE").and_then(|p| {
558 * Returns the path to a temporary directory.
560 * On Unix, returns the value of the 'TMPDIR' environment variable if it is
561 * set, otherwise for non-Android it returns '/tmp'. If Android, since there
562 * is no global temporary folder (it is usually allocated per-app), we return
565 * On Windows, returns the value of, in order, the 'TMP', 'TEMP',
566 * 'USERPROFILE' environment variable if any are set and not the empty
567 * string. Otherwise, tmpdir returns the path to the Windows directory.
569 pub fn tmpdir() -> Path {
572 fn getenv_nonempty(v: &str) -> Option<Path> {
585 fn lookup() -> Path {
586 let default = if cfg!(target_os = "android") {
587 Path::new("/data/local/tmp")
592 getenv_nonempty("TMPDIR").unwrap_or(default)
596 fn lookup() -> Path {
597 getenv_nonempty("TMP").or(
598 getenv_nonempty("TEMP").or(
599 getenv_nonempty("USERPROFILE").or(
600 getenv_nonempty("WINDIR")))).unwrap_or(Path::new("C:\\Windows"))
605 * Convert a relative path to an absolute path
607 * If the given path is relative, return it prepended with the current working
608 * directory. If the given path is already an absolute path, return it
611 // NB: this is here rather than in path because it is a form of environment
612 // querying; what it does depends on the process working directory, not just
614 pub fn make_absolute(p: &Path) -> Path {
618 let mut ret = getcwd();
624 /// Changes the current working directory to the specified path, returning
625 /// whether the change was completed successfully or not.
626 pub fn change_dir(p: &Path) -> bool {
630 fn chdir(p: &Path) -> bool {
632 use os::win32::as_utf16_p;
633 return as_utf16_p(p.as_str().unwrap(), |buf| {
634 libc::SetCurrentDirectoryW(buf) != (0 as libc::BOOL)
640 fn chdir(p: &Path) -> bool {
643 libc::chdir(buf) == (0 as c_int)
650 /// Returns the platform-specific value of errno
651 pub fn errno() -> int {
652 #[cfg(target_os = "macos")]
653 #[cfg(target_os = "freebsd")]
654 fn errno_location() -> *c_int {
656 fn __error() -> *c_int;
663 #[cfg(target_os = "linux")]
664 #[cfg(target_os = "android")]
665 fn errno_location() -> *c_int {
667 fn __errno_location() -> *c_int;
675 (*errno_location()) as int
680 /// Returns the platform-specific value of errno
681 pub fn errno() -> uint {
682 use libc::types::os::arch::extra::DWORD;
684 #[link_name = "kernel32"]
686 fn GetLastError() -> DWORD;
690 GetLastError() as uint
694 /// Return the string corresponding to an `errno()` value of `errnum`.
695 pub fn error_string(errnum: uint) -> StrBuf {
696 return strerror(errnum);
699 fn strerror(errnum: uint) -> StrBuf {
700 #[cfg(target_os = "macos")]
701 #[cfg(target_os = "android")]
702 #[cfg(target_os = "freebsd")]
703 fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: libc::size_t)
706 fn strerror_r(errnum: c_int, buf: *mut c_char,
707 buflen: libc::size_t) -> c_int;
710 strerror_r(errnum, buf, buflen)
714 // GNU libc provides a non-compliant version of strerror_r by default
715 // and requires macros to instead use the POSIX compliant variant.
716 // So we just use __xpg_strerror_r which is always POSIX compliant
717 #[cfg(target_os = "linux")]
718 fn strerror_r(errnum: c_int, buf: *mut c_char,
719 buflen: libc::size_t) -> c_int {
721 fn __xpg_strerror_r(errnum: c_int,
723 buflen: libc::size_t)
727 __xpg_strerror_r(errnum, buf, buflen)
731 let mut buf = [0 as c_char, ..TMPBUF_SZ];
733 let p = buf.as_mut_ptr();
735 if strerror_r(errnum as c_int, p, buf.len() as libc::size_t) < 0 {
736 fail!("strerror_r failure");
739 str::raw::from_c_str(p as *c_char).into_strbuf()
744 fn strerror(errnum: uint) -> StrBuf {
745 use libc::types::os::arch::extra::DWORD;
746 use libc::types::os::arch::extra::LPWSTR;
747 use libc::types::os::arch::extra::LPVOID;
748 use libc::types::os::arch::extra::WCHAR;
750 #[link_name = "kernel32"]
752 fn FormatMessageW(flags: DWORD,
762 static FORMAT_MESSAGE_FROM_SYSTEM: DWORD = 0x00001000;
763 static FORMAT_MESSAGE_IGNORE_INSERTS: DWORD = 0x00000200;
765 // This value is calculated from the macro
766 // MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT)
767 let langId = 0x0800 as DWORD;
769 let mut buf = [0 as WCHAR, ..TMPBUF_SZ];
772 let res = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
773 FORMAT_MESSAGE_IGNORE_INSERTS,
781 // Sometimes FormatMessageW can fail e.g. system doesn't like langId,
782 let fm_err = errno();
783 return format!("OS Error {} (FormatMessageW() returned error {})", errnum, fm_err);
786 let msg = str::from_utf16(str::truncate_utf16_at_nul(buf));
788 Some(msg) => format!("OS Error {}: {}", errnum, msg),
789 None => format!("OS Error {} (FormatMessageW() returned invalid UTF-16)", errnum),
795 /// Get a string representing the platform-dependent last error
796 pub fn last_os_error() -> StrBuf {
797 error_string(errno() as uint)
800 static mut EXIT_STATUS: AtomicInt = INIT_ATOMIC_INT;
803 * Sets the process exit code
805 * Sets the exit code returned by the process if all supervised tasks
806 * terminate successfully (without failing). If the current root task fails
807 * and is supervised by the scheduler then any user-specified exit status is
808 * ignored and the process exits with the default failure status.
810 * Note that this is not synchronized against modifications of other threads.
812 pub fn set_exit_status(code: int) {
813 unsafe { EXIT_STATUS.store(code, SeqCst) }
816 /// Fetches the process's current exit code. This defaults to 0 and can change
817 /// by calling `set_exit_status`.
818 pub fn get_exit_status() -> int {
819 unsafe { EXIT_STATUS.load(SeqCst) }
822 #[cfg(target_os = "macos")]
823 unsafe fn load_argc_and_argv(argc: int, argv: **c_char) -> Vec<Vec<u8>> {
826 Vec::from_fn(argc as uint, |i| {
827 Vec::from_slice(CString::new(*argv.offset(i as int),
828 false).as_bytes_no_nul())
833 * Returns the command line arguments
835 * Returns a list of the command line arguments.
837 #[cfg(target_os = "macos")]
838 fn real_args_as_bytes() -> Vec<Vec<u8>> {
840 let (argc, argv) = (*_NSGetArgc() as int,
841 *_NSGetArgv() as **c_char);
842 load_argc_and_argv(argc, argv)
846 #[cfg(target_os = "linux")]
847 #[cfg(target_os = "android")]
848 #[cfg(target_os = "freebsd")]
849 fn real_args_as_bytes() -> Vec<Vec<u8>> {
852 match rt::args::clone() {
854 None => fail!("process arguments not initialized")
859 fn real_args() -> Vec<StrBuf> {
860 real_args_as_bytes().move_iter()
862 str::from_utf8_lossy(v.as_slice()).into_strbuf()
867 fn real_args() -> Vec<StrBuf> {
871 let mut nArgs: c_int = 0;
872 let lpArgCount: *mut c_int = &mut nArgs;
873 let lpCmdLine = unsafe { GetCommandLineW() };
874 let szArgList = unsafe { CommandLineToArgvW(lpCmdLine, lpArgCount) };
876 let args = Vec::from_fn(nArgs as uint, |i| unsafe {
877 // Determine the length of this argument.
878 let ptr = *szArgList.offset(i as int);
880 while *ptr.offset(len as int) != 0 { len += 1; }
882 // Push it onto the list.
883 let opt_s = slice::raw::buf_as_slice(ptr, len, |buf| {
884 str::from_utf16(str::truncate_utf16_at_nul(buf))
886 opt_s.expect("CommandLineToArgvW returned invalid UTF-16")
890 LocalFree(szArgList as *c_void);
897 fn real_args_as_bytes() -> Vec<Vec<u8>> {
898 real_args().move_iter().map(|s| s.into_bytes()).collect()
904 #[link_name="kernel32"]
906 fn GetCommandLineW() -> LPCWSTR;
907 fn LocalFree(ptr: *c_void);
911 #[link_name="shell32"]
913 fn CommandLineToArgvW(lpCmdLine: LPCWSTR, pNumArgs: *mut c_int) -> **u16;
916 /// Returns the arguments which this program was started with (normally passed
917 /// via the command line).
919 /// The arguments are interpreted as utf-8, with invalid bytes replaced with \uFFFD.
920 /// See `str::from_utf8_lossy` for details.
922 pub fn args() -> Vec<StrBuf> {
927 #[allow(missing_doc)]
928 pub fn args() -> ::realstd::vec::Vec<::realstd::strbuf::StrBuf> {
929 ::realstd::os::args()
932 /// Returns the arguments which this program was started with (normally passed
933 /// via the command line) as byte vectors.
934 pub fn args_as_bytes() -> Vec<Vec<u8>> {
938 #[cfg(target_os = "macos")]
940 // These functions are in crt_externs.h.
941 pub fn _NSGetArgc() -> *c_int;
942 pub fn _NSGetArgv() -> ***c_char;
945 // Round up `from` to be divisible by `to`
946 fn round_up(from: uint, to: uint) -> uint {
947 let r = if from % to == 0 {
950 from + to - (from % to)
959 /// Returns the page size of the current architecture in bytes.
961 pub fn page_size() -> uint {
963 libc::sysconf(libc::_SC_PAGESIZE) as uint
967 /// Returns the page size of the current architecture in bytes.
969 pub fn page_size() -> uint {
972 let mut info = mem::uninit();
973 libc::GetSystemInfo(&mut info);
975 return info.dwPageSize as uint;
979 /// A memory mapped file or chunk of memory. This is a very system-specific
980 /// interface to the OS's memory mapping facilities (`mmap` on POSIX,
981 /// `VirtualAlloc`/`CreateFileMapping` on win32). It makes no attempt at
982 /// abstracting platform differences, besides in error values returned. Consider
985 /// The memory map is released (unmapped) when the destructor is run, so don't
986 /// let it leave scope by accident if you want it to stick around.
987 pub struct MemoryMap {
988 /// Pointer to the memory created or modified by this map.
990 /// Number of bytes this map applies to
993 pub kind: MemoryMapKind,
996 /// Type of memory map
997 pub enum MemoryMapKind {
998 /// Virtual memory map. Usually used to change the permissions of a given
999 /// chunk of memory. Corresponds to `VirtualAlloc` on Windows.
1001 /// Virtual memory map. Usually used to change the permissions of a given
1002 /// chunk of memory, or for allocation. Corresponds to `VirtualAlloc` on
1007 /// Options the memory map is created with
1008 pub enum MapOption {
1009 /// The memory should be readable
1011 /// The memory should be writable
1013 /// The memory should be executable
1015 /// Create a map for a specific address range. Corresponds to `MAP_FIXED` on
1018 /// Create a memory mapping for a file with a given fd.
1020 /// When using `MapFd`, the start of the map is `uint` bytes from the start
1023 /// On POSIX, this can be used to specify the default flags passed to
1024 /// `mmap`. By default it uses `MAP_PRIVATE` and, if not using `MapFd`,
1025 /// `MAP_ANON`. This will override both of those. This is platform-specific
1026 /// (the exact values used) and ignored on Windows.
1027 MapNonStandardFlags(c_int),
1030 /// Possible errors when creating a map.
1032 /// ## The following are POSIX-specific
1034 /// fd was not open for reading or, if using `MapWritable`, was not open for
1037 /// fd was not valid
1039 /// Either the address given by `MapAddr` or offset given by `MapOffset` was
1040 /// not a multiple of `MemoryMap::granularity` (unaligned to page size).
1042 /// With `MapFd`, the fd does not support mapping.
1044 /// If using `MapAddr`, the address + `min_len` was outside of the process's
1045 /// address space. If using `MapFd`, the target of the fd didn't have enough
1046 /// resources to fulfill the request.
1048 /// A zero-length map was requested. This is invalid according to
1049 /// [POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mmap.html).
1050 /// Not all platforms obey this, but this wrapper does.
1052 /// Unrecognized error. The inner value is the unrecognized errno.
1054 /// ## The following are win32-specific
1056 /// Unsupported combination of protection flags
1057 /// (`MapReadable`/`MapWritable`/`MapExecutable`).
1059 /// When using `MapFd`, `MapOffset` was given (Windows does not support this
1062 /// When using `MapFd`, there was already a mapping to the file.
1064 /// Unrecognized error from `VirtualAlloc`. The inner value is the return
1065 /// value of GetLastError.
1066 ErrVirtualAlloc(uint),
1067 /// Unrecognized error from `CreateFileMapping`. The inner value is the
1068 /// return value of `GetLastError`.
1069 ErrCreateFileMappingW(uint),
1070 /// Unrecognized error from `MapViewOfFile`. The inner value is the return
1071 /// value of `GetLastError`.
1072 ErrMapViewOfFile(uint)
1075 impl fmt::Show for MapError {
1076 fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
1077 let str = match *self {
1078 ErrFdNotAvail => "fd not available for reading or writing",
1079 ErrInvalidFd => "Invalid fd",
1081 "Unaligned address, invalid flags, negative length or \
1084 ErrNoMapSupport=> "File doesn't support mapping",
1085 ErrNoMem => "Invalid address, or not enough available memory",
1086 ErrUnsupProt => "Protection mode unsupported",
1087 ErrUnsupOffset => "Offset in virtual memory mode is unsupported",
1088 ErrAlreadyExists => "File mapping for specified file already exists",
1089 ErrZeroLength => "Zero-length mapping not allowed",
1090 ErrUnknown(code) => {
1091 return write!(out, "Unknown error = {}", code)
1093 ErrVirtualAlloc(code) => {
1094 return write!(out, "VirtualAlloc failure = {}", code)
1096 ErrCreateFileMappingW(code) => {
1097 return write!(out, "CreateFileMappingW failure = {}", code)
1099 ErrMapViewOfFile(code) => {
1100 return write!(out, "MapViewOfFile failure = {}", code)
1103 write!(out, "{}", str)
1109 /// Create a new mapping with the given `options`, at least `min_len` bytes
1110 /// long. `min_len` must be greater than zero; see the note on
1111 /// `ErrZeroLength`.
1112 pub fn new(min_len: uint, options: &[MapOption]) -> Result<MemoryMap, MapError> {
1117 return Err(ErrZeroLength)
1119 let mut addr: *u8 = ptr::null();
1121 let mut flags = libc::MAP_PRIVATE;
1124 let mut custom_flags = false;
1125 let len = round_up(min_len, page_size());
1127 for &o in options.iter() {
1129 MapReadable => { prot |= libc::PROT_READ; },
1130 MapWritable => { prot |= libc::PROT_WRITE; },
1131 MapExecutable => { prot |= libc::PROT_EXEC; },
1133 flags |= libc::MAP_FIXED;
1137 flags |= libc::MAP_FILE;
1140 MapOffset(offset_) => { offset = offset_ as off_t; },
1141 MapNonStandardFlags(f) => { custom_flags = true; flags = f },
1144 if fd == -1 && !custom_flags { flags |= libc::MAP_ANON; }
1147 libc::mmap(addr as *c_void, len as libc::size_t, prot, flags, fd,
1150 if r.equiv(&libc::MAP_FAILED) {
1151 Err(match errno() as c_int {
1152 libc::EACCES => ErrFdNotAvail,
1153 libc::EBADF => ErrInvalidFd,
1154 libc::EINVAL => ErrUnaligned,
1155 libc::ENODEV => ErrNoMapSupport,
1156 libc::ENOMEM => ErrNoMem,
1157 code => ErrUnknown(code as int)
1166 MapFile(ptr::null())
1172 /// Granularity that the offset or address must be for `MapOffset` and
1173 /// `MapAddr` respectively.
1174 pub fn granularity() -> uint {
1180 impl Drop for MemoryMap {
1181 /// Unmap the mapping. Fails the task if `munmap` fails.
1182 fn drop(&mut self) {
1183 if self.len == 0 { /* workaround for dummy_stack */ return; }
1186 // FIXME: what to do if this fails?
1187 let _ = libc::munmap(self.data as *c_void, self.len as libc::size_t);
1194 /// Create a new mapping with the given `options`, at least `min_len` bytes long.
1195 pub fn new(min_len: uint, options: &[MapOption]) -> Result<MemoryMap, MapError> {
1196 use libc::types::os::arch::extra::{LPVOID, DWORD, SIZE_T, HANDLE};
1198 let mut lpAddress: LPVOID = ptr::mut_null();
1199 let mut readable = false;
1200 let mut writable = false;
1201 let mut executable = false;
1202 let mut fd: c_int = -1;
1203 let mut offset: uint = 0;
1204 let len = round_up(min_len, page_size());
1206 for &o in options.iter() {
1208 MapReadable => { readable = true; },
1209 MapWritable => { writable = true; },
1210 MapExecutable => { executable = true; }
1211 MapAddr(addr_) => { lpAddress = addr_ as LPVOID; },
1212 MapFd(fd_) => { fd = fd_; },
1213 MapOffset(offset_) => { offset = offset_; },
1214 MapNonStandardFlags(..) => {}
1218 let flProtect = match (executable, readable, writable) {
1219 (false, false, false) if fd == -1 => libc::PAGE_NOACCESS,
1220 (false, true, false) => libc::PAGE_READONLY,
1221 (false, true, true) => libc::PAGE_READWRITE,
1222 (true, false, false) if fd == -1 => libc::PAGE_EXECUTE,
1223 (true, true, false) => libc::PAGE_EXECUTE_READ,
1224 (true, true, true) => libc::PAGE_EXECUTE_READWRITE,
1225 _ => return Err(ErrUnsupProt)
1230 return Err(ErrUnsupOffset);
1233 libc::VirtualAlloc(lpAddress,
1235 libc::MEM_COMMIT | libc::MEM_RESERVE,
1239 0 => Err(ErrVirtualAlloc(errno())),
1247 let dwDesiredAccess = match (executable, readable, writable) {
1248 (false, true, false) => libc::FILE_MAP_READ,
1249 (false, true, true) => libc::FILE_MAP_WRITE,
1250 (true, true, false) => libc::FILE_MAP_READ | libc::FILE_MAP_EXECUTE,
1251 (true, true, true) => libc::FILE_MAP_WRITE | libc::FILE_MAP_EXECUTE,
1252 _ => return Err(ErrUnsupProt) // Actually, because of the check above,
1253 // we should never get here.
1256 let hFile = libc::get_osfhandle(fd) as HANDLE;
1257 let mapping = libc::CreateFileMappingW(hFile,
1263 if mapping == ptr::mut_null() {
1264 return Err(ErrCreateFileMappingW(errno()));
1266 if errno() as c_int == libc::ERROR_ALREADY_EXISTS {
1267 return Err(ErrAlreadyExists);
1269 let r = libc::MapViewOfFile(mapping,
1271 ((len as u64) >> 32) as DWORD,
1272 (offset & 0xffff_ffff) as DWORD,
1275 0 => Err(ErrMapViewOfFile(errno())),
1279 kind: MapFile(mapping as *u8)
1286 /// Granularity of MapAddr() and MapOffset() parameter values.
1287 /// This may be greater than the value returned by page_size().
1288 pub fn granularity() -> uint {
1291 let mut info = mem::uninit();
1292 libc::GetSystemInfo(&mut info);
1294 return info.dwAllocationGranularity as uint;
1300 impl Drop for MemoryMap {
1301 /// Unmap the mapping. Fails the task if any of `VirtualFree`,
1302 /// `UnmapViewOfFile`, or `CloseHandle` fail.
1303 fn drop(&mut self) {
1304 use libc::types::os::arch::extra::{LPCVOID, HANDLE};
1305 use libc::consts::os::extra::FALSE;
1306 if self.len == 0 { return }
1311 if libc::VirtualFree(self.data as *mut c_void, 0,
1312 libc::MEM_RELEASE) == 0 {
1313 println!("VirtualFree failed: {}", errno());
1316 MapFile(mapping) => {
1317 if libc::UnmapViewOfFile(self.data as LPCVOID) == FALSE {
1318 println!("UnmapViewOfFile failed: {}", errno());
1320 if libc::CloseHandle(mapping as HANDLE) == FALSE {
1321 println!("CloseHandle failed: {}", errno());
1329 #[cfg(target_os = "linux")]
1331 pub use std::os::arch_consts::ARCH;
1333 pub static FAMILY: &'static str = "unix";
1335 /// A string describing the specific operating system in use: in this
1337 pub static SYSNAME: &'static str = "linux";
1339 /// Specifies the filename prefix used for shared libraries on this
1340 /// platform: in this case, `lib`.
1341 pub static DLL_PREFIX: &'static str = "lib";
1343 /// Specifies the filename suffix used for shared libraries on this
1344 /// platform: in this case, `.so`.
1345 pub static DLL_SUFFIX: &'static str = ".so";
1347 /// Specifies the file extension used for shared libraries on this
1348 /// platform that goes after the dot: in this case, `so`.
1349 pub static DLL_EXTENSION: &'static str = "so";
1351 /// Specifies the filename suffix used for executable binaries on this
1352 /// platform: in this case, the empty string.
1353 pub static EXE_SUFFIX: &'static str = "";
1355 /// Specifies the file extension, if any, used for executable binaries
1356 /// on this platform: in this case, the empty string.
1357 pub static EXE_EXTENSION: &'static str = "";
1360 #[cfg(target_os = "macos")]
1362 pub use std::os::arch_consts::ARCH;
1364 pub static FAMILY: &'static str = "unix";
1366 /// A string describing the specific operating system in use: in this
1368 pub static SYSNAME: &'static str = "macos";
1370 /// Specifies the filename prefix used for shared libraries on this
1371 /// platform: in this case, `lib`.
1372 pub static DLL_PREFIX: &'static str = "lib";
1374 /// Specifies the filename suffix used for shared libraries on this
1375 /// platform: in this case, `.dylib`.
1376 pub static DLL_SUFFIX: &'static str = ".dylib";
1378 /// Specifies the file extension used for shared libraries on this
1379 /// platform that goes after the dot: in this case, `dylib`.
1380 pub static DLL_EXTENSION: &'static str = "dylib";
1382 /// Specifies the filename suffix used for executable binaries on this
1383 /// platform: in this case, the empty string.
1384 pub static EXE_SUFFIX: &'static str = "";
1386 /// Specifies the file extension, if any, used for executable binaries
1387 /// on this platform: in this case, the empty string.
1388 pub static EXE_EXTENSION: &'static str = "";
1391 #[cfg(target_os = "freebsd")]
1393 pub use std::os::arch_consts::ARCH;
1395 pub static FAMILY: &'static str = "unix";
1397 /// A string describing the specific operating system in use: in this
1398 /// case, `freebsd`.
1399 pub static SYSNAME: &'static str = "freebsd";
1401 /// Specifies the filename prefix used for shared libraries on this
1402 /// platform: in this case, `lib`.
1403 pub static DLL_PREFIX: &'static str = "lib";
1405 /// Specifies the filename suffix used for shared libraries on this
1406 /// platform: in this case, `.so`.
1407 pub static DLL_SUFFIX: &'static str = ".so";
1409 /// Specifies the file extension used for shared libraries on this
1410 /// platform that goes after the dot: in this case, `so`.
1411 pub static DLL_EXTENSION: &'static str = "so";
1413 /// Specifies the filename suffix used for executable binaries on this
1414 /// platform: in this case, the empty string.
1415 pub static EXE_SUFFIX: &'static str = "";
1417 /// Specifies the file extension, if any, used for executable binaries
1418 /// on this platform: in this case, the empty string.
1419 pub static EXE_EXTENSION: &'static str = "";
1422 #[cfg(target_os = "android")]
1424 pub use std::os::arch_consts::ARCH;
1426 pub static FAMILY: &'static str = "unix";
1428 /// A string describing the specific operating system in use: in this
1429 /// case, `android`.
1430 pub static SYSNAME: &'static str = "android";
1432 /// Specifies the filename prefix used for shared libraries on this
1433 /// platform: in this case, `lib`.
1434 pub static DLL_PREFIX: &'static str = "lib";
1436 /// Specifies the filename suffix used for shared libraries on this
1437 /// platform: in this case, `.so`.
1438 pub static DLL_SUFFIX: &'static str = ".so";
1440 /// Specifies the file extension used for shared libraries on this
1441 /// platform that goes after the dot: in this case, `so`.
1442 pub static DLL_EXTENSION: &'static str = "so";
1444 /// Specifies the filename suffix used for executable binaries on this
1445 /// platform: in this case, the empty string.
1446 pub static EXE_SUFFIX: &'static str = "";
1448 /// Specifies the file extension, if any, used for executable binaries
1449 /// on this platform: in this case, the empty string.
1450 pub static EXE_EXTENSION: &'static str = "";
1453 #[cfg(target_os = "win32")]
1455 pub use std::os::arch_consts::ARCH;
1457 pub static FAMILY: &'static str = "windows";
1459 /// A string describing the specific operating system in use: in this
1461 pub static SYSNAME: &'static str = "win32";
1463 /// Specifies the filename prefix used for shared libraries on this
1464 /// platform: in this case, the empty string.
1465 pub static DLL_PREFIX: &'static str = "";
1467 /// Specifies the filename suffix used for shared libraries on this
1468 /// platform: in this case, `.dll`.
1469 pub static DLL_SUFFIX: &'static str = ".dll";
1471 /// Specifies the file extension used for shared libraries on this
1472 /// platform that goes after the dot: in this case, `dll`.
1473 pub static DLL_EXTENSION: &'static str = "dll";
1475 /// Specifies the filename suffix used for executable binaries on this
1476 /// platform: in this case, `.exe`.
1477 pub static EXE_SUFFIX: &'static str = ".exe";
1479 /// Specifies the file extension, if any, used for executable binaries
1480 /// on this platform: in this case, `exe`.
1481 pub static EXE_EXTENSION: &'static str = "exe";
1484 #[cfg(target_arch = "x86")]
1486 pub static ARCH: &'static str = "x86";
1489 #[cfg(target_arch = "x86_64")]
1491 pub static ARCH: &'static str = "x86_64";
1494 #[cfg(target_arch = "arm")]
1496 pub static ARCH: &'static str = "arm";
1499 #[cfg(target_arch = "mips")]
1501 pub static ARCH: &'static str = "mips";
1510 use os::{env, getcwd, getenv, make_absolute, args};
1511 use os::{setenv, unsetenv};
1517 pub fn last_os_error() {
1518 debug!("{}", os::last_os_error());
1522 pub fn test_args() {
1524 assert!(a.len() >= 1);
1527 fn make_rand_name() -> StrBuf {
1528 let mut rng = rand::task_rng();
1529 let n = format_strbuf!("TEST{}", rng.gen_ascii_str(10u).as_slice());
1530 assert!(getenv(n.as_slice()).is_none());
1536 let n = make_rand_name();
1537 setenv(n.as_slice(), "VALUE");
1538 assert_eq!(getenv(n.as_slice()), option::Some("VALUE".to_strbuf()));
1542 fn test_unsetenv() {
1543 let n = make_rand_name();
1544 setenv(n.as_slice(), "VALUE");
1545 unsetenv(n.as_slice());
1546 assert_eq!(getenv(n.as_slice()), option::None);
1551 fn test_setenv_overwrite() {
1552 let n = make_rand_name();
1553 setenv(n.as_slice(), "1");
1554 setenv(n.as_slice(), "2");
1555 assert_eq!(getenv(n.as_slice()), option::Some("2".to_strbuf()));
1556 setenv(n.as_slice(), "");
1557 assert_eq!(getenv(n.as_slice()), option::Some("".to_strbuf()));
1560 // Windows GetEnvironmentVariable requires some extra work to make sure
1561 // the buffer the variable is copied into is the right size
1564 fn test_getenv_big() {
1565 let mut s = "".to_strbuf();
1568 s.push_str("aaaaaaaaaa");
1571 let n = make_rand_name();
1572 setenv(n.as_slice(), s.as_slice());
1573 debug!("{}", s.clone());
1574 assert_eq!(getenv(n.as_slice()), option::Some(s));
1578 fn test_self_exe_name() {
1579 let path = os::self_exe_name();
1580 assert!(path.is_some());
1581 let path = path.unwrap();
1582 debug!("{:?}", path.clone());
1584 // Hard to test this function
1585 assert!(path.is_absolute());
1589 fn test_self_exe_path() {
1590 let path = os::self_exe_path();
1591 assert!(path.is_some());
1592 let path = path.unwrap();
1593 debug!("{:?}", path.clone());
1595 // Hard to test this function
1596 assert!(path.is_absolute());
1601 fn test_env_getenv() {
1603 assert!(e.len() > 0u);
1605 let (n, v) = (*p).clone();
1606 debug!("{:?}", n.clone());
1607 let v2 = getenv(n.as_slice());
1608 // MingW seems to set some funky environment variables like
1609 // "=C:=C:\MinGW\msys\1.0\bin" and "!::=::\" that are returned
1610 // from env() but not visible from getenv().
1611 assert!(v2.is_none() || v2 == option::Some(v));
1616 fn test_env_set_get_huge() {
1617 let n = make_rand_name();
1618 let s = "x".repeat(10000).to_strbuf();
1619 setenv(n.as_slice(), s.as_slice());
1620 assert_eq!(getenv(n.as_slice()), Some(s));
1621 unsetenv(n.as_slice());
1622 assert_eq!(getenv(n.as_slice()), None);
1626 fn test_env_setenv() {
1627 let n = make_rand_name();
1630 setenv(n.as_slice(), "VALUE");
1631 assert!(!e.contains(&(n.clone(), "VALUE".to_strbuf())));
1634 assert!(e.contains(&(n, "VALUE".to_strbuf())));
1639 assert!((!Path::new("test-path").is_absolute()));
1642 debug!("Current working directory: {}", cwd.display());
1644 debug!("{:?}", make_absolute(&Path::new("test-path")));
1645 debug!("{:?}", make_absolute(&Path::new("/usr/bin")));
1651 let oldhome = getenv("HOME");
1653 setenv("HOME", "/home/MountainView");
1654 assert!(os::homedir() == Some(Path::new("/home/MountainView")));
1657 assert!(os::homedir().is_none());
1659 for s in oldhome.iter() {
1660 setenv("HOME", s.as_slice())
1668 let oldhome = getenv("HOME");
1669 let olduserprofile = getenv("USERPROFILE");
1672 setenv("USERPROFILE", "");
1674 assert!(os::homedir().is_none());
1676 setenv("HOME", "/home/MountainView");
1677 assert!(os::homedir() == Some(Path::new("/home/MountainView")));
1681 setenv("USERPROFILE", "/home/MountainView");
1682 assert!(os::homedir() == Some(Path::new("/home/MountainView")));
1684 setenv("HOME", "/home/MountainView");
1685 setenv("USERPROFILE", "/home/PaloAlto");
1686 assert!(os::homedir() == Some(Path::new("/home/MountainView")));
1688 for s in oldhome.iter() {
1689 setenv("HOME", s.as_slice())
1691 for s in olduserprofile.iter() {
1692 setenv("USERPROFILE", s.as_slice())
1697 fn memory_map_rw() {
1698 use result::{Ok, Err};
1700 let chunk = match os::MemoryMap::new(16, [
1705 Err(msg) => fail!("{}", msg)
1707 assert!(chunk.len >= 16);
1711 assert!(*chunk.data == 0xBE);
1716 fn memory_map_file() {
1717 use result::{Ok, Err};
1723 fn lseek_(fd: c_int, size: uint) {
1725 assert!(lseek(fd, size as off_t, SEEK_SET) == size as off_t);
1729 fn lseek_(fd: c_int, size: uint) {
1731 assert!(lseek(fd, size as c_long, SEEK_SET) == size as c_long);
1735 let mut path = tmpdir();
1736 path.push("mmap_file.tmp");
1737 let size = MemoryMap::granularity() * 2;
1740 let fd = path.with_c_str(|path| {
1741 open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR)
1744 "x".with_c_str(|x| assert!(write(fd, x as *c_void, 1) == 1));
1747 let chunk = match MemoryMap::new(size / 2, [
1754 Err(msg) => fail!("{}", msg)
1756 assert!(chunk.len > 0);
1760 assert!(*chunk.data == 0xbe);
1765 fs::unlink(&path).unwrap();
1768 // More recursive_mkdir tests are in extra::tempfile