• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Getting process table info from within a Cocoa app
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting process table info from within a Cocoa app


  • Subject: Re: Getting process table info from within a Cocoa app
  • From: "Sumner Trammell" <email@hidden>
  • Date: Mon, 28 Jul 2008 13:50:41 -0400

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@hidden> 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 (email@hidden)

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:

This email sent to email@hidden

References: 
 >Getting process table info from within a Cocoa app (From: "Sumner Trammell" <email@hidden>)
 >Re: Getting process table info from within a Cocoa app (From: Ken Thomases <email@hidden>)

  • Prev by Date: Re: [Newbie] Communication between two Views?
  • Next by Date: Re: NSTimer and a problem with document-based apps
  • Previous by thread: Re: Getting process table info from within a Cocoa app
  • Next by thread: How to locate coding examples for specified commands [Newbie]
  • Index(es):
    • Date
    • Thread