int main(int argc, const char * argv[]) { syslog(LOG_NOTICE, "SMJobBlessHelper: uid = %d, euid = %d, pid = %d\n", getuid(), geteuid(), getpid()); return respondToRequests(); }
//this function is started as a thread by startipc() int MVipc(const int environmentn, var& pgconnparams) { //TODO prevent or handle SELECT in dictionary functions //flag to connect NOT to be recursive and open yet another ipc thread tss_ipcstarted.reset(new bool(true)); //clone the postgres connection because the parent thread is running a select with it if (!var().connect(pgconnparams)) { throw var(L"MVipc Cannot connect additional thread to postgres"); return false; } //set the threads environment number (same as and provided by the parent thread) //AFTER opening the database connection setenvironmentn(environmentn); var processn=getprocessn(); //this is a dict library caller //TODO check if possible to have no environment; //*COPY in mvipc_posix.cpp mvipc_boost.cpp mvipc_win.cpp MvEnvironment standalone_mv; MvEnvironment* mv=global_environments[environmentn]; if (not mv) { #if TRACING >= 2 std::clog<<"MVipc() Using a standalone MvEnvironment"<<std::endl; #endif mv=&standalone_mv; mv->init(environmentn); } ExodusFunctorBase exodusfunctorbase(*mv); //"\\\\.\\pipe\\exoduspipexyz" //strings of MS tchars //typedef basic_string<TCHAR> tstring; //wchar_t* exoduspipename="\\\\.\\pipe\\exoduspipexyz"; var temp="requestexodusservice-"^processn^L"."^environmentn; std::string requestqueuename=temp.toString(); var temp2="responseexodusservice-"^processn^L"."^environmentn; std::string responsequeuename=temp2.toString(); //string requestqueuename="requestexodusqueue"; //requestqueuename+=environmentn; //string responsequeuename="responseexodusqueue"; //responsequeuename+=environmentn; closeipcqueues(requestqueuename, responsequeuename); try { //open request queue boost::interprocess::message_queue request_queue ( boost::interprocess::open_or_create //only create ,requestqueuename.c_str() //name ,100 //max message number ,BUFSIZ //max message size ); try { //open response queue boost::interprocess::message_queue response_queue ( boost::interprocess::open_or_create //only create ,responsequeuename.c_str() //name ,100 //max message number ,BUFSIZ //max message size ); //indicate to waiting/paused parent thread that the pipe is open //(the pipe is not actually waiting until the next step) //scoped so that the scoped_lock is automatically released after the notification { boost::mutex::scoped_lock lock(global_ipcmutex); #if TRACING >= 3 std::clog<<"MVipc() Notifying that pipe has been opened\n"; #endif //TODO make sure notifies CORRECT parent thread by using an array of ipcmutexes and tss_environmentn global_ipccondition.notify_one(); #if TRACING >= 3 std::clog<<L"MVipc() Notified that pipe has been opened\n"; #endif } respondToRequests(request_queue,response_queue,exodusfunctorbase); std::clog << "finished responding to queue " << requestqueuename<<std::endl; } catch(boost::interprocess::interprocess_exception &ex) { global_ipccondition.notify_one(); std::cerr << "cannot open " << responsequeuename << " " << ex.what() << std::endl; closeipcqueues(requestqueuename, responsequeuename); return 0; } } catch(boost::interprocess::interprocess_exception &ex) { global_ipccondition.notify_one(); std::cerr << "cannot open " << requestqueuename << " " << ex.what() << std::endl; closeipcqueues(requestqueuename, responsequeuename); return 0; } closeipcqueues(requestqueuename, responsequeuename); return 1; }
//this function is started as a thread by startipc() int MVipc(const int environmentn, var& pgconnparams) { //flag to connect NOT to be recursive and open yet another ipc thread tss_ipcstarted.reset(new bool(true)); //clone the postgres connection because the parent thread is running a select with it if (!var().connect(pgconnparams)) { throw var(L"MVipc Cannot connect additional thread to postgres"); return false; } //set the threads environment number (same as and provided by the parent thread) //AFTER opening the database connection setenvironmentn(environmentn); var processn=getprocessn(); //this is a dict library caller //TODO check if possible to have no environment; //*COPY in mvipc_posix.cpp mvipc_boost.cpp mvipc_win.cpp MvEnvironment standalone_mv; MvEnvironment* mv=global_environments[environmentn]; if (not mv) { #if TRACING >= 2 std::clog<<"MVipc() Using a standalone MvEnvironment"<<std::endl; #endif mv=&standalone_mv; mv->init(environmentn); } ExodusFunctorBase exodusfunctorbase(*mv); /*create the socket directory*/ //std::string socketdir="/tmp/exodus"; //if (!var(socketdir).osdir()) // var(socketdir).osmkdir(); //std::string socketpath=socketdir+"/ipc"; std::string socketpath="/tmp/"; socketpath+="exodusservice-"+processn.toString()+"."+var(environmentn).toString(); closeipcqueues(0,socketpath); int sock; //int fromlen; sock = socket(AF_UNIX, SOCK_STREAM, 0); /*grant owner/group/others rw access to the socket*/ /*TODO review security of this*/ mode_t oldumask=umask(0111); /*server binds to the socket*/ struct sockaddr_un addr; strncpy(addr.sun_path, socketpath.c_str(),sizeof(addr.sun_path)); addr.sun_family = AF_UNIX; if (bind (sock, (struct sockaddr *) &addr, strlen(addr.sun_path) + sizeof (addr.sun_family))<0) { boost::mutex::scoped_lock lock(global_ipcmutex); global_ipccondition.notify_one(); std::cout << "cannot open " << socketpath << " " << errno << " " << strerror(errno) << std::endl; closeipcqueues(sock, socketpath); return 0; } /*restore permissions mask*/ umask(oldumask); //log #if TRACING >= 3 wprintf(L"---------------------------------\nMVipc: bound socket %s\n",socketpath.c_str()); #endif //indicate to waiting/paused parent thread that the pipe is open //(the pipe is not actually waiting until the next step) //scoped so that the scoped_lock is automatically released after the notification { boost::mutex::scoped_lock lock(global_ipcmutex); #if TRACING >= 3 cout<<"MVipc() Notifying that pipe has been opened\n"; #endif //TODO make sure notifies CORRECT parent thread by using an array of ipcmutexes and tss_environmentn global_ipccondition.notify_one(); #if TRACING >= 3 cout<<"MVipc() Notified that pipe has been opened\n"; #endif } respondToRequests(sock,socketpath,exodusfunctorbase); std::cout << "stopped responding to socket " << socketpath << std::endl; closeipcqueues(sock, socketpath); return 1; }