system.c 20 KB

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