title: How to find location of executable on Linux when normal methods fail?
body: <p>In another question, the answer states that on Unixes with <code>/proc</code>, the really straight and reliable way is to <code>readlink(&quot;/proc/self/exe&quot;, buf, bufsize)</code> and it then proceeds to give backup solutions as follows:</p>
  <h1>On Unixes without /proc (i.e. if above fails):</h1>
  <ul>
  <li>If argv[0] starts with &quot;/&quot; (absolute path) this is the path.</li>
  <li>Otherwise if argv[0] contains &quot;/&quot; (relative path) append it to cwd (assuming it hasn't been changed yet).</li>
  </ul>
  <blockquote>
  <pre><code>getcwd(buf, bufsize); strncat(buf, &quot;/&quot;, bufsize-strlen(buf)-1);
  strncat(buf, argv[0], bufsize-strlen(buf)-1);
  </code></pre>
  </blockquote>
  <ul>
  <li>Otherwise search directories in <code>$PATH</code> for executable <code>argv[0]</code>.</li>
  </ul>
  <p>Afterward it may be reasonable to check whether the executable isn't actually a symlink. If it is resolve it relative to the symlink directory.</p>
  <p>Now in my case, unfortunately, none of the above works:</p>
  <ol>
  <li><code>/proc/self/exe exists</code> but fail to <code>readlink()</code> due to permission denied errno 13.</li>
  <li>The <code>argv[0]</code> has no <code>/</code> for absolute or relative path.</li>
  <li>The <code>$PATH</code> does not contain the executable found in <code>argv[0]</code>.</li>
  </ol>
  <p>It appears this issue is faced also when sgid applications run.  In my case, it is not sgid, but an inetd launch.</p>