summaryrefslogtreecommitdiff
path: root/NativeLauncher/installer-plugin/common.cc
blob: fe896095d393432f18a31eeec5cff883f9cf130b (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <pkgmgr-info.h>
#include <pkgmgr_installer_info.h>
#include <aul.h>

#include "log.h"
#include "utils.h"
#include "pkgmgr_parser_plugin_interface.h"

#include <wait.h>
#include <dirent.h>
#include <sys/stat.h>

#include <algorithm>
#include <string>

#include <pwd.h>
#include <grp.h>
#include <sys/stat.h>
#include <unistd.h>

#include "common.h"

#ifdef  LOG_TAG
#undef  LOG_TAG
#endif
#define LOG_TAG "NETCORE_INSTALLER_PLUGIN"

#ifndef DEVICE_API_DIR
#error "DEVICE_API_DIR is missed"
#endif

#ifndef RUNTIME_DIR
#error "RUNTIME_DIR is missed"
#endif

#ifndef CROSSGEN_PATH
#error "CROSSGEN_PATH is missed"
#endif

#define __XSTR(x) #x
#define __STR(x) __XSTR(x)
static const char* __DEVICE_API_DIR = __STR(DEVICE_API_DIR);
static const char* __RUNTIME_DIR = __STR(RUNTIME_DIR);
static const char* __CROSSGEN_PATH = __STR(CROSSGEN_PATH);
static const char* __JIT_PATH = __STR(RUNTIME_DIR)"/libclrjit.so";
#undef __STR
#undef __XSTR

static void crossgen(const char* dllPath, const char* appPath);
static void smack_(const char* dllPath, const char* label);

std::string replace(std::string &str, const std::string& from, const std::string& to)
{
	size_t startPos = 0;
	while ((startPos = str.find(from, startPos)) != std::string::npos) {
		str.replace(startPos, from.length(), to);
		startPos += to.length();
	}
	return str;
}

void createNiPlatform()
{
	std::string coreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.dll");
	std::string niCoreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.ni.dll");

	if (fileNotExist(niCoreLib)) {
		crossgen(coreLib.c_str(), nullptr);
		smack_(niCoreLib.c_str(), "_");
	}

	const char* platformDirs[] = {__RUNTIME_DIR, __DEVICE_API_DIR, "/usr/bin"};
	const char* ignores[] = {coreLib.c_str()};

	createNiUnderDirs(platformDirs, 3, ignores, 1, [](const char* ni) {
		smack_(ni, "_");
	});
}

void createNiSelect(const char* dllPath)
{
	std::string coreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.dll");
	std::string niCoreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.ni.dll");

	if (fileNotExist(niCoreLib)) {
		crossgen(coreLib.c_str(), nullptr);
		smack_(niCoreLib.c_str(), "_");
	}

	if (!fileNotExist(dllPath)) {
		std::string strPath = dllPath;
		std::string niPath = replace(strPath, std::string(".dll"), std::string(".ni.dll"));
		if (fileNotExist(niPath))
			crossgen(dllPath, nullptr);
		else
			printf("Already [%s] file is exist\n", niPath.c_str());
		smack_(niPath.c_str(), "_");
	}
}

static void smack_(const char* dllPath, const char* label)
{
	static const char* chsmack = "/usr/bin/chsmack";
	pid_t pid = fork();
	if (pid == -1)
		return;

	if (pid > 0) {
		int status;
		waitpid(pid, &status, 0);
		if (WIFEXITED(status))
			return;
	} else {
		const char* args[] = {
			chsmack,
			"-a", label,
			dllPath,
			nullptr
		};
		execv(chsmack, const_cast<char*const*>(args));

		exit(0);
	}
}

static void crossgen(const char* dllPath, const char* appPath)
{
	//pid_t parent = getpid();
	pid_t pid = fork();
	if (pid == -1)
		return;

	if (pid > 0) {
		int status;
		waitpid(pid, &status, 0);
		if (WIFEXITED(status))
			return;
	} else {
		// search dlls in the application directory first, to use application dlls
		// instead of system dlls when proceeding NI
		std::vector<std::string> tpaDir;
		if (appPath != NULL) {
			std::string path(appPath);
			std::string::size_type prevPos = 0, pos = 0;
			while ((pos = path.find(':', pos)) != std::string::npos) {
				std::string substring(path.substr(prevPos, pos - prevPos));
				tpaDir.push_back(substring);
				prevPos = ++pos;
			}
			std::string substring(path.substr(prevPos, pos - prevPos));
			tpaDir.push_back(substring);
		}
		tpaDir.push_back(__RUNTIME_DIR);
		tpaDir.push_back(__DEVICE_API_DIR);

		// get reference API directory ([DEVICE_API_DIR]/ref)
		int len = strlen(__DEVICE_API_DIR);
		char* refAPIDir = (char*)calloc(len + 4, 1);
		snprintf(refAPIDir, len + 4, "%s%s", __DEVICE_API_DIR, "/ref");
		tpaDir.push_back(refAPIDir);

		std::string tpa;
		assembliesInDirectory(tpaDir, tpa);

		std::vector<const char*> argv = {
			__CROSSGEN_PATH,
			"/Trusted_Platform_Assemblies", tpa.c_str(),
			"/JITPath", __JIT_PATH,
			"/FragileNonVersionable"
		};
		if (appPath != nullptr) {
			argv.push_back("/App_Paths");
			argv.push_back(appPath);
		}
		argv.push_back(dllPath);
		argv.push_back(nullptr);

		printf("+ %s\n", dllPath);

		execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
		exit(0);
	}
}

static int getRootPath(const char *pkgId, std::string& rootPath)
{
	int ret = 0;
	char *path = 0;

	uid_t uid = 0;

	if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
		_ERR("Failed to get UID");
		return -1;
	}

	_INFO("user id is %d", uid);

	pkgmgrinfo_pkginfo_h handle;
	if (uid == 0) {
		ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId, &handle);
		if (ret != PMINFO_R_OK)
			return -1;
	} else {
		ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId, uid, &handle);
		if (ret != PMINFO_R_OK)
			return -1;
	}

	ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
	if (ret != PMINFO_R_OK) {
		pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
		return -1;
	}
	rootPath = path;
	pkgmgrinfo_pkginfo_destroy_pkginfo(handle);

	return 0;
}

