Support for POSIX ACLs
[tar.git] / src / system.c
blobba4ac2d391faf25966ecff4905cebc8344231add
1 /* System-dependent calls for tar.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007,
4 2008, 2010 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any later
9 version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 #include <system.h>
22 #include "common.h"
23 #include <priv-set.h>
24 #include <rmt.h>
25 #include <signal.h>
27 #if MSDOS
29 bool
30 sys_get_archive_stat (void)
32 return 0;
35 bool
36 sys_file_is_archive (struct tar_stat_info *p)
38 return false;
41 void
42 sys_save_archive_dev_ino (void)
46 void
47 sys_detect_dev_null_output (void)
49 static char const dev_null[] = "nul";
51 dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0
52 || (! _isrmt (archive)));
55 void
56 sys_wait_for_child (pid_t child_pid, bool eof)
60 void
61 sys_spawn_shell (void)
63 spawnl (P_WAIT, getenv ("COMSPEC"), "-", 0);
66 /* stat() in djgpp's C library gives a constant number of 42 as the
67 uid and gid of a file. So, comparing an FTP'ed archive just after
68 unpack would fail on MSDOS. */
70 bool
71 sys_compare_uid (struct stat *a, struct stat *b)
73 return true;
76 bool
77 sys_compare_gid (struct stat *a, struct stat *b)
79 return true;
82 void
83 sys_compare_links (struct stat *link_data, struct stat *stat_data)
85 return true;
88 int
89 sys_truncate (int fd)
91 return write (fd, "", 0);
94 size_t
95 sys_write_archive_buffer (void)
97 return full_write (archive, record_start->buffer, record_size);
100 /* Set ARCHIVE for writing, then compressing an archive. */
101 void
102 sys_child_open_for_compress (void)
104 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
107 /* Set ARCHIVE for uncompressing, then reading an archive. */
108 void
109 sys_child_open_for_uncompress (void)
111 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
114 #else
116 extern union block *record_start; /* FIXME */
118 static struct stat archive_stat; /* stat block for archive file */
120 bool
121 sys_get_archive_stat (void)
123 return fstat (archive, &archive_stat) == 0;
126 bool
127 sys_file_is_archive (struct tar_stat_info *p)
129 return (ar_dev && p->stat.st_dev == ar_dev && p->stat.st_ino == ar_ino);
132 /* Save archive file inode and device numbers */
133 void
134 sys_save_archive_dev_ino (void)
136 if (!_isrmt (archive) && S_ISREG (archive_stat.st_mode))
138 ar_dev = archive_stat.st_dev;
139 ar_ino = archive_stat.st_ino;
141 else
142 ar_dev = 0;
145 /* Detect if outputting to "/dev/null". */
146 void
147 sys_detect_dev_null_output (void)
149 static char const dev_null[] = "/dev/null";
150 struct stat dev_null_stat;
152 dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0
153 || (! _isrmt (archive)
154 && S_ISCHR (archive_stat.st_mode)
155 && stat (dev_null, &dev_null_stat) == 0
156 && archive_stat.st_dev == dev_null_stat.st_dev
157 && archive_stat.st_ino == dev_null_stat.st_ino));
160 void
161 sys_wait_for_child (pid_t child_pid, bool eof)
163 if (child_pid)
165 int wait_status;
167 while (waitpid (child_pid, &wait_status, 0) == -1)
168 if (errno != EINTR)
170 waitpid_error (use_compress_program_option);
171 break;
174 if (WIFSIGNALED (wait_status))
176 int sig = WTERMSIG (wait_status);
177 if (!(!eof && sig == SIGPIPE))
178 FATAL_ERROR ((0, 0, _("Child died with signal %d"), sig));
180 else if (WEXITSTATUS (wait_status) != 0)
181 FATAL_ERROR ((0, 0, _("Child returned status %d"),
182 WEXITSTATUS (wait_status)));
186 void
187 sys_spawn_shell (void)
189 pid_t child;
190 const char *shell = getenv ("SHELL");
191 if (! shell)
192 shell = "/bin/sh";
193 child = xfork ();
194 if (child == 0)
196 priv_set_restore_linkdir ();
197 execlp (shell, "-sh", "-i", (char *) 0);
198 exec_fatal (shell);
200 else
202 int wait_status;
203 while (waitpid (child, &wait_status, 0) == -1)
204 if (errno != EINTR)
206 waitpid_error (shell);
207 break;
212 bool
213 sys_compare_uid (struct stat *a, struct stat *b)
215 return a->st_uid == b->st_uid;
218 bool
219 sys_compare_gid (struct stat *a, struct stat *b)
221 return a->st_gid == b->st_gid;
224 bool
225 sys_compare_links (struct stat *link_data, struct stat *stat_data)
227 return stat_data->st_dev == link_data->st_dev
228 && stat_data->st_ino == link_data->st_ino;
232 sys_truncate (int fd)
234 off_t pos = lseek (fd, (off_t) 0, SEEK_CUR);
235 return pos < 0 ? -1 : ftruncate (fd, pos);
238 /* Return nonzero if NAME is the name of a regular file, or if the file
239 does not exist (so it would be created as a regular file). */
240 static int
241 is_regular_file (const char *name)
243 struct stat stbuf;
245 if (stat (name, &stbuf) == 0)
246 return S_ISREG (stbuf.st_mode);
247 else
248 return errno == ENOENT;
251 size_t
252 sys_write_archive_buffer (void)
254 return rmtwrite (archive, record_start->buffer, record_size);
257 #define PREAD 0 /* read file descriptor from pipe() */
258 #define PWRITE 1 /* write file descriptor from pipe() */
260 /* Duplicate file descriptor FROM into becoming INTO.
261 INTO is closed first and has to be the next available slot. */
262 static void
263 xdup2 (int from, int into)
265 if (from != into)
267 int status = close (into);
269 if (status != 0 && errno != EBADF)
271 int e = errno;
272 FATAL_ERROR ((0, e, _("Cannot close")));
274 status = dup (from);
275 if (status != into)
277 if (status < 0)
279 int e = errno;
280 FATAL_ERROR ((0, e, _("Cannot dup")));
282 abort ();
284 xclose (from);
288 static void wait_for_grandchild (pid_t pid) __attribute__ ((__noreturn__));
290 /* Propagate any failure of the grandchild back to the parent. */
291 static void
292 wait_for_grandchild (pid_t pid)
294 int wait_status;
295 int exit_code = 0;
297 while (waitpid (pid, &wait_status, 0) == -1)
298 if (errno != EINTR)
300 waitpid_error (use_compress_program_option);
301 break;
304 if (WIFSIGNALED (wait_status))
305 raise (WTERMSIG (wait_status));
306 else if (WEXITSTATUS (wait_status) != 0)
307 exit_code = WEXITSTATUS (wait_status);
309 exit (exit_code);
312 /* Set ARCHIVE for writing, then compressing an archive. */
313 pid_t
314 sys_child_open_for_compress (void)
316 int parent_pipe[2];
317 int child_pipe[2];
318 pid_t grandchild_pid;
319 pid_t child_pid;
321 xpipe (parent_pipe);
322 child_pid = xfork ();
324 if (child_pid > 0)
326 /* The parent tar is still here! Just clean up. */
328 archive = parent_pipe[PWRITE];
329 xclose (parent_pipe[PREAD]);
330 return child_pid;
333 /* The new born child tar is here! */
335 set_program_name (_("tar (child)"));
336 signal (SIGPIPE, SIG_DFL);
338 xdup2 (parent_pipe[PREAD], STDIN_FILENO);
339 xclose (parent_pipe[PWRITE]);
341 /* Check if we need a grandchild tar. This happens only if either:
342 a) the file is to be accessed by rmt: compressor doesn't know how;
343 b) the file is not a plain file. */
345 if (!_remdev (archive_name_array[0])
346 && is_regular_file (archive_name_array[0]))
348 if (backup_option)
349 maybe_backup_file (archive_name_array[0], 1);
351 /* We don't need a grandchild tar. Open the archive and launch the
352 compressor. */
353 if (strcmp (archive_name_array[0], "-"))
355 archive = creat (archive_name_array[0], MODE_RW);
356 if (archive < 0)
358 int saved_errno = errno;
360 if (backup_option)
361 undo_last_backup ();
362 errno = saved_errno;
363 open_fatal (archive_name_array[0]);
365 xdup2 (archive, STDOUT_FILENO);
367 priv_set_restore_linkdir ();
368 execlp (use_compress_program_option, use_compress_program_option, NULL);
369 exec_fatal (use_compress_program_option);
372 /* We do need a grandchild tar. */
374 xpipe (child_pipe);
375 grandchild_pid = xfork ();
377 if (grandchild_pid == 0)
379 /* The newborn grandchild tar is here! Launch the compressor. */
381 set_program_name (_("tar (grandchild)"));
383 xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
384 xclose (child_pipe[PREAD]);
385 priv_set_restore_linkdir ();
386 execlp (use_compress_program_option, use_compress_program_option,
387 (char *) 0);
388 exec_fatal (use_compress_program_option);
391 /* The child tar is still here! */
393 /* Prepare for reblocking the data from the compressor into the archive. */
395 xdup2 (child_pipe[PREAD], STDIN_FILENO);
396 xclose (child_pipe[PWRITE]);
398 if (strcmp (archive_name_array[0], "-") == 0)
399 archive = STDOUT_FILENO;
400 else
402 archive = rmtcreat (archive_name_array[0], MODE_RW, rsh_command_option);
403 if (archive < 0)
404 open_fatal (archive_name_array[0]);
407 /* Let's read out of the stdin pipe and write an archive. */
409 while (1)
411 size_t status = 0;
412 char *cursor;
413 size_t length;
415 /* Assemble a record. */
417 for (length = 0, cursor = record_start->buffer;
418 length < record_size;
419 length += status, cursor += status)
421 size_t size = record_size - length;
423 status = safe_read (STDIN_FILENO, cursor, size);
424 if (status == SAFE_READ_ERROR)
425 read_fatal (use_compress_program_option);
426 if (status == 0)
427 break;
430 /* Copy the record. */
432 if (status == 0)
434 /* We hit the end of the file. Write last record at
435 full length, as the only role of the grandchild is
436 doing proper reblocking. */
438 if (length > 0)
440 memset (record_start->buffer + length, 0, record_size - length);
441 status = sys_write_archive_buffer ();
442 if (status != record_size)
443 archive_write_error (status);
446 /* There is nothing else to read, break out. */
447 break;
450 status = sys_write_archive_buffer ();
451 if (status != record_size)
452 archive_write_error (status);
455 wait_for_grandchild (grandchild_pid);
458 static void
459 run_decompress_program (void)
461 int i;
462 const char *p, *prog = NULL;
464 for (p = first_decompress_program (&i); p; p = next_decompress_program (&i))
466 if (prog)
468 WARNOPT (WARN_DECOMPRESS_PROGRAM,
469 (0, errno, _("cannot run %s"), prog));
470 WARNOPT (WARN_DECOMPRESS_PROGRAM,
471 (0, 0, _("trying %s"), p));
473 prog = p;
474 execlp (p, p, "-d", NULL);
476 if (!prog)
477 FATAL_ERROR ((0, 0, _("unable to run decompression program")));
478 exec_fatal (prog);
481 /* Set ARCHIVE for uncompressing, then reading an archive. */
482 pid_t
483 sys_child_open_for_uncompress (void)
485 int parent_pipe[2];
486 int child_pipe[2];
487 pid_t grandchild_pid;
488 pid_t child_pid;
490 xpipe (parent_pipe);
491 child_pid = xfork ();
493 if (child_pid > 0)
495 /* The parent tar is still here! Just clean up. */
497 archive = parent_pipe[PREAD];
498 xclose (parent_pipe[PWRITE]);
499 return child_pid;
502 /* The newborn child tar is here! */
504 set_program_name (_("tar (child)"));
505 signal (SIGPIPE, SIG_DFL);
507 xdup2 (parent_pipe[PWRITE], STDOUT_FILENO);
508 xclose (parent_pipe[PREAD]);
510 /* Check if we need a grandchild tar. This happens only if either:
511 a) we're reading stdin: to force unblocking;
512 b) the file is to be accessed by rmt: compressor doesn't know how;
513 c) the file is not a plain file. */
515 if (strcmp (archive_name_array[0], "-") != 0
516 && !_remdev (archive_name_array[0])
517 && is_regular_file (archive_name_array[0]))
519 /* We don't need a grandchild tar. Open the archive and lauch the
520 uncompressor. */
522 archive = open (archive_name_array[0], O_RDONLY | O_BINARY, MODE_RW);
523 if (archive < 0)
524 open_fatal (archive_name_array[0]);
525 xdup2 (archive, STDIN_FILENO);
526 priv_set_restore_linkdir ();
527 run_decompress_program ();
530 /* We do need a grandchild tar. */
532 xpipe (child_pipe);
533 grandchild_pid = xfork ();
535 if (grandchild_pid == 0)
537 /* The newborn grandchild tar is here! Launch the uncompressor. */
539 set_program_name (_("tar (grandchild)"));
541 xdup2 (child_pipe[PREAD], STDIN_FILENO);
542 xclose (child_pipe[PWRITE]);
543 priv_set_restore_linkdir ();
544 run_decompress_program ();
547 /* The child tar is still here! */
549 /* Prepare for unblocking the data from the archive into the
550 uncompressor. */
552 xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
553 xclose (child_pipe[PREAD]);
555 if (strcmp (archive_name_array[0], "-") == 0)
556 archive = STDIN_FILENO;
557 else
558 archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
559 MODE_RW, rsh_command_option);
560 if (archive < 0)
561 open_fatal (archive_name_array[0]);
563 /* Let's read the archive and pipe it into stdout. */
565 while (1)
567 char *cursor;
568 size_t maximum;
569 size_t count;
570 size_t status;
572 clear_read_error_count ();
574 error_loop:
575 status = rmtread (archive, record_start->buffer, record_size);
576 if (status == SAFE_READ_ERROR)
578 archive_read_error ();
579 goto error_loop;
581 if (status == 0)
582 break;
583 cursor = record_start->buffer;
584 maximum = status;
585 while (maximum)
587 count = maximum < BLOCKSIZE ? maximum : BLOCKSIZE;
588 if (full_write (STDOUT_FILENO, cursor, count) != count)
589 write_error (use_compress_program_option);
590 cursor += count;
591 maximum -= count;
595 xclose (STDOUT_FILENO);
597 wait_for_grandchild (grandchild_pid);
602 static void
603 dec_to_env (char const *envar, uintmax_t num)
605 char buf[UINTMAX_STRSIZE_BOUND];
606 char *numstr;
608 numstr = STRINGIFY_BIGINT (num, buf);
609 if (setenv (envar, numstr, 1) != 0)
610 xalloc_die ();
613 static void
614 time_to_env (char const *envar, struct timespec t)
616 char buf[TIMESPEC_STRSIZE_BOUND];
617 if (setenv (envar, code_timespec (t, buf), 1) != 0)
618 xalloc_die ();
621 static void
622 oct_to_env (char const *envar, unsigned long num)
624 char buf[1+1+(sizeof(unsigned long)*CHAR_BIT+2)/3];
626 snprintf (buf, sizeof buf, "0%lo", num);
627 if (setenv (envar, buf, 1) != 0)
628 xalloc_die ();
631 static void
632 str_to_env (char const *envar, char const *str)
634 if (str)
636 if (setenv (envar, str, 1) != 0)
637 xalloc_die ();
639 else
640 unsetenv (envar);
643 static void
644 chr_to_env (char const *envar, char c)
646 char buf[2];
647 buf[0] = c;
648 buf[1] = 0;
649 if (setenv (envar, buf, 1) != 0)
650 xalloc_die ();
653 static void
654 stat_to_env (char *name, char type, struct tar_stat_info *st)
656 str_to_env ("TAR_VERSION", PACKAGE_VERSION);
657 str_to_env ("TAR_ARCHIVE", *archive_name_cursor);
658 dec_to_env ("TAR_VOLUME", archive_name_cursor - archive_name_array + 1);
659 dec_to_env ("TAR_BLOCKING_FACTOR", blocking_factor);
660 str_to_env ("TAR_FORMAT",
661 archive_format_string (current_format == DEFAULT_FORMAT ?
662 archive_format : current_format));
663 chr_to_env ("TAR_FILETYPE", type);
664 oct_to_env ("TAR_MODE", st->stat.st_mode);
665 str_to_env ("TAR_FILENAME", name);
666 str_to_env ("TAR_REALNAME", st->file_name);
667 str_to_env ("TAR_UNAME", st->uname);
668 str_to_env ("TAR_GNAME", st->gname);
669 time_to_env ("TAR_ATIME", st->atime);
670 time_to_env ("TAR_MTIME", st->mtime);
671 time_to_env ("TAR_CTIME", st->ctime);
672 dec_to_env ("TAR_SIZE", st->stat.st_size);
673 dec_to_env ("TAR_UID", st->stat.st_uid);
674 dec_to_env ("TAR_GID", st->stat.st_gid);
676 switch (type)
678 case 'b':
679 case 'c':
680 dec_to_env ("TAR_MINOR", minor (st->stat.st_rdev));
681 dec_to_env ("TAR_MAJOR", major (st->stat.st_rdev));
682 unsetenv ("TAR_LINKNAME");
683 break;
685 case 'l':
686 case 'h':
687 unsetenv ("TAR_MINOR");
688 unsetenv ("TAR_MAJOR");
689 str_to_env ("TAR_LINKNAME", st->link_name);
690 break;
692 default:
693 unsetenv ("TAR_MINOR");
694 unsetenv ("TAR_MAJOR");
695 unsetenv ("TAR_LINKNAME");
696 break;
700 static pid_t global_pid;
701 static RETSIGTYPE (*pipe_handler) (int sig);
704 sys_exec_command (char *file_name, int typechar, struct tar_stat_info *st)
706 int p[2];
707 char *argv[4];
709 xpipe (p);
710 pipe_handler = signal (SIGPIPE, SIG_IGN);
711 global_pid = xfork ();
713 if (global_pid != 0)
715 xclose (p[PREAD]);
716 return p[PWRITE];
719 /* Child */
720 xdup2 (p[PREAD], STDIN_FILENO);
721 xclose (p[PWRITE]);
723 stat_to_env (file_name, typechar, st);
725 argv[0] = "/bin/sh";
726 argv[1] = "-c";
727 argv[2] = to_command_option;
728 argv[3] = NULL;
730 priv_set_restore_linkdir ();
731 execv ("/bin/sh", argv);
733 exec_fatal (file_name);
736 void
737 sys_wait_command (void)
739 int status;
741 if (global_pid < 0)
742 return;
744 signal (SIGPIPE, pipe_handler);
745 while (waitpid (global_pid, &status, 0) == -1)
746 if (errno != EINTR)
748 global_pid = -1;
749 waitpid_error (to_command_option);
750 return;
753 if (WIFEXITED (status))
755 if (!ignore_command_error_option && WEXITSTATUS (status))
756 ERROR ((0, 0, _("%lu: Child returned status %d"),
757 (unsigned long) global_pid, WEXITSTATUS (status)));
759 else if (WIFSIGNALED (status))
761 WARN ((0, 0, _("%lu: Child terminated on signal %d"),
762 (unsigned long) global_pid, WTERMSIG (status)));
764 else
765 ERROR ((0, 0, _("%lu: Child terminated on unknown reason"),
766 (unsigned long) global_pid));
768 global_pid = -1;
772 sys_exec_info_script (const char **archive_name, int volume_number)
774 pid_t pid;
775 char *argv[4];
776 char uintbuf[UINTMAX_STRSIZE_BOUND];
777 int p[2];
778 static RETSIGTYPE (*saved_handler) (int sig);
780 xpipe (p);
781 saved_handler = signal (SIGPIPE, SIG_IGN);
783 pid = xfork ();
785 if (pid != 0)
787 /* Master */
789 int rc;
790 int status;
791 char *buf = NULL;
792 size_t size = 0;
793 FILE *fp;
795 xclose (p[PWRITE]);
796 fp = fdopen (p[PREAD], "r");
797 rc = getline (&buf, &size, fp);
798 fclose (fp);
800 if (rc > 0 && buf[rc-1] == '\n')
801 buf[--rc] = 0;
803 while (waitpid (pid, &status, 0) == -1)
804 if (errno != EINTR)
806 signal (SIGPIPE, saved_handler);
807 waitpid_error (info_script_option);
808 return -1;
811 signal (SIGPIPE, saved_handler);
813 if (WIFEXITED (status))
815 if (WEXITSTATUS (status) == 0 && rc > 0)
816 *archive_name = buf;
817 else
818 free (buf);
819 return WEXITSTATUS (status);
822 free (buf);
823 return -1;
826 /* Child */
827 setenv ("TAR_VERSION", PACKAGE_VERSION, 1);
828 setenv ("TAR_ARCHIVE", *archive_name, 1);
829 setenv ("TAR_VOLUME", STRINGIFY_BIGINT (volume_number, uintbuf), 1);
830 setenv ("TAR_BLOCKING_FACTOR",
831 STRINGIFY_BIGINT (blocking_factor, uintbuf), 1);
832 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option), 1);
833 setenv ("TAR_FORMAT",
834 archive_format_string (current_format == DEFAULT_FORMAT ?
835 archive_format : current_format), 1);
836 setenv ("TAR_FD", STRINGIFY_BIGINT (p[PWRITE], uintbuf), 1);
838 xclose (p[PREAD]);
840 argv[0] = "/bin/sh";
841 argv[1] = "-c";
842 argv[2] = (char *) info_script_option;
843 argv[3] = NULL;
845 priv_set_restore_linkdir ();
846 execv (argv[0], argv);
848 exec_fatal (info_script_option);
851 void
852 sys_exec_checkpoint_script (const char *script_name,
853 const char *archive_name,
854 int checkpoint_number)
856 pid_t pid;
857 char *argv[4];
858 char uintbuf[UINTMAX_STRSIZE_BOUND];
860 pid = xfork ();
862 if (pid != 0)
864 /* Master */
866 int status;
868 while (waitpid (pid, &status, 0) == -1)
869 if (errno != EINTR)
871 waitpid_error (script_name);
872 break;
875 return;
878 /* Child */
879 setenv ("TAR_VERSION", PACKAGE_VERSION, 1);
880 setenv ("TAR_ARCHIVE", archive_name, 1);
881 setenv ("TAR_CHECKPOINT", STRINGIFY_BIGINT (checkpoint_number, uintbuf), 1);
882 setenv ("TAR_BLOCKING_FACTOR",
883 STRINGIFY_BIGINT (blocking_factor, uintbuf), 1);
884 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option), 1);
885 setenv ("TAR_FORMAT",
886 archive_format_string (current_format == DEFAULT_FORMAT ?
887 archive_format : current_format), 1);
888 argv[0] = "/bin/sh";
889 argv[1] = "-c";
890 argv[2] = (char *) script_name;
891 argv[3] = NULL;
893 priv_set_restore_linkdir ();
894 execv (argv[0], argv);
896 exec_fatal (script_name);
899 #endif /* not MSDOS */