1 /* System-dependent calls for tar.
3 Copyright 2003-2022 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any later
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include <system-ioctl.h>
25 #include <wordsplit.h>
28 xexec (const char *cmd
)
32 argv
[0] = (char *) "/bin/sh";
33 argv
[1] = (char *) "-c";
34 argv
[2] = (char *) cmd
;
37 execv ("/bin/sh", argv
);
41 /* True if the archive is seekable via ioctl and MTIOCTOP,
42 or if it is not known whether it is seekable.
43 False if it is known to be not seekable. */
44 static bool mtioseekable_archive
;
47 mtioseek (bool count_files
, off_t count
)
49 if (mtioseekable_archive
)
52 struct mtop operation
;
53 operation
.mt_op
= (count_files
54 ? (count
< 0 ? MTBSF
: MTFSF
)
55 : (count
< 0 ? MTBSR
: MTFSR
));
57 ? INT_SUBTRACT_WRAPV (0, count
, &operation
.mt_count
)
58 : INT_ADD_WRAPV (count
, 0, &operation
.mt_count
))
59 && (0 <= rmtioctl (archive
, MTIOCTOP
, &operation
)
61 && 0 <= rmtioctl (archive
, MTIOCTOP
, &operation
))))
65 mtioseekable_archive
= false;
73 sys_get_archive_stat (void)
79 sys_file_is_archive (struct tar_stat_info
*p
)
85 sys_detect_dev_null_output (void)
87 static char const dev_null
[] = "nul";
89 dev_null_output
= (strcmp (archive_name_array
[0], dev_null
) == 0
90 || (! _isrmt (archive
)));
94 sys_wait_for_child (pid_t child_pid
, bool eof
)
99 sys_spawn_shell (void)
101 spawnl (P_WAIT
, getenv ("COMSPEC"), "-", 0);
104 /* stat() in djgpp's C library gives a constant number of 42 as the
105 uid and gid of a file. So, comparing an FTP'ed archive just after
106 unpack would fail on MSDOS. */
109 sys_compare_uid (struct stat
*a
, struct stat
*b
)
115 sys_compare_gid (struct stat
*a
, struct stat
*b
)
121 sys_compare_links (struct stat
*link_data
, struct stat
*stat_data
)
127 sys_truncate (int fd
)
129 return write (fd
, "", 0);
133 sys_write_archive_buffer (void)
135 return full_write (archive
, record_start
->buffer
, record_size
);
138 /* Set ARCHIVE for writing, then compressing an archive. */
140 sys_child_open_for_compress (void)
142 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
145 /* Set ARCHIVE for uncompressing, then reading an archive. */
147 sys_child_open_for_uncompress (void)
149 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
154 extern union block
*record_start
; /* FIXME */
157 sys_get_archive_stat (void)
159 bool remote
= _isrmt (archive
);
160 mtioseekable_archive
= true;
161 if (!remote
&& 0 <= archive
&& fstat (archive
, &archive_stat
) == 0)
163 if (!S_ISCHR (archive_stat
.st_mode
))
164 mtioseekable_archive
= false;
169 /* FIXME: This memset should not be needed. It is present only
170 because other parts of tar may incorrectly access
171 archive_stat even if it's not the archive status. */
172 memset (&archive_stat
, 0, sizeof archive_stat
);
179 sys_file_is_archive (struct tar_stat_info
*p
)
181 return (!dev_null_output
&& !_isrmt (archive
)
182 && p
->stat
.st_dev
== archive_stat
.st_dev
183 && p
->stat
.st_ino
== archive_stat
.st_ino
);
186 /* Detect if outputting to "/dev/null". */
188 sys_detect_dev_null_output (void)
190 static char const dev_null
[] = "/dev/null";
191 static struct stat dev_null_stat
;
193 dev_null_output
= (strcmp (archive_name_array
[0], dev_null
) == 0
194 || (! _isrmt (archive
)
195 && S_ISCHR (archive_stat
.st_mode
)
196 && (dev_null_stat
.st_ino
!= 0
197 || stat (dev_null
, &dev_null_stat
) == 0)
198 && archive_stat
.st_ino
== dev_null_stat
.st_ino
199 && archive_stat
.st_dev
== dev_null_stat
.st_dev
));
203 sys_wait_for_child (pid_t child_pid
, bool eof
)
209 while (waitpid (child_pid
, &wait_status
, 0) == -1)
212 waitpid_error (use_compress_program_option
);
216 if (WIFSIGNALED (wait_status
))
218 int sig
= WTERMSIG (wait_status
);
219 if (!(!eof
&& sig
== SIGPIPE
))
220 FATAL_ERROR ((0, 0, _("Child died with signal %d"), sig
));
222 else if (WEXITSTATUS (wait_status
) != 0)
223 FATAL_ERROR ((0, 0, _("Child returned status %d"),
224 WEXITSTATUS (wait_status
)));
229 sys_spawn_shell (void)
232 const char *shell
= getenv ("SHELL");
238 priv_set_restore_linkdir ();
239 execlp (shell
, "-sh", "-i", NULL
);
245 while (waitpid (child
, &wait_status
, 0) == -1)
248 waitpid_error (shell
);
255 sys_compare_uid (struct stat
*a
, struct stat
*b
)
257 return a
->st_uid
== b
->st_uid
;
261 sys_compare_gid (struct stat
*a
, struct stat
*b
)
263 return a
->st_gid
== b
->st_gid
;
267 sys_compare_links (struct stat
*link_data
, struct stat
*stat_data
)
269 return stat_data
->st_dev
== link_data
->st_dev
270 && stat_data
->st_ino
== link_data
->st_ino
;
274 sys_truncate (int fd
)
276 off_t pos
= lseek (fd
, (off_t
) 0, SEEK_CUR
);
277 return pos
< 0 ? -1 : ftruncate (fd
, pos
);
280 /* Return nonzero if NAME is the name of a regular file, or if the file
281 does not exist (so it would be created as a regular file). */
283 is_regular_file (const char *name
)
287 if (stat (name
, &stbuf
) == 0)
288 return S_ISREG (stbuf
.st_mode
);
290 return errno
== ENOENT
;
294 sys_write_archive_buffer (void)
296 return rmtwrite (archive
, record_start
->buffer
, record_size
);
299 #define PREAD 0 /* read file descriptor from pipe() */
300 #define PWRITE 1 /* write file descriptor from pipe() */
302 /* Duplicate file descriptor FROM into becoming INTO.
303 INTO is closed first and has to be the next available slot. */
305 xdup2 (int from
, int into
)
309 int status
= close (into
);
311 if (status
!= 0 && errno
!= EBADF
)
314 FATAL_ERROR ((0, e
, _("Cannot close")));
322 FATAL_ERROR ((0, e
, _("Cannot dup")));
330 /* Propagate any failure of the grandchild back to the parent. */
331 static _Noreturn
void
332 wait_for_grandchild (pid_t pid
)
337 while (waitpid (pid
, &wait_status
, 0) == -1)
340 waitpid_error (use_compress_program_option
);
344 if (WIFSIGNALED (wait_status
))
345 raise (WTERMSIG (wait_status
));
346 else if (WEXITSTATUS (wait_status
) != 0)
347 exit_code
= WEXITSTATUS (wait_status
);
352 /* Set ARCHIVE for writing, then compressing an archive. */
354 sys_child_open_for_compress (void)
358 pid_t grandchild_pid
;
361 signal (SIGPIPE
, SIG_IGN
);
363 child_pid
= xfork ();
367 /* The parent tar is still here! Just clean up. */
369 archive
= parent_pipe
[PWRITE
];
370 xclose (parent_pipe
[PREAD
]);
374 /* The new born child tar is here! */
376 set_program_name (_("tar (child)"));
377 signal (SIGPIPE
, SIG_DFL
);
379 xdup2 (parent_pipe
[PREAD
], STDIN_FILENO
);
380 xclose (parent_pipe
[PWRITE
]);
382 /* Check if we need a grandchild tar. This happens only if either:
383 a) the file is to be accessed by rmt: compressor doesn't know how;
384 b) the file is not a plain file. */
386 if (!_remdev (archive_name_array
[0])
387 && is_regular_file (archive_name_array
[0]))
390 maybe_backup_file (archive_name_array
[0], 1);
392 /* We don't need a grandchild tar. Open the archive and launch the
394 if (strcmp (archive_name_array
[0], "-"))
396 archive
= creat (archive_name_array
[0], MODE_RW
);
399 int saved_errno
= errno
;
404 open_fatal (archive_name_array
[0]);
406 xdup2 (archive
, STDOUT_FILENO
);
408 priv_set_restore_linkdir ();
409 xexec (use_compress_program_option
);
412 /* We do need a grandchild tar. */
415 grandchild_pid
= xfork ();
417 if (grandchild_pid
== 0)
419 /* The newborn grandchild tar is here! Launch the compressor. */
421 set_program_name (_("tar (grandchild)"));
423 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
);
424 xclose (child_pipe
[PREAD
]);
425 priv_set_restore_linkdir ();
426 xexec (use_compress_program_option
);
429 /* The child tar is still here! */
431 /* Prepare for reblocking the data from the compressor into the archive. */
433 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
);
434 xclose (child_pipe
[PWRITE
]);
436 if (strcmp (archive_name_array
[0], "-") == 0)
437 archive
= STDOUT_FILENO
;
440 archive
= rmtcreat (archive_name_array
[0], MODE_RW
, rsh_command_option
);
442 open_fatal (archive_name_array
[0]);
445 /* Let's read out of the stdin pipe and write an archive. */
453 /* Assemble a record. */
455 for (length
= 0, cursor
= record_start
->buffer
;
456 length
< record_size
;
457 length
+= status
, cursor
+= status
)
459 size_t size
= record_size
- length
;
461 status
= safe_read (STDIN_FILENO
, cursor
, size
);
462 if (status
== SAFE_READ_ERROR
)
463 read_fatal (use_compress_program_option
);
468 /* Copy the record. */
472 /* We hit the end of the file. Write last record at
473 full length, as the only role of the grandchild is
474 doing proper reblocking. */
478 memset (record_start
->buffer
+ length
, 0, record_size
- length
);
479 status
= sys_write_archive_buffer ();
480 if (status
!= record_size
)
481 archive_write_error (status
);
484 /* There is nothing else to read, break out. */
488 status
= sys_write_archive_buffer ();
489 if (status
!= record_size
)
490 archive_write_error (status
);
493 wait_for_grandchild (grandchild_pid
);
497 run_decompress_program (void)
500 const char *p
, *prog
= NULL
;
502 int wsflags
= (WRDSF_DEFFLAGS
| WRDSF_ENV
| WRDSF_DOOFFS
) & ~WRDSF_NOVAR
;
504 ws
.ws_env
= (const char **) environ
;
507 for (p
= first_decompress_program (&i
); p
; p
= next_decompress_program (&i
))
511 WARNOPT (WARN_DECOMPRESS_PROGRAM
,
512 (0, errno
, _("cannot run %s"), prog
));
513 WARNOPT (WARN_DECOMPRESS_PROGRAM
,
514 (0, 0, _("trying %s"), p
));
516 if (wordsplit (p
, &ws
, wsflags
))
517 FATAL_ERROR ((0, 0, _("cannot split string '%s': %s"),
518 p
, wordsplit_strerror (&ws
)));
519 wsflags
|= WRDSF_REUSE
;
520 memmove(ws
.ws_wordv
, ws
.ws_wordv
+ ws
.ws_offs
,
521 sizeof(ws
.ws_wordv
[0])*ws
.ws_wordc
);
522 ws
.ws_wordv
[ws
.ws_wordc
] = (char *) "-d";
524 execvp (ws
.ws_wordv
[0], ws
.ws_wordv
);
525 ws
.ws_wordv
[ws
.ws_wordc
] = NULL
;
528 FATAL_ERROR ((0, 0, _("unable to run decompression program")));
532 /* Set ARCHIVE for uncompressing, then reading an archive. */
534 sys_child_open_for_uncompress (void)
538 pid_t grandchild_pid
;
542 child_pid
= xfork ();
546 /* The parent tar is still here! Just clean up. */
548 archive
= parent_pipe
[PREAD
];
549 xclose (parent_pipe
[PWRITE
]);
553 /* The newborn child tar is here! */
555 set_program_name (_("tar (child)"));
556 signal (SIGPIPE
, SIG_DFL
);
558 xdup2 (parent_pipe
[PWRITE
], STDOUT_FILENO
);
559 xclose (parent_pipe
[PREAD
]);
561 /* Check if we need a grandchild tar. This happens only if either:
562 a) we're reading stdin: to force unblocking;
563 b) the file is to be accessed by rmt: compressor doesn't know how;
564 c) the file is not a plain file. */
566 if (strcmp (archive_name_array
[0], "-") != 0
567 && !_remdev (archive_name_array
[0])
568 && is_regular_file (archive_name_array
[0]))
570 /* We don't need a grandchild tar. Open the archive and launch the
573 archive
= open (archive_name_array
[0], O_RDONLY
| O_BINARY
, MODE_RW
);
575 open_fatal (archive_name_array
[0]);
576 xdup2 (archive
, STDIN_FILENO
);
577 priv_set_restore_linkdir ();
578 run_decompress_program ();
581 /* We do need a grandchild tar. */
584 grandchild_pid
= xfork ();
586 if (grandchild_pid
== 0)
588 /* The newborn grandchild tar is here! Launch the uncompressor. */
590 set_program_name (_("tar (grandchild)"));
592 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
);
593 xclose (child_pipe
[PWRITE
]);
594 priv_set_restore_linkdir ();
595 run_decompress_program ();
598 /* The child tar is still here! */
600 /* Prepare for unblocking the data from the archive into the
603 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
);
604 xclose (child_pipe
[PREAD
]);
606 if (strcmp (archive_name_array
[0], "-") == 0)
607 archive
= STDIN_FILENO
;
609 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
610 MODE_RW
, rsh_command_option
);
612 open_fatal (archive_name_array
[0]);
614 /* Let's read the archive and pipe it into stdout. */
623 clear_read_error_count ();
626 status
= rmtread (archive
, record_start
->buffer
, record_size
);
627 if (status
== SAFE_READ_ERROR
)
629 archive_read_error ();
634 cursor
= record_start
->buffer
;
638 count
= maximum
< BLOCKSIZE
? maximum
: BLOCKSIZE
;
639 if (full_write (STDOUT_FILENO
, cursor
, count
) != count
)
640 write_error (use_compress_program_option
);
646 xclose (STDOUT_FILENO
);
648 wait_for_grandchild (grandchild_pid
);
654 dec_to_env (char const *envar
, uintmax_t num
)
656 char buf
[UINTMAX_STRSIZE_BOUND
];
659 numstr
= STRINGIFY_BIGINT (num
, buf
);
660 if (setenv (envar
, numstr
, 1) != 0)
665 time_to_env (char const *envar
, struct timespec t
)
667 char buf
[TIMESPEC_STRSIZE_BOUND
];
668 if (setenv (envar
, code_timespec (t
, buf
), 1) != 0)
673 oct_to_env (char const *envar
, unsigned long num
)
675 char buf
[1+1+(sizeof(unsigned long)*CHAR_BIT
+2)/3];
677 snprintf (buf
, sizeof buf
, "0%lo", num
);
678 if (setenv (envar
, buf
, 1) != 0)
683 str_to_env (char const *envar
, char const *str
)
687 if (setenv (envar
, str
, 1) != 0)
695 chr_to_env (char const *envar
, char c
)
700 if (setenv (envar
, buf
, 1) != 0)
705 stat_to_env (char *name
, char type
, struct tar_stat_info
*st
)
707 str_to_env ("TAR_VERSION", PACKAGE_VERSION
);
708 str_to_env ("TAR_ARCHIVE", *archive_name_cursor
);
709 dec_to_env ("TAR_VOLUME", archive_name_cursor
- archive_name_array
+ 1);
710 dec_to_env ("TAR_BLOCKING_FACTOR", blocking_factor
);
711 str_to_env ("TAR_FORMAT",
712 archive_format_string (current_format
== DEFAULT_FORMAT
?
713 archive_format
: current_format
));
714 chr_to_env ("TAR_FILETYPE", type
);
715 oct_to_env ("TAR_MODE", st
->stat
.st_mode
);
716 str_to_env ("TAR_FILENAME", name
);
717 str_to_env ("TAR_REALNAME", st
->file_name
);
718 str_to_env ("TAR_UNAME", st
->uname
);
719 str_to_env ("TAR_GNAME", st
->gname
);
720 time_to_env ("TAR_ATIME", st
->atime
);
721 time_to_env ("TAR_MTIME", st
->mtime
);
722 time_to_env ("TAR_CTIME", st
->ctime
);
723 dec_to_env ("TAR_SIZE", st
->stat
.st_size
);
724 dec_to_env ("TAR_UID", st
->stat
.st_uid
);
725 dec_to_env ("TAR_GID", st
->stat
.st_gid
);
731 dec_to_env ("TAR_MINOR", minor (st
->stat
.st_rdev
));
732 dec_to_env ("TAR_MAJOR", major (st
->stat
.st_rdev
));
733 unsetenv ("TAR_LINKNAME");
738 unsetenv ("TAR_MINOR");
739 unsetenv ("TAR_MAJOR");
740 str_to_env ("TAR_LINKNAME", st
->link_name
);
744 unsetenv ("TAR_MINOR");
745 unsetenv ("TAR_MAJOR");
746 unsetenv ("TAR_LINKNAME");
751 static pid_t global_pid
;
752 static void (*pipe_handler
) (int sig
);
755 sys_exec_command (char *file_name
, int typechar
, struct tar_stat_info
*st
)
760 pipe_handler
= signal (SIGPIPE
, SIG_IGN
);
761 global_pid
= xfork ();
770 xdup2 (p
[PREAD
], STDIN_FILENO
);
773 stat_to_env (file_name
, typechar
, st
);
775 priv_set_restore_linkdir ();
776 xexec (to_command_option
);
780 sys_wait_command (void)
787 signal (SIGPIPE
, pipe_handler
);
788 while (waitpid (global_pid
, &status
, 0) == -1)
792 waitpid_error (to_command_option
);
796 if (WIFEXITED (status
))
798 if (!ignore_command_error_option
&& WEXITSTATUS (status
))
799 ERROR ((0, 0, _("%lu: Child returned status %d"),
800 (unsigned long) global_pid
, WEXITSTATUS (status
)));
802 else if (WIFSIGNALED (status
))
804 WARN ((0, 0, _("%lu: Child terminated on signal %d"),
805 (unsigned long) global_pid
, WTERMSIG (status
)));
808 ERROR ((0, 0, _("%lu: Child terminated on unknown reason"),
809 (unsigned long) global_pid
));
815 sys_exec_info_script (const char **archive_name
, int volume_number
)
818 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
820 static void (*saved_handler
) (int sig
);
823 saved_handler
= signal (SIGPIPE
, SIG_IGN
);
838 fp
= fdopen (p
[PREAD
], "r");
839 rc
= getline (&buf
, &size
, fp
);
842 if (rc
> 0 && buf
[rc
-1] == '\n')
845 while (waitpid (pid
, &status
, 0) == -1)
848 signal (SIGPIPE
, saved_handler
);
849 waitpid_error (info_script_option
);
853 signal (SIGPIPE
, saved_handler
);
855 if (WIFEXITED (status
))
857 if (WEXITSTATUS (status
) == 0 && rc
> 0)
861 return WEXITSTATUS (status
);
869 setenv ("TAR_VERSION", PACKAGE_VERSION
, 1);
870 setenv ("TAR_ARCHIVE", *archive_name
, 1);
871 setenv ("TAR_VOLUME", STRINGIFY_BIGINT (volume_number
, uintbuf
), 1);
872 setenv ("TAR_BLOCKING_FACTOR",
873 STRINGIFY_BIGINT (blocking_factor
, uintbuf
), 1);
874 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option
), 1);
875 setenv ("TAR_FORMAT",
876 archive_format_string (current_format
== DEFAULT_FORMAT
?
877 archive_format
: current_format
), 1);
878 setenv ("TAR_FD", STRINGIFY_BIGINT (p
[PWRITE
], uintbuf
), 1);
882 priv_set_restore_linkdir ();
883 xexec (info_script_option
);
887 sys_exec_checkpoint_script (const char *script_name
,
888 const char *archive_name
,
889 int checkpoint_number
)
892 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
902 while (waitpid (pid
, &status
, 0) == -1)
905 waitpid_error (script_name
);
913 setenv ("TAR_VERSION", PACKAGE_VERSION
, 1);
914 setenv ("TAR_ARCHIVE", archive_name
, 1);
915 setenv ("TAR_CHECKPOINT", STRINGIFY_BIGINT (checkpoint_number
, uintbuf
), 1);
916 setenv ("TAR_BLOCKING_FACTOR",
917 STRINGIFY_BIGINT (blocking_factor
, uintbuf
), 1);
918 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option
), 1);
919 setenv ("TAR_FORMAT",
920 archive_format_string (current_format
== DEFAULT_FORMAT
?
921 archive_format
: current_format
), 1);
922 priv_set_restore_linkdir ();
926 #endif /* not MSDOS */