system.c 20 KB

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