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