Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Add new property for getting process path #14319

Closed
Giorgi opened this issue Mar 3, 2015 · 30 comments
Closed

Proposal: Add new property for getting process path #14319

Giorgi opened this issue Mar 3, 2015 · 30 comments
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation area-System.Diagnostics.Process backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity

Comments

@Giorgi
Copy link
Contributor

Giorgi commented Mar 3, 2015

Rationale and Usage

In order to get process path you need to use someProcess.MainModule.FileName property. However it suffers from a couple of issue:

  • Accessing MainModule of 64 bit process from a 32 bit one throws an exception
  • You can't access MainModule of elevated process from a non elevated one

As you can see many developers encounter these issues.

I suggest adding a new property which returns the path to executable of the Process. To overcome the issues that I described we can use QueryFullProcessImageName together with PROCESS_QUERY_LIMITED_INFORMATION access right.

Api Proposal

namespace System.Diagnostics
{
     public class  Process : Component
     {
           public string MainModuleFilePath { get; }
     }
}

Open Questions

Should the new property be called MainModuleFilePath or something else? From my own experience most of people use Path when asking question on stackoverflow but @Priya91 suggested calling it MainModuleFilePath as it is returning Path of the MainModule property.

Cross Platform Support

To implement this property on other platforms on Linux we can use readlink to read the value of "/proc/%d/exe" symlink and on OS X we can use proc_pidpath which returns process path given process pid.

@ellismg
Copy link
Contributor

ellismg commented Mar 5, 2015

If we do this, on Linux, we could use /proc//exe to figure out where the on disk image is. There are some caveats listed in the man pages, but in general I think it would be OK. Not sure how (or if) we can do this on OS X.

@mikedn
Copy link
Contributor

mikedn commented Mar 5, 2015

Possibly related discussion/work in progress: dotnet/roslyn#884 (diff)

@stephentoub
Copy link
Member

on Linux, we could use /proc//exe to figure out where the on disk image is.

Yeah, we already use this in our Process implementation on Linux. In order to do path resolution for the file name passed to Start, we use /proc/self/exe to get the path of the exe and look in its directory.

@Giorgi
Copy link
Contributor Author

Giorgi commented Mar 5, 2015

This has two ways for getting process path on Mac:
Programatically retrieving the absolute path of an OS X command-line app

@xanather
Copy link

xanather commented Mar 7, 2015

This is needed! Environment.ExecutingPath? Something like that. Ive always had to play with reflection, AppDomains (doesn't exist in .Net Core), or use System.Windows.Forms.Application.StartupPath (doesn't exist in .Net Core) to get such a simple thing.

@mikedn
Copy link
Contributor

mikedn commented Mar 7, 2015

@xanather This issue is specific to the System.Diagnostics.Process class, you seem to be asking for something different.

