Skip to content

Need getParentProcessId() proc #25502

@incansvl

Description

@incansvl

Summary

The std/os library module includes getCurrentProcessId(), but not the equivalent getParentProcessId(). The parent Id is important, e.g. to detect whether the current process was started as a daemon by systemd, when ppid will be 1 on most modern linux systems, so the lack of this feature in the Nim standard library is an issue

Description

The code for getCurrentProcessId() is quite short-

  proc getCurrentProcessId*(): int {.noWeirdTarget.} =
    ## Return current process ID.
    ##
    ## See also:
    ## * `osproc.processID(p: Process) <osproc.html#processID,Process>`_
    when defined(windows):
      proc GetCurrentProcessId(): DWORD {.stdcall, dynlib: "kernel32",
                                          importc: "GetCurrentProcessId".}
      result = GetCurrentProcessId().int
    else:
      result = getpid()

On linux, I believe adapting this to get the parent pid is as simple as changing the last line from getpid() to getppid().

From what I have found online, it is more complex on Windows. One solution (in C) is given here- https://gist.github.com/mattn/253013/d47b90159cf8ffa4d92448614b748aa1d235ebe4.

Unfortunately I am new to Nim and this is well beyond my own limited coding skills.

Alternatives

No response

Examples

No response

Backwards Compatibility

No response

Links

getppid() for Windows (C)
https://gist.github.com/mattn/253013/d47b90159cf8ffa4d92448614b748aa1d235ebe4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions