Avoid overwriting exit_status with a value indicating less important condition.
[tar/ericb.git] / src / system.c
blob7df8122bb41e50c382bc1936c17c99de6a979b46
1 /* System-dependent calls for tar.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007,
4 2008 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 <rmt.h>
24 #include <signal.h>
26 #if MSDOS
28 bool
29 sys_get_archive_stat (void)
31 return 0;
34 bool
35 sys_file_is_archive (struct tar_stat_info *p)
37 return false;
40 void
41 sys_save_archive_dev_ino (void)
45 void
46 sys_detect_dev_null_output (void)
48 static char const dev_null[] = "nul";
50 dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0
51 || (! _isrmt (archive)));
54 void
55 sys_wait_for_child (pid_t child_pid, bool eof)
59 void
60 sys_spawn_shell (void)
62 spawnl (P_WAIT, getenv ("COMSPEC"), "-", 0);
65 /* stat() in djgpp's C library gives a constant number of 42 as the
66 uid and gid of a file. So, comparing an FTP'ed archive just after
67 unpack would fail on MSDOS. */
69 bool
70 sys_compare_uid (struct stat *a, struct stat *b)
72 return true;
75 bool
76 sys_compare_gid (struct stat *a, struct stat *b)
78 return true;
81 void
82 sys_compare_links (struct stat *link_data, struct stat *stat_data)
84 return true;
87 int
88 sys_truncate (int fd)
90 return write (fd, "", 0);
93 size_t
94 sys_write_archive_buffer (void)
96 return full_write (archive, record_start->buffer, record_size);
99 /* Set ARCHIVE for writing, then compressing an archive. */
100 void
101 sys_child_open_for_compress (void)
103 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
106 /* Set ARCHIVE for uncompressing, then reading an archive. */
107 void
108 sys_child_open_for_uncompress (void)
110 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
113 #else
115 extern union block *record_start; /* FIXME */
117 static struct stat archive_stat; /* stat block for archive file */
119 bool
120 sys_get_archive_stat (void)
122 return fstat (archive, &archive_stat) == 0;
125 bool
126 sys_file_is_archive (struct tar_stat_info *p)
128 return (ar_dev && p->stat.st_dev == ar_dev && p->stat.st_ino == ar_ino);
131 /* Save archive file inode and device numbers */
132 void
133 sys_save_archive_dev_ino (void)
135 if (!_isrmt (archive) && S_ISREG (archive_stat.st_mode))
137 ar_dev = archive_stat.st_dev;
138 ar_ino = archive_stat.st_ino;
140 else
141 ar_dev = 0;
144 /* Detect if outputting to "/dev/null". */
145 void
146 sys_detect_dev_null_output (void)
148 static char const dev_null[] = "/dev/null";
149 struct stat dev_null_stat;
151 dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0
152 || (! _isrmt (archive)
153 && S_ISCHR (archive_stat.st_mode)
154 && stat (dev_null, &dev_null_stat) == 0
155 && archive_stat.st_dev == dev_null_stat.st_dev
156 && archive_stat.st_ino == dev_null_stat.st_ino));
159 void
160 sys_wait_for_child (pid_t child_pid, bool eof)
162 if (child_pid)
164 int wait_status;
166 while (waitpid (child_pid, &wait_status, 0) == -1)
167 if (errno != EINTR)
169 waitpid_error (use_compress_program_option);
170 break;
173 if (WIFSIGNALED (wait_status))
175 int sig = WTERMSIG (wait_status);
176 if (!(!eof && sig == SIGPIPE))
177 ERROR ((0, 0, _("Child died with signal %d"), sig));
179 else if (WEXITSTATUS (wait_status) != 0)
180 ERROR ((0, 0, _("Child returned status %d"),
181 WEXITSTATUS (wait_status)));
185 void
186 sys_spawn_shell (void)
188 pid_t child;
189 const char *shell = getenv ("SHELL");
190 if (! shell)
191 shell = "/bin/sh";
192 child = xfork ();
193 if (child == 0)
195 execlp (shell, "-sh", "-i", (char *) 0);
196 exec_fatal (shell);
198 else
200 int wait_status;
201 while (waitpid (child, &wait_status, 0) == -1)
202 if (errno != EINTR)
204 waitpid_error (shell);
205 break;
210 bool
211 sys_compare_uid (struct stat *a, struct stat *b)
213 return a->st_uid == b->st_uid;
216 bool
217 sys_compare_gid (struct stat *a, struct stat *b)
219 return a->st_gid == b->st_gid;
222 bool
223 sys_compare_links (struct stat *link_data, struct stat *stat_data)
225 return stat_data->st_dev == link_data->st_dev
226 && stat_data->st_ino == link_data->st_ino;
230 sys_truncate (int fd)
232 off_t pos = lseek (fd, (off_t) 0, SEEK_CUR);
233 return pos < 0 ? -1 : ftruncate (fd, pos);
236 /* Return nonzero if NAME is the name of a regular file, or if the file
237 does not exist (so it would be created as a regular file). */
238 static int
239 is_regular_file (const char *name)
241 struct stat stbuf;
243 if (stat (name, &stbuf) == 0)
244 return S_ISREG (stbuf.st_mode);
245 else
246 return errno == ENOENT;
249 size_t
250 sys_write_archive_buffer (void)
252 return rmtwrite (archive, record_start->buffer, record_size);
255 #define PREAD 0 /* read file descriptor from pipe() */
256 #define PWRITE 1 /* write file descriptor from pipe() */
258 /* Duplicate file descriptor FROM into becoming INTO.
259 INTO is closed first and has to be the next available slot. */
260 static void
261 xdup2 (int from, int into)
263 if (from != into)
265 int status = close (into);
267 if (status != 0 && errno != EBADF)
269 int e = errno;
270 FATAL_ERROR ((0, e, _("Cannot close")));
272 status = dup (from);
273 if (status != into)
275 if (status < 0)
277 int e = errno;
278 FATAL_ERROR ((0, e, _("Cannot dup")));
280 abort ();
282 xclose (from);
286 void wait_for_grandchild (pid_t pid) __attribute__ ((__noreturn__));
288 /* Propagate any failure of the grandchild back to the parent. */
289 void
290 wait_for_grandchild (pid_t pid)
292 int wait_status;
293 int exit_code = 0;
295 while (waitpid (pid, &wait_status, 0) == -1)
296 if (errno != EINTR)
298 waitpid_error (use_compress_program_option);
299 break;
302 if (WIFSIGNALED (wait_status))
303 raise (WTERMSIG (wait_status));
304 else if (WEXITSTATUS (wait_status) != 0)
305 exit_code = WEXITSTATUS (wait_status);
307 exit (exit_code);
310 /* Set ARCHIVE for writing, then compressing an archive. */
311 pid_t
312 sys_child_open_for_compress (void)
314 int parent_pipe[2];
315 int child_pipe[2];
316 pid_t grandchild_pid;
317 pid_t child_pid;
319 xpipe (parent_pipe);
320 child_pid = xfork ();
322 if (child_pid > 0)
324 /* The parent tar is still here! Just clean up. */
326 archive = parent_pipe[PWRITE];
327 xclose (parent_pipe[PREAD]);
328 return child_pid;
331 /* The new born child tar is here! */
333 program_name = _("tar (child)");
335 xdup2 (parent_pipe[PREAD], STDIN_FILENO);
336 xclose (parent_pipe[PWRITE]);
338 /* Check if we need a grandchild tar. This happens only if either:
339 a) the file is to be accessed by rmt: compressor doesn't know how;
340 b) the file is not a plain file. */
342 if (!_remdev (archive_name_array[0])
343 && is_regular_file (archive_name_array[0]))
345 if (backup_option)
346 maybe_backup_file (archive_name_array[0], 1);
348 /* We don't need a grandchild tar. Open the archive and launch the
349 compressor. */
350 if (strcmp (archive_name_array[0], "-"))
352 archive = creat (archive_name_array[0], MODE_RW);
353 if (archive < 0)
355 int saved_errno = errno;
357 if (backup_option)
358 undo_last_backup ();
359 errno = saved_errno;
360 open_fatal (archive_name_array[0]);
362 xdup2 (archive, STDOUT_FILENO);
364 execlp (use_compress_program_option, use_compress_program_option, NULL);
365 exec_fatal (use_compress_program_option);
368 /* We do need a grandchild tar. */
370 xpipe (child_pipe);
371 grandchild_pid = xfork ();
373 if (grandchild_pid == 0)
375 /* The newborn grandchild tar is here! Launch the compressor. */
377 program_name = _("tar (grandchild)");
379 xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
380 xclose (child_pipe[PREAD]);
381 execlp (use_compress_program_option, use_compress_program_option,
382 (char *) 0);
383 exec_fatal (use_compress_program_option);
386 /* The child tar is still here! */
388 /* Prepare for reblocking the data from the compressor into the archive. */
390 xdup2 (child_pipe[PREAD], STDIN_FILENO);
391 xclose (child_pipe[PWRITE]);
393 if (strcmp (archive_name_array[0], "-") == 0)
394 archive = STDOUT_FILENO;
395 else
397 archive = rmtcreat (archive_name_array[0], MODE_RW, rsh_command_option);
398 if (archive < 0)
399 open_fatal (archive_name_array[0]);
402 /* Let's read out of the stdin pipe and write an archive. */
404 while (1)
406 size_t status = 0;
407 char *cursor;
408 size_t length;
410 /* Assemble a record. */
412 for (length = 0, cursor = record_start->buffer;
413 length < record_size;
414 length += status, cursor += status)
416 size_t size = record_size - length;
418 status = safe_read (STDIN_FILENO, cursor, size);
419 if (status == SAFE_READ_ERROR)
420 read_fatal (use_compress_program_option);
421 if (status == 0)
422 break;
425 /* Copy the record. */
427 if (status == 0)
429 /* We hit the end of the file. Write last record at
430 full length, as the only role of the grandchild is
431 doing proper reblocking. */
433 if (length > 0)
435 memset (record_start->buffer + length, 0, record_size - length);
436 status = sys_write_archive_buffer ();
437 if (status != record_size)
438 archive_write_error (status);
441 /* There is nothing else to read, break out. */
442 break;
445 status = sys_write_archive_buffer ();
446 if (status != record_size)
447 archive_write_error (status);
450 wait_for_grandchild (grandchild_pid);
453 /* Set ARCHIVE for uncompressing, then reading an archive. */
454 pid_t
455 sys_child_open_for_uncompress (void)
457 int parent_pipe[2];
458 int child_pipe[2];
459 pid_t grandchild_pid;
460 pid_t child_pid;
462 xpipe (parent_pipe);
463 child_pid = xfork ();
465 if (child_pid > 0)
467 /* The parent tar is still here! Just clean up. */
469 archive = parent_pipe[PREAD];
470 xclose (parent_pipe[PWRITE]);
471 return child_pid;
474 /* The newborn child tar is here! */
476 program_name = _("tar (child)");
478 xdup2 (parent_pipe[PWRITE], STDOUT_FILENO);
479 xclose (parent_pipe[PREAD]);
481 /* Check if we need a grandchild tar. This happens only if either:
482 a) we're reading stdin: to force unblocking;
483 b) the file is to be accessed by rmt: compressor doesn't know how;
484 c) the file is not a plain file. */
486 if (strcmp (archive_name_array[0], "-") != 0
487 && !_remdev (archive_name_array[0])
488 && is_regular_file (archive_name_array[0]))
490 /* We don't need a grandchild tar. Open the archive and lauch the
491 uncompressor. */
493 archive = open (archive_name_array[0], O_RDONLY | O_BINARY, MODE_RW);
494 if (archive < 0)
495 open_fatal (archive_name_array[0]);
496 xdup2 (archive, STDIN_FILENO);
497 execlp (use_compress_program_option, use_compress_program_option,
498 "-d", (char *) 0);
499 exec_fatal (use_compress_program_option);
502 /* We do need a grandchild tar. */
504 xpipe (child_pipe);
505 grandchild_pid = xfork ();
507 if (grandchild_pid == 0)
509 /* The newborn grandchild tar is here! Launch the uncompressor. */
511 program_name = _("tar (grandchild)");
513 xdup2 (child_pipe[PREAD], STDIN_FILENO);
514 xclose (child_pipe[PWRITE]);
515 execlp (use_compress_program_option, use_compress_program_option,
516 "-d", (char *) 0);
517 exec_fatal (use_compress_program_option);
520 /* The child tar is still here! */
522 /* Prepare for unblocking the data from the archive into the
523 uncompressor. */
525 xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
526 xclose (child_pipe[PREAD]);
528 if (strcmp (archive_name_array[0], "-") == 0)
529 archive = STDIN_FILENO;
530 else
531 archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
532 MODE_RW, rsh_command_option);
533 if (archive < 0)
534 open_fatal (archive_name_array[0]);
536 /* Let's read the archive and pipe it into stdout. */
538 while (1)
540 char *cursor;
541 size_t maximum;
542 size_t count;
543 size_t status;
545 clear_read_error_count ();
547 error_loop:
548 status = rmtread (archive, record_start->buffer, record_size);
549 if (status == SAFE_READ_ERROR)
551 archive_read_error ();
552 goto error_loop;
554 if (status == 0)
555 break;
556 cursor = record_start->buffer;
557 maximum = status;
558 while (maximum)
560 count = maximum < BLOCKSIZE ? maximum : BLOCKSIZE;
561 if (full_write (STDOUT_FILENO, cursor, count) != count)
562 write_error (use_compress_program_option);
563 cursor += count;
564 maximum -= count;
568 xclose (STDOUT_FILENO);
570 wait_for_grandchild (grandchild_pid);
575 static void
576 dec_to_env (char *envar, uintmax_t num)
578 char buf[UINTMAX_STRSIZE_BOUND];
579 char *numstr;
581 numstr = STRINGIFY_BIGINT (num, buf);
582 if (setenv (envar, numstr, 1) != 0)
583 xalloc_die ();
586 static void
587 time_to_env (char *envar, struct timespec t)
589 char buf[TIMESPEC_STRSIZE_BOUND];
590 if (setenv (envar, code_timespec (t, buf), 1) != 0)
591 xalloc_die ();
594 static void
595 oct_to_env (char *envar, unsigned long num)
597 char buf[1+1+(sizeof(unsigned long)*CHAR_BIT+2)/3];
599 snprintf (buf, sizeof buf, "0%lo", num);
600 if (setenv (envar, buf, 1) != 0)
601 xalloc_die ();
604 static void
605 str_to_env (char *envar, char const *str)
607 if (str)
609 if (setenv (envar, str, 1) != 0)
610 xalloc_die ();
612 else
613 unsetenv (envar);
616 static void
617 chr_to_env (char *envar, char c)
619 char buf[2];
620 buf[0] = c;
621 buf[1] = 0;
622 if (setenv (envar, buf, 1) != 0)
623 xalloc_die ();
626 static void
627 stat_to_env (char *name, char type, struct tar_stat_info *st)
629 str_to_env ("TAR_VERSION", PACKAGE_VERSION);
630 chr_to_env ("TAR_FILETYPE", type);
631 oct_to_env ("TAR_MODE", st->stat.st_mode);
632 str_to_env ("TAR_FILENAME", name);
633 str_to_env ("TAR_REALNAME", st->file_name);
634 str_to_env ("TAR_UNAME", st->uname);
635 str_to_env ("TAR_GNAME", st->gname);
636 time_to_env ("TAR_ATIME", st->atime);
637 time_to_env ("TAR_MTIME", st->mtime);
638 time_to_env ("TAR_CTIME", st->ctime);
639 dec_to_env ("TAR_SIZE", st->stat.st_size);
640 dec_to_env ("TAR_UID", st->stat.st_uid);
641 dec_to_env ("TAR_GID", st->stat.st_gid);
643 switch (type)
645 case 'b':
646 case 'c':
647 dec_to_env ("TAR_MINOR", minor (st->stat.st_rdev));
648 dec_to_env ("TAR_MAJOR", major (st->stat.st_rdev));
649 unsetenv ("TAR_LINKNAME");
650 break;
652 case 'l':
653 case 'h':
654 unsetenv ("TAR_MINOR");
655 unsetenv ("TAR_MAJOR");
656 str_to_env ("TAR_LINKNAME", st->link_name);
657 break;
659 default:
660 unsetenv ("TAR_MINOR");
661 unsetenv ("TAR_MAJOR");
662 unsetenv ("TAR_LINKNAME");
663 break;
667 static pid_t global_pid;
668 static RETSIGTYPE (*pipe_handler) (int sig);
671 sys_exec_command (char *file_name, int typechar, struct tar_stat_info *st)
673 int p[2];
674 char *argv[4];
676 xpipe (p);
677 pipe_handler = signal (SIGPIPE, SIG_IGN);
678 global_pid = xfork ();
680 if (global_pid != 0)
682 xclose (p[PREAD]);
683 return p[PWRITE];
686 /* Child */
687 xdup2 (p[PREAD], STDIN_FILENO);
688 xclose (p[PWRITE]);
690 stat_to_env (file_name, typechar, st);
692 argv[0] = "/bin/sh";
693 argv[1] = "-c";
694 argv[2] = to_command_option;
695 argv[3] = NULL;
697 execv ("/bin/sh", argv);
699 exec_fatal (file_name);
702 void
703 sys_wait_command (void)
705 int status;
707 if (global_pid < 0)
708 return;
710 signal (SIGPIPE, pipe_handler);
711 while (waitpid (global_pid, &status, 0) == -1)
712 if (errno != EINTR)
714 global_pid = -1;
715 waitpid_error (to_command_option);
716 return;
719 if (WIFEXITED (status))
721 if (!ignore_command_error_option && WEXITSTATUS (status))
722 ERROR ((0, 0, _("%lu: Child returned status %d"),
723 (unsigned long) global_pid, WEXITSTATUS (status)));
725 else if (WIFSIGNALED (status))
727 WARN ((0, 0, _("%lu: Child terminated on signal %d"),
728 (unsigned long) global_pid, WTERMSIG (status)));
730 else
731 ERROR ((0, 0, _("%lu: Child terminated on unknown reason"),
732 (unsigned long) global_pid));
734 global_pid = -1;
738 sys_exec_info_script (const char **archive_name, int volume_number)
740 pid_t pid;
741 char *argv[4];
742 char uintbuf[UINTMAX_STRSIZE_BOUND];
743 int p[2];
744 static RETSIGTYPE (*saved_handler) (int sig);
746 xpipe (p);
747 saved_handler = signal (SIGPIPE, SIG_IGN);
749 pid = xfork ();
751 if (pid != 0)
753 /* Master */
755 int rc;
756 int status;
757 char *buf = NULL;
758 size_t size = 0;
759 FILE *fp;
761 xclose (p[PWRITE]);
762 fp = fdopen (p[PREAD], "r");
763 rc = getline (&buf, &size, fp);
764 fclose (fp);
766 if (rc > 0 && buf[rc-1] == '\n')
767 buf[--rc] = 0;
769 while (waitpid (pid, &status, 0) == -1)
770 if (errno != EINTR)
772 signal (SIGPIPE, saved_handler);
773 waitpid_error (info_script_option);
774 return -1;
777 signal (SIGPIPE, saved_handler);
779 if (WIFEXITED (status))
781 if (WEXITSTATUS (status) == 0 && rc > 0)
782 *archive_name = buf;
783 else
784 free (buf);
785 return WEXITSTATUS (status);
788 free (buf);
789 return -1;
792 /* Child */
793 setenv ("TAR_VERSION", PACKAGE_VERSION, 1);
794 setenv ("TAR_ARCHIVE", *archive_name, 1);
795 setenv ("TAR_VOLUME", STRINGIFY_BIGINT (volume_number, uintbuf), 1);
796 setenv ("TAR_BLOCKING_FACTOR",
797 STRINGIFY_BIGINT (blocking_factor, uintbuf), 1);
798 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option), 1);
799 setenv ("TAR_FORMAT",
800 archive_format_string (current_format == DEFAULT_FORMAT ?
801 archive_format : current_format), 1);
802 setenv ("TAR_FD", STRINGIFY_BIGINT (p[PWRITE], uintbuf), 1);
804 xclose (p[PREAD]);
806 argv[0] = "/bin/sh";
807 argv[1] = "-c";
808 argv[2] = (char*) info_script_option;
809 argv[3] = NULL;
811 execv (argv[0], argv);
813 exec_fatal (info_script_option);
816 void
817 sys_exec_checkpoint_script (const char *script_name,
818 const char *archive_name,
819 int checkpoint_number)
821 pid_t pid;
822 char *argv[4];
823 char uintbuf[UINTMAX_STRSIZE_BOUND];
825 pid = xfork ();
827 if (pid != 0)
829 /* Master */
831 int status;
833 while (waitpid (pid, &status, 0) == -1)
834 if (errno != EINTR)
836 waitpid_error (script_name);
837 break;
840 return;
843 /* Child */
844 setenv ("TAR_VERSION", PACKAGE_VERSION, 1);
845 setenv ("TAR_ARCHIVE", archive_name, 1);
846 setenv ("TAR_CHECKPOINT", STRINGIFY_BIGINT (checkpoint_number, uintbuf), 1);
847 setenv ("TAR_BLOCKING_FACTOR",
848 STRINGIFY_BIGINT (blocking_factor, uintbuf), 1);
849 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option), 1);
850 setenv ("TAR_FORMAT",
851 archive_format_string (current_format == DEFAULT_FORMAT ?
852 archive_format : current_format), 1);
853 argv[0] = "/bin/sh";
854 argv[1] = "-c";
855 argv[2] = (char*) script_name;
856 argv[3] = NULL;
858 execv (argv[0], argv);
860 exec_fatal (script_name);
863 #endif /* not MSDOS */