xref: /netbsd-src/external/gpl2/lvm2/dist/lib/misc/lvm-exec.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: lvm-exec.c,v 1.1.1.1 2008/12/22 00:18:12 haad Exp $	*/
2 
3 /*
4  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5  * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
6  *
7  * This file is part of LVM2.
8  *
9  * This copyrighted material is made available to anyone wishing to use,
10  * modify, copy, or redistribute it subject to the terms and conditions
11  * of the GNU Lesser General Public License v.2.1.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17 
18 #include "lib.h"
19 #include "lvm-exec.h"
20 
21 #include <unistd.h>
22 #include <sys/wait.h>
23 
24 /*
25  * Execute and wait for external command
26  */
27 int exec_cmd(const char *command, const char *fscmd, const char *lv_path,
28 	     const char *size)
29 {
30 	pid_t pid;
31 	int status;
32 
33 	log_verbose("Executing: %s %s %s %s", command, fscmd, lv_path, size);
34 
35 	if ((pid = fork()) == -1) {
36 		log_error("fork failed: %s", strerror(errno));
37 		return 0;
38 	}
39 
40 	if (!pid) {
41 		/* Child */
42 		/* FIXME Use execve directly */
43 		execlp(command, command, fscmd, lv_path, size, NULL);
44 		log_sys_error("execlp", command);
45 		exit(errno);
46 	}
47 
48 	/* Parent */
49 	if (wait4(pid, &status, 0, NULL) != pid) {
50 		log_error("wait4 child process %u failed: %s", pid,
51 			  strerror(errno));
52 		return 0;
53 	}
54 
55 	if (!WIFEXITED(status)) {
56 		log_error("Child %u exited abnormally", pid);
57 		return 0;
58 	}
59 
60 	if (WEXITSTATUS(status)) {
61 		log_error("%s failed: %u", command, WEXITSTATUS(status));
62 		return 0;
63 	}
64 
65 	return 1;
66 }
67