Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 : : * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
4 : : *
5 : : * This file is part of LVM2.
6 : : *
7 : : * This copyrighted material is made available to anyone wishing to use,
8 : : * modify, copy, or redistribute it subject to the terms and conditions
9 : : * of the GNU Lesser General Public License v.2.1.
10 : : *
11 : : * You should have received a copy of the GNU Lesser General Public License
12 : : * along with this program; if not, write to the Free Software Foundation,
13 : : * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 : : */
15 : :
16 : : #include "lib.h"
17 : : #include "device.h"
18 : : #include "locking.h"
19 : : #include "lvm-exec.h"
20 : : #include "toolcontext.h"
21 : :
22 : : #include <unistd.h>
23 : : #include <sys/wait.h>
24 : :
25 : : /*
26 : : * Create verbose string with list of parameters
27 : : */
28 : 0 : static char *_verbose_args(const char *const argv[], char *buf, size_t sz)
29 : : {
30 : 0 : int pos = 0;
31 : : int len;
32 : : unsigned i;
33 : :
34 : 0 : buf[0] = '\0';
35 [ # # ]: 0 : for (i = 0; argv[i]; i++) {
36 [ # # ]: 0 : if ((len = dm_snprintf(buf + pos, sz - pos,
37 : 0 : "%s ", argv[i])) < 0)
38 : : /* Truncated */
39 : 0 : break;
40 : 0 : pos += len;
41 : : }
42 : :
43 : 0 : return buf;
44 : : }
45 : :
46 : : /*
47 : : * Execute and wait for external command
48 : : */
49 : 0 : int exec_cmd(struct cmd_context *cmd, const char *const argv[])
50 : : {
51 : : pid_t pid;
52 : : int status;
53 : : char buf[PATH_MAX * 2];
54 : :
55 : 0 : log_verbose("Executing: %s", _verbose_args(argv, buf, sizeof(buf)));
56 : :
57 [ # # ]: 0 : if ((pid = fork()) == -1) {
58 : 0 : log_error("fork failed: %s", strerror(errno));
59 : 0 : return 0;
60 : : }
61 : :
62 [ # # ]: 0 : if (!pid) {
63 : : /* Child */
64 : 0 : reset_locking();
65 : 0 : dev_close_all();
66 : : /* FIXME Fix effect of reset_locking on cache then include this */
67 : : /* destroy_toolcontext(cmd); */
68 : : /* FIXME Use execve directly */
69 : 0 : execvp(argv[0], (char **const) argv);
70 : 0 : log_sys_error("execvp", argv[0]);
71 : 0 : _exit(errno);
72 : : }
73 : :
74 : : /* Parent */
75 [ # # ]: 0 : if (wait4(pid, &status, 0, NULL) != pid) {
76 : 0 : log_error("wait4 child process %u failed: %s", pid,
77 : : strerror(errno));
78 : 0 : return 0;
79 : : }
80 : :
81 [ # # ]: 0 : if (!WIFEXITED(status)) {
82 : 0 : log_error("Child %u exited abnormally", pid);
83 : 0 : return 0;
84 : : }
85 : :
86 [ # # ]: 0 : if (WEXITSTATUS(status)) {
87 : 0 : log_error("%s failed: %u", argv[0], WEXITSTATUS(status));
88 : 0 : return 0;
89 : : }
90 : :
91 : 0 : return 1;
92 : : }
|