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