• 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 path of a process
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting path of a process


  • Subject: Re: Getting path of a process
  • From: ber <email@hidden>
  • Date: Fri, 20 Sep 2002 13:08:06 -0400

You're on the right track David. If you excise the entire
getproclline() function from print.c you should get your path names
where they are available or short names otherwise exactly like
ps(1) except if your not running setuid root you won't get full
paths for many system processes. Perhaps your problem was you
didn't make a copy of the string returned from getproclline() as
it frees the pointer returned to you.

I can send you a working example offlist if you like.

I assume you don't want to just exec ps from within your program
with NSTask or whatever.

brian


On Friday, September 20, 2002, at 07:47 AM, David Remahl wrote:

Dear list,

I need to find the path of a process, actually an application, very early
after it got its process id.

My first approach was using the NSWorkspace method -launchedApplications,
but at that early point the newly launched application had not appeared in
that list yet.

I read this article: <http://developer.apple.com/qa/qa2001/qa1123.html>. I
used the sample code for the function GetBSDProcessList(). I found the newly
launched process.

I then tried using the functions in Processes.h to get the PSN of the
process and get the path from that. It turned out that the PSN hadn't been
assigned either. NSWorkspace is probably layered on top of Processes.h.
Therefore I could not use GetNextProcess() to enumerate the apps either.

Back to the BSD approach. I had a look at the source for ps. Ps uses sysctl
with a mib shown below to get the list of arguments. Unfortunately, I cannot
decipher how the returned arglist is constructed from the ps source
(advanced user commands tarball in the darwin repository)...KERN_PROCARGS is
the only KERN_ define which isn't documented with even a single word in
sysctl.h!

int mib[4];
char *arguments;
int arguments_size = 4096;
int end_argc;

mib[0] = CTL_KERN;
mib[1] = KERN_PROCARGS;
mib[2] = procList[i].kp_proc.p_pid;
mib[3] = 0;

arguments = (char *) malloc(arguments_size);
if (sysctl(mib, 3, arguments, &arguments_size, NULL, 0) < 0) {
NSLog(@"No args found");
}
else
{
end_argc = &arguments[arguments_size];
NSLog(@"%s",&arguments[1]);
}

The above outputs an empty string for about half of the running processes,
and "No args found" for the other half. If no args are found i should fall
back on using the command name.

Does anyone know of a better way to get the full path of a process? If the
started application is TextEdit, then i would like to get
/Applications/TextEdit.app, or
/Applications/TextEdit.app/Contents/MacOS/TextEdit

/ TIA, David Remahl
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Getting path of a process (From: David Remahl <email@hidden>)

  • Prev by Date: Newbie: AppleScript support in Non-Document based app
  • Next by Date: Re: runModalForWindow allows window switching
  • Previous by thread: Getting path of a process
  • Next by thread: NSRunLoop sample code
  • Index(es):
    • Date
    • Thread