Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2017-11-17
04:56 • Ticket [1224888fff] possible to have an empty info nameofexecutable status s... artifact: 6c095dcca3 user: aspect
2017-11-16
12:55 • Ticket [1224888fff]: 3 changes artifact: 70c43eadbe user: aspect
2015-03-17
20:32 • Ticket [1224888fff]: 4 changes artifact: 14fc56c7d7 user: dkf
2015-02-04
18:12 • Ticket [1224888fff]: 4 changes artifact: 0a541b8c2d user: drh
11:01 • Ticket [1224888fff]: 3 changes artifact: a3b72962c5 user: dkf
2015-01-22
21:02 • Ticket [1224888fff]: 3 changes artifact: 0f3d73eba1 user: dgp
20:04 • Ticket [1224888fff]: 3 changes artifact: da49855214 user: jan.nijtmans
2015-01-13
14:34 • Ticket [1224888fff]: 4 changes artifact: 0483fa57cb user: dgp
2015-01-07
23:30 • Ticket [1224888fff]: 6 changes artifact: 2781718882 user: dkf
2011-11-01
03:16 • Ticket [1224888fff]: 4 changes artifact: 7b60780896 user: dgp
02:43 • Ticket [1224888fff]: 4 changes artifact: df88dd79e3 user: nijtmans
00:14 • Ticket [1224888fff]: 4 changes artifact: 2157dbb6b6 user: dgp
2011-10-18
00:34 • Ticket [1224888fff]: 4 changes artifact: 175b1a8996 user: dgp
2011-10-14
04:04 • Ticket [1224888fff]: 4 changes artifact: ca3864f4a0 user: dkf
01:51 • Ticket [1224888fff]: 4 changes artifact: 73bfd37afa user: dgp
01:47 • Ticket [1224888fff]: 4 changes artifact: 3c01b98d61 user: dgp
2011-10-13
19:17 • Ticket [1224888fff]: 5 changes artifact: ebfbadffa6 user: dkf
17:58 • Ticket [1224888fff]: 5 changes artifact: ea34728bb4 user: dkf
17:54 • Ticket [1224888fff]: 4 changes artifact: 68373a0c50 user: dkf
2011-10-12
19:19 • Ticket [1224888fff]: 4 changes artifact: 234a2c3285 user: rkeene
17:43 • Ticket [1224888fff]: 4 changes artifact: e18a498fc0 user: stevenaaus
16:49 • Ticket [1224888fff]: 4 changes artifact: 7a54891f43 user: dkf
2011-10-11
08:39 • Ticket [1224888fff]: 4 changes artifact: c177c97a6b user: stevenaaus
2009-03-21
00:59 • Ticket [1224888fff]: 4 changes artifact: 57ec9c8334 user: rkeene
2005-07-19
22:40 • Ticket [1224888fff]: 4 changes artifact: 6ddca0d7b2 user: davidw
2005-06-23
15:38 • Ticket [1224888fff]: 4 changes artifact: 5b204b4665 user: das
2005-06-22
05:45 • Ticket [1224888fff]: 4 changes artifact: 0c23aab189 user: dkf
05:07 • Ticket [1224888fff]: 4 changes artifact: 92755af9ca user: dkf
04:30 • Ticket [1224888fff]: 4 changes artifact: a316c04030 user: patthoyts
00:23 • Ticket [1224888fff]: 4 changes artifact: 6378be75f7 user: dgp
2005-06-21
23:35 • Ticket [1224888fff]: 4 changes artifact: 22e155a333 user: davidw
22:52 • Ticket [1224888fff]: 4 changes artifact: 1d1946267c user: gwlester
22:28 • Ticket [1224888fff]: 4 changes artifact: ff67465fd6 user: dgp
22:25 • Ticket [1224888fff]: 4 changes artifact: 8c3c3296b4 user: jcw
22:24 • Ticket [1224888fff]: 4 changes artifact: 2f0d6e5f5d user: dgp
21:43 • Ticket [1224888fff]: 4 changes artifact: 4e2f2cc3b5 user: davidw
14:29 • New ticket [1224888fff]. artifact: e4b3e5ab35 user: davidw

possible to have an empty info nameofexecutable

Open1224888fffffffffffffffffffffffffffffffffCreated 21 years agoUpdated 9 years ago
Descriptionplain2005-06-21 14:29:27
How to reproduce:

1) Make a local copy of tclsh called something
*besides* tclsh or anything else that is in your path.
 Make sure that '.' is not in your path.

2) Compile this, using the right paths and name of the
executable for your environment:

