00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "config.h"
00022 #include "libiberty.h"
00023 #include "pex-common.h"
00024
00025 #include <stdio.h>
00026 #include <errno.h>
00027 #ifdef NEED_DECLARATION_ERRNO
00028 extern int errno;
00029 #endif
00030 #ifdef HAVE_STDLIB_H
00031 #include <stdlib.h>
00032 #endif
00033 #ifdef HAVE_STRING_H
00034 #include <string.h>
00035 #endif
00036 #ifdef HAVE_UNISTD_H
00037 #include <unistd.h>
00038 #endif
00039
00040 extern int mkstemps (char *, int);
00041
00042
00043
00044
00045
00046 static void pex_add_remove (struct pex_obj *, const char *, int);
00047 static int pex_get_status_and_time (struct pex_obj *, int, const char **,
00048 int *);
00049
00050
00051
00052 struct pex_obj *
00053 pex_init_common (int flags, const char *pname, const char *tempbase,
00054 const struct pex_funcs *funcs)
00055 {
00056 struct pex_obj *obj;
00057
00058 obj = XNEW (struct pex_obj);
00059 obj->flags = flags;
00060 obj->pname = pname;
00061 obj->tempbase = tempbase;
00062 obj->next_input = STDIN_FILE_NO;
00063 obj->next_input_name = NULL;
00064 obj->next_input_name_allocated = 0;
00065 obj->count = 0;
00066 obj->children = NULL;
00067 obj->status = NULL;
00068 obj->time = NULL;
00069 obj->number_waited = 0;
00070 obj->input_file = NULL;
00071 obj->read_output = NULL;
00072 obj->remove_count = 0;
00073 obj->remove = NULL;
00074 obj->funcs = funcs;
00075 obj->sysdep = NULL;
00076 return obj;
00077 }
00078
00079
00080
00081 static void
00082 pex_add_remove (struct pex_obj *obj, const char *name, int allocated)
00083 {
00084 char *add;
00085
00086 ++obj->remove_count;
00087 obj->remove = XRESIZEVEC (char *, obj->remove, obj->remove_count);
00088 if (allocated)
00089 add = (char *) name;
00090 else
00091 add = xstrdup (name);
00092 obj->remove[obj->remove_count - 1] = add;
00093 }
00094
00095
00096
00097
00098
00099
00100 static char *
00101 temp_file (struct pex_obj *obj, int flags, char *name)
00102 {
00103 if (name == NULL)
00104 {
00105 if (obj->tempbase == NULL)
00106 {
00107 name = make_temp_file (NULL);
00108 }
00109 else
00110 {
00111 int len = strlen (obj->tempbase);
00112 int out;
00113
00114 if (len >= 6
00115 && strcmp (obj->tempbase + len - 6, "XXXXXX") == 0)
00116 name = xstrdup (obj->tempbase);
00117 else
00118 name = concat (obj->tempbase, "XXXXXX", NULL);
00119
00120 out = mkstemps (name, 0);
00121 if (out < 0)
00122 {
00123 free (name);
00124 return NULL;
00125 }
00126
00127
00128
00129
00130
00131 close (out);
00132 }
00133 }
00134 else if ((flags & PEX_SUFFIX) != 0)
00135 {
00136 if (obj->tempbase == NULL)
00137 name = make_temp_file (name);
00138 else
00139 name = concat (obj->tempbase, name, NULL);
00140 }
00141
00142 return name;
00143 }
00144
00145
00146
00147
00148
00149 const char *
00150 pex_run_in_environment (struct pex_obj *obj, int flags, const char *executable,
00151 char * const * argv, char * const * env,
00152 const char *orig_outname, const char *errname,
00153 int *err)
00154 {
00155 const char *errmsg;
00156 int in, out, errdes;
00157 char *outname;
00158 int outname_allocated;
00159 int p[2];
00160 int toclose;
00161 long pid;
00162
00163 in = -1;
00164 out = -1;
00165 errdes = -1;
00166 outname = (char *) orig_outname;
00167 outname_allocated = 0;
00168
00169
00170 if (obj->input_file)
00171 {
00172 if (fclose (obj->input_file) == EOF)
00173 {
00174 errmsg = "closing pipeline input file";
00175 goto error_exit;
00176 }
00177 obj->input_file = NULL;
00178 }
00179
00180
00181
00182 if (obj->next_input_name != NULL)
00183 {
00184
00185
00186 if (!pex_get_status_and_time (obj, 0, &errmsg, err))
00187 goto error_exit;
00188
00189 in = obj->funcs->open_read (obj, obj->next_input_name,
00190 (flags & PEX_BINARY_INPUT) != 0);
00191 if (in < 0)
00192 {
00193 *err = errno;
00194 errmsg = "open temporary file";
00195 goto error_exit;
00196 }
00197 if (obj->next_input_name_allocated)
00198 {
00199 free (obj->next_input_name);
00200 obj->next_input_name_allocated = 0;
00201 }
00202 obj->next_input_name = NULL;
00203 }
00204 else
00205 {
00206 in = obj->next_input;
00207 if (in < 0)
00208 {
00209 *err = 0;
00210 errmsg = "pipeline already complete";
00211 goto error_exit;
00212 }
00213 }
00214
00215
00216
00217 if ((flags & PEX_LAST) != 0)
00218 {
00219 if (outname == NULL)
00220 out = STDOUT_FILE_NO;
00221 else if ((flags & PEX_SUFFIX) != 0)
00222 {
00223 outname = concat (obj->tempbase, outname, NULL);
00224 outname_allocated = 1;
00225 }
00226 obj->next_input = -1;
00227 }
00228 else if ((obj->flags & PEX_USE_PIPES) == 0)
00229 {
00230 outname = temp_file (obj, flags, outname);
00231 if (! outname)
00232 {
00233 *err = 0;
00234 errmsg = "could not create temporary file";
00235 goto error_exit;
00236 }
00237
00238 if (outname != orig_outname)
00239 outname_allocated = 1;
00240
00241 if ((obj->flags & PEX_SAVE_TEMPS) == 0)
00242 {
00243 pex_add_remove (obj, outname, outname_allocated);
00244 outname_allocated = 0;
00245 }
00246
00247
00248 obj->next_input_name = outname;
00249 obj->next_input_name_allocated = outname_allocated;
00250 outname_allocated = 0;
00251 }
00252 else
00253 {
00254 if (obj->funcs->pipe (obj, p, (flags & PEX_BINARY_OUTPUT) != 0) < 0)
00255 {
00256 *err = errno;
00257 errmsg = "pipe";
00258 goto error_exit;
00259 }
00260
00261 out = p[WRITE_PORT];
00262 obj->next_input = p[READ_PORT];
00263 }
00264
00265 if (out < 0)
00266 {
00267 out = obj->funcs->open_write (obj, outname,
00268 (flags & PEX_BINARY_OUTPUT) != 0);
00269 if (out < 0)
00270 {
00271 *err = errno;
00272 errmsg = "open temporary output file";
00273 goto error_exit;
00274 }
00275 }
00276
00277 if (outname_allocated)
00278 {
00279 free (outname);
00280 outname_allocated = 0;
00281 }
00282
00283
00284
00285 if (errname == NULL)
00286 errdes = STDERR_FILE_NO;
00287 else
00288 {
00289
00290
00291
00292 errdes = obj->funcs->open_write (obj, errname, 0);
00293 if (errdes < 0)
00294 {
00295 *err = errno;
00296 errmsg = "open error file";
00297 goto error_exit;
00298 }
00299 }
00300
00301
00302
00303
00304 if ((obj->flags & PEX_USE_PIPES) == 0)
00305 toclose = -1;
00306 else
00307 toclose = obj->next_input;
00308
00309
00310
00311 pid = obj->funcs->exec_child (obj, flags, executable, argv, env,
00312 in, out, errdes, toclose, &errmsg, err);
00313 if (pid < 0)
00314 goto error_exit;
00315
00316 ++obj->count;
00317 obj->children = XRESIZEVEC (long, obj->children, obj->count);
00318 obj->children[obj->count - 1] = pid;
00319
00320 return NULL;
00321
00322 error_exit:
00323 if (in >= 0 && in != STDIN_FILE_NO)
00324 obj->funcs->close (obj, in);
00325 if (out >= 0 && out != STDOUT_FILE_NO)
00326 obj->funcs->close (obj, out);
00327 if (errdes >= 0 && errdes != STDERR_FILE_NO)
00328 obj->funcs->close (obj, errdes);
00329 if (outname_allocated)
00330 free (outname);
00331 return errmsg;
00332 }
00333
00334
00335
00336 const char *
00337 pex_run (struct pex_obj *obj, int flags, const char *executable,
00338 char * const * argv, const char *orig_outname, const char *errname,
00339 int *err)
00340 {
00341 return pex_run_in_environment (obj, flags, executable, argv, NULL,
00342 orig_outname, errname, err);
00343 }
00344
00345
00346
00347 FILE *
00348 pex_input_file (struct pex_obj *obj, int flags, const char *in_name)
00349 {
00350 char *name = (char *) in_name;
00351 FILE *f;
00352
00353
00354
00355 if (obj->count != 0
00356 || (obj->next_input >= 0 && obj->next_input != STDIN_FILE_NO)
00357 || obj->next_input_name)
00358 {
00359 errno = EINVAL;
00360 return NULL;
00361 }
00362
00363 name = temp_file (obj, flags, name);
00364 if (! name)
00365 return NULL;
00366
00367 f = fopen (name, (flags & PEX_BINARY_OUTPUT) ? "wb" : "w");
00368 if (! f)
00369 {
00370 free (name);
00371 return NULL;
00372 }
00373
00374 obj->input_file = f;
00375 obj->next_input_name = name;
00376 obj->next_input_name_allocated = (name != in_name);
00377
00378 return f;
00379 }
00380
00381
00382
00383 FILE *
00384 pex_input_pipe (struct pex_obj *obj, int binary)
00385 {
00386 int p[2];
00387 FILE *f;
00388
00389
00390 if (obj->count > 0)
00391 goto usage_error;
00392
00393
00394
00395 if (! (obj->flags & PEX_USE_PIPES))
00396 goto usage_error;
00397
00398
00399
00400 if ((obj->next_input >= 0 && obj->next_input != STDIN_FILE_NO)
00401 || obj->next_input_name)
00402 goto usage_error;
00403
00404 if (obj->funcs->pipe (obj, p, binary != 0) < 0)
00405 return NULL;
00406
00407 f = obj->funcs->fdopenw (obj, p[WRITE_PORT], binary != 0);
00408 if (! f)
00409 {
00410 int saved_errno = errno;
00411 obj->funcs->close (obj, p[READ_PORT]);
00412 obj->funcs->close (obj, p[WRITE_PORT]);
00413 errno = saved_errno;
00414 return NULL;
00415 }
00416
00417 obj->next_input = p[READ_PORT];
00418
00419 return f;
00420
00421 usage_error:
00422 errno = EINVAL;
00423 return NULL;
00424 }
00425
00426
00427
00428
00429 FILE *
00430 pex_read_output (struct pex_obj *obj, int binary)
00431 {
00432 if (obj->next_input_name != NULL)
00433 {
00434 const char *errmsg;
00435 int err;
00436
00437
00438
00439 if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
00440 {
00441 errno = err;
00442 return NULL;
00443 }
00444
00445 obj->read_output = fopen (obj->next_input_name, binary ? "rb" : "r");
00446
00447 if (obj->next_input_name_allocated)
00448 {
00449 free (obj->next_input_name);
00450 obj->next_input_name_allocated = 0;
00451 }
00452 obj->next_input_name = NULL;
00453 }
00454 else
00455 {
00456 int o;
00457
00458 o = obj->next_input;
00459 if (o < 0 || o == STDIN_FILE_NO)
00460 return NULL;
00461 obj->read_output = obj->funcs->fdopenr (obj, o, binary);
00462 obj->next_input = -1;
00463 }
00464
00465 return obj->read_output;
00466 }
00467
00468
00469
00470
00471 static int
00472 pex_get_status_and_time (struct pex_obj *obj, int done, const char **errmsg,
00473 int *err)
00474 {
00475 int ret;
00476 int i;
00477
00478 if (obj->number_waited == obj->count)
00479 return 1;
00480
00481 obj->status = XRESIZEVEC (int, obj->status, obj->count);
00482 if ((obj->flags & PEX_RECORD_TIMES) != 0)
00483 obj->time = XRESIZEVEC (struct pex_time, obj->time, obj->count);
00484
00485 ret = 1;
00486 for (i = obj->number_waited; i < obj->count; ++i)
00487 {
00488 if (obj->funcs->wait (obj, obj->children[i], &obj->status[i],
00489 obj->time == NULL ? NULL : &obj->time[i],
00490 done, errmsg, err) < 0)
00491 ret = 0;
00492 }
00493 obj->number_waited = i;
00494
00495 return ret;
00496 }
00497
00498
00499
00500 int
00501 pex_get_status (struct pex_obj *obj, int count, int *vector)
00502 {
00503 if (obj->status == NULL)
00504 {
00505 const char *errmsg;
00506 int err;
00507
00508 if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
00509 return 0;
00510 }
00511
00512 if (count > obj->count)
00513 {
00514 memset (vector + obj->count, 0, (count - obj->count) * sizeof (int));
00515 count = obj->count;
00516 }
00517
00518 memcpy (vector, obj->status, count * sizeof (int));
00519
00520 return 1;
00521 }
00522
00523
00524
00525 int
00526 pex_get_times (struct pex_obj *obj, int count, struct pex_time *vector)
00527 {
00528 if (obj->status == NULL)
00529 {
00530 const char *errmsg;
00531 int err;
00532
00533 if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
00534 return 0;
00535 }
00536
00537 if (obj->time == NULL)
00538 return 0;
00539
00540 if (count > obj->count)
00541 {
00542 memset (vector + obj->count, 0,
00543 (count - obj->count) * sizeof (struct pex_time));
00544 count = obj->count;
00545 }
00546
00547 memcpy (vector, obj->time, count * sizeof (struct pex_time));
00548
00549 return 1;
00550 }
00551
00552
00553
00554 void
00555 pex_free (struct pex_obj *obj)
00556 {
00557 if (obj->next_input >= 0 && obj->next_input != STDIN_FILE_NO)
00558 obj->funcs->close (obj, obj->next_input);
00559
00560
00561
00562 if (obj->status == NULL)
00563 {
00564 const char *errmsg;
00565 int err;
00566
00567 obj->flags &= ~ PEX_RECORD_TIMES;
00568 pex_get_status_and_time (obj, 1, &errmsg, &err);
00569 }
00570
00571 if (obj->next_input_name_allocated)
00572 free (obj->next_input_name);
00573 if (obj->children != NULL)
00574 free (obj->children);
00575 if (obj->status != NULL)
00576 free (obj->status);
00577 if (obj->time != NULL)
00578 free (obj->time);
00579 if (obj->read_output != NULL)
00580 fclose (obj->read_output);
00581
00582 if (obj->remove_count > 0)
00583 {
00584 int i;
00585
00586 for (i = 0; i < obj->remove_count; ++i)
00587 {
00588 remove (obj->remove[i]);
00589 free (obj->remove[i]);
00590 }
00591 free (obj->remove);
00592 }
00593
00594 if (obj->funcs->cleanup != NULL)
00595 obj->funcs->cleanup (obj);
00596
00597 free (obj);
00598 }