@@ -417,22 +417,27 @@ func (c *Conn) listUnitsInternal(f storeFunc) ([]UnitStatus, error) {
417417 return status , nil
418418}
419419
420- func (c * Conn ) getUnitInternal (f storeFunc ) (string , error ) {
420+ // GetUnitByPID returns the unit object path of the unit a process ID
421+ // belongs to. It takes a UNIX PID and returns the object path. The PID must
422+ // refer to an existing system process
423+ func (c * Conn ) GetUnitByPID (ctx context.Context , pid uint32 ) (dbus.ObjectPath , error ) {
421424 var result dbus.ObjectPath
422425
423- err := f (& result )
424-
425- // Nothing in this library actually accepts a dbus.ObjectPath, so it's much
426- // more useful as a name
427- name := unitName (result )
426+ err := c .sysobj .CallWithContext (ctx , "org.freedesktop.systemd1.Manager.GetUnitByPID" , 0 , pid ).Store (& result )
428427
429- return name , err
428+ return result , err
430429}
431430
432- // GetUnitByPIDContext returns the unit name for a given PID. The PID must refer
433- // to an existing system process
434- func (c * Conn ) GetUnitByPIDContext (ctx context.Context , pid uint32 ) (string , error ) {
435- return c .getUnitInternal (c .sysobj .CallWithContext (ctx , "org.freedesktop.systemd1.Manager.GetUnitByPID" , 0 , pid ).Store )
431+ // GetUnitNameByPID returns the name of the unit a process ID belongs to. It
432+ // takes a UNIX PID and returns the object path. The PID must refer to an
433+ // existing system process
434+ func (c * Conn ) GetUnitNameByPID (ctx context.Context , pid uint32 ) (string , error ) {
435+ path , err := c .GetUnitByPID (ctx , pid )
436+ if err != nil {
437+ return "" , err
438+ }
439+
440+ return unitName (path ), nil
436441}
437442
438443// Deprecated: use ListUnitsContext instead.
0 commit comments