summaryrefslogtreecommitdiffstats
path: root/lib/system/system.c
blob: 13dc146821958561699598040948654b7a8cc78a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>

#include "log/log.h"
#include <talloc/talloc.h>
#include "system.h"

const struct pb_system_apps pb_system_apps = {
	.prefix		= PREFIX,
	.cp		= HOST_PROG_CP,
	.kexec		= HOST_PROG_KEXEC,
	.mount		= HOST_PROG_MOUNT,
	.shutdown	= HOST_PROG_SHUTDOWN,
	.sftp		= HOST_PROG_SFTP,
	.tftp		= HOST_PROG_TFTP,
	.umount		= HOST_PROG_UMOUNT,
	.wget		= HOST_PROG_WGET,
};

int pb_mkdir_recursive(const char *dir)
{
	struct stat statbuf;
	char *str, *sep;
	int mode = 0755;

	if (!*dir)
		return 0;

	if (!stat(dir, &statbuf)) {
		if (!S_ISDIR(statbuf.st_mode)) {
			pb_log("%s: %s exists, but isn't a directory\n",
					__func__, dir);
			return -1;
		}
		return 0;
	}

	str = talloc_strdup(NULL, dir);
	sep = strchr(*str == '/' ? str + 1 : str, '/');

	while (1) {

		/* terminate the path at sep */
		if (sep)
			*sep = '\0';

		if (mkdir(str, mode) && errno != EEXIST) {
			pb_log("mkdir(%s): %s\n", str, strerror(errno));
			return -1;
		}

		if (!sep)
			break;

		/* reset dir to the full path */
		strcpy(str, dir);
		sep = strchr(sep + 1, '/');
	}

	talloc_free(str);

	return 0;
}

int pb_rmdir_recursive(const char *base, const char *dir)
{
	char *cur, *pos;

	/* sanity check: make sure that dir is within base */
	if (strncmp(base, dir, strlen(base)))
		return -1;

	cur = talloc_strdup(NULL, dir);

	while (strcmp(base, dir)) {

		rmdir(dir);

		/* null-terminate at the last slash */
		pos = strrchr(dir, '/');
		if (!pos)
			break;

		*pos = '\0';
	}

	talloc_free(cur);

	return 0;
}

/**
 * pb_run_cmd - Run the supplied command.
 * @cmd_argv: An argument list array for execv.
 * @wait: Wait for the child process to complete before returning.
 * @dry_run: Don't actually fork and exec.
 */

int pb_run_cmd(const char *const *cmd_argv, int wait, int dry_run)
{
#if defined(DEBUG)
	enum {do_debug = 1};
#else
	enum {do_debug = 0};
#endif
	int status;
	pid_t pid;

	if (do_debug) {
		const char *const *p = cmd_argv;

		pb_log("%s: %s", __func__, (dry_run ? "(dry-run) " : ""));

		while (*p) {
			pb_log("%s ", *p);
			p++;
		}
		pb_log("\n");
	} else
		pb_log("%s: %s%s\n", __func__, (dry_run ? "(dry-run) " : ""),
			cmd_argv[0]);

	if (dry_run)
		return 0;

	pid = fork();

	if (pid == -1) {
		pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
		return -1;
	}

	if (pid == 0) {
		int log = fileno(pb_log_get_stream());

		/* Redirect child output to log. */

		status = dup2(log, STDOUT_FILENO);
		assert(status != -1);

		status = dup2(log, STDERR_FILENO);
		assert(status != -1);

		execvp(cmd_argv[0], (char *const *)cmd_argv);
		pb_log("%s: exec failed: %s\n", __func__, strerror(errno));
		exit(EXIT_FAILURE);
	}

	if (!wait && !waitpid(pid, &status, WNOHANG))
		return 0;

	if (waitpid(pid, &status, 0) == -1) {
		pb_log("%s: waitpid failed: %s\n", __func__,
				strerror(errno));
		return -1;
	}

	if (do_debug && WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
		pb_log("%s: signaled\n", __func__);

	if (!WIFEXITED(status)) {
		pb_log("%s: %s failed\n", __func__, cmd_argv[0]);
		return -1;
	}

	if (WEXITSTATUS(status))
		pb_log("%s: WEXITSTATUS %d\n", __func__, WEXITSTATUS(status));

	return WEXITSTATUS(status);
}
OpenPOWER on IntegriCloud