help on finding a process by name...

Darkshadow

wandering shadow
Anybody out there know how to do this one programatically? I'm currently using an NSTask to run a shell script that does ps | grep then echo to return if it's running or not, but this creates 4 new processes...this thing polls every 45 seconds, so my process numbers shoot up like no tomorrow. This isn't a bad thing, but it annoys the heck outta me, and if it annoys me, I'm sure it'll annoy someone else if I release it as it is now.

I know Cocoa can't do this, so I'm turnin to good 'ole C to figure it out. I tried sys/proc.h, but I can't even figure out how to fill out the structs and such. Anyone out there done this before? Basically, I'm just trying to re-invent ps myself (I suppose), and then see if the process is running. If anyone knows an easier way than this, let me know! (The process, BTW, is a CLI one).
 
The good ole way was reading /dev/kmem, and following proc structs. Now, there might be a libkmem to do that for you. However, the process digging through /dev/kmem has to execute with effective gid of kmem!

A more modern way is to represent the running processes as directories of proc pseudo filesystem. IIRC, OS X does not offer that capability.

So, you're back to square 1, using ps to get the information for you. BTW, we unixers are used to seeing pids skyrocket and turn over more than once a day.

But, to make your life easier, you can try to hack the ps source, and make it repeat scans every n seconds, so that you can have only one running instance of the helper program--I wouldn't bother, though. Don't underestimate unixers: they are used to putting applications together out of the tools available in the box (/usr/bin :) )
 
Yeah, I got that. Ran the app as setuid root so I had permission to read from /dev/kmem, but the only thing I was able to get was the parent apps name from the struct. Which definitely wasn't what I wanted.

I'm used to the pids rising too...always found it annoying, but more or less got used to it. I'm more thinking of a "Maccer" who isn't used to that sort of thing. Besides, I've always thought it bad coding to make something that runs more than one process when there's an alternative. Plus, any C code I came up with would definitely be faster than calling a shell script via NSTask.

Source to ps would be nice, but anyone know where the heck the source code Apple is using is at? I tried getting the source code for both FreeBSD's ps and OpenBSD's ps, and neither would compile. Go figure.
 
Aha! Perfect. Thanks :) That's exactly what I was looking for. And I gotta smack my head...I was actually there at the opensource part, and <i>didn't</i> click on the Darwin link thinking it had only to do with the kernel. Doh!

Man, OS X's ps is a mess! It even has a variable named 'FIXME' - though that's not even defined in the source...must be from somewhere else.

I can see why I was having problems...have to do stuff with the mach system...sheesh, perhaps I'll be keeping this as a shell script anyway. ;)

I'll ask now - anyone interested in this? If anyone is, I'll post up how I get it working (that is, <b>IF</b> I get it working :p)
 
Back
Top