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