7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $PostgreSQL: pgsql/src/port/exec.c,v 1.30 2004/10/18 19:08:58 momjian Exp $
10
+ * $PostgreSQL: pgsql/src/port/exec.c,v 1.31 2004/11/06 01:16:22 tgl Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
42
42
43
43
#ifndef FRONTEND
44
44
/* We use only 3-parameter elog calls in this file, for simplicity */
45
- #define log_error (str , param ) elog(LOG, ( str), ( param) )
45
+ #define log_error (str , param ) elog(LOG, str, param)
46
46
#else
47
- #define log_error (str , param ) fprintf(stderr, ( str), (param ))
47
+ #define log_error (str , param ) ( fprintf(stderr, str, param ), fputc('\n', stderr ))
48
48
#endif
49
49
50
50
51
- static void win32_make_absolute (char * path );
52
-
53
-
54
51
/*
55
52
* validate_exec -- validate "path" as an executable file
56
53
*
@@ -165,7 +162,7 @@ validate_exec(const char *path)
165
162
* executable's location. Also, we need a full path not a relative
166
163
* path because we will later change working directory.
167
164
*
168
- * This function is not thread-safe because of it calls validate_exec(),
165
+ * This function is not thread-safe because it calls validate_exec(),
169
166
* which calls getgrgid(). This function should be used only in
170
167
* non-threaded binaries, not in library routines.
171
168
*/
@@ -178,61 +175,40 @@ find_my_exec(const char *argv0, char *retpath)
178
175
179
176
#ifndef WIN32_CLIENT_ONLY
180
177
if (!getcwd (cwd , MAXPGPATH ))
178
+ strcpy (cwd , "." ); /* cheesy, but better than nothing */
181
179
#else
182
180
if (!GetCurrentDirectory (MAXPGPATH , cwd ))
181
+ strcpy (cwd , "." ); /* cheesy, but better than nothing */
183
182
#endif
184
- cwd [0 ] = '\0' ;
185
183
186
184
/*
187
- * First try: use the binary that's located in the same directory if
188
- * it was invoked with an explicit path. Presumably the user used an
189
- * explicit path because it wasn't in PATH, and we don't want to use
190
- * incompatible executables.
191
- *
192
- * For the binary: First try: if we're given some kind of path, use it
193
- * (making sure that a relative path is made absolute before returning
194
- * it).
185
+ * If argv0 contains a separator, then PATH wasn't used.
195
186
*/
196
- /* Does argv0 have a separator? */
197
- if ((path = last_dir_separator (argv0 )))
187
+ if (first_dir_separator (argv0 ) != NULL )
198
188
{
199
- if (* ++ path == '\0' )
200
- {
201
- log_error ("argv[0] ends with a path separator \"%s\"" , argv0 );
202
- return -1 ;
203
- }
204
-
205
189
if (is_absolute_path (argv0 ))
206
190
StrNCpy (retpath , argv0 , MAXPGPATH );
207
191
else
208
- snprintf (retpath , MAXPGPATH , "%s/%s" , cwd , argv0 );
209
-
192
+ join_path_components (retpath , cwd , argv0 );
210
193
canonicalize_path (retpath );
194
+
211
195
if (validate_exec (retpath ) == 0 )
212
- {
213
- win32_make_absolute (retpath );
214
196
return 0 ;
215
- }
216
- else
217
- {
218
- log_error ("invalid binary \"%s\"" , retpath );
219
- return -1 ;
220
- }
197
+
198
+ log_error ("invalid binary \"%s\"" , retpath );
199
+ return -1 ;
221
200
}
222
201
223
202
#ifdef WIN32
224
203
/* Win32 checks the current directory first for names without slashes */
225
- if (validate_exec (argv0 ) == 0 )
226
- {
227
- snprintf (retpath , MAXPGPATH , "%s/%s" , cwd , argv0 );
228
- win32_make_absolute (retpath );
204
+ join_path_components (retpath , cwd , argv0 );
205
+ if (validate_exec (retpath ) == 0 )
229
206
return 0 ;
230
- }
231
207
#endif
232
208
233
209
/*
234
- * Second try: since no explicit path was supplied, the user must have
235
- * been relying on PATH. We'll use the same PATH.
210
+ * Since no explicit path was supplied, the user must have
211
+ * been relying on PATH. We'll search the same PATH.
236
212
*/
237
213
if ((path = getenv ("PATH" )) && * path )
238
214
{
@@ -253,40 +229,33 @@ find_my_exec(const char *argv0, char *retpath)
253
229
StrNCpy (test_path , startp , Min (endp - startp + 1 , MAXPGPATH ));
254
230
255
231
if (is_absolute_path (test_path ))
256
- snprintf (retpath , MAXPGPATH , "%s/%s" , test_path , argv0 );
232
+ join_path_components (retpath , test_path , argv0 );
257
233
else
258
- snprintf (retpath , MAXPGPATH , "%s/%s/%s" , cwd , test_path , argv0 );
259
-
234
+ {
235
+ join_path_components (retpath , cwd , test_path );
236
+ join_path_components (retpath , retpath , argv0 );
237
+ }
260
238
canonicalize_path (retpath );
239
+
261
240
switch (validate_exec (retpath ))
262
241
{
263
- case 0 : /* found ok */
264
- win32_make_absolute (retpath );
242
+ case 0 : /* found ok */
265
243
return 0 ;
266
244
case -1 : /* wasn't even a candidate, keep looking */
267
- continue ;
245
+ break ;
268
246
case -2 : /* found but disqualified */
269
247
log_error ("could not read binary \"%s\"" , retpath );
270
- continue ;
248
+ break ;
271
249
}
272
250
} while (* endp );
273
251
}
274
252
275
253
log_error ("could not find a \"%s\" to execute" , argv0 );
276
254
return -1 ;
277
-
278
- #if NOT_USED
279
- /*
280
- * Win32 has a native way to find the executable name, but the above
281
- * method works too.
282
- */
283
- if (GetModuleFileName (NULL , retpath , MAXPGPATH ) == 0 )
284
- log_error ("GetModuleFileName failed (%i)" , (int ) GetLastError ());
285
- #endif
286
255
}
287
256
288
257
/*
289
- * The runtime librarys popen() on win32 does not work when being
258
+ * The runtime library's popen() on win32 does not work when being
290
259
* called from a service when running on windows <= 2000, because
291
260
* there is no stdin/stdout/stderr.
292
261
*
@@ -427,10 +396,9 @@ pipe_read_line(char *cmd, char *line, int maxsize)
427
396
}
428
397
429
398
430
-
431
399
/*
432
- * Find our binary directory, then make sure the "target" executable
433
- * is the proper version.
400
+ * Find another program in our binary's directory,
401
+ * then make sure it is the proper version.
434
402
*/
435
403
int
436
404
find_other_exec (const char * argv0 , const char * target ,
@@ -487,41 +455,19 @@ pclose_check(FILE *stream)
487
455
}
488
456
else if (WIFEXITED (exitstatus ))
489
457
{
490
- log_error (_ ("child process exited with exit code %d\n " ),
458
+ log_error (_ ("child process exited with exit code %d" ),
491
459
WEXITSTATUS (exitstatus ));
492
460
}
493
461
else if (WIFSIGNALED (exitstatus ))
494
462
{
495
- log_error (_ ("child process was terminated by signal %d\n " ),
463
+ log_error (_ ("child process was terminated by signal %d" ),
496
464
WTERMSIG (exitstatus ));
497
465
}
498
466
else
499
467
{
500
- log_error (_ ("child process exited with unrecognized status %d\n " ),
468
+ log_error (_ ("child process exited with unrecognized status %d" ),
501
469
exitstatus );
502
470
}
503
471
504
472
return -1 ;
505
473
}
506
-
507
-
508
- /*
509
- * Windows doesn't like relative paths to executables (other things work fine)
510
- * so we call its builtin function to expand them. Elsewhere this is a NOOP
511
- */
512
- static void
513
- win32_make_absolute (char * path )
514
- {
515
- #ifdef WIN32
516
- char abspath [MAXPGPATH ];
517
-
518
- if (_fullpath (abspath , path , MAXPGPATH ) == NULL )
519
- {
520
- log_error ("Win32 path expansion failed: %s" , strerror (errno ));
521
- StrNCpy (abspath , path , MAXPGPATH );
522
- }
523
- canonicalize_path (abspath );
524
-
525
- StrNCpy (path , abspath , MAXPGPATH );
526
- #endif
527
- }
0 commit comments