I have a small C++ program that pings other machines on a network and send status information out over another network. The program runs as a daemon, so the startup process forks off a child and then calls exit. This program is cross-compiled to run on two different architectures: x86 and ARM. The GCC versions are 4.4 and 3.5 respectively. I can compile and run the program on x86 and it works flawlessly. However, when I run the program on the ARM, it hangs any time I call exit, not just after the fork. I have no functions registered with atexit or on_exit. The following are my includes:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <set>
#include <vector>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include "telnet_client.h"
#include <stdint.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <netdb.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <syslog.h>
#include <customlib1.h>
#include <customlib2.h>
The following are my GCC commands:
arm-none-linux-gnueabi-g++ -Wall -DBUILDSTAMP="\"`date '+%F %T'`\"" -g -ggdb -O0 -I/usr/local/arm/arm-2007q1/arm-none-linux-gnueabi/libc/usr/include -DEMBEST_ARM -I/usr/local/share/arm/boost/src -I../include/ -I../include_rms -c can_wifid.cpp -o can_wifid.o
arm-none-linux-gnueabi-g++ -Wall -DBUILDSTAMP="\"`date '+%F %T'`\"" -g -ggdb -O0 -I/usr/local/arm/arm-2007q1/arm-none-linux-gnueabi/libc/usr/include -DEMBEST_ARM -I/usr/local/share/arm/boost/src -I../include/ -I../include_rms -c telnet_client.cpp -o telnet_client.o
arm-none-linux-gnueabi-g++ -Wall -DBUILDSTAMP="\"`date '+%F %T'`\"" -g -ggdb -O0 -I/usr/local/arm/arm-2007q1/arm-none-linux-gnueabi/libc/usr/include -DEMBEST_ARM -I/usr/local/share/arm/boost/src can_wifid.o telnet_client.o -L/usr/local/arm/arm-2007q1/arm-none-linux-gnueabi/libc/usr/lib -L../lib_embest_arm -L../lib_rms_embest_arm -Wl,-Bdynamic -lutilities -lboost_system -lboost_thread -lcanprovider -lembestcan -o can_wifid
Even just parsing my command line parameters with getopt then calling exit after a version message causes this problem (simplest case in my program). Has anybody ever experienced anything like this where exit just hangs ?
EDIT: Added code for the first part of the main function:
struct canwifid_options
{
public:
bool daemon_mode;
int verbosity;
canwifid_options()
{
this->daemon_mode = false;
this->verbosity = LOG_NOTICE;
}
};
static canwifid_options options;
int main(int argc, char * argv[])
{
int LoggingOptions = LOG_CONS|LOG_NDELAY|LOG_PID;
pid_t Pid;
ParseCommandLine(argc, argv);
if (!options.daemon_mode)
{
LoggingOptions |= LOG_PERROR;
}
openlog("can_wifid", LoggingOptions, LOG_USER);
setlogmask(LOG_UPTO(options.verbosity));
if (options.daemon_mode)
{
Pid = fork();
if (Pid < 0)
{
// couldn't fork off and create a child process
// log it, %m is a special syslog flag
syslog(LOG_CRIT, "Unable to create daemon [Error: %m]");
exit(ESRCH);
}
else if (Pid > 0)
{
// we're the parent, so we're done and out of here
exit(EXIT_SUCCESS);
}
else
{
// we're the child, take control of the session.
setsid();
// change to the root directory so we don't retain unnecessary control
// of any mounted volumes
chdir("/");
// clear our file mode creation mask
umask(0000);
}
}
else
{
// get our process ID
Pid = getpid();
}
syslog(LOG_INFO, "Running as %s", options.daemon_mode ? "daemon" : "standalone");
// Network code here, snipped for clarity
}
And there ParseCommandLine function:
static void ParseCommandLine(int argc, char *argv[])
{
int c;
while ((c = getopt(argc, argv, "dhqvDV?")) > 0)
{
switch (c)
{
case 'd':
options.daemon_mode = true;
break;
case 'V':
VersionMessage(argv);
exit(EXIT_SUCCESS);
break;
case 'q':
options.verbosity = LOG_WARNING;
break;
case 'v':
options.verbosity = LOG_INFO;
break;
case 'D':
options.verbosity = LOG_DEBUG;
break;
case 'h':
case '?':
default:
HelpMessage(argv);
exit(EXIT_SUCCESS);
break;
}
}
return; //done
}
An issue I ran into once is that calling exit attempts to exit and calls global destructors WITHOUT trying to unwind the stack or call any destructors for local objects on the stack. This can easily manifest if you have any sort of locks that might be required for global destructors. For example, this program deadlocks in exit (actually in the global dtor):
#include <iostream>
#include <mutex>
std::mutex lock;
class A {
public:
A() {
std::lock_guard<std::mutex> acquire(lock);
std::cout << "ctor A" << std::endl;
}
~A() {
std::lock_guard<std::mutex> acquire(lock);
std::cout << "dtor A" << std::endl;
}
};
A a;
int main()
{
std::lock_guard<std::mutex> acquire(lock);
exit(0);
}
Now your problem may be something completely different, but there's a strong possibility its related to something in a global destructor that is misbehaving because some object on the stack has not been destroyed properly.
If you want to exit while unwinding the stack properly (which is required for RAII structured code), you can't call exit. Instead you need to throw an exception which is caught (only) in main and causes main to return.
Related
I'm trying to hook some ncurses functions but they don't have any effect.
ncurses isn't statically linked, so I don't see why it wouldn't work.
test.cpp
#include <cstdio>
#include <cstdlib>
#include <curses.h>
int main() {
initscr();
cbreak();
noecho();
getch();
endwin();
return 0;
}
Compiled with: gcc test.cpp -o test -std=c++11 -lncurses
hook.cpp
#include <dlfcn.h>
#include <cstdio>
#include <cstdlib>
int getch() {
typedef int getch ();
getch* old_getch = (getch*) dlsym(RTLD_NEXT, "getch");
int result = old_getch();
fprintf(stderr, "getch() = %i\n", result);
return result;
}
int noecho() {
typedef int noecho ();
noecho* old_noecho = (noecho*) dlsym(RTLD_NEXT, "noecho");
int result = old_noecho();
fprintf(stderr, "noecho() = %i\n", result);
return result;
}
int endwin() {
typedef int endwin ();
endwin* old_endwin = (endwin*) dlsym(RTLD_NEXT, "endwin");
int result = old_endwin();
printf("endwin called");
return result;
}
Compiled with: gcc hook.cpp -o hook.so -shared -ldl -fPIC -std=c++11
It sadly outputs nothing, and I'm completely stumped.
The specification doesn't state getch has to be a function (not a macro). Actually, in ncurses-6.1, getch defined as
#define getch() wgetch(stdscr)
Nonetheless, there is a getch function in libncurses (which simply calls wgetch(stdscr)), so dlsym(libncurses_handle,"getch") does work.
This is main.h, I shared with you just in case there would be something wrong here. But I don't think so.
Then, there is main.cpp which is the entry point of my program.
If I generate it with Visual Studio 2017, it will compile and run without errors but it will not print anything on the console.
I can't figure out why.
main.h:
#pragma once
#include <thread>
#include "GabEngine/MainEngine.h"
#include "GraphicInterface/Console.h"
#include "GabEngine/Globals.h"
int m_ScreenWidth = 500, m_ScreenHeight = 500;
GabEngine::Wind m_RootWindow;
GabEngine::MainEngine m_MainEngine(&m_RootWindow);
Globals::NetworkStatus NetStatus;
Networking::MainNetwork m_MainNetwork(&NetStatus);
void TaskConsole();
void TaskNetwork();
main.cpp:
#include "GraphicInterface/main.h"
#include <iostream>
#undef main
void TaskConsole()
{
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
GabEngine::FatalError("Failed to initialize SDL");
}
m_RootWindow.Create("Utryon", m_ScreenWidth, m_ScreenHeight, 2);
m_MainEngine.InitShaders();
m_MainEngine.InitCEGUI("C:/Users/Bob/Documents/Visual Studio 2017/Projects/Utryon/GabEngine/GUI");
m_MainEngine.LoadScheme("UtryonLook.scheme");
m_MainEngine.SetFont("DejaVuSans-10");
GraphicInterface::Console MainConsole(&m_MainEngine, &m_MainNetwork);
MainConsole.InitConsole();
MainConsole.Run();
}
void TaskNetwork()
{
m_MainNetwork.Run();
}
int main(int argc, char** argv)
{
std::cout << "Here 1" << std::endl; //It is supposed to print Here 1
thread ConsoleThread(TaskConsole);
thread NetworkThread(TaskNetwork);
ConsoleThread.join();
NetworkThread.join();
// End of program
return 0;
}
Here is my source code:
#include "stdio.h"
#include <stdlib.h>
#include <string.h>
#include "thread"
#include "mutex"
int count0=0 ,count1 =0;
std::mutex main_thread;
std::unique_lock<std::mutex> lck(main_thread, std::defer_lock);
void Function00(long millisecond){
while (true){
lck.lock();
count1++;
printf("count0:%d count1:%d \n",count0,count1);
lck.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(millisecond));
}
}
void Function01(){
std::thread th(Function00, 1000);//count per 1 s
do{
lck.lock();
count0++;
lck.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(500));//count per 0.5 s
} while (1);
}
int main(int argc, char *argv[])
{
Function01();
return 0;
}
I then use the command to build my .o file:
g++ -std=c++11 -pthread testa.cpp -o a.o
However, it shows the error:
terminate called after throwing an instance of 'std::system_error'
what(): Resource deadlock avoided
Aborted
I feel confuse and no idea to solve it, so i try in Microsoft VS2013, and it runs with no error... I feel confuse to it. Is it the problem in linux? What should i modify to prevent the deadlock?
unique_lock cannot be locked twice, if you want to lock a mutex on two threads so that one of them will block, you need to use two unique_locks.
void Function00(long millisecond){
while (true){
{
std::unique_lock<std::mutex> lck(main_thread);
count1++;
printf("count0:%d count1:%d \n",count0,count1);
}
std::this_thread::sleep_for(std::chrono::milliseconds(millisecond));
}
}
void Function01(){
std::thread th(Function00, 1000);//count per 1 s
do{
{
std::unique_lock<std::mutex> lck(main_thread);
count0++;
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));//count per 0.5 s
} while (1);
}
I'm writing a fuzzer in C++ on linux. It spawns multiple threads and has a timeout function if the thread hangs for whatever reason. I cannot figure out the correct way to kill the thread off after it's timer runs out. What I am doing now is something along the lines of:
`
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <chrono>
#include <thread>
#include <stdlib.h>
#include <sys/wait.h>
#include <stdio.h>
#include <vector>
#include <iostream>
#include <sys/time.h>
#include <random>
#include <cstdlib>
#include <climits>
#include <sstream>
#include <iomanip>
#include <unistd.h>
#include <cstring>
#define READ 0
#define WRITE 1
void reaper (int c_pid, int t_timeout) {
std::this_thread::sleep_for(std::chrono::milliseconds(t_timeout));
kill (c_pid, 9);
}
FILE * popen2 (std::string command, std::string type, int & pid, std::string low_lvl_user) {
pid_t child_pid;
int fd[2];
pipe(fd);
if((child_pid = fork()) == -1) {
perror("fork");
exit(1);
}
if (child_pid == 0) { // child begins
if (type == "r") {
close(fd[READ]); //Close the READ
dup2(fd[WRITE], 1); //Redirect stdout to pipe
}
else {
close(fd[WRITE]); //Close the WRITE
dup2(fd[READ], 0); //Redirect stdin to pipe
}
if (getuid() == 0) {
execl("/bin/su", "su", "-c", "/bin/sh", "-c", command.c_str(), low_lvl_user.c_str(), NULL); // fixes not being able to reap suid 0 processes
}
else {
execl("/bin/sh", "/bin/sh", "-c", command.c_str(), NULL); // runs it all
}
exit(0);
}
else {
if (type == "r") {
close(fd[WRITE]); //Close the WRITE
}
else {
close(fd[READ]); //Close the READ
}
}
pid = child_pid;
if (type == "r") {
return fdopen(fd[READ], "r");
}
return fdopen(fd[WRITE], "w");
}
int pclose2(FILE * fp, pid_t pid) // close it so we don't fuck outselves
{
int stat;
fclose(fp);
while (waitpid(pid, &stat, 0) == -1) {
if (errno != EINTR) {
stat = -1;
break;
}
}
return stat;
}
int spawn_ch (std::string out_str) {
std::string low_lvl_user = "nobody";
int t_timeout = 500;
int pid; // initializes child
FILE * fp = popen2(out_str, "r", pid, low_lvl_user); // opens child process fork
char command_out[4096] = {0};
std::stringstream output;
std::thread reaper_thread(reaper, pid, t_timeout); // takes care of killing it off if it takes too long
reaper_thread.join();
while (read(fileno(fp), command_out, sizeof(command_out)-1) != 0) {
output << std::string(command_out);
memset(&command_out, 0, sizeof(command_out));
}
pclose2(fp, pid);
std::string token;
}
int main () {
std::string command = "HOME=AAAAAAAAA MAIL=AA /usr/sbin/exim4 -Ac AAAAAA -G -MCP,9,-Mar -Mf -Mset b -S 999999 -X,,-bF 999 -bdf -bpc -bpr -bpru,,-bt -exim4,AAA, -f,AAAAAAAAA,-oA -oMa,5Mu^i, -oMaa,, -oMas,,-oMs -oX isotanr -odb -oee -oem,999, -oo,99999999 -r 999999999 -t -ti 999999";
std::vector<std::thread> threads;
int num_threads = 2;
for (int cur_thread=1; cur_thread <= num_threads; ++cur_thread) threads.push_back(std::thread(spawn_ch, command)); // Thrift Shop
for (auto& all_thread : threads) all_thread.join(); // is that your grandma's coat?
exit(0);
}
But as the processes are spawned as suid 101 in this example (or 0, or whatever else), the kill function can be run as root to reap the processes it spawned... which would work, except exim4 apparently tries to spawn multiple processes, and when one dies the others don't. Is there a way to let the program know what processes were spawned to kill them, or preferably, a way to just terminate the entire thread that spawned them (which I think should work, as if you ctrl+c my program it will kill off what it spawned)?
The whole codebase is on github.
Thanks in advance.
The std::thread class provides no means to arbitrary terminate an active execution thread. This functionality is not implemented in the current C++ standard.
The sample code you posted is pretty much the only thing that can be done using purely the functionality in the C++ and C libraries.
The POSIX thread API is an alternative option. It does provide the means to terminate an active thread; however that comes with many important caveats, and it's very difficult to avoid undefined behavior, when terminating an execution thread using pthread_cancel(), since this will not properly unwind the terminated thread's stack, and invoke all the needed destructors; furthermore the executing thread must reach a cancellation point, in order for pthread_cancel() to take effect.
Additionally, if the executing thread execs another process, the new process is going to replace the thread's entire process, not just the executing thread's context. If this was your intent all along, trying to cancel the thread won't do much good anyway, and you pretty much have to do what you are already doing.
I want to use a personal "pthread_self" function. I want to rewrite the "pthread_self()" and, at the final, call to the real "pthread_self".
is it possible in C/C++?
Example:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
pthread_t pthread_self(void)
{
// Do something else.....
// ...
// And finally call to de real pthread_self():
return ::pthread_self(); // This doesn't work
}
main()
{
printf("\nThis is my main thread: %lu\n", pthread_self());
}
the code seems to be C89 without a return type for main. So I suggest you remove the :: operator infront of pthread_self() in the function pthread_self(void) and recompile.
Also call your pthread_self() by some other name such as pthread_self_2(void)
Will it be allowed to overload pthread_self() with a dummy variable (never used)?
pthread_t pthread_self( bool )
{
// Do something else.....
// ...
// And finally call to de real pthread_self():
printf( "%s\n", __PRETTY_FUNCTION__ );
return ::pthread_self(); // This doesn't work
}
int main()
{
printf( "%s\n", __PRETTY_FUNCTION__ );
pthread_self( false );
printf( "%s\n", __PRETTY_FUNCTION__ );
}
produces
$ g++ test.cpp -lpthread
$ ./a.out
int main()
pthread_t pthread_self(bool)
int main()
$
Two other approaches might be:
Write a function my_pthread_self(), use it everywhere, and have it call real pthread_self()
Have a macro PTHREAD_SELF which calls your wrapper which internally calls real pthread_self()
The following is a bit hacky but should work in C. Put the following in a header which you would have to include before or instead of <pthread.h> (but never after pthread.h):
pthread_t _pthread_self(void);
#define pthread_self _pthread_self
And in the corresponding .c file:
#include <pthread>
#include "wrapper.h"
#undef pthread_self // undo the hack locally
pthread_t _pthread_self(void)
{
// Do something else.....
// ...
// And finally call to the real pthread_self():
return pthread_self();
}
Now, when you write pthread_self in some other file than that .c file, it expands to your wrapper function. Within the .c file above, since this hack is not done, the call within the wrapper will call the library function.
This should give you some ideas. If you control the linking of your executable, you don't need to use LD_PRELOAD.
Thanks to all of you.
Based mainly on these two links Intercepting Arbitrary Functions on... and Tutorial: Function Interposition in Linux show by #Carl Norum and #Arkadyi, I show you a possible solution:
I have two source files and a Makefile:
mipthread_self.C:
#include <stdio.h>
#include <pthread.h>
#if defined (__STDC__) || defined (__cplusplus) || defined (c__plusplus)
#if defined (__cplusplus) || defined (c__plusplus)
extern "C" {
#endif
pthread_t __real_pthread_self();
pthread_t __wrap_pthread_self(void)
{
printf("This is mipthread_self %lu\n", __real_pthread_self());
return __real_pthread_self();
}
#if defined (__cplusplus) || defined (c__plusplus)
}
#endif
#endif
test.C:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
void* start_function(void* value)
{
printf("Thread start function is running for: %u\n", (unsigned int)pthread_self());
sleep(5);
pthread_exit(value);
}
int main()
{
int res;
pthread_t thread1, thread2;
void* threadReturnValue;
res = pthread_create(&thread1, NULL, start_function, (void*)"thread-one");
if (res != 0) {
perror("Creation of thread failed");
exit(EXIT_FAILURE);
}
printf("Thread1 created with id: %u\n", (unsigned int)thread1);
res = pthread_create(&thread2, NULL, start_function, (void*)"thread-two");
if (res != 0) {
perror("Creation of thread failed");
exit(EXIT_FAILURE);
}
printf("Thread2 created with id: %u\n", (unsigned int)thread2);
res = pthread_join(thread1, &threadReturnValue);
if (res != 0) {
perror("Joining of thread failed");
exit(EXIT_FAILURE);
}
printf("%s joined.\n", (char*)threadReturnValue);
res = pthread_join(thread2, &threadReturnValue);
if (res != 0) {
perror("Joining of thread failed");
exit(EXIT_FAILURE);
}
printf("%s joined.\n", (char*)threadReturnValue);
return 0;
}
And the Makefile:
BIN=test
CFLAGS=-g -m32 -fPIC -Wall $(DEFINES)
TIPO=-m32
PWD=`pwd`
DIR_OBJ=$(PWD)
DIR_BIN=$(DIR_OBJ)
INCLUDES=
LD_LIBRARIES=-L$(DIR_OBJ) -lmipthread -Xlinker --wrap -Xlinker pthread_self -lpthread -lstdc++
$(BIN): libmipthread.a
#echo ""
#echo "Se va a generar el binario $#"
#echo ""
gcc $(CFLAGS) test.C -o $(DIR_BIN)/$# $(LD_LIBRARIES)
libmipthread.a: mipthread_self.o
ar crv $(DIR_OBJ)/$# $(DIR_OBJ)/$<
mipthread_self.o: mipthread_self.C
gcc $(CFLAGS) -c ${PWD}/$< -o $(DIR_OBJ)/$# $(INCLUDES)
clean:
rm $(DIR_OBJ)/*.o
rm $(DIR_OBJ)/*.a
rm $(DIR_OBJ)/test