system.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /* System-dependent calls for tar.
  2. Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 2, or (at your option) any later
  6. version.
  7. This program is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  10. Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. #include <system.h>
  15. #include <getline.h>
  16. #include <setenv.h>
  17. #include "common.h"
  18. #include <rmt.h>
  19. #include <signal.h>
  20. #if MSDOS
  21. bool
  22. sys_get_archive_stat (void)
  23. {
  24. return 0;
  25. }
  26. bool
  27. sys_file_is_archive (struct tar_stat_info *p)
  28. {
  29. return false;
  30. }
  31. void
  32. sys_save_archive_dev_ino (void)
  33. {
  34. }
  35. void
  36. sys_detect_dev_null_output (void)
  37. {
  38. static char const dev_null[] = "nul";
  39. dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0
  40. || (! _isrmt (archive)));
  41. }
  42. void
  43. sys_drain_input_pipe (void)
  44. {
  45. }
  46. void
  47. sys_wait_for_child (pid_t child_pid)
  48. {
  49. }
  50. void
  51. sys_spawn_shell (void)
  52. {
  53. spawnl (P_WAIT, getenv ("COMSPEC"), "-", 0);
  54. }
  55. /* stat() in djgpp's C library gives a constant number of 42 as the
  56. uid and gid of a file. So, comparing an FTP'ed archive just after
  57. unpack would fail on MSDOS. */
  58. bool
  59. sys_compare_uid (struct stat *a, struct stat *b)
  60. {
  61. return true;
  62. }
  63. bool
  64. sys_compare_gid (struct stat *a, struct stat *b)
  65. {
  66. return true;
  67. }
  68. void
  69. sys_compare_links (struct stat *link_data, struct stat *stat_data)
  70. {
  71. return true;
  72. }
  73. int
  74. sys_truncate (int fd)
  75. {
  76. return write (fd, "", 0);
  77. }
  78. size_t
  79. sys_write_archive_buffer (void)
  80. {
  81. return full_write (archive, record_start->buffer, record_size);
  82. }
  83. /* Set ARCHIVE for writing, then compressing an archive. */
  84. void
  85. sys_child_open_for_compress (void)
  86. {
  87. FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
  88. }
  89. /* Set ARCHIVE for uncompressing, then reading an archive. */
  90. void
  91. sys_child_open_for_uncompress (void)
  92. {
  93. FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
  94. }
  95. #else
  96. extern union block *record_start; /* FIXME */
  97. static struct stat archive_stat; /* stat block for archive file */
  98. bool
  99. sys_get_archive_stat (void)
  100. {
  101. return fstat (archive, &archive_stat) == 0;
  102. }
  103. bool
  104. sys_file_is_archive (struct tar_stat_info *p)
  105. {
  106. return (ar_dev && p->stat.st_dev == ar_dev && p->stat.st_ino == ar_ino);
  107. }
  108. /* Save archive file inode and device numbers */
  109. void
  110. sys_save_archive_dev_ino (void)
  111. {
  112. if (!_isrmt (archive) && S_ISREG (archive_stat.st_mode))
  113. {
  114. ar_dev = archive_stat.st_dev;
  115. ar_ino = archive_stat.st_ino;
  116. }
  117. else
  118. ar_dev = 0;
  119. }
  120. /* Detect if outputting to "/dev/null". */
  121. void
  122. sys_detect_dev_null_output (void)
  123. {
  124. static char const dev_null[] = "/dev/null";
  125. struct stat dev_null_stat;
  126. dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0
  127. || (! _isrmt (archive)
  128. && S_ISCHR (archive_stat.st_mode)
  129. && stat (dev_null, &dev_null_stat) == 0
  130. && archive_stat.st_dev == dev_null_stat.st_dev
  131. && archive_stat.st_ino == dev_null_stat.st_ino));
  132. }
  133. /* Manage to fully drain a pipe we might be reading, so to not break it on
  134. the producer after the EOF block. FIXME: one of these days, GNU tar
  135. might become clever enough to just stop working, once there is no more
  136. work to do, we might have to revise this area in such time. */
  137. void
  138. sys_drain_input_pipe (void)
  139. {
  140. size_t r;
  141. if (access_mode == ACCESS_READ
  142. && ! _isrmt (archive)
  143. && (S_ISFIFO (archive_stat.st_mode) || S_ISSOCK (archive_stat.st_mode)))
  144. while ((r = rmtread (archive, record_start->buffer, record_size)) != 0
  145. && r != SAFE_READ_ERROR)
  146. continue;
  147. }
  148. void
  149. sys_wait_for_child (pid_t child_pid)
  150. {
  151. if (child_pid)
  152. {
  153. int wait_status;
  154. while (waitpid (child_pid, &wait_status, 0) == -1)
  155. if (errno != EINTR)
  156. {
  157. waitpid_error (use_compress_program_option);
  158. break;
  159. }
  160. if (WIFSIGNALED (wait_status))
  161. ERROR ((0, 0, _("Child died with signal %d"),
  162. WTERMSIG (wait_status)));
  163. else if (WEXITSTATUS (wait_status) != 0)
  164. ERROR ((0, 0, _("Child returned status %d"),
  165. WEXITSTATUS (wait_status)));
  166. }
  167. }
  168. void
  169. sys_spawn_shell (void)
  170. {
  171. pid_t child;
  172. const char *shell = getenv ("SHELL");
  173. if (! shell)
  174. shell = "/bin/sh";
  175. child = xfork ();
  176. if (child == 0)
  177. {
  178. execlp (shell, "-sh", "-i", (char *) 0);
  179. exec_fatal (shell);
  180. }
  181. else
  182. {
  183. int wait_status;
  184. while (waitpid (child, &wait_status, 0) == -1)
  185. if (errno != EINTR)
  186. {
  187. waitpid_error (shell);
  188. break;
  189. }
  190. }
  191. }
  192. bool
  193. sys_compare_uid (struct stat *a, struct stat *b)
  194. {
  195. return a->st_uid == b->st_uid;
  196. }
  197. bool
  198. sys_compare_gid (struct stat *a, struct stat *b)
  199. {
  200. return a->st_gid == b->st_gid;
  201. }
  202. bool
  203. sys_compare_links (struct stat *link_data, struct stat *stat_data)
  204. {
  205. return stat_data->st_dev == link_data->st_dev
  206. && stat_data->st_ino == link_data->st_ino;
  207. }
  208. int
  209. sys_truncate (int fd)
  210. {
  211. off_t pos = lseek (fd, (off_t) 0, SEEK_CUR);
  212. return pos < 0 ? -1 : ftruncate (fd, pos);
  213. }
  214. /* Return nonzero if NAME is the name of a regular file, or if the file
  215. does not exist (so it would be created as a regular file). */
  216. static int
  217. is_regular_file (const char *name)
  218. {
  219. struct stat stbuf;
  220. if (stat (name, &stbuf) == 0)
  221. return S_ISREG (stbuf.st_mode);
  222. else
  223. return errno == ENOENT;
  224. }
  225. size_t
  226. sys_write_archive_buffer (void)
  227. {
  228. return rmtwrite (archive, record_start->buffer, record_size);
  229. }
  230. #define PREAD 0 /* read file descriptor from pipe() */
  231. #define PWRITE 1 /* write file descriptor from pipe() */
  232. /* Duplicate file descriptor FROM into becoming INTO.
  233. INTO is closed first and has to be the next available slot. */
  234. static void
  235. xdup2 (int from, int into)
  236. {
  237. if (from != into)
  238. {
  239. int status = close (into);
  240. if (status != 0 && errno != EBADF)
  241. {
  242. int e = errno;
  243. FATAL_ERROR ((0, e, _("Cannot close")));
  244. }
  245. status = dup (from);
  246. if (status != into)
  247. {
  248. if (status < 0)
  249. {
  250. int e = errno;
  251. FATAL_ERROR ((0, e, _("Cannot dup")));
  252. }
  253. abort ();
  254. }
  255. xclose (from);
  256. }
  257. }
  258. /* Set ARCHIVE for writing, then compressing an archive. */
  259. pid_t
  260. sys_child_open_for_compress (void)
  261. {
  262. int parent_pipe[2];
  263. int child_pipe[2];
  264. pid_t grandchild_pid;
  265. pid_t child_pid;
  266. int wait_status;
  267. xpipe (parent_pipe);
  268. child_pid = xfork ();
  269. if (child_pid > 0)
  270. {
  271. /* The parent tar is still here! Just clean up. */
  272. archive = parent_pipe[PWRITE];
  273. xclose (parent_pipe[PREAD]);
  274. return child_pid;
  275. }
  276. /* The new born child tar is here! */
  277. program_name = _("tar (child)");
  278. xdup2 (parent_pipe[PREAD], STDIN_FILENO);
  279. xclose (parent_pipe[PWRITE]);
  280. /* Check if we need a grandchild tar. This happens only if either:
  281. a) the file is to be accessed by rmt: compressor doesn't know how;
  282. b) the file is not a plain file. */
  283. if (!_remdev (archive_name_array[0])
  284. && is_regular_file (archive_name_array[0]))
  285. {
  286. if (backup_option)
  287. maybe_backup_file (archive_name_array[0], 1);
  288. /* We don't need a grandchild tar. Open the archive and launch the
  289. compressor. */
  290. if (strcmp (archive_name_array[0], "-"))
  291. {
  292. archive = creat (archive_name_array[0], MODE_RW);
  293. if (archive < 0)
  294. {
  295. int saved_errno = errno;
  296. if (backup_option)
  297. undo_last_backup ();
  298. errno = saved_errno;
  299. open_fatal (archive_name_array[0]);
  300. }
  301. xdup2 (archive, STDOUT_FILENO);
  302. }
  303. execlp (use_compress_program_option, use_compress_program_option, NULL);
  304. exec_fatal (use_compress_program_option);
  305. }
  306. /* We do need a grandchild tar. */
  307. xpipe (child_pipe);
  308. grandchild_pid = xfork ();
  309. if (grandchild_pid == 0)
  310. {
  311. /* The newborn grandchild tar is here! Launch the compressor. */
  312. program_name = _("tar (grandchild)");
  313. xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
  314. xclose (child_pipe[PREAD]);
  315. execlp (use_compress_program_option, use_compress_program_option,
  316. (char *) 0);
  317. exec_fatal (use_compress_program_option);
  318. }
  319. /* The child tar is still here! */
  320. /* Prepare for reblocking the data from the compressor into the archive. */
  321. xdup2 (child_pipe[PREAD], STDIN_FILENO);
  322. xclose (child_pipe[PWRITE]);
  323. if (strcmp (archive_name_array[0], "-") == 0)
  324. archive = STDOUT_FILENO;
  325. else
  326. {
  327. archive = rmtcreat (archive_name_array[0], MODE_RW, rsh_command_option);
  328. if (archive < 0)
  329. open_fatal (archive_name_array[0]);
  330. }
  331. /* Let's read out of the stdin pipe and write an archive. */
  332. while (1)
  333. {
  334. size_t status = 0;
  335. char *cursor;
  336. size_t length;
  337. /* Assemble a record. */
  338. for (length = 0, cursor = record_start->buffer;
  339. length < record_size;
  340. length += status, cursor += status)
  341. {
  342. size_t size = record_size - length;
  343. status = safe_read (STDIN_FILENO, cursor, size);
  344. if (status == SAFE_READ_ERROR)
  345. read_fatal (use_compress_program_option);
  346. if (status == 0)
  347. break;
  348. }
  349. /* Copy the record. */
  350. if (status == 0)
  351. {
  352. /* We hit the end of the file. Write last record at
  353. full length, as the only role of the grandchild is
  354. doing proper reblocking. */
  355. if (length > 0)
  356. {
  357. memset (record_start->buffer + length, 0, record_size - length);
  358. status = sys_write_archive_buffer ();
  359. if (status != record_size)
  360. archive_write_error (status);
  361. }
  362. /* There is nothing else to read, break out. */
  363. break;
  364. }
  365. status = sys_write_archive_buffer ();
  366. if (status != record_size)
  367. archive_write_error (status);
  368. }
  369. /* Propagate any failure of the grandchild back to the parent. */
  370. while (waitpid (grandchild_pid, &wait_status, 0) == -1)
  371. if (errno != EINTR)
  372. {
  373. waitpid_error (use_compress_program_option);
  374. break;
  375. }
  376. if (WIFSIGNALED (wait_status))
  377. {
  378. kill (child_pid, WTERMSIG (wait_status));
  379. exit_status = TAREXIT_FAILURE;
  380. }
  381. else if (WEXITSTATUS (wait_status) != 0)
  382. exit_status = WEXITSTATUS (wait_status);
  383. exit (exit_status);
  384. }
  385. /* Set ARCHIVE for uncompressing, then reading an archive. */
  386. pid_t
  387. sys_child_open_for_uncompress (void)
  388. {
  389. int parent_pipe[2];
  390. int child_pipe[2];
  391. pid_t grandchild_pid;
  392. pid_t child_pid;
  393. int wait_status;
  394. xpipe (parent_pipe);
  395. child_pid = xfork ();
  396. if (child_pid > 0)
  397. {
  398. /* The parent tar is still here! Just clean up. */
  399. archive = parent_pipe[PREAD];
  400. xclose (parent_pipe[PWRITE]);
  401. return child_pid;
  402. }
  403. /* The newborn child tar is here! */
  404. program_name = _("tar (child)");
  405. xdup2 (parent_pipe[PWRITE], STDOUT_FILENO);
  406. xclose (parent_pipe[PREAD]);
  407. /* Check if we need a grandchild tar. This happens only if either:
  408. a) we're reading stdin: to force unblocking;
  409. b) the file is to be accessed by rmt: compressor doesn't know how;
  410. c) the file is not a plain file. */
  411. if (strcmp (archive_name_array[0], "-") != 0
  412. && !_remdev (archive_name_array[0])
  413. && is_regular_file (archive_name_array[0]))
  414. {
  415. /* We don't need a grandchild tar. Open the archive and lauch the
  416. uncompressor. */
  417. archive = open (archive_name_array[0], O_RDONLY | O_BINARY, MODE_RW);
  418. if (archive < 0)
  419. open_fatal (archive_name_array[0]);
  420. xdup2 (archive, STDIN_FILENO);
  421. execlp (use_compress_program_option, use_compress_program_option,
  422. "-d", (char *) 0);
  423. exec_fatal (use_compress_program_option);
  424. }
  425. /* We do need a grandchild tar. */
  426. xpipe (child_pipe);
  427. grandchild_pid = xfork ();
  428. if (grandchild_pid == 0)
  429. {
  430. /* The newborn grandchild tar is here! Launch the uncompressor. */
  431. program_name = _("tar (grandchild)");
  432. xdup2 (child_pipe[PREAD], STDIN_FILENO);
  433. xclose (child_pipe[PWRITE]);
  434. execlp (use_compress_program_option, use_compress_program_option,
  435. "-d", (char *) 0);
  436. exec_fatal (use_compress_program_option);
  437. }
  438. /* The child tar is still here! */
  439. /* Prepare for unblocking the data from the archive into the
  440. uncompressor. */
  441. xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
  442. xclose (child_pipe[PREAD]);
  443. if (strcmp (archive_name_array[0], "-") == 0)
  444. archive = STDIN_FILENO;
  445. else
  446. archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
  447. MODE_RW, rsh_command_option);
  448. if (archive < 0)
  449. open_fatal (archive_name_array[0]);
  450. /* Let's read the archive and pipe it into stdout. */
  451. while (1)
  452. {
  453. char *cursor;
  454. size_t maximum;
  455. size_t count;
  456. size_t status;
  457. clear_read_error_count ();
  458. error_loop:
  459. status = rmtread (archive, record_start->buffer, record_size);
  460. if (status == SAFE_READ_ERROR)
  461. {
  462. archive_read_error ();
  463. goto error_loop;
  464. }
  465. if (status == 0)
  466. break;
  467. cursor = record_start->buffer;
  468. maximum = status;
  469. while (maximum)
  470. {
  471. count = maximum < BLOCKSIZE ? maximum : BLOCKSIZE;
  472. if (full_write (STDOUT_FILENO, cursor, count) != count)
  473. write_error (use_compress_program_option);
  474. cursor += count;
  475. maximum -= count;
  476. }
  477. }
  478. xclose (STDOUT_FILENO);
  479. /* Propagate any failure of the grandchild back to the parent. */
  480. while (waitpid (grandchild_pid, &wait_status, 0) == -1)
  481. if (errno != EINTR)
  482. {
  483. waitpid_error (use_compress_program_option);
  484. break;
  485. }
  486. if (WIFSIGNALED (wait_status))
  487. {
  488. kill (child_pid, WTERMSIG (wait_status));
  489. exit_status = TAREXIT_FAILURE;
  490. }
  491. else if (WEXITSTATUS (wait_status) != 0)
  492. exit_status = WEXITSTATUS (wait_status);
  493. exit (exit_status);
  494. }
  495. static void
  496. dec_to_env (char *envar, uintmax_t num)
  497. {
  498. char buf[UINTMAX_STRSIZE_BOUND];
  499. char *numstr;
  500. numstr = STRINGIFY_BIGINT (num, buf);
  501. if (setenv (envar, numstr, 1) != 0)
  502. xalloc_die ();
  503. }
  504. static void
  505. time_to_env (char *envar, struct timespec t)
  506. {
  507. char buf[TIMESPEC_STRSIZE_BOUND];
  508. if (setenv (envar, code_timespec (t, buf), 1) != 0)
  509. xalloc_die ();
  510. }
  511. static void
  512. oct_to_env (char *envar, unsigned long num)
  513. {
  514. char buf[1+1+(sizeof(unsigned long)*CHAR_BIT+2)/3];
  515. snprintf (buf, sizeof buf, "0%lo", num);
  516. if (setenv (envar, buf, 1) != 0)
  517. xalloc_die ();
  518. }
  519. static void
  520. str_to_env (char *envar, char const *str)
  521. {
  522. if (str)
  523. {
  524. if (setenv (envar, str, 1) != 0)
  525. xalloc_die ();
  526. }
  527. else
  528. unsetenv (envar);
  529. }
  530. static void
  531. chr_to_env (char *envar, char c)
  532. {
  533. char buf[2];
  534. buf[0] = c;
  535. buf[1] = 0;
  536. if (setenv (envar, buf, 1) != 0)
  537. xalloc_die ();
  538. }
  539. static void
  540. stat_to_env (char *name, char type, struct tar_stat_info *st)
  541. {
  542. str_to_env ("TAR_VERSION", PACKAGE_VERSION);
  543. chr_to_env ("TAR_FILETYPE", type);
  544. oct_to_env ("TAR_MODE", st->stat.st_mode);
  545. str_to_env ("TAR_FILENAME", name);
  546. str_to_env ("TAR_REALNAME", st->file_name);
  547. str_to_env ("TAR_UNAME", st->uname);
  548. str_to_env ("TAR_GNAME", st->gname);
  549. time_to_env ("TAR_ATIME", st->atime);
  550. time_to_env ("TAR_MTIME", st->mtime);
  551. time_to_env ("TAR_CTIME", st->ctime);
  552. dec_to_env ("TAR_SIZE", st->stat.st_size);
  553. dec_to_env ("TAR_UID", st->stat.st_uid);
  554. dec_to_env ("TAR_GID", st->stat.st_gid);
  555. switch (type)
  556. {
  557. case 'b':
  558. case 'c':
  559. dec_to_env ("TAR_MINOR", minor (st->stat.st_rdev));
  560. dec_to_env ("TAR_MAJOR", major (st->stat.st_rdev));
  561. unsetenv ("TAR_LINKNAME");
  562. break;
  563. case 'l':
  564. case 'h':
  565. unsetenv ("TAR_MINOR");
  566. unsetenv ("TAR_MAJOR");
  567. str_to_env ("TAR_LINKNAME", st->link_name);
  568. break;
  569. default:
  570. unsetenv ("TAR_MINOR");
  571. unsetenv ("TAR_MAJOR");
  572. unsetenv ("TAR_LINKNAME");
  573. break;
  574. }
  575. }
  576. static pid_t pid;
  577. static RETSIGTYPE (*pipe_handler) (int sig);
  578. int
  579. sys_exec_command (char *file_name, int typechar, struct tar_stat_info *st)
  580. {
  581. int p[2];
  582. char *argv[4];
  583. xpipe (p);
  584. pipe_handler = signal (SIGPIPE, SIG_IGN);
  585. pid = xfork ();
  586. if (pid != 0)
  587. {
  588. xclose (p[PREAD]);
  589. return p[PWRITE];
  590. }
  591. /* Child */
  592. xdup2 (p[PREAD], STDIN_FILENO);
  593. xclose (p[PWRITE]);
  594. stat_to_env (file_name, typechar, st);
  595. argv[0] = "/bin/sh";
  596. argv[1] = "-c";
  597. argv[2] = to_command_option;
  598. argv[3] = NULL;
  599. execv ("/bin/sh", argv);
  600. exec_fatal (file_name);
  601. }
  602. void
  603. sys_wait_command (void)
  604. {
  605. int status;
  606. if (pid < 0)
  607. return;
  608. signal (SIGPIPE, pipe_handler);
  609. while (waitpid (pid, &status, 0) == -1)
  610. if (errno != EINTR)
  611. {
  612. pid = -1;
  613. waitpid_error (to_command_option);
  614. return;
  615. }
  616. if (WIFEXITED (status))
  617. {
  618. if (!ignore_command_error_option && WEXITSTATUS (status))
  619. ERROR ((0, 0, _("%lu: Child returned status %d"),
  620. (unsigned long) pid, WEXITSTATUS (status)));
  621. }
  622. else if (WIFSIGNALED (status))
  623. {
  624. WARN ((0, 0, _("%lu: Child terminated on signal %d"),
  625. (unsigned long) pid, WTERMSIG (status)));
  626. }
  627. else
  628. ERROR ((0, 0, _("%lu: Child terminated on unknown reason"),
  629. (unsigned long) pid));
  630. pid = -1;
  631. }
  632. int
  633. sys_exec_info_script (const char **archive_name, int volume_number)
  634. {
  635. pid_t pid;
  636. char *argv[4];
  637. char uintbuf[UINTMAX_STRSIZE_BOUND];
  638. int p[2];
  639. xpipe (p);
  640. pipe_handler = signal (SIGPIPE, SIG_IGN);
  641. pid = xfork ();
  642. if (pid != 0)
  643. {
  644. /* Master */
  645. int rc;
  646. int status;
  647. char *buf;
  648. size_t size = 0;
  649. FILE *fp;
  650. xclose (p[PWRITE]);
  651. fp = fdopen (p[PREAD], "r");
  652. rc = getline (&buf, &size, fp);
  653. fclose (fp);
  654. if (rc > 0 && buf[rc-1] == '\n')
  655. buf[--rc] = 0;
  656. while (waitpid (pid, &status, 0) == -1)
  657. if (errno != EINTR)
  658. {
  659. waitpid_error (info_script_option);
  660. return -1;
  661. }
  662. if (WIFEXITED (status))
  663. {
  664. if (WEXITSTATUS (status) == 0 && rc > 0)
  665. *archive_name = buf;
  666. else
  667. free (buf);
  668. return WEXITSTATUS (status);
  669. }
  670. free (buf);
  671. return -1;
  672. }
  673. /* Child */
  674. setenv ("TAR_VERSION", PACKAGE_VERSION, 1);
  675. setenv ("TAR_ARCHIVE", *archive_name, 1);
  676. setenv ("TAR_VOLUME", STRINGIFY_BIGINT (volume_number, uintbuf), 1);
  677. setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option), 1);
  678. setenv ("TAR_FORMAT",
  679. archive_format_string (current_format == DEFAULT_FORMAT ?
  680. archive_format : current_format), 1);
  681. xclose (p[PREAD]);
  682. xdup2 (p[PWRITE], 3);
  683. argv[0] = "/bin/sh";
  684. argv[1] = "-c";
  685. argv[2] = (char*) info_script_option;
  686. argv[3] = NULL;
  687. execv (argv[0], argv);
  688. exec_fatal (info_script_option);
  689. }
  690. #endif /* not MSDOS */