#include <unistd.h>

int main(int argc, char *argv[], char *envp[]) {
    char *myargv[] = {"mytclsh", NULL};
    execve("/home/davidw/workshop/protect/mytclsh",
myargv, envp);
}

3) Run it, try 'info name' - it's empty.

Here's why:

5454  execve("/home/davidw/workshop/protect/mytclsh",
["mytclsh"], [/* 29 vars *
/]) = 0

argv0 has no / in it, and mytclsh is not in PATH, so we
hit tclUnixFile.c line 123, and thus have no info
nameofexecutable.

I found this bug while trying to get starpacks working
as CGI's under Apache.  The execve program above is a
recreation of how Apache launches CGI's.

Thanks,
Dave
davidwplain2005-06-21 21:43:36
Logged In: YES 
user_id=240

Quick patch inline:

This seems to fix the problem, but I don't know if it causes
others.

--- tclUnixFile.c       1 Dec 2004 23:18:55 -0000       1.44
+++ tclUnixFile.c       21 Jun 2005 14:42:29 -0000
@@ -120,8 +120,12 @@
            p++;
        }
     }
-    TclSetObjNameOfExecutable(Tcl_NewObj(), NULL);
-    goto done;
+    /* It's not in the path, so just use it in the current
working
+     * directory. */
+    Tcl_DStringSetLength(&buffer, 0);
+    name = Tcl_DStringAppend(&buffer, "./", -1);
+    name = Tcl_DStringAppend(&buffer, argv0, -1);
+    goto gotName;
dgpplain2005-06-21 22:24:12
Logged In: YES 
user_id=80530


hmmm....

the proposed patch takes effect
only after the PATH has been
searched.

So whenever a program by the
same truncated name *is* on
the PATH, Tcl will find the wrong one.

Seems that Apache really ought to
pass the absolute name in argv0.
jcwplain2005-06-21 22:25:38
Logged In: YES 
user_id=1983

Great catch!

Possible solutions: on Linux and systems which support /proc, you could get at 
the exe's own path via "/proc/self/exe" which is a symlink pointing to the right file.

Another way out might be http://autopackage.org/docs/binreloc/

As workaround, I s'pose one could use full pathnames in apache?

-jcw
dgpplain2005-06-21 22:28:39
Logged In: YES 
user_id=80530


and on the other side of things,
TclpFindExecutable() comments
say [info nameof] will be set to NULL
if the executable can't be found.

Won't this patch prevent that by
always supplying ./foo as a 
(possibly false) last resort ?
gwlesterplain2005-06-21 22:52:56
Logged In: YES 
user_id=121110

While this bug appears to be a Unix only, the correct
behavior needs to be verified on Windows and, if applicable,
MacOsX.
davidwplain2005-06-21 23:35:13
Logged In: YES 
user_id=240

The patch was a quick hack and the objections to it are
quite valid... I'm sure we can come up with something better.

However, even if Apache isn't doing things quite right, I
don't think it's egregiously wrong, either, and I think that
Tcl ought to be "liberal with what it accepts" as a general
principle.

How does this sound for an algorithm:

1) If argv0 has slashes in it, we are ok, we just use that.

2) If there are no slashes, and a file that matches
pwd+argv0 exists and is executable, use that.

3) Search the PATH.

4) Can't figure anything out, so just make it NULL.
dgpplain2005-06-22 00:23:25
Logged In: YES 
user_id=80530


the thing I dislike about that
is its implicit assumption that
"." is first on the PATH.

For executables actually launched
from the shell (still the vast majority,
right?), that can give false answers.

If we have no choice but to give
a false answer to one or the other,
I'd give it to Apache.
patthoytsplain2005-06-22 04:30:16
Logged In: YES 
user_id=202636

To reply to gwlester's point - this isn't really an issue on
Windows as on windows there is a reliable way to find the
path to the binary using GetModuleFilename() and this is
used in win/tclAppInit.c to set argv[0]  appropriately.
dkfplain2005-06-22 05:07:19
Logged In: YES 
user_id=79902

I'd be tempted to put the search of the PATH first too.

OTOH, doing a readlink("/proc/self/exe") would be better on
those OSes that support it (alas, neither Solaris or Irix as
far as I can see) though if we do that, it perhaps ought to
be a feature searched for using configure? (We'd need a
fallback anyway in case /proc isn't mounted for some odd
reason.)
dkfplain2005-06-22 05:45:38
Logged In: YES 
user_id=79902

FreeBSD (of uncertain vintage but probably fairly old)
vintage supports the following readlink() target:
  /proc/[pid]/file
