Giter Site home page Giter Site logo

process_path's Introduction

process_path

A Rust library to get the path of the currently executing process or the the current dynamic library.

The latter is particularly useful for ‘plug-in‘ type dynamic libraries that need to load resources stored relative to the location of the library in the file system.

Usage

Add this to your Cargo.toml:

[dependencies]
process_path = "0.1.4"

and this to your crate root:

use process_path;

Example

This program prints its path to stdout:

use process_path::get_executable_path;

fn main() {
    let path = get_executable_path();
    match path {
        None => println!("The process path could not be determined"),
        Some(path) => println!("{:?}", path)
    }
}

Supported Platforms

Platform Underlying API get_executable_path() get_dylib_path()
Linux readlink(/proc/self/exe) dladdr()
FreeBSD sysctl(3) or readlink(/proc/curproc/file) dladdr()
NetBSD readlink(/proc/curproc/exe) dladdr()
DragonflyBSD readlink(/proc/curproc/file) dladdr()
macOS _NSGetExecutablePath() dladdr()
Windows GetModuleFileName() GetModuleHandleEx()
illumos readlink(/proc/self/exe) dladdr()

License

Copyright Wesley Wiser and process_path contributors.

Licensed under either of

at your option.

process_path's People

Contributors

conradkleinespel avatar ifurther avatar justinrlle avatar pkubaj avatar talagrand avatar toasterson avatar virtualritz avatar wesleywiser avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

process_path's Issues

Build error

    Compiling process_path v0.1.4
error[E0433]: failed to resolve: use of undeclared crate or module `os`
  --> /home/builder/.cargo/registry/src/github.com-1ecc6299db9ec823/process_path-0.1.4/src/lib.rs:58:5
   |
58 |     os::get_executable_path()
   |     ^^ use of undeclared crate or module `os`

error[E0433]: failed to resolve: use of undeclared crate or module `nix`
  --> /home/builder/.cargo/registry/src/github.com-1ecc6299db9ec823/process_path-0.1.4/src/lib.rs:71:9
   |
71 |         nix::get_dylib_path()
   |         ^^^ use of undeclared crate or module `nix`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `process_path` due to 2 previous errors
warning: build failed, waiting for other jobs to finish...

Some little refactorings

