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