(no /proc/self, different name inside)
dasplain2005-06-23 15:38:17
Logged In: YES 
user_id=90580

Mac OS X 10.2 and later has 
#include <mach-o/dyld.h>
int _NSGetExecutablePath(char *buf, unsigned long *bufsize);
davidwplain2005-07-19 22:40:13
Logged In: YES 
user_id=240

The apache folks seem to be of the opinion that that system
behaves like it does as historical accident.  However, they
don't really regard it as a bug, and aren't about to change
it (who knows who may depend on it at this point), so it
doesn't look as if we can shuffle this problem off to them.
rkeeneplain2009-03-21 00:59:58
This is still a problem for me.  I would like TclpFindExecutable() to be more robust and look for OS-specific ways to determine the executable name.

Something similar to:

     const char *exepaths[] = { "/proc/self/exe", "/proc/%lu/exe",  "/proc/%lu/file", "/proc/%lu/object/a.out" };
     char pathbuf[PATH_MAX + 1], pathbuf_copy[PATH_MAX + 1];
     int pathbuf_len;
     int exefileexists, recursedepth, idx;
 
     if (name == NULL || *name == '\0') {
         /* Attempt to use "/proc/self/exe" to determine where we are */
         for (idx = 0; idx < (sizeof(exepaths) / sizeof(*exepaths)); idx++) {
             if (strchr(exepaths[idx], '%') == NULL) {
                 sprintf(pathbuf_copy, exepaths[idx]);
             } else {
                 sprintf(pathbuf_copy, exepaths[idx], (unsigned long) getpid());
             }
             
             exefileexists = access(pathbuf_copy, F_OK);
             
             if (exefileexists == 0) {
                 for (recursedepth = 0; recursedepth < 100; recursedepth++) {
                    pathbuf_len = readlink(pathbuf_copy, pathbuf, sizeof(pathbuf));
                    if (pathbuf_len < 0) {
                        break;
                    }
                    if (pathbuf_len >= sizeof(pathbuf) || pathbuf_len == 0) {
                        return;
                    }
                     
                     memcpy(pathbuf_copy, pathbuf, pathbuf_len);
                     pathbuf_copy[pathbuf_len] = '\0';
                 }
                 
                 name = pathbuf_copy;
                 goto gotName;     
             }
         }
     }


in unix/tclUnixFile.c's TclpFindExecutable if it can't find the executable using the current methods.
stevenaausplain2011-10-11 08:39:55
Hmmm... Scid vs. PC now suffers from this problem with wish8.6b2. We compile our own custom wish - tkscid - and install it into local/bin (or bin - they both bug out), and from it [info nameofexecutable] just returns {}. Never seen this bug before 8.6b2, and not sure what to do.
dkfplain2011-10-12 16:49:56
Doing a readlink() on /proc/[pid]/exe is elegant, but won't work on OSX. There, the only reliable method I've found for getting the executable path from the PID is by calling lsof in a subprocess:

  lsof -a -p [pid] -F n -d txt

