Index: Service.cpp =================================================================== RCS file: /home/cvsroot/ice/src/Ice/Service.cpp,v retrieving revision 1.16 retrieving revision 1.27 diff -c -r1.16 -r1.27 *** Service.cpp 2 Nov 2004 13:53:34 -0000 1.16 --- Service.cpp 7 Mar 2005 17:31:51 -0000 1.27 *************** *** 1,6 **** // ********************************************************************** // ! // Copyright (c) 2003-2004 ZeroC, Inc. All rights reserved. // // This copy of Ice is licensed to you under the terms described in the // ICE_LICENSE file included in this distribution. --- 1,6 ---- // ********************************************************************** // ! // Copyright (c) 2003-2005 ZeroC, Inc. All rights reserved. // // This copy of Ice is licensed to you under the terms described in the // ICE_LICENSE file included in this distribution. *************** *** 131,137 **** } int ! Ice::Service::main(int argc, char* argv[]) { _name = argv[0]; --- 131,137 ---- } int ! Ice::Service::main(int& argc, char* argv[]) { _name = argv[0]; *************** *** 260,275 **** } vector args; - for(idx = 1; idx < argc; ++idx) - { - args.push_back(argv[idx]); - } // ! // Append the arguments "--service NAME" so that the service // starts properly. // args.push_back("--service"); args.push_back(name); return installService(name, display, executable, args); } else if(op == "--uninstall") --- 260,275 ---- } vector args; // ! // Prepend the arguments "--service NAME" so that the service // starts properly. // args.push_back("--service"); args.push_back(name); + for(idx = 1; idx < argc; ++idx) + { + args.push_back(argv[idx]); + } return installService(name, display, executable, args); } else if(op == "--uninstall") *************** *** 348,359 **** return EXIT_FAILURE; } - if(!changeDirectory && !daemonize) - { - cerr << argv[0] << ": --nochdir must be used with --daemon" << endl; - return EXIT_FAILURE; - } - if(daemonize) { configureDaemon(changeDirectory, closeFiles); --- 348,353 ---- *************** *** 404,410 **** } int ! Ice::Service::run(int argc, char* argv[]) { if(_service) { --- 398,404 ---- } int ! Ice::Service::run(int& argc, char* argv[]) { if(_service) { *************** *** 469,474 **** --- 463,486 ---- ostr << "service caught unhandled Ice exception:" << endl << ex; error(ostr.str()); } + catch(const std::exception& ex) + { + ostringstream ostr; + ostr << "service caught unhandled std::exception:" << endl << ex.what(); + error(ostr.str()); + } + catch(const std::string& msg) + { + ostringstream ostr; + ostr << "service caught unhandled exception:" << endl << msg; + error(ostr.str()); + } + catch(const char* msg) + { + ostringstream ostr; + ostr << "service caught unhandled exception:" << endl << msg; + error(ostr.str()); + } catch(...) { error("service caught unhandled C++ exception"); *************** *** 639,650 **** } // ! // Create argument vector. // ! const int argc = args.size() + 1; LPCSTR* argv = new LPCSTR[argc]; ! argv[0] = strdup(name.c_str()); ! int i = 1; for(vector::const_iterator p = args.begin(); p != args.end(); ++p) { argv[i++] = strdup(p->c_str()); --- 651,663 ---- } // ! // Create argument vector. Note that StartService() automatically adds the service name ! // in argv[0], so the argv that is passed to StartService() must *not* include the ! // the service name in argv[0]. // ! const int argc = args.size(); LPCSTR* argv = new LPCSTR[argc]; ! int i = 0; for(vector::const_iterator p = args.begin(); p != args.end(); ++p) { argv[i++] = strdup(p->c_str()); *************** *** 765,770 **** --- 778,784 ---- << " Check point: " << status.dwCheckPoint << endl << " Wait hint: " << status.dwWaitHint; trace(ostr.str()); + return EXIT_FAILURE; } return EXIT_SUCCESS; *************** *** 964,969 **** --- 978,996 ---- } void + Ice::Service::print(const string& msg) + { + if(_logger) + { + _logger->print(msg); + } + else + { + cerr << msg << endl; + } + } + + void Ice::Service::enableInterrupt() { _ctrlCHandler->setCallback(ctrlCHandlerCallback); *************** *** 1102,1109 **** } catch(const Ice::Exception& ex) { ! // TODO: Enable delete when we figure out why it can cause a crash. ! //delete[] args; ostringstream ostr; ostr << ex; error(ostr.str()); --- 1129,1135 ---- } catch(const Ice::Exception& ex) { ! delete[] args; ostringstream ostr; ostr << ex; error(ostr.str()); *************** *** 1168,1175 **** error("service caught unhandled C++ exception"); } ! // TODO: Enable delete when we figure out why it can cause a crash. ! //delete[] args; try { --- 1194,1200 ---- error("service caught unhandled C++ exception"); } ! delete[] args; try { *************** *** 1321,1327 **** size_t pos = 0; while(pos < sizeof(msg)) { ! int n = read(fds[0], &msg[pos], sizeof(msg) - pos); if(n == -1) { if(IceInternal::interrupted()) --- 1346,1352 ---- size_t pos = 0; while(pos < sizeof(msg)) { ! ssize_t n = read(fds[0], &msg[pos], sizeof(msg) - pos); if(n == -1) { if(IceInternal::interrupted()) *************** *** 1336,1342 **** pos += n; break; } ! cerr << argv[0] << ": failure occurred in daemon:" << endl << msg << endl << flush; _exit(EXIT_FAILURE); } --- 1361,1372 ---- pos += n; break; } ! cerr << argv[0] << ": failure occurred in daemon"; ! if(strlen(msg) > 0) ! { ! cerr << ':' << endl << msg; ! } ! cerr << endl << flush; _exit(EXIT_FAILURE); } *************** *** 1404,1410 **** } fd_set fdsToClose; ! int fdMax; if(_closeFiles) { // --- 1434,1440 ---- } fd_set fdsToClose; ! int fdMax = 0; if(_closeFiles) { // *************** *** 1414,1420 **** // conveniently allows the Ice.PrintProcessId property to work as expected. // FD_ZERO(&fdsToClose); ! fdMax = sysconf(_SC_OPEN_MAX); if(fdMax <= 0) { SyscallException ex(__FILE__, __LINE__); --- 1444,1450 ---- // conveniently allows the Ice.PrintProcessId property to work as expected. // FD_ZERO(&fdsToClose); ! fdMax = static_cast(sysconf(_SC_OPEN_MAX)); if(fdMax <= 0) { SyscallException ex(__FILE__, __LINE__); *************** *** 1450,1458 **** { // // Close unnecessary file descriptors. ! // for(int i = 0; i < fdMax; ++i) { if(FD_ISSET(i, &fdsToClose)) { close(i); --- 1480,1499 ---- { // // Close unnecessary file descriptors. ! // ! PropertiesPtr properties = _communicator->getProperties(); ! string stdOut = properties->getProperty("Ice.StdOut"); ! string stdErr = properties->getProperty("Ice.StdErr"); ! for(int i = 0; i < fdMax; ++i) { + // + // NOTE: Do not close stdout if Ice.StdOut is defined. Likewise for Ice.StdErr. + // + if((i == 1 && !stdOut.empty()) || (i == 2 && !stdErr.empty())) + { + continue; + } if(FD_ISSET(i, &fdsToClose)) { close(i); *************** *** 1465,1474 **** int fd; fd = open("/dev/null", O_RDWR); assert(fd == 0); ! fd = dup2(0, 1); ! assert(fd == 1); ! fd = dup2(1, 2); ! assert(fd == 2); } // --- 1506,1521 ---- int fd; fd = open("/dev/null", O_RDWR); assert(fd == 0); ! if(stdOut.empty()) ! { ! fd = dup2(0, 1); ! assert(fd == 1); ! } ! if(stdErr.empty()) ! { ! fd = dup2(1, 2); ! assert(fd == 2); ! } } // *************** *** 1549,1555 **** size_t pos = 0; while(len > 0) { ! int n = write(fds[1], &msg[pos], len); if(n == -1) { if(IceInternal::interrupted()) --- 1596,1602 ---- size_t pos = 0; while(len > 0) { ! ssize_t n = write(fds[1], &msg[pos], len); if(n == -1) { if(IceInternal::interrupted())