AppDomains (doesn't exist in .Net Core),

There is AppContext.

@terrajobst
Copy link
Member

If Process.MainModule will end up throwing on certain platforms, we should also expose a query API, like Process.CanQueryModuleInformation or something along those lines.

@Giorgi
Copy link
Contributor Author

Giorgi commented Mar 12, 2015

It already throws exception if the target process is elevated and the caller isn't

@joshfree joshfree assigned Priya91 and unassigned pallavit Apr 26, 2016
@Priya91
Copy link
Contributor

Priya91 commented Dec 7, 2016

@Giorgi Can you provide an api proposal, a good example here

@Priya91 Priya91 removed their assignment Dec 7, 2016
@Giorgi
Copy link
Contributor Author

Giorgi commented Dec 7, 2016

The api is very simple:

 string Path { get; }

@Priya91
Copy link
Contributor

Priya91 commented Dec 7, 2016

@Giorgi The api proposal needs to be in the form of a speclet as mentioned in the link. You can edit your first comment on this issue, with the api speclet, mainly outlining on the usage scenarios to justify need of the API and the API structure like below. This will help during api review meeting to get to the necessary information quickly rather that perusing the whole issue.

Note: Path doesn't seem very obvious that it's indicating the main module file path, so renamed API below. Open to other suggestions.

namespace System.Diagnostics
{
     public class  Process : Component
     {
           public string MainModuleFilePath { get; }    
     }
}

@Giorgi
Copy link
Contributor Author

Giorgi commented Dec 9, 2016

@Priya91 Post updated.

@Priya91
Copy link
Contributor

Priya91 commented Dec 9, 2016

@Giorgi The proposal looks good, you have mentioned that it's possible to implement on windows, can you also update the post on similar investigations on Linux and OSX. .NETCore supports Linux and OSX, so we want to have data on whether this API returns meaningful value on other platforms as well.

@Giorgi
Copy link
Contributor Author

Giorgi commented Dec 26, 2016

@Priya91 Done!

@danmoseley
Copy link
Member

@Priya91 I marked as ready for review per your note above, feel free to flip back if there is still more feedback.

@terrajobst
Copy link
Member

terrajobst commented Jan 17, 2017

We just reviewed this today:

  • I understand that accessing the main module currently throws. Any reason we cannot make this API work and have other properties on ProcessModule throw if the bitness differs or the process is elevated?
  • The concern being, that adding multiple properties to access the same information might make the API more confusing.

@karelz @Priya91 @Giorgi: could you answer the question?

@mellinoe
Copy link
Contributor

mellinoe commented Jan 17, 2017

I understand that accessing the main module currently throws.

I don't think that's true, barring the caveats in the original post (e.g. 64->32-bit or non-elevated->elevated processes on Windows). We just recently started using it in the corefx tests here, and I do believe that code is being run on all of our platforms.

EDIT: To clarify, I just mean that the API is working outside of Windows. There was some discussion above indicating that it might only work on Windows. There are probably still issues with Windows bitness and elevated-ness.

@Priya91
Copy link
Contributor

Priya91 commented Jan 18, 2017

Any reason we cannot make this API work and have other properties on ProcessModule throw if the bitness differs or the process is elevated?

We can defer throwing exceptions in the implementation to when the properties are accessed, and use a different win32 API for ProcessModule.FileName, but that will cause a behavior change from desktop. And there could be code, for, if MainModule is null assume querying process is elevated so no access, or if MainModule throws, assume different bitness, hence do something else, etc.

@stephentoub
Copy link
Member

stephentoub commented Jan 18, 2017

From an implementation standpoint, in what situations is it possible to provide MainModuleFilePath and not MainModule.FilePath? On macOS we simply construct a single ProcessModule from getting the file path (https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.OSX.cs#L113-L123), so if we can get the latter we can get the former. Similarly for Linux, we get the module information by parsing a virtual file from procfs (https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Linux.cs#L51), so if we can get the file path, we can get the rest of the data.

Or is this just an issue on Windows?

@Giorgi
Copy link
Contributor Author

Giorgi commented Jan 18, 2017

@stephentoub I haven't tried it on other platforms but I do have encountered this issue on Windows. Here is this bug on Microsoft connect with a small repro and workaround: https://connect.microsoft.com/VisualStudio/feedback/details/595717/add-path-property-to-process-class-so-that-it-works-with-elavated-processes-too

@wtgodbe
Copy link
Member

wtgodbe commented Nov 30, 2017

@Giorgi @Priya91 @stephentoub do we still plan on taking this change/was a PR ever put up for this issue? Would like to get closure one way or another.

@stephentoub
Copy link
Member

The API is still marked "api-needs-work", which suggests additional follow-up is necessary before it can be re-reviewed. And the milestone is set as future, which suggests there's no rush here. What kind of closure are you looking for, and why?

@Giorgi
Copy link
Contributor Author

Giorgi commented Nov 30, 2017

@wtgodbe I haven't sent a PR because the suggested api was rejected but I can't think of a better api for getting process path.

I think it would be very intuitive to have a Path property for accessing process path instead of using MainModule.FileName and it would also work in cases when accessing MainModule throws an exception.

@wtgodbe
Copy link
Member

wtgodbe commented Nov 30, 2017

I will leave this open then, if you or someone else can come up with a proposal that addresses @terrajobst concerns then feel free to do so.

madsiberian referenced this issue in madsiberian/corefx Dec 2, 2017
@Giorgi
Copy link
Contributor Author

Giorgi commented Dec 2, 2017

@wtgodbe As @Priya91 has said in her comment addressing @terrajobst concerns isn't possible without introducing breaking change so I don't think there is any other way.

@Giorgi
Copy link
Contributor Author

Giorgi commented Dec 3, 2017

@terrajobst @mellinoe @stephentoub What if we add an extension method in Windows Compatibility Pack ?

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 5.0 milestone Jan 31, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label Apr 2, 2020
@eiriktsarpalis eiriktsarpalis modified the milestones: 5.0, Future Apr 2, 2020
@eiriktsarpalis
Copy link
Member

This seems like a reasonable request, however given there has been limited interest for producing an API proposal, perhaps we should close?

@Giorgi
Copy link
Contributor Author

Giorgi commented Apr 2, 2020

@eiriktsarpalis As I have already said I haven't sent a PR because the suggested api was rejected but I can't think of a better api for getting process path.

Copy link
Contributor

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of our issue cleanup automation.

@dotnet-policy-service dotnet-policy-service bot added backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity labels Oct 3, 2024
Copy link
Contributor

This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

@dotnet-policy-service dotnet-policy-service bot removed this from the Future milestone Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation area-System.Diagnostics.Process backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity
Projects
None yet
Development

No branches or pull requests