-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathprog_write_and_dlopen.cc
60 lines (52 loc) · 1.77 KB
/
prog_write_and_dlopen.cc
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
/*
* ===============================================================
* Description: Implement write_and_dlopen
*
* Author: Ayush Dubey, [email protected]
*
* Copyright (C) 2015, Cornell University, see the LICENSE file
* for licensing agreement
* ===============================================================
*/
#include <sys/stat.h>
#include <wordexp.h>
#include <dlfcn.h>
#define weaver_debug_
#include <fstream>
#include "common/weaver_constants.h"
#include "common/prog_write_and_dlopen.h"
std::shared_ptr<dynamic_prog_table>
write_and_dlopen(std::vector<uint8_t> &buf, const std::string &prog_handle)
{
std::string dir_unexp = "~/weaver_runtime";
wordexp_t dir_exp;
wordexp(dir_unexp.c_str(), &dir_exp, 0);
std::string dir = dir_exp.we_wordv[0];
mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
std::string so_file_name = dir + "/" + prog_handle;
WDEBUG << "so file name = " << so_file_name << std::endl;
FILE *test_file = fopen(so_file_name.c_str(), "r");
bool created;
if (test_file) {
fclose(test_file);
created = false;
} else {
std::ofstream write_so;
write_so.open(so_file_name, std::ofstream::out | std::ofstream::binary);
write_so.write((const char*)&buf[0], buf.size());
write_so.close();
created = true;
}
void *prog_ptr = dlopen(so_file_name.c_str(), RTLD_NOW);
if (created) {
remove(so_file_name.c_str());
}
if (prog_ptr == NULL) {
WDEBUG << "dlopen error: " << dlerror() << std::endl;
WDEBUG << "failed registering node prog" << std::endl;
return nullptr;
} else {
auto prog_table = std::make_shared<dynamic_prog_table>(prog_ptr);
return prog_table;
}
}