static bool niExist(const std::string& path, std::string& ni)
{
	static const char* possibleExts[] = {
		".ni.dll", ".NI.dll", ".NI.DLL", ".ni.DLL",
		".ni.exe", ".NI.exe", ".NI.EXE", ".ni.EXE"
	};
	std::string fName = path.substr(0, path.size() - 4);

	struct stat sb;

	for (const char* ext : possibleExts) {
		std::string f = fName + ext;
		if (stat(f.c_str(), &sb) == 0) {
			ni = f;
			return true;
		}
	}

	return false;
}

void createNiUnderDirs(const char* rootPaths[], int count, const char* ignores[], int igcount, afterCreate cb)
{
	std::string appPaths;
	for (int i = 0; i < count; i++) {
		appPaths += rootPaths[i];
		appPaths += ':';
	}

	if (appPaths.back() == ':')
		appPaths.pop_back();

	auto convert = [&appPaths, ignores, igcount, &cb](const char* path, const char* name) {
		for (int i = 0; i < igcount; i++) {
			if (strcmp(path, ignores[i]) == 0)
				return;
		}
		std::string ni;
		if (isManagedAssembly(path) && !isNativeImage(path) && !niExist(path, ni)) {
			crossgen(path, appPaths.c_str());
			if (niExist(path, ni)) {
				// change owner and groups for generated ni file.
				struct stat info;
				if (!stat(path, &info)) {
					if (chown(ni.c_str(), info.st_uid, info.st_gid) == -1)
						_ERR("Failed to change owner and group name");
				}

				if (cb != nullptr)
					cb(ni.c_str());
			}
		}
	};

	for (int i = 0; i < count; i++)
		scanFilesInDir(rootPaths[i], convert, -1);
}
void createNiUnderDirs(const char* rootPaths[], int count, afterCreate cb)
{
	createNiUnderDirs(rootPaths, count, nullptr, 0, cb);
}
void createNiUnderDirs(const char* rootPaths[], int count)
{
	createNiUnderDirs(rootPaths, count, nullptr);
}

int createNiUnderPkgRoot(const char* pkgName)
{
	std::string pkgRoot;
	if (getRootPath(pkgName, pkgRoot) < 0)
		return 1;

	std::string binDir = concatPath(pkgRoot, "bin");
	std::string libDir = concatPath(pkgRoot, "lib");
	_INFO("bindir : %s", binDir.c_str());
	_INFO("libdir : %s", libDir.c_str());

	const char* paths[] = {
		binDir.c_str(),
		libDir.c_str()
	};

	// change smack label for generated ni file.
	std::string label = "User::Pkg::" + std::string(pkgName) + "::RO";
	createNiUnderDirs(paths, 2, [label](const char* ni) {
			smack_(ni, label.c_str());
	});

	return 0;
}