]> git.wh0rd.org - dump.git/blob - compat/lib/system.c
6b73d63ef730db05ea2e269f431f8d63d5303d84
[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.2 2001/08/13 16:17:52 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 #include "system.h"
56
57 /*
58 * Executes the command in a shell.
59 * Returns -1 if an error occured, the exit status of
60 * the command on success.
61 */
62 int system_command(const char *command, const char *device, int volnum) {
63 int pid, status;
64 char commandstr[4096];
65
66 pid = fork();
67 if (pid == -1) {
68 perror(" DUMP: unable to fork");
69 return -1;
70 }
71 if (pid == 0) {
72 setuid(getuid());
73 setgid(getgid());
74 #if OLD_STYLE_FSCRIPT
75 snprintf(commandstr, sizeof(commandstr), "%s", command);
76 #else
77 snprintf(commandstr, sizeof(commandstr), "%s %s %d", command, device, volnum);
78 #endif
79 commandstr[sizeof(commandstr) - 1] = '\0';
80 execl("/bin/sh", "sh", "-c", commandstr, NULL);
81 perror("unable to execute shell");
82 exit(-1);
83 }
84 do {
85 if (waitpid(pid, &status, 0) == -1) {
86 if (errno != EINTR) {
87 perror("waitpid error");
88 return -1;
89 }
90 } else {
91 if (WIFEXITED(status))
92 return WEXITSTATUS(status);
93 else
94 return -1;
95 }
96 } while(1);
97 }
98