Then you need the first line that starts with 'n', and you need all of that line except the first character. Ugh. Works though. (Poking in the normal system process information doesn't work; that gives you the process name without the path to it.)
stevenaausplain2011-10-12 17:43:57
> Doing a readlink() on /proc/[pid]/exe is elegant

This should come in handy.   Cheers

Just clarifying - i never had this issue with any version of 8.4, 8.5 or 8.6a1, so perhaps it is a regression.
rkeeneplain2011-10-12 19:19:43
In KitCreator I also attempt to use dladdr() if an acceptable implementation is found.  Mac OS X appears to support this ( http://developer.apple.com/library/ios/#documentation/system/conceptual/manpages_iphoneos/man3/dladdr.3.html )

http://kitcreator.rkeene.org/fossil/artifact?name=0cfdc14cf7c88b31081e9e0ac689a710a26d6375&ln=294-301
dkfplain2011-10-13 17:54:23
I also like the dladdr() solution, though the name it yields is not necessarily normalized. Still, that's a practical solution (and would be easy to check for with autoconf).

My little test code:

#include <dlfcn.h>
#include <stdio.h>
int main(int argc,char**argv) {
  Dl_info info;
  if (!dladdr(main,&info)) {
    printf("not found\n");
    return 1;
  }
  printf("\"%s\"\n",info.dli_fname);
  return 0;
}
dkfplain2011-10-13 17:58:08
dgp: What's the position on normalization of [info nameofexecutable]?

(dladdr() is present on OSX, Solaris and Linux for sure: I've checked.)
dkfplain2011-10-13 19:17:23
I've committed something on the bug-1224888 branch. It needs more work (especially detection of the validity of the magic!) but it would be interesting to see if it works.

https://core.tcl.tk/tcl/timeline?r=bug-1224888
dgpplain2011-10-14 01:47:45
The branch produces segfault in test unixFile-1.1

This is on a platform where..

checking for dladdr... no
dgpplain2011-10-14 01:51:55
Program received signal SIGSEGV, Segmentation fault.
0x0000003716e7288e in free () from /lib64/libc.so.6
(gdb) bt
#0  0x0000003716e7288e in free () from /lib64/libc.so.6
#1  0x00000000004cb7d2 in TclpFree (
    cp=0x2f6c74692f3a6e69 <Address 0x2f6c74692f3a6e69 out of bounds>)
    at /home/dgp/fossil/tcl8.4/unix/../generic/tclAlloc.c:710
#2  0x0000000000429ea5 in Tcl_Free (
    ptr=0x2f6c74692f3a6e69 <Address 0x2f6c74692f3a6e69 out of bounds>)
    at /home/dgp/fossil/tcl8.4/unix/../generic/tclCkalloc.c:1167
#3  0x00000000004a86d6 in Tcl_DStringFree (dsPtr=0x7fffffff7f10)
    at /home/dgp/fossil/tcl8.4/unix/../generic/tclUtil.c:1698
#4  0x00000000004b6788 in TclpFindExecutable (argv0=0x783ca0 "junk")
    at /home/dgp/fossil/tcl8.4/unix/tclUnixFile.c:225
#5  0x0000000000454329 in Tcl_FindExecutable (argv0=0x783ca0 "junk")
    at /home/dgp/fossil/tcl8.4/unix/../generic/tclEncoding.c:1228
#6  0x0000000000420239 in TestfindexecutableCmd (clientData=0x0, 
    interp=0x700030, argc=2, argv=0x7fffffff8320)
    at /home/dgp/fossil/tcl8.4/unix/tclUnixTest.c:465
dkfplain2011-10-14 04:04:51
Thank you for testing; I'd put the code two lines too high. :-)
dgpplain2011-10-18 00:34:17
==== unixFile-1.1 Tcl_FindExecutable FAILED
==== Contents of test case:

    set env(PATH) ""
    testfindexecutable junk

---- Result was:
/local/tmp/dgp/fossil/tcl8.4/unix/tcltest"j
---- Result should have been (exact matching):
/local/tmp/dgp/fossil/tcl8.4/unix/junk
==== unixFile-1.1 FAILED


==== unixFile-1.2 Tcl_FindExecutable FAILED
==== Contents of test case:

    set env(PATH) "/dummy"
    testfindexecutable junk

---- Result was:
/local/tmp/dgp/fossil/tcl8.4/unix/tcltest"j
---- Result should have been (exact matching):

==== unixFile-1.2 FAILED


==== unixFile-1.3 Tcl_FindExecutable FAILED
==== Contents of test case:

    set env(PATH) "/dummy:[pwd]"
    testfindexecutable junk

---- Result was:
/local/tmp/dgp/fossil/tcl8.4/unix/tcltest"j
---- Result should have been (exact matching):
/local/tmp/dgp/fossil/tcl8.4/unix/junk
==== unixFile-1.3 FAILED


==== unixFile-1.4 Tcl_FindExecutable FAILED
==== Contents of test case:

    set env(PATH) "/dummy:"
    testfindexecutable junk

---- Result was:
/local/tmp/dgp/fossil/tcl8.4/unix/tcltest"j
---- Result should have been (exact matching):
/local/tmp/dgp/fossil/tcl8.4/unix/junk
==== unixFile-1.4 FAILED


==== unixFile-1.5 Tcl_FindExecutable FAILED
==== Contents of test case:

    set env(PATH) "/dummy:/dummy"
    testfindexecutable junk

---- Result was:
/local/tmp/dgp/fossil/tcl8.4/unix/tcltest"j
---- Result should have been (exact matching):

==== unixFile-1.5 FAILED


==== unixFile-1.6 Tcl_FindExecutable FAILED
==== Contents of test case:

    set env(PATH) "/dummy::/dummy"
    testfindexecutable junk

---- Result was:
/local/tmp/dgp/fossil/tcl8.4/unix/tcltest"j
---- Result should have been (exact matching):
/local/tmp/dgp/fossil/tcl8.4/unix/junk
==== unixFile-1.6 FAILED


