-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleimagewrap.c
186 lines (163 loc) · 5.41 KB
/
simpleimagewrap.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
#include <windows.h>
#include <process.h>
#include <stdio.h>
#include "ini.h"
#define N 30000
typedef struct
{
const char* mounter;
const char* app;
const char* args;
const char* vcdDrive;
const char* vcdPath;
const char* dtType;
const char* dtDrive;
const char* dtPath;
int sleep;
} configuration;
static int handler(void* user, const char* section, const char* name, const char* value)
{
configuration* pconfig = (configuration*)user;
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
if (MATCH("application", "mounter")) {
pconfig->mounter = strdup(value);
}
else if (MATCH("application", "executable")) {
pconfig->app = strdup(value);
}
else if (MATCH("application", "arguments")) {
pconfig->args = strdup(value);
}
else if (MATCH("application", "sleep")) {
pconfig->sleep = atoi(value);
}
else if (MATCH("virtualclonedrive", "path")) {
pconfig->vcdPath = strdup(value);
}
else if (MATCH("virtualclonedrive", "drive")) {
pconfig->vcdDrive = strdup(value);
}
else if (MATCH("daemontools", "type")) {
pconfig->dtType = strdup(value);
}
else if (MATCH("daemontools", "path")) {
pconfig->dtPath = strdup(value);
}
else if (MATCH("daemontools", "drive")) {
pconfig->dtDrive = strdup(value);
}
else {
return 0; /* unknown section/name, error */
}
return 1;
}
char *remove_three(const char *filename) {
size_t len = strlen(filename);
char *newfilename = malloc(len-2);
if (!newfilename) /* handle error */;
memcpy(newfilename, filename, len-3);
newfilename[len - 3] = 0;
return newfilename;
}
int main (int argc, char **argv) {
int rc;
STARTUPINFO si;
PROCESS_INFORMATION pi;
configuration config;
TCHAR szFileName[MAX_PATH];
static char buffer[4096];
char *iniName;
char ini[3];
char *image;
char *dirsep;
char *path;
char *drive;
int isvcd;
dirsep = strrchr( argv[0], '\\' );
if( dirsep != NULL ) {
*dirsep = 0;
}
// change working directory
if(chdir(argv[0]) != 0) {
printf("failed to set working directory to %s\n", argv[0]);
}
else {
printf("changed working directory to %s\n", argv[0]);
}
// figure out ini name, and load it
GetModuleFileName( NULL, szFileName, MAX_PATH );
sprintf(buffer, "%s", basename(szFileName));
iniName = remove_three(buffer);
strcpy(ini, "ini");
strcat(iniName, ini);
config.mounter = strdup("");
config.app = strdup("");
config.args = strdup("");
config.sleep = 0;
if (ini_parse(iniName, handler, &config) < 0) {
printf("Can't load '%s'\n", buffer);
return 1;
}
char *foobar = "virtualclonedrive";
if(strcmp(config.mounter, foobar) == 0) {
isvcd = 1;
path = strdup(config.vcdPath);
drive = strdup(config.vcdDrive);
image = malloc(snprintf(NULL, 0, " -mount %s,\"%s\"", drive, argv[1]) + 1);
sprintf(image, " -mount %s,\"%s\"", drive, argv[1]);
}
else {
isvcd = 0;
path = strdup(config.dtPath);
drive = strdup(config.dtDrive);
image = malloc(snprintf(NULL, 0, " -mount %s,%s,\"%s\"", config.dtType, drive, argv[1]) + 1);
sprintf(image, " -mount %s,%s,\"%s\"", config.dtType, drive, argv[1]);
}
// print out a summary
printf("config\t%s\npath\t%s\ndrive\t%s\nimage\t%s\napp\t%s\nargs\t%s\nsleep\t%d\nmounter\t%s\n\n", iniName, path, drive, argv[1], config.app, config.args, config.sleep, config.mounter);
fflush(stdout);
// mount drive
printf ("mounting \"%s\" to drive %s\n", argv[1], drive);
fflush(stdout);
memset (&si, 0, sizeof (si));
si.cb = sizeof (si);
rc = CreateProcess (path, image, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
if (!rc) {
printf ("failed to launch process: %s%s; Errorcode: %lu\n", path, image, GetLastError ());
exit(1);
}
// sleep a given amount of time after loading the image, before running the executable
sleep(config.sleep);
// run the executable
printf ("starting %s%s\n", config.app, image);
fflush(stdout);
realloc(image, snprintf(NULL, 0, "\"%s\" %s", config.app, config.args) + 1);
sprintf(image, "\"%s\" %s", config.app, config.args);
rc = CreateProcess (config.app, image, NULL, NULL, TRUE, 0, NULL, argv[0], &si, &pi);
if (!rc) {
printf ("failed to launch process: %s%s; Errorcode: %lu\n", config.app, image, GetLastError ());
exit(1);
}
// wait for the executable to finish
printf ("waiting for application to exit\n");
fflush(stdout);
WaitForSingleObject(pi.hProcess, INFINITE);
// unmount the image we mounted earlier
printf ("unmounting drive %s\n", drive);
fflush(stdout);
if(isvcd == 1) {
realloc(image, snprintf(NULL, 0, " -unmount %s", drive) + 1);
sprintf(image, " -unmount %s", drive);
}
else {
realloc(image, snprintf(NULL, 0, " -unmount %s,%s", config.dtType, drive) + 1);
sprintf(image, " -unmount %s,%s", config.dtType,drive);
}
rc = CreateProcess (path, image, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
if (!rc) {
printf ("failed to launch process: %s%s; Code %lu\n", path, image, GetLastError ());
exit(1);
}
// all done
return 0;
}