]> git.wh0rd.org Git - dump.git/blob - compat/lib/system.c
3f94008d6cedcc2f3e36d7d97f1d828db4fb670a
[dump.git] / compat / lib / system.c
1 /*
2  *      Ported to Linux's Second Extended File System as part of the
3  *      dump and restore backup suit
4  *      Remy Card <card@Linux.EU.Org>, 1994-1997
5  *      Stelian Pop <pop@noos.fr>, 1999-2000
6  *      Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
7  */
8
9 /*-
10  * Copyright (c) 1980, 1991, 1993
11  *      The Regents of the University of California.  All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *      This product includes software developed by the University of
24  *      California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  */
41
42 #ifndef lint
43 static const char rcsid[] =
44         "$Id: system.c,v 1.1 2001/07/18 12:54:06 stelian Exp $";
45 #endif /* not lint */
46
47 #include <config.h>
48 #include <errno.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <sys/types.h>
54 #include <sys/wait.h>
55
56 /*
57  * Executes the command in a shell.
58  * Returns -1 if an error occured, the exit status of
59  * the command on success.
60  */
61 int system_command(const char *command, const char *device, int volnum) {
62         int pid, status;
63         char commandstr[4096];
64
65         pid = fork();
66         if (pid == -1) {
67                 perror("  DUMP: unable to fork");
68                 return -1;
69         }
70         if (pid == 0) {
71                 setuid(getuid());
72                 setgid(getgid());
73 #if OLD_STYLE_FSCRIPT
74                 snprintf(commandstr, sizeof(commandstr), "%s", command);
75 #else
76                 snprintf(commandstr, sizeof(commandstr), "%s %s %d", command, device, volnum);
77 #endif
78                 commandstr[sizeof(commandstr) - 1] = '\0';
79                 execl("/bin/sh", "sh", "-c", commandstr, NULL);
80                 perror("unable to execute shell");
81                 exit(-1);
82         }
83         do {
84                 if (waitpid(pid, &status, 0) == -1) {
85                         if (errno != EINTR) {
86                                 perror("waitpid error");
87                                 return -1;
88                         }
89                 } else {
90                         if (WIFEXITED(status))
91                                 return WEXITSTATUS(status);
92                         else
93                                 return -1;
94                 }
95         } while(1);
96 }
97