std/sys/pal/unix/
args.rs

1//! Global initialization and retrieval of command line arguments.
2//!
3//! On some platforms these are stored during runtime startup,
4//! and on some they are retrieved from the system on demand.
5
6#![allow(dead_code)] // runtime init functions not used during testing
7
8use crate::ffi::{CStr, OsString};
9use crate::os::unix::ffi::OsStringExt;
10use crate::{fmt, vec};
11
12/// One-time global initialization.
13pub unsafe fn init(argc: isize, argv: *const *const u8) {
14    imp::init(argc, argv)
15}
16
17/// Returns the command line arguments
18pub fn args() -> Args {
19    let (argc, argv) = imp::argc_argv();
20
21    let mut vec = Vec::with_capacity(argc as usize);
22
23    for i in 0..argc {
24        // SAFETY: `argv` is non-null if `argc` is positive, and it is
25        // guaranteed to be at least as long as `argc`, so reading from it
26        // should be safe.
27        let ptr = unsafe { argv.offset(i).read() };
28
29        // Some C commandline parsers (e.g. GLib and Qt) are replacing already
30        // handled arguments in `argv` with `NULL` and move them to the end.
31        //
32        // Since they can't directly ensure updates to `argc` as well, this
33        // means that `argc` might be bigger than the actual number of
34        // non-`NULL` pointers in `argv` at this point.
35        //
36        // To handle this we simply stop iterating at the first `NULL`
37        // argument. `argv` is also guaranteed to be `NULL`-terminated so any
38        // non-`NULL` arguments after the first `NULL` can safely be ignored.
39        if ptr.is_null() {
40            // NOTE: On Apple platforms, `-[NSProcessInfo arguments]` does not
41            // stop iterating here, but instead `continue`, always iterating
42            // up until it reached `argc`.
43            //
44            // This difference will only matter in very specific circumstances
45            // where `argc`/`argv` have been modified, but in unexpected ways,
46            // so it likely doesn't really matter which option we choose.
47            // See the following PR for further discussion:
48            // <https://github.com/rust-lang/rust/pull/125225>
49            break;
50        }
51
52        // SAFETY: Just checked that the pointer is not NULL, and arguments
53        // are otherwise guaranteed to be valid C strings.
54        let cstr = unsafe { CStr::from_ptr(ptr) };
55        vec.push(OsStringExt::from_vec(cstr.to_bytes().to_vec()));
56    }
57
58    Args { iter: vec.into_iter() }
59}
60
61pub struct Args {
62    iter: vec::IntoIter<OsString>,
63}
64
65impl !Send for Args {}
66impl !Sync for Args {}
67
68impl fmt::Debug for Args {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        self.iter.as_slice().fmt(f)
71    }
72}
73
74impl Iterator for Args {
75    type Item = OsString;
76    fn next(&mut self) -> Option<OsString> {
77        self.iter.next()
78    }
79    fn size_hint(&self) -> (usize, Option<usize>) {
80        self.iter.size_hint()
81    }
82}
83
84impl ExactSizeIterator for Args {
85    fn len(&self) -> usize {
86        self.iter.len()
87    }
88}
89
90impl DoubleEndedIterator for Args {
91    fn next_back(&mut self) -> Option<OsString> {
92        self.iter.next_back()
93    }
94}
95
96#[cfg(any(
97    target_os = "linux",
98    target_os = "android",
99    target_os = "freebsd",
100    target_os = "dragonfly",
101    target_os = "netbsd",
102    target_os = "openbsd",
103    target_os = "solaris",
104    target_os = "illumos",
105    target_os = "emscripten",
106    target_os = "haiku",
107    target_os = "l4re",
108    target_os = "fuchsia",
109    target_os = "redox",
110    target_os = "vxworks",
111    target_os = "horizon",
112    target_os = "aix",
113    target_os = "nto",
114    target_os = "hurd",
115    target_os = "rtems",
116    target_os = "nuttx",
117))]
118mod imp {
119    use crate::ffi::c_char;
120    use crate::ptr;
121    use crate::sync::atomic::{AtomicIsize, AtomicPtr, Ordering};
122
123    // The system-provided argc and argv, which we store in static memory
124    // here so that we can defer the work of parsing them until its actually
125    // needed.
126    //
127    // Note that we never mutate argv/argc, the argv array, or the argv
128    // strings, which allows the code in this file to be very simple.
129    static ARGC: AtomicIsize = AtomicIsize::new(0);
130    static ARGV: AtomicPtr<*const u8> = AtomicPtr::new(ptr::null_mut());
131
132    unsafe fn really_init(argc: isize, argv: *const *const u8) {
133        // These don't need to be ordered with each other or other stores,
134        // because they only hold the unmodified system-provide argv/argc.
135        ARGC.store(argc, Ordering::Relaxed);
136        ARGV.store(argv as *mut _, Ordering::Relaxed);
137    }
138
139    #[inline(always)]
140    pub unsafe fn init(argc: isize, argv: *const *const u8) {
141        // on GNU/Linux if we are main then we will init argv and argc twice, it "duplicates work"
142        // BUT edge-cases are real: only using .init_array can break most emulators, dlopen, etc.
143        really_init(argc, argv);
144    }
145
146    /// glibc passes argc, argv, and envp to functions in .init_array, as a non-standard extension.
147    /// This allows `std::env::args` to work even in a `cdylib`, as it does on macOS and Windows.
148    #[cfg(all(target_os = "linux", target_env = "gnu"))]
149    #[used]
150    #[unsafe(link_section = ".init_array.00099")]
151    static ARGV_INIT_ARRAY: extern "C" fn(
152        crate::os::raw::c_int,
153        *const *const u8,
154        *const *const u8,
155    ) = {
156        extern "C" fn init_wrapper(
157            argc: crate::os::raw::c_int,
158            argv: *const *const u8,
159            _envp: *const *const u8,
160        ) {
161            unsafe {
162                really_init(argc as isize, argv);
163            }
164        }
165        init_wrapper
166    };
167
168    pub fn argc_argv() -> (isize, *const *const c_char) {
169        // Load ARGC and ARGV, which hold the unmodified system-provided
170        // argc/argv, so we can read the pointed-to memory without atomics or
171        // synchronization.
172        //
173        // If either ARGC or ARGV is still zero or null, then either there
174        // really are no arguments, or someone is asking for `args()` before
175        // initialization has completed, and we return an empty list.
176        let argv = ARGV.load(Ordering::Relaxed);
177        let argc = if argv.is_null() { 0 } else { ARGC.load(Ordering::Relaxed) };
178
179        // Cast from `*mut *const u8` to `*const *const c_char`
180        (argc, argv.cast())
181    }
182}
183
184// Use `_NSGetArgc` and `_NSGetArgv` on Apple platforms.
185//
186// Even though these have underscores in their names, they've been available
187// since the first versions of both macOS and iOS, and are declared in
188// the header `crt_externs.h`.
189//
190// NOTE: This header was added to the iOS 13.0 SDK, which has been the source
191// of a great deal of confusion in the past about the availability of these
192// APIs.
193//
194// NOTE(madsmtm): This has not strictly been verified to not cause App Store
195// rejections; if this is found to be the case, the previous implementation
196// of this used `[[NSProcessInfo processInfo] arguments]`.
197#[cfg(target_vendor = "apple")]
198mod imp {
199    use crate::ffi::{c_char, c_int};
200
201    pub unsafe fn init(_argc: isize, _argv: *const *const u8) {
202        // No need to initialize anything in here, `libdyld.dylib` has already
203        // done the work for us.
204    }
205
206    pub fn argc_argv() -> (isize, *const *const c_char) {
207        unsafe extern "C" {
208            // These functions are in crt_externs.h.
209            fn _NSGetArgc() -> *mut c_int;
210            fn _NSGetArgv() -> *mut *mut *mut c_char;
211        }
212
213        // SAFETY: The returned pointer points to a static initialized early
214        // in the program lifetime by `libdyld.dylib`, and as such is always
215        // valid.
216        //
217        // NOTE: Similar to `_NSGetEnviron`, there technically isn't anything
218        // protecting us against concurrent modifications to this, and there
219        // doesn't exist a lock that we can take. Instead, it is generally
220        // expected that it's only modified in `main` / before other code
221        // runs, so reading this here should be fine.
222        let argc = unsafe { _NSGetArgc().read() };
223        // SAFETY: Same as above.
224        let argv = unsafe { _NSGetArgv().read() };
225
226        // Cast from `*mut *mut c_char` to `*const *const c_char`
227        (argc as isize, argv.cast())
228    }
229}
230
231#[cfg(any(target_os = "espidf", target_os = "vita"))]
232mod imp {
233    use crate::ffi::c_char;
234    use crate::ptr;
235
236    #[inline(always)]
237    pub unsafe fn init(_argc: isize, _argv: *const *const u8) {}
238
239    pub fn argc_argv() -> (isize, *const *const c_char) {
240        (0, ptr::null())
241    }
242}