Hi, I saw the other day your post on /r/rust, and I have a few remarks which could simplify the code (not much, but still, I think they're may be more idiomatic).

The first one is one the exported function get_executable_path. Instead of exporting a function which calls always the same function (os::get_executable_path()), you just re-export it:

// before
pub fn get_executable_path() -> Option<PathBuf> {
    os::get_executable_path()
}
// after
pub use os::get_executable_path;

In fact you could do this when you rename the different modules, but I don't think it adds much:

#[cfg(target_os = "linux")]
// before
use linux as os;
// after
pub use linux::get_executable_path;

But you use the solutions that suits you best, of course!

I have a second remark, and it's about the platforms. Even though compiling winapi and kernel32 on another platform than Windows won't be compiled, they will still be fetched, and it's the same for the libc depencie. You just need to add the right #[cfg(...)] before the extern crate ..., and specify in the Cargo.toml the dependencies to be platform dependent:

[target.'cfg(windows)'.dependencies]
winapi = "0.2"
kernel32-sys = "0.2"
[taget.'cfg(target_os = "linux")'.dependencies]
libc = "0.2"

I haven't tested these "fixes", but they should work, and show how elegant Rust can be!

Support for OpenBSD

After some research, I found out that OpenBSD developers don't provide a reliable API to retrieve the full path of the executable file used to load the current process, and they don't think implementing this feature is worthwhile.

I'll keep this issue open just as reminder of the reasons why OpenBSD is not supported by this crate.

Compiling on FreeBSD arm64 fails

Hello,

I'm having build failure with process_path-0.1.4.crate on FreeBSD 13.1-RELEASE-p2 arm64 arch and the program that uses this dependency is lapce:

Compiling process_path v0.1.4
     Running `CARGO=/usr/local/bin/cargo CARGO_CRATE_NAME=process_path CARGO_MANIFEST_DIR=/wrkdirs/usr/ports/editors/lapce/work/lapce-0.2.0/cargo-crates/process_path-0.1.4 CARGO_PKG_AUTHORS='Wesley Wiser <[email protected]>:Moritz Moeller <[email protected]>' CARGO_PKG_DESCRIPTION='Gets the path of the currently executing process or dynamic library.' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE=MIT/Apache-2.0 CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=process_path CARGO_PKG_REPOSITORY='https://github.com/wesleywiser/process_path' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.4 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=4 CARGO_PKG_VERSION_PRE='' LD_LIBRARY_PATH='/wrkdirs/usr/ports/editors/lapce/work/target/release/deps:/usr/local/lib' /usr/local/bin/rustc --crate-name process_path --edition=2018 /wrkdirs/usr/ports/editors/lapce/work/lapce-0.2.0/cargo-crates/process_path-0.1.4/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=dep-info,metadata,link -C opt-level=2 -C embed-bitcode=no -C metadata=7bca79f83b5b1e43 -C extra-filename=-7bca79f83b5b1e43 --out-dir /wrkdirs/usr/ports/editors/lapce/work/target/aarch64-unknown-freebsd/release/deps --target aarch64-unknown-freebsd -C linker=cc -L dependency=/wrkdirs/usr/ports/editors/lapce/work/target/aarch64-unknown-freebsd/release/deps -L dependency=/wrkdirs/usr/ports/editors/lapce/work/target/release/deps --extern libc=/wrkdirs/usr/ports/editors/lapce/work/target/aarch64-unknown-freebsd/release/deps/liblibc-a111083825407380.rmeta --cap-lints warn -C link-arg=-fstack-protector-strong -C link-arg=-L/usr/local/lib`
error[E0308]: mismatched types
   --> /wrkdirs/usr/ports/editors/lapce/work/lapce-0.2.0/cargo-crates/process_path-0.1.4/src/bsd.rs:38:35
    |
38  |         let len = unsafe { strlen(buf.as_ptr() as *const i8) };
    |                            ------ ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `i8`
    |                            |
    |                            arguments to this function are incorrect
    |
    = note: expected raw pointer `*const u8`
               found raw pointer `*const i8`
note: function defined here
   --> /wrkdirs/usr/ports/editors/lapce/work/lapce-0.2.0/cargo-crates/libc-0.2.132/src/unix/mod.rs:534:12
    |
534 |     pub fn strlen(cs: *const c_char) -> size_t;
    |            ^^^^^^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `process_path` due to previous error

Caused by:
  process didn't exit successfully: `CARGO=/usr/local/bin/cargo CARGO_CRATE_NAME=process_path CARGO_MANIFEST_DIR=/wrkdirs/usr/ports/editors/lapce/work/lapce-0.2.0/cargo-crates/process_path-0.1.4 CARGO_PKG_AUTHORS='Wesley Wiser <[email protected]>:Moritz Moeller <[email protected]>' CARGO_PKG_DESCRIPTION='Gets the path of the currently executing process or dynamic library.' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE=MIT/Apache-2.0 CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=process_path CARGO_PKG_REPOSITORY='https://github.com/wesleywiser/process_path' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.4 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=4 CARGO_PKG_VERSION_PRE='' LD_LIBRARY_PATH='/wrkdirs/usr/ports/editors/lapce/work/target/release/deps:/usr/local/lib' /usr/local/bin/rustc --crate-name process_path --edition=2018 /wrkdirs/usr/ports/editors/lapce/work/lapce-0.2.0/cargo-crates/process_path-0.1.4/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=dep-info,metadata,link -C opt-level=2 -C embed-bitcode=no -C metadata=7bca79f83b5b1e43 -C extra-filename=-7bca79f83b5b1e43 --out-dir /wrkdirs/usr/ports/editors/lapce/work/target/aarch64-unknown-freebsd/release/deps --target aarch64-unknown-freebsd -C linker=cc -L dependency=/wrkdirs/usr/ports/editors/lapce/work/target/aarch64-unknown-freebsd/release/deps -L dependency=/wrkdirs/usr/ports/editors/lapce/work/target/release/deps --extern libc=/wrkdirs/usr/ports/editors/lapce/work/target/aarch64-unknown-freebsd/release/deps/liblibc-a111083825407380.rmeta --cap-lints warn -C link-arg=-fstack-protector-strong -C link-arg=-L/usr/local/lib` (exit status: 1)
warning: build failed, waiting for other jobs to finish...
*** Error code 101

full log

Same issue is at lapce/lapce#1185

Thanks,
Nuno Teixeira

support for illumos

This package is currently blocking building lapce/lapce on illumos. This issue is to track fixing this issue

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.