==== unixFile-1.7 Tcl_FindExecutable FAILED
==== Contents of test case:

    set env(PATH) ":/dummy"
    testfindexecutable junk

---- Result was:
/local/tmp/dgp/fossil/tcl8.4/unix/tcltest"j
---- Result should have been (exact matching):
/local/tmp/dgp/fossil/tcl8.4/unix/junk
==== unixFile-1.7 FAILED
dgpplain2011-11-01 00:14:33
On another of my systems, this branch fails
to build:

/home/dgp/fossil/tcl8.4/unix/tclUnixFile.c: In function ‘TclpFindExecutable’:
/home/dgp/fossil/tcl8.4/unix/tclUnixFile.c:64: error: ‘Dl_info’ undeclared (first use in this function)
/home/dgp/fossil/tcl8.4/unix/tclUnixFile.c:64: error: (Each undeclared identifier is reported only once
/home/dgp/fossil/tcl8.4/unix/tclUnixFile.c:64: error: for each function it appears in.)
/home/dgp/fossil/tcl8.4/unix/tclUnixFile.c:64: error: expected ‘;’ before ‘dlinfoBuffer’
/home/dgp/fossil/tcl8.4/unix/tclUnixFile.c:100: error: ‘RTLD_DEFAULT’ undeclared (first use in this function)
/home/dgp/fossil/tcl8.4/unix/tclUnixFile.c:104: warning: implicit declaration of function ‘dladdr’
/home/dgp/fossil/tcl8.4/unix/tclUnixFile.c:104: error: ‘dlinfoBuffer’ undeclared (first use in this function)
nijtmansplain2011-11-01 02:43:11
>On another of my systems, this branch fails
>to build:

Probably because configure.in is changed but configure
is not re-generated. done now. Does that help?
dgpplain2011-11-01 03:16:32
thanks; now back to the unixFile-1.* fails.
dkf Ticket [1224888fff]: 6 changes [History diff]2015-01-07 23:30:29
dgpplain2015-01-13 14:34:06
Can't get anywhere with rebasing the branch to something live.

Everything I try fails the test suite catastrophically with apparent
mem corruption in the name of the tcltest executable.

I'll happily test when someone else gets the branch resurrected.
jan.nijtmansplain2015-01-22 20:04:28
Please try again ;-)

The reason probably was that "main" was made MODULE_SCOPE, so dlsym() cannot find it any more .....
dgpplain2015-01-22 21:02:43
No improvement....

...
Tests began at Thu Jan 22 16:02:41 EST 2015
aaa_exit.test
Test file error: couldn't execute "/local/tmp/dgp/fossil/tcl-only/unix/tcltestQÿ": no such file or directory
append.test
Test file error: couldn't execute "/local/tmp/dgp/fossil/tcl-only/unix/tcltestQÿ": no such file or directory
...
dkf Ticket [1224888fff]: 3 changes [History diff]2015-02-04 11:01:14
drhplain2015-02-04 18:12:23
Adding a comment merely to test Fossil ticket handle.  Ignore this comment.
dkfhtml2015-03-17 20:32:10

It's going to be really hard to figure out what is wrong without knowing the path through the tangle of code that is being taken. It's an awful tangle right now.

I've added support for _NSGetExecutablePath on OSX to the branch. A bunch of tests in unixFile.test now fail because Tcl ceases to be fool-able as to its executable name. The name isn't (always) a normalized one; will that matter?

Anyone digging into the depths of this should start with http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe which summarizes a lot of the key techniques on different platforms.

aspectplain2017-11-16 12:55:34
The errors below come from readlink's silly failure to terminate its result.
I've pushed a patch that handles it properly and passes the test suite.

The implementation in TclpFindExecutable looks pretty good, but it could do
with being broken out into individually-testable parts.  Looking at the
linked SO discussion, it seems all we're missing for "full" coverage is the
Solaris getexecname() and FreeBSD sysctl approaches.  Both these platforms
apparently "often" have /proc/ with one of the paths included in exenames[].

I think this branch (once brought up to date with trunk) should eliminate
the need for kitcreator's workaround.

I know we have people who can test on FreeBSD (gahr?) but how about
Solaris/Illumos?  Anyone have access to one of those?
aspectplain2017-11-17 04:56:48
merged trunk.

Correction to previous comment: unixFile-1.x are FAILing, as expected:  these 
tests prove that TclpFindExecutable can be tricked by a bogus argv[0], which 
is the point of this ticket.

Updating these tests is the obvious step, but as mentioned below a test suite 
which exercises the different paths on various platforms would be a Good Idea.
Log in (anonymous is fine) to add a comment.