Thanks guys. I thought I'd post my solution. The difference between my
solution and the examples on the net that I was able to find is that I
already know the pid -- I just want to see if that pid is still
running, if it's running as root, and if it has a certain name.

Although the Cocoa content is minimal, this might be useful for
someone who might otherwise end up using NSTask and NSPipe to look for
running processes, creating lots of overhead.

Studying sysctl(3), /usr/include/sys/proc.h, and
/usr/include/sys/sysctl.h, here's what I came up with:

#import <sys/types.h>
#import <sys/sysctl.h>
#import <string.h>
#import <stdio.h>
#define MAXCHARS 8
#define MIBSIZE 4
#define PIDFILE "/var/run/some_VPN_pppd.pid"

- (BOOL)isVPNRunning {
        
        char line[MAXCHARS];
        FILE *fp;
        
        if ((fp = fopen (PIDFILE, "r")) == NULL) {
                //pid file does not exist; vpn is not running
                //NSLog (@"vpn is not running 1");
                return NO;
        } else {
                if ((fgets (line, MAXCHARS, fp)) == NULL) {
                        //pid file exists but is weirdly empty or otherwise 
unreadable;
presume vpn is not running
                        fclose (fp);            
                        //NSLog (@"vpn is not running 2");
                        return NO;
                } else {
                        fclose (fp);            
                        int pid = atoi (line);
                        
                        //got the pid from the pid file
                        //get the process table info for that pid
                        
                        int mib[MIBSIZE];                       
                        struct kinfo_proc kp;
                        size_t len = sizeof(kp);
                        
                        mib[0]=CTL_KERN;
                        mib[1]=KERN_PROC;
                        mib[2]=KERN_PROC_PID;
                        mib[3]=pid;
                        
                        if (sysctl(mib, MIBSIZE, &kp, &len, NULL, 0) == -1) {
                                //there was a problem getting the process info; 
presume vpn is not running
                                //NSLog (@"vpn is not running 3");
                                return NO;
                        } else {
                                if (len > 0) {
                                        //there is a running process with that 
pid
                                        //is the process owned by root?
                                        if (kp.kp_eproc.e_pcred.p_svuid == 0) {
                                                
                                                //process is owned by root
                                                //is that process named pppd?
                                                if (!strcmp("pppd", 
kp.kp_proc.p_comm)) {
                                                        //VPN IS RUNNING
                                                        //NSLog (@"vpn is 
running with pid = %d, uid = %d, process name
= %s", pid, kp.kp_eproc.e_pcred.p_svuid, kp.kp_proc.p_comm);
                                                        return YES;             
                                        
                                                } else {
                                                        //process is owned by 
root but is not pppd; vpn is not running
                                                        //NSLog (@"vpn is not 
running 4");
                                                        return NO;
                                                }
                                        } else {
                                                //process is not owned by root; 
vpn is not running
                                                //NSLog (@"vpn is not running 
5");
                                                return NO;
                                        }
                                        
                                } else {
                                        //there was some weird error in sysctl; 
presume vpn is not running
                                        //NSLog (@"vpn is not running 6");
                                        return NO;
                                }
                        }
                }
        }
}                                       


On Sat, Jul 26, 2008 at 8:44 PM, Ken Thomases <[EMAIL PROTECTED]> wrote:
>
> On Jul 26, 2008, at 12:58 AM, Sumner Trammell wrote:
>
>> Hi. A daemon process is running independently of my Cocoa app.  Given a pid
>> file of the daemon process in a known location, say /var/run/somedaemon.pid,
>> I would like my Cocoa app to read that file and check the process table to
>> see if the daemon is actually running. If the daemon IS running, I want my
>> Cocoa app to change its Dock icon.
>> Is there a canonical Cocoa way of doing this?
>
> You can adapt the techniques described in this tech note: 
> http://developer.apple.com/qa/qa2001/qa1123.html
>
> Cheers,
> Ken
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to