Fix for some VM's

This commit is contained in:
WolverinDEV 2020-06-16 19:40:32 +02:00
parent 14d2578a67
commit c59346ea68

View File

@ -1,3 +1,4 @@
#include <dlfcn.h>
#include <client/linux/handler/exception_handler.h>
#include <iostream>
#include <misc/strobf.h>
@ -439,3 +440,32 @@ int main(int argc, char** argv) {
mainThreadDone = true;
return 0;
}
/* Fix for Virtuzzo 7 where sometimes the pthread create fails! */
typedef int (*pthread_create_t)(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
pthread_create_t original_pthread_create{nullptr};
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg) {
if(!original_pthread_create) {
original_pthread_create = (pthread_create_t) dlsym(RTLD_NEXT, "pthread_create");
if(!original_pthread_create) {
std::cerr << "[CRITICAL] Missing original pthread_create function. Aborting execution!" << std::endl;
std::abort();
}
}
int result, attempt{0};
while((result = original_pthread_create(thread, attr, start_routine, arg)) != 0 && errno == EAGAIN) {
if(attempt > 10) {
std::cerr << "[CRITICAL] pthread_create(...) cause EAGAIN for the last 10 attempts! Aborting application execution!" << std::endl;
std::abort();
} else if(attempt > 5) {
/* let some other threads do work */
pthread_yield();
}
std::cerr << "[CRITICAL] pthread_create(...) cause EAGAIN! Trying again in 5usec (Attempt: " << attempt << ")" << std::endl;
usleep(5);
attempt++;
}
return result;
}