1 /* Provide relocatable programs.
2 Copyright (C) 2003-2004 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published
7 by the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
38 #if defined _WIN32 || defined __WIN32__
39 # undef WIN32 /* avoid warning on mingw32 */
44 # define WIN32_LEAN_AND_MEAN
48 #include "xreadlink.h"
49 #include "canonicalize.h"
50 #include "relocatable.h"
53 # define xmalloc malloc
54 # define xstrdup strdup
60 ISSLASH(C) tests whether C is a directory separator character.
61 IS_PATH_WITH_DIR(P) tests whether P contains a directory specification.
63 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
64 /* Win32, Cygwin, OS/2, DOS */
65 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
66 # define HAS_DEVICE(P) \
67 ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
69 # define IS_PATH_WITH_DIR(P) \
70 (strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P))
71 # define FILE_SYSTEM_PREFIX_LEN(P) (HAS_DEVICE (P) ? 2 : 0)
74 # define ISSLASH(C) ((C) == '/')
75 # define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL)
76 # define FILE_SYSTEM_PREFIX_LEN(P) 0
79 #undef set_program_name
82 #if ENABLE_RELOCATABLE
85 /* File descriptor of the executable.
86 (Only used to verify that we find the correct executable.) */
87 static int executable_fd
= -1;
90 /* Tests whether a given pathname may belong to the executable. */
92 maybe_executable (const char *filename
)
95 if (access (filename
, X_OK
) < 0)
99 if (executable_fd
>= 0)
101 /* If we already have an executable_fd, check that filename points to
104 struct stat statfile
;
106 if (fstat (executable_fd
, &statexe
) >= 0)
108 if (stat (filename
, &statfile
) < 0)
110 if (!(statfile
.st_dev
111 && statfile
.st_dev
== statexe
.st_dev
112 && statfile
.st_ino
== statexe
.st_ino
))
122 /* Determine the full pathname of the current executable, freshly allocated.
123 Return NULL if unknown.
124 Guaranteed to work on Linux and Woe32. Likely to work on the other
125 Unixes (maybe except BeOS), under most conditions. */
127 find_executable (const char *argv0
)
131 int length
= GetModuleFileName (NULL
, buf
, sizeof (buf
));
134 if (!IS_PATH_WITH_DIR (buf
))
135 /* Shouldn't happen. */
137 return xstrdup (buf
);
140 /* The executable is accessible as /proc/<pid>/exe. In newer Linux
141 versions, also as /proc/self/exe. Linux >= 2.1 provides a symlink
142 to the true pathname; older Linux versions give only device and ino,
143 enclosed in brackets, which we cannot use here. */
147 link
= xreadlink ("/proc/self/exe");
148 if (link
!= NULL
&& link
[0] != '[')
150 if (executable_fd
< 0)
151 executable_fd
= open ("/proc/self/exe", O_RDONLY
, 0);
155 sprintf (buf
, "/proc/%d/exe", getpid ());
156 link
= xreadlink (buf
);
157 if (link
!= NULL
&& link
[0] != '[')
159 if (executable_fd
< 0)
160 executable_fd
= open (buf
, O_RDONLY
, 0);
164 /* Guess the executable's full path. We assume the executable has been
165 called via execlp() or execvp() with properly set up argv[0]. The
166 login(1) convention to add a '-' prefix to argv[0] is not supported. */
168 bool has_slash
= false;
171 for (p
= argv0
; *p
; p
++)
180 /* exec searches paths without slashes in the directory list given
182 const char *path
= getenv ("PATH");
189 for (p
= path
; *p
; p
= p_next
)
199 p_next
= (*q
== '\0' ? q
: q
+ 1);
201 /* We have a path item at p, of length p_len.
202 Now concatenate the path item and argv0. */
203 concat_name
= (char *) xmalloc (p_len
+ strlen (argv0
) + 2);
205 if (concat_name
== NULL
)
209 /* An empty PATH element designates the current directory. */
210 strcpy (concat_name
, argv0
);
213 memcpy (concat_name
, p
, p_len
);
214 concat_name
[p_len
] = '/';
215 strcpy (concat_name
+ p_len
+ 1, argv0
);
217 if (maybe_executable (concat_name
))
218 return canonicalize_file_name (concat_name
);
222 /* Not found in the PATH, assume the current directory. */
224 /* exec treats paths containing slashes as relative to the current
226 if (maybe_executable (argv0
))
227 return canonicalize_file_name (argv0
);
229 /* No way to find the executable. */
234 /* Full pathname of executable, or NULL. */
235 static char *executable_fullname
;
238 prepare_relocate (const char *orig_installprefix
, const char *orig_installdir
,
241 const char *curr_prefix
;
243 /* Determine the full pathname of the current executable. */
244 executable_fullname
= find_executable (argv0
);
246 /* Determine the current installation prefix from it. */
247 curr_prefix
= compute_curr_prefix (orig_installprefix
, orig_installdir
,
248 executable_fullname
);
249 if (curr_prefix
!= NULL
)
250 /* Now pass this prefix to all copies of the relocate.c source file. */
251 set_relocation_prefix (orig_installprefix
, curr_prefix
);
254 /* Set program_name, based on argv[0], and original installation prefix and
255 directory, for relocatability. */
257 set_program_name_and_installdir (const char *argv0
,
258 const char *orig_installprefix
,
259 const char *orig_installdir
)
261 const char *argv0_stripped
= argv0
;
263 /* Relocatable programs are renamed to .bin by install-reloc. Remove
266 size_t argv0_len
= strlen (argv0
);
267 if (argv0_len
> 4 && memcmp (argv0
+ argv0_len
- 4, ".bin", 4) == 0)
269 char *shorter
= (char *) xmalloc (argv0_len
- 4 + 1);
274 memcpy (shorter
, argv0
, argv0_len
- 4);
275 shorter
[argv0_len
- 4] = '\0';
276 argv0_stripped
= shorter
;
281 set_program_name (argv0_stripped
);
283 prepare_relocate (orig_installprefix
, orig_installdir
, argv0
);
286 /* Return the full pathname of the current executable, based on the earlier
287 call to set_program_name_and_installdir. Return NULL if unknown. */
289 get_full_program_name (void)
291 return executable_fullname
;