system.c 20 KB

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