diff options
author | Simon Busch <morphis@gravedo.de> | 2011-02-16 21:39:06 +0100 |
---|---|---|
committer | Simon Busch <morphis@gravedo.de> | 2011-02-16 21:39:06 +0100 |
commit | e15960e29e20cefb00231b2b304ae9e365b0a50f (patch) | |
tree | 46baddfb7379bbff9178de53a76e10f54d8cf559 /libfsobasics | |
parent | c1ddf78bebf62671e0cdd310ed467e27808783c8 (diff) | |
download | cornucopia-e15960e29e20cefb00231b2b304ae9e365b0a50f.tar.gz cornucopia-e15960e29e20cefb00231b2b304ae9e365b0a50f.tar.bz2 cornucopia-e15960e29e20cefb00231b2b304ae9e365b0a50f.zip |
libfsobasics: add own implementation of pidof method
Diffstat (limited to 'libfsobasics')
-rw-r--r-- | libfsobasics/fsobasics/process.vala | 51 | ||||
-rw-r--r-- | libfsobasics/tests/testprocess.vala | 8 |
2 files changed, 59 insertions, 0 deletions
diff --git a/libfsobasics/fsobasics/process.vala b/libfsobasics/fsobasics/process.vala index 83205554..28244a69 100644 --- a/libfsobasics/fsobasics/process.vala +++ b/libfsobasics/fsobasics/process.vala @@ -17,6 +17,8 @@ * */ +using FsoFramework.FileHandling; + /** * @interface FsoFramework.IProcessGuard **/ @@ -34,6 +36,55 @@ public interface FsoFramework.IProcessGuard : GLib.Object } /** + * Some process relevant utility functions + **/ +namespace FsoFramework.Process +{ + private static const string PROC_PATH = "/proc"; + + public Posix.pid_t pidof( string name ) + { + int result = 0; + int pid = 0; + string statfile, pstat, comm, pname; + string[] stat_info; + string[] subdirs = listDirectory( PROC_PATH ); + + foreach ( var dirname in subdirs ) + { + pid = dirname.to_int(); + + // check for invalid pid and ignore them + if ( pid <= 0 ) + continue; + + // Now we have a valid pid, find out more about the process! (see man 5 proc + // for more details) + statfile = @"$(PROC_PATH)/$(pid)/stat"; + if ( !isPresent(statfile) ) + continue; + + pstat = read( statfile ); + stat_info = pstat.split( " " ); + + // validate process command name for correct length + if ( !( stat_info.length >= 2 ) && stat_info[0].length > 2 ) + continue; + + // extract and check process name for the correct one + pname = (stat_info[1])[1:stat_info[1].length-2]; + if ( name.has_prefix( pname ) ) + { + result = pid; + break; + } + } + + return (Posix.pid_t) result; + } +} + +/** * @class FsoFramework.GProcessGuard **/ public class FsoFramework.GProcessGuard : FsoFramework.IProcessGuard, GLib.Object diff --git a/libfsobasics/tests/testprocess.vala b/libfsobasics/tests/testprocess.vala index 257e107f..30286230 100644 --- a/libfsobasics/tests/testprocess.vala +++ b/libfsobasics/tests/testprocess.vala @@ -81,6 +81,13 @@ void test_process_kill_implicit() } //=========================================================================== +void test_pidof() +//=========================================================================== +{ + assert( FsoFramework.Process.pidof( "init" ) == 1 ); +} + +//=========================================================================== void main (string[] args) //=========================================================================== { @@ -90,6 +97,7 @@ void main (string[] args) Test.add_func( "/Process/signals", test_process_signals ); Test.add_func( "/Process/kill/explicit", test_process_kill_explicit ); Test.add_func( "/Process/kill/implicit", test_process_kill_implicit ); + Test.add_func( "/Process/pidof", test_pidof ); Test.run (); } |