-
Notifications
You must be signed in to change notification settings - Fork 12
/
exec.c
197 lines (174 loc) · 4.65 KB
/
exec.c
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
luksipc - Tool to convert block devices to LUKS in-place.
Copyright (C) 2011-2015 Johannes Bauer
This file is part of luksipc.
luksipc is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; this program is ONLY licensed under
version 3 of the License, later versions are explicitly excluded.
luksipc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with luksipc; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Johannes Bauer <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>
#include "exec.h"
#include "logging.h"
#include "globals.h"
int argCount(const char **aArgs) {
int cnt = 0;
while (aArgs[cnt++]);
return cnt - 1;
}
bool argAppend(const char **aArgs, const char *aNewArg, int *aArgCount, int aArraySize) {
bool success = true;
if ((*aArgCount) < 0) {
*aArgCount = argCount(aArgs);
}
if ((*aArgCount + 2) > aArraySize) {
/* Cannot copy next argument */
success = false;
} else {
aArgs[*aArgCount] = aNewArg;
(*aArgCount)++;
aArgs[*aArgCount] = NULL;
}
return success;
}
bool argAppendParse(const char **aArgs, char *aNewArgs, int *aArgCount, int aArraySize) {
bool success = true;
char *savePtr = NULL;
char *nextToken;
if ((*aArgCount) < 0) {
*aArgCount = argCount(aArgs);
}
while ((nextToken = strtok_r(aNewArgs, ",", &savePtr))) {
if ((*aArgCount + 2) > aArraySize) {
/* Cannot copy next argument */
success = false;
break;
} else {
aArgs[*aArgCount] = nextToken;
(*aArgCount)++;
}
aNewArgs = NULL;
}
aArgs[*aArgCount] = NULL;
return success;
}
void argDump(const char **aArgs) {
int i = 0;
while (aArgs[i]) {
printf(" %2d: '%s'\n", i, aArgs[i]);
i++;
}
}
static char **argCopy(const char **aArgs) {
char **result = NULL;
int i;
result = malloc(sizeof(char*) * EXEC_MAX_ARGCNT);
if (!result) {
perror("malloc");
exit(EXIT_FAILURE);
}
result[EXEC_MAX_ARGCNT - 1] = NULL;
for (i = 0; i < EXEC_MAX_ARGCNT - 1; i++) {
if (aArgs[i] == NULL) {
result[i] = NULL;
break;
}
result[i] = strdup(aArgs[i]);
if (!result[i]) {
perror("strdup");
exit(EXIT_FAILURE);
}
}
return result;
}
static void freeArgCopy(char** aArgCopy) {
int i;
for (i = 0; i < EXEC_MAX_ARGCNT - 1; i++) {
if (aArgCopy[i] == NULL) {
break;
}
free(aArgCopy[i]);
}
free((void*)aArgCopy);
}
static void convertCommandLine(char *aBuffer, int aBufSize, const char **aArguments) {
if ((!aBuffer) || (aBufSize < 4)) {
return;
}
aBuffer[0] = 0;
int remaining = aBufSize - 4;
int position = 0;
int i = 0;
bool truncated = false;
while (aArguments[i]) {
int newChars = snprintf(aBuffer + position, remaining, "%s ", aArguments[i]);
if (newChars >= remaining) {
truncated = true;
break;
}
position += newChars;
remaining -= newChars;
i++;
}
if (truncated) {
strcpy(aBuffer + aBufSize - 5, "...");
} else {
aBuffer[position - 1] = 0;
}
}
struct execResult_t execGetReturnCode(const char **aArguments) {
struct execResult_t execResult;
char **argcopy = argCopy(aArguments);
pid_t pid;
int status;
memset(&execResult, 0, sizeof(execResult));
execResult.success = true;
pid = fork();
if (pid == -1) {
perror("fork");
execResult.success = false;
return execResult;
}
if (pid > 0) {
char commandLineBuffer[256];
convertCommandLine(commandLineBuffer, sizeof(commandLineBuffer), aArguments);
logmsg(LLVL_DEBUG, "Subprocess [PID %d]: Will execute '%s'\n", pid, commandLineBuffer);
}
if (pid == 0) {
/* Child */
if (getLogLevel() < LLVL_DEBUG) {
/* Shut up the child if user did not request debug output */
close(1);
close(2);
}
execvp(aArguments[0], argcopy);
perror("execvp");
logmsg(LLVL_ERROR, "Execution of %s in forked child process failed at execvp: %s\n", aArguments[0], strerror(errno));
/* Exec failed, terminate chExec failed, terminate child process
* (parent will catch this as the return code) */
exit(EXIT_FAILURE);
}
if (waitpid(pid, &status, 0) == (pid_t)-1) {
perror("waitpid");
execResult.success = false;
return execResult;
}
freeArgCopy(argcopy);
execResult.returnCode = WEXITSTATUS(status);
logmsg(LLVL_DEBUG, "Subprocess [PID %d]: %s returned %d\n", pid, aArguments[0], execResult.returnCode);
return execResult;
}