service_controller.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : service_controller                                                *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 2000-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include <basis/istring.h>
00016 #include <loggers/console_logger.h>
00017 #include <opsystem/application_shell.h>
00018 #include <opsystem/filename.h>
00019 #include <data_struct/static_memory_gremlin.h>
00020 #include <service_ext/service_control.h>
00021 
00022 const int SERVICE_SHUTDOWN_DELAY = 4 * SECOND_ms;
00023   // this is the interval we pause between stopping a service and restarting
00024   // it.  apparently the fact that windows returns from the stop call doesn't
00025   // mean the process is gone yet.
00026 
00027 class service_controller : public application_shell
00028 {
00029 public:
00030   service_controller() : application_shell(class_name()) {}
00031 
00032   virtual int execute();
00033 
00034   IMPLEMENT_CLASS_NAME("service_controller");
00035 
00036   int instruct();
00037     // print the instructions.
00038 };
00039 
00041 
00042 int service_controller::instruct()
00043 {
00044   log(filename(__argv[0]).rootname() + " Usage:");
00045   log("\
00046 This program provides control over services in MS-Win32 operating systems.\n\
00047 The first parameter must be \"start\", \"stop\", \"restart\", \"manual\", or\n\
00048 \"automatic\".  This specifies how to control the service.  The second\n\
00049 parameter is the name of the service.  This must be identical to the service\n\
00050 name registered with the operating system.  The start, stop and restart\n\
00051 flags operate on the running state of a service.  The manual and automatic\n\
00052 flags simply change the service launch configuration.\n\
00053 \n\
00054 Example:\n\
00055 \tservice_controller restart app_launcher\n");
00056   return 1;
00057 }
00058 
00059 int service_controller::execute()
00060 {
00061   SET_DEFAULT_CONSOLE_LOGGER;
00062   if (__argc < 3) return instruct();
00063 
00064   istring control_word(__argv[1]);
00065   control_word.to_lower();
00066 
00067   istring service_name(__argv[2]);
00068   service_name.to_lower();
00069 
00070   service_control ctl;
00071 
00072   if (control_word == "stop")
00073     return !(ctl.control_service(service_name, false, false)
00074         == service_control::OKAY);
00075   else if (control_word == "start")
00076     return !(ctl.control_service(service_name, true, false)
00077         == service_control::OKAY);
00078   else if (control_word == "restart") {
00079     // stop it and then restart.  we don't worry too much if it wasn't stopped,
00080     // since it might not have been started in the first place.
00081     ctl.control_service(service_name, false, true);
00082 //argh!  no, we should be waiting until the app actually is gone.
00083     portable::sleep_ms(SERVICE_SHUTDOWN_DELAY);
00084     return !(ctl.control_service(service_name, true, false)
00085         == service_control::OKAY);
00086   } else if (control_word == "manual") {
00087     return !(ctl.reconfigure_service(service_name, service_control::MANUAL)
00088         == service_control::OKAY);
00089   } else if (control_word == "automatic") {
00090     return !(ctl.reconfigure_service(service_name, service_control::AUTOMATIC)
00091         == service_control::OKAY);
00092   } else return instruct();
00093 }
00094 
00096 
00097 HOOPLE_MAIN(service_controller, )
00098 
00099 #ifdef __BUILD_STATIC_APPLICATION__
00100   // static dependencies found by buildor_gen_deps.sh:
00101   #include <basis/array.cpp>
00102   #include <basis/byte_array.cpp>
00103   #include <basis/callstack_tracker.cpp>
00104   #include <basis/chaos.cpp>
00105   #include <basis/convert_utf.cpp>
00106   #include <basis/definitions.cpp>
00107   #include <basis/earth_time.cpp>
00108   #include <basis/guards.cpp>
00109   #include <basis/istring.cpp>
00110   #include <basis/log_base.cpp>
00111   #include <basis/memory_checker.cpp>
00112   #include <basis/mutex.cpp>
00113   #include <basis/object_base.cpp>
00114   #include <basis/outcome.cpp>
00115   #include <basis/packable.cpp>
00116   #include <basis/portable.cpp>
00117   #include <basis/sequence.cpp>
00118   #include <basis/set.cpp>
00119   #include <basis/utility.cpp>
00120   #include <basis/version_record.cpp>
00121   #include <data_struct/amorph.cpp>
00122   #include <data_struct/bit_vector.cpp>
00123   #include <data_struct/byte_hasher.cpp>
00124   #include <data_struct/configurator.cpp>
00125   #include <data_struct/hash_table.cpp>
00126   #include <data_struct/pointer_hash.cpp>
00127   #include <data_struct/stack.cpp>
00128   #include <data_struct/static_memory_gremlin.cpp>
00129   #include <data_struct/string_hash.cpp>
00130   #include <data_struct/string_hasher.cpp>
00131   #include <data_struct/string_table.cpp>
00132   #include <data_struct/symbol_table.cpp>
00133   #include <data_struct/table_configurator.cpp>
00134   #include <loggers/console_logger.cpp>
00135   #include <loggers/file_logger.cpp>
00136   #include <loggers/locked_logger.cpp>
00137   #include <loggers/null_logger.cpp>
00138   #include <loggers/program_wide_logger.cpp>
00139   #include <opsystem/application_base.cpp>
00140   #include <opsystem/application_shell.cpp>
00141   #include <opsystem/byte_filer.cpp>
00142   #include <opsystem/command_line.cpp>
00143   #include <opsystem/critical_events.cpp>
00144   #include <opsystem/directory.cpp>
00145   #include <opsystem/filename.cpp>
00146   #include <opsystem/ini_config.cpp>
00147   #include <opsystem/ini_parser.cpp>
00148   #include <opsystem/path_configuration.cpp>
00149   #include <opsystem/rendezvous.cpp>
00150   #include <service_ext/service_control.cpp>
00151   #include <textual/byte_format.cpp>
00152   #include <textual/parser_bits.cpp>
00153   #include <textual/string_manipulation.cpp>
00154   #include <textual/tokenizer.cpp>
00155 #endif // __BUILD_STATIC_APPLICATION__
00156 

Generated on Fri Nov 28 04:28:51 2008 for HOOPLE Libraries by  doxygen 1.5.1