system.c 17 KB

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