Note that there are some explanatory texts on larger screens.

plurals
  1. POGet other process' argv in OS X using C
    text
    copied!<p>I want to get other process' argv like ps.</p> <p>I'm using Mac OS X 10.4.11 running on Intel or PowerPC.</p> <p>First, I read code of ps and man kvm, then I wrote some C code.</p> <pre><code>#include &lt;kvm.h&gt; #include &lt;fcntl.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;sys/sysctl.h&gt; #include &lt;paths.h&gt; int main(void) { char errbuf[1024]; kvm_t *kd = kvm_openfiles(_PATH_DEVNULL, NULL, _PATH_DEVNULL, O_RDONLY, errbuf); int num_procs; if (!kd) { fprintf(stderr, "kvm_openfiles failed : %s\n", errbuf); return 0; } struct kinfo_proc *proc_table = kvm_getprocs(kd, KERN_PROC_ALL, 0, &amp;num_procs); for (int i = 0; i &lt; num_procs; i++) { struct kinfo_proc *pproc = &amp;proc_table[i]; char **proc_argv = kvm_getargv(kd, pproc, 0); printf("%p\n", proc_argv); } kvm_close(kd); return 0; } </code></pre> <p>When ran on PowerPC, <code>kvm_getargv()</code> always returned NULL. When ran on Intel, <code>kvm_openfiles()</code> failed with error <code>/dev/mem: No such file or directory</code>.</p> <p>Of cource, I know about permission.</p> <p>Second, I tried sysctl.</p> <pre><code>#include &lt;sys/sysctl.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define pid_of(pproc) pproc-&gt;kp_proc.p_pid int main(void) { int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 }; int buffer_size; sysctl(mib, 4, NULL, &amp;buffer_size, NULL, 0); struct kinfo_proc *result = malloc(buffer_size); sysctl(mib, 4, result, &amp;buffer_size, NULL, 0); int num_procs = buffer_size / sizeof(struct kinfo_proc); for (int i = 0; i &lt; num_procs; i++) { struct kinfo_proc *pproc = result + i; int mib[3] = { CTL_KERN, KERN_PROCARGS, pid_of(pproc) }; // KERN_PROC_ARGS is not defined char *proc_argv; int argv_len; sysctl(mib, 3, NULL, &amp;argv_len, NULL, 0); proc_argv = malloc(sizeof(char) * argv_len); sysctl(mib, 3, proc_argv, &amp;argv_len, NULL, 0); fwrite(proc_argv, sizeof(char), argv_len, stdout); printf("\n"); free(proc_argv); } return 0; } </code></pre> <p>By fwrite, I got argv[0] but argv[1..] are not (environment variables are printed out.)</p> <p>There is no more way to do it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload