title: Retrieve names of running processes
body: <p>First off, I know that similar questions have been asked, but the answers provided haven't been very helpful so far (they all recommend one of the following options).</p>
  
  <p>I have a user application that needs to determine if a particular process is running.  Here's what I know about the process:</p>
  
  <ul>
  <li>The name</li>
  <li>The user (<code>root</code>)</li>
  <li>It <em>should</em> already be running, since it's a LaunchDaemon, which means</li>
  <li>Its parent process should be <code>launchd</code> (pid 1)</li>
  </ul>
  
  <p>I've tried several ways to get this, but none have worked so far.  Here's what I've tried:</p>
  
  <ol>
  <li><p>Running <code>ps</code> and parsing the output.  This works, but it's slow (<code>fork</code>/<code>exec</code> is expensive), and I'd like this to be as fast as possible.</p></li>
  <li><p>Using the <code>GetBSDProcessList</code> function <a href="http://developer.apple.com/legacy/mac/library/#qa/qa2001/qa1123.html" rel="noreferrer">listed here</a>.  This also works, but the way in which they say to retrieve the process name (accessing <code>kp_proc.p_comm</code> from each <code>kinfo_proc</code> structure) is flawed.  The resulting <code>char*</code> only contains the first 16 characters of the process name, which can be seen in the definition of the <code>kp_proc</code> structure:</p>
  
  <pre>#define MAXCOMLEN 16 //defined in param.h
  struct extern_proc {  //defined in proc.h
    ...snip...
    char p_comm[MAXCOMLEN+1];
    ...snip...
  };</pre></li>
  <li><p>Using <a href="http://www.opensource.apple.com/source/Libc/Libc-594.1.4/darwin/libproc.h" rel="noreferrer">libProc.h</a> to retrieve process information:</p>
  
  <pre>pid_t pids[1024];
  int numberOfProcesses = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);   
  proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(pids));    
  for (int i = 0; i &lt; numberOfProcesses; ++i) {
    if (pids[i] == 0) { continue; }
    char name[1024];
    proc_name(pids[i], name, sizeof(name));
    printf("Found process: %s\n", name);
  }</pre>
  
  <p>This works, except it has the same flaw as <code>GetBSDProcessList</code>.  Only the first portion of the process name is returned.</p></li>
  <li><p>Using the <a href="http://developer.apple.com/mac/library/documentation/Carbon/Reference/Process_Manager/Reference/reference.html" rel="noreferrer">ProcessManager function</a> in Carbon:</p>
  
  <pre>ProcessSerialNumber psn;
  psn.lowLongOfPSN = kNoProcess;
  psn.highLongOfPSN = 0;
  while (GetNextProcess(&psn) == noErr) {
    CFStringRef procName = NULL;
    if (CopyProcessName(&psn, &procName) == noErr) {
      NSLog(@"Found process: %@", (NSString *)procName);
    }
    CFRelease(procName);
  }</pre>
  
  <p>This does not work.  It only returns process that are registered with the WindowServer (or something like that).  In other words, it only returns apps with UIs, and only for the current user.</p></li>
  <li><p>I can't use <a href="http://developer.apple.com/mac/library/documentation/cocoa/reference/ApplicationKit/Classes/NSWorkspace_Class/Reference/Reference.html#//apple_ref/doc/uid/20000391-BCIEGAJI" rel="noreferrer"><code>-[NSWorkspace launchedApplications]</code></a>, since this must be 10.5-compatible.  In addition, this only returns information about applications that appear in the Dock for the current user.</p></li>
  </ol>
  
  <p>I know that it's <em>possible</em> to retrieve the name of running processes (since <code>ps</code> can do it), but the question is "Can I do it without forking and exec'ing <code>ps</code>?".</p>
  
  <p>Any suggestions?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>After doing a lot more research, I've been unable to find a way to do this.  I found <a href="https://stackoverflow.com/questions/210397/get-other-process-argv-in-os-x-using-c">this SO question</a>, which referred to <a href="http://bitbucket.org/chrismiles/psi/src/tip/src/arch/darwin_process.c" rel="noreferrer">this C file in a python module</a>.  This was really useful in trying to use the <code>KERN_PROCARGS</code> values in a <code>sysctl</code> call.</p>
  
  <p>However, Python module code seemed to be derived from the source to <code>ps</code>, <a href="http://www.opensource.apple.com/source/adv_cmds/adv_cmds-138.1/ps/" rel="noreferrer">which I found here</a>.  <code>ps</code> can somehow get the executable path of every running process, but my best efforts to extract <em>how</em> its doing this have been unsuccessful.  There's a function in <code>print.c</code> called <code>getproclline</code> that seems to be doing the magic, but when I run the same code from within my own command line tool, I'm unable to retrieve the process executable for any processes other than my own.</p>
  
  <p>I'll keep experimenting, but without more conclusive evidence, it looks like @drawnonward's answer is the most correct so far.</p>
  
  <hr>
  
  <p><strong>EDIT (a long time later)</strong></p>
  
  <p>Thanks to the answer <a href="https://stackoverflow.com/a/12468907/115730">pointed to by Quinn Taylor</a>, I've found something that works.  It gets the executable path of each process, and then I can just grab the last path component to get the actual process name.</p>
  
  <pre><code>#import &lt;sys/proc_info.h&gt;
  #import &lt;libproc.h&gt;
  
  int numberOfProcesses = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
  pid_t pids[numberOfProcesses];
  bzero(pids, sizeof(pids));
  proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(pids));
  for (int i = 0; i &lt; numberOfProcesses; ++i) {
      if (pids[i] == 0) { continue; }
      char pathBuffer[PROC_PIDPATHINFO_MAXSIZE];
      bzero(pathBuffer, PROC_PIDPATHINFO_MAXSIZE);
      proc_pidpath(pids[i], pathBuffer, sizeof(pathBuffer));
      if (strlen(pathBuffer) &gt; 0) {
          printf("path: %s\n", pathBuffer);
      }
  }
  </code></pre>