00001
00002 #include "ampi_funcptr_loader.h"
00003
00004 #if defined _WIN32
00005 # ifndef WIN32_LEAN_AND_MEAN
00006 # define WIN32_LEAN_AND_MEAN
00007 # endif
00008 # ifndef NOMINMAX
00009 # define NOMINMAX
00010 # endif
00011 # include <windows.h>
00012 # include <io.h>
00013 # define access _access
00014 #elif defined __APPLE__
00015 # include <unistd.h>
00016 # include <copyfile.h>
00017 # include <errno.h>
00018 #else
00019 # include <unistd.h>
00020 # include <sys/types.h>
00021 # include <sys/wait.h>
00022 # include <errno.h>
00023 #endif
00024
00025 #include <string>
00026 #include <atomic>
00027
00028 static void fs_copy(const char * src, const char * dst)
00029 {
00030 static const char abortmsg[] = "Could not copy fsglobals user program!";
00031
00032 #if defined _WIN32
00033 BOOL ret = CopyFile(src, dst, true);
00034 if (ret == 0)
00035 {
00036 CkError("ERROR> CopyFile(): %d\n", (int)GetLastError());
00037 CkAbort(abortmsg);
00038 }
00039 #elif defined __APPLE__
00040 int ret = copyfile(src, dst, 0, COPYFILE_ALL);
00041 if (ret < 0)
00042 {
00043 CkError("ERROR> copyfile(): %d %s\n", ret, strerror(errno));
00044 CkAbort(abortmsg);
00045 }
00046 #else
00047 pid_t pid = fork();
00048 if (pid == 0)
00049 {
00050 execl("/bin/cp", "/bin/cp", src, dst, NULL);
00051 CkError("ERROR> execl(): %s\n", strerror(errno));
00052 CkAbort(abortmsg);
00053 }
00054 else if (pid < 0)
00055 {
00056 CkError("ERROR> fork(): %s\n", strerror(errno));
00057 CkAbort(abortmsg);
00058 }
00059 else
00060 {
00061 int status;
00062 pid_t ws = waitpid(pid, &status, 0);
00063 if (ws == -1)
00064 CkError("ERROR> waitpid(): %s\n", strerror(errno));
00065 }
00066 #endif
00067 }
00068
00069 static std::atomic<size_t> rank_count{};
00070
00071 int main(int argc, char ** argv)
00072 {
00073 SharedObject myexe;
00074
00075
00076 {
00077 static const char FUNCPTR_SHIM_SUFFIX[] = ".user";
00078
00079 std::string src{ampi_binary_path};
00080 src += FUNCPTR_SHIM_SUFFIX;
00081
00082 std::string dst{src};
00083 dst += '.';
00084 dst += std::to_string(rank_count++);
00085 const char * dststr = dst.c_str();
00086
00087 if (access(dststr, R_OK) != 0)
00088 fs_copy(src.c_str(), dststr);
00089
00090 myexe = dlopen(dststr, RTLD_NOW|RTLD_LOCAL);
00091 }
00092
00093 if (myexe == nullptr)
00094 {
00095 CkError("dlopen error: %s\n", dlerror());
00096 CkAbort("Could not open fsglobals user program!");
00097 }
00098
00099 return AMPI_FuncPtr_Loader(myexe, argc, argv);
00100 }