]> git.lizzy.rs Git - rust.git/blobdiff - src/shims/env.rs
Add getpid shim
[rust.git] / src / shims / env.rs
index ae9b8c75145f78a2cfabf1f00dd0e322dadaf07e..a65919286117c1051360c2c92d06459b6eed565f 100644 (file)
@@ -381,7 +381,7 @@ fn chdir(&mut self, path_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
         let target_os = &this.tcx.sess.target.os;
         assert!(
             target_os == "linux" || target_os == "macos",
-            "`getcwd` is only available for the UNIX target family"
+            "`chdir` is only available for the UNIX target family"
         );
 
         let path = this.read_path_from_c_str(this.read_pointer(path_op)?)?;
@@ -465,4 +465,31 @@ fn update_environ(&mut self) -> InterpResult<'tcx> {
 
         Ok(())
     }
+
+    fn getpid(&mut self) -> InterpResult<'tcx, i32> {
+        let this = self.eval_context_mut();
+        let target_os = &this.tcx.sess.target.os;
+        assert!(
+            target_os == "linux" || target_os == "macos",
+            "`getpid` is only available for the UNIX target family"
+        );
+
+        this.check_no_isolation("`getpid`")?;
+
+        // The reason we need to do this wacky of a conversion is because
+        // `libc::getpid` returns an i32, however, `std::process::id()` return an u32.
+        // So we un-do the conversion that stdlib does and turn it back into an i32.
+
+        Ok(std::process::id() as i32)
+    }
+
+    #[allow(non_snake_case)]
+    fn GetCurrentProcessId(&mut self) -> InterpResult<'tcx, u32> {
+        let this = self.eval_context_mut();
+        this.assert_target_os("windows", "GetCurrentProcessId");
+
+        this.check_no_isolation("`GetCurrentProcessId`")?;
+
+        Ok(std::process::id())
+    }
 }