xref: /netbsd-src/external/gpl2/lvm2/dist/lib/misc/lvm-exec.c (revision 7c604eea85b4f330dc75ffe65e947f4d73758aa0)
1*7c604eeaShaad /*	$NetBSD: lvm-exec.c,v 1.1.1.2 2009/12/02 00:26:43 haad Exp $	*/
256a34939Shaad 
356a34939Shaad /*
456a34939Shaad  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5*7c604eeaShaad  * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
656a34939Shaad  *
756a34939Shaad  * This file is part of LVM2.
856a34939Shaad  *
956a34939Shaad  * This copyrighted material is made available to anyone wishing to use,
1056a34939Shaad  * modify, copy, or redistribute it subject to the terms and conditions
1156a34939Shaad  * of the GNU Lesser General Public License v.2.1.
1256a34939Shaad  *
1356a34939Shaad  * You should have received a copy of the GNU Lesser General Public License
1456a34939Shaad  * along with this program; if not, write to the Free Software Foundation,
1556a34939Shaad  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1656a34939Shaad  */
1756a34939Shaad 
1856a34939Shaad #include "lib.h"
19*7c604eeaShaad #include "device.h"
20*7c604eeaShaad #include "locking.h"
2156a34939Shaad #include "lvm-exec.h"
22*7c604eeaShaad #include "toolcontext.h"
2356a34939Shaad 
2456a34939Shaad #include <unistd.h>
2556a34939Shaad #include <sys/wait.h>
2656a34939Shaad 
2756a34939Shaad /*
28*7c604eeaShaad  * Create verbose string with list of parameters
29*7c604eeaShaad  */
_verbose_args(const char * const argv[],char * buf,size_t sz)30*7c604eeaShaad static char *_verbose_args(const char *const argv[], char *buf, size_t sz)
31*7c604eeaShaad {
32*7c604eeaShaad 	int pos = 0;
33*7c604eeaShaad 	int len;
34*7c604eeaShaad 	unsigned i;
35*7c604eeaShaad 
36*7c604eeaShaad 	buf[0] = '\0';
37*7c604eeaShaad 	for (i = 0; argv[i]; i++) {
38*7c604eeaShaad 		if ((len = dm_snprintf(buf + pos, sz - pos,
39*7c604eeaShaad 				       "%s ", argv[i])) < 0)
40*7c604eeaShaad 			/* Truncated */
41*7c604eeaShaad 			break;
42*7c604eeaShaad 		pos += len;
43*7c604eeaShaad 	}
44*7c604eeaShaad 
45*7c604eeaShaad 	return buf;
46*7c604eeaShaad }
47*7c604eeaShaad 
48*7c604eeaShaad /*
4956a34939Shaad  * Execute and wait for external command
5056a34939Shaad  */
exec_cmd(struct cmd_context * cmd,const char * const argv[])51*7c604eeaShaad int exec_cmd(struct cmd_context *cmd, const char *const argv[])
5256a34939Shaad {
5356a34939Shaad 	pid_t pid;
5456a34939Shaad 	int status;
55*7c604eeaShaad 	char buf[PATH_MAX * 2];
5656a34939Shaad 
57*7c604eeaShaad 	log_verbose("Executing: %s", _verbose_args(argv, buf, sizeof(buf)));
5856a34939Shaad 
5956a34939Shaad 	if ((pid = fork()) == -1) {
6056a34939Shaad 		log_error("fork failed: %s", strerror(errno));
6156a34939Shaad 		return 0;
6256a34939Shaad 	}
6356a34939Shaad 
6456a34939Shaad 	if (!pid) {
6556a34939Shaad 		/* Child */
66*7c604eeaShaad 		reset_locking();
67*7c604eeaShaad 		dev_close_all();
68*7c604eeaShaad 		/* FIXME Fix effect of reset_locking on cache then include this */
69*7c604eeaShaad 		/* destroy_toolcontext(cmd); */
7056a34939Shaad 		/* FIXME Use execve directly */
71*7c604eeaShaad 		execvp(argv[0], (char **const) argv);
72*7c604eeaShaad 		log_sys_error("execvp", argv[0]);
73*7c604eeaShaad 		_exit(errno);
7456a34939Shaad 	}
7556a34939Shaad 
7656a34939Shaad 	/* Parent */
7756a34939Shaad 	if (wait4(pid, &status, 0, NULL) != pid) {
7856a34939Shaad 		log_error("wait4 child process %u failed: %s", pid,
7956a34939Shaad 			  strerror(errno));
8056a34939Shaad 		return 0;
8156a34939Shaad 	}
8256a34939Shaad 
8356a34939Shaad 	if (!WIFEXITED(status)) {
8456a34939Shaad 		log_error("Child %u exited abnormally", pid);
8556a34939Shaad 		return 0;
8656a34939Shaad 	}
8756a34939Shaad 
8856a34939Shaad 	if (WEXITSTATUS(status)) {
89*7c604eeaShaad 		log_error("%s failed: %u", argv[0], WEXITSTATUS(status));
9056a34939Shaad 		return 0;
9156a34939Shaad 	}
9256a34939Shaad 
9356a34939Shaad 	return 1;
9456a34939Shaad }
95