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

Freebsd(jail): process.NewProcess doesnt work if no procfs mounted #1049

Closed
1 task done
stiray opened this issue Mar 17, 2021 · 2 comments · Fixed by #1051
Closed
1 task done

Freebsd(jail): process.NewProcess doesnt work if no procfs mounted #1049

stiray opened this issue Mar 17, 2021 · 2 comments · Fixed by #1051

Comments

@stiray
Copy link

stiray commented Mar 17, 2021

Describe the bug
Important, this is within jail. I don't have freshly installed FreeBSD to check what is situation on fresh install.

Within jails procfs is not mounted by default, but /proc exists.

Due to this, process.NewProcess(pid) doesnt work.

The issue is handling within process_posix.go

func PidExistsWithContext(ctx context.Context, pid int32) (bool, error)

if _, err := os.Stat(common.HostProc()); err == nil { //Means that proc filesystem exist

The issue is that HostProc returns /proc by default, and the directory is really there but if procfs is not in use, it is just empty directory. When stat checks for /proc it is found and presumes procfs is there.

Further down the code it checks if process is within the /proc direcectory and always fails.

_, err := os.Stat(common.HostProc(strconv.Itoa(int(pid))))

Even if you are having handling for systems without procfs mounted, it is never called.

To Reproduce

    _, err := process.NewProcess(int32(os.Getpid()))

Expected behavior
The check for procfs is inadequate, fast fix would be to instead rather check if /proc is empty while the ultimate solution would be to actually check if procfs is mounted. Workaround would be to mount procfs but it would be nice if this wouldnt be a requirement.

Environment (please complete the following information):

  • FreeBSD: [
    FreeBSD build 12.2-RELEASE FreeBSD 12.2-RELEASE r366954 GENERIC amd64
    12.2-RELEASE-p3

Additional context
No cross compiling.

@stiray stiray changed the title Freebsd: process.NewProcess doesnt work if no procfs mounted Freebsd(jail): process.NewProcess doesnt work if no procfs mounted Mar 17, 2021
Lomanic added a commit to Lomanic/gopsutil that referenced this issue Mar 18, 2021
…ecking if pid exists there

Benchmark before this change (process.NewProcess() calls process.PidExistsWithContext()
internally)

    go test -bench=BenchmarkNewProcess github.com/shirou/gopsutil/process
    goos: linux
    goarch: amd64
    pkg: github.com/shirou/gopsutil/process
    BenchmarkNewProcess-4              14722             78751 ns/op
    PASS
    ok      github.com/shirou/gopsutil/process      3.685s

Benchmark with this change applied

    go test -bench=BenchmarkNewProcess github.com/shirou/gopsutil/process
    goos: linux
    goarch: amd64
    pkg: github.com/shirou/gopsutil/process
    BenchmarkNewProcess-4              14835             80180 ns/op
    PASS
    ok      github.com/shirou/gopsutil/process      3.761s
@Lomanic
Copy link
Collaborator

Lomanic commented Mar 18, 2021

Consequence of this change #821

I'm surprised you have a /proc dir on freebsd, but one can indeed mount a procfs on freebsd. https://docs.freebsd.org/en/articles/linux-users/procfs.html https://man.freebsd.org/procfs/

@stiray could you test #1051 in your jail?

shirou added a commit that referenced this issue Mar 21, 2021
[process][posix] Fix #1049 check if procfs is mounted before checking if pid exists there
@shirou
Copy link
Owner

shirou commented Mar 21, 2021

I can confirm it works on my PC. but if @stiray can not work on your jail, please let us know. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants