await_app_exit.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : await_app_exit                                                    *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *  Purpose:                                                                   *
00007 *                                                                             *
00008 *    This program waits for a particular application to exit before this app  *
00009 *  itself exits.  This allows a pause while another possibly slow process is  *
00010 *  leaving.                                                                   *
00011 *                                                                             *
00012 *******************************************************************************
00013 * Copyright (c) 2003-$now By Author.  This program is free software; you can  *
00014 * redistribute it and/or modify it under the terms of the GNU General Public  *
00015 * License as published by the Free Software Foundation; either version 2 of   *
00016 * the License or (at your option) any later version.  This is online at:      *
00017 *     http://www.fsf.org/copyleft/gpl.html                                    *
00018 * Please send any updates to: fred@gruntose.com                               *
00019 \*****************************************************************************/
00020 
00021 #include <basis/function.h>
00022 #include <basis/istring.h>
00023 #include <basis/set.cpp>
00024 #include <mechanisms/time_stamp.h>
00025 #include <opsystem/application_shell.h>
00026 #include <opsystem/byte_filer.h>
00027 #include <loggers/console_logger.h>
00028 #include <loggers/file_logger.h>
00029 #include <opsystem/filename.h>
00030 #include <opsystem/ini_config.h>
00031 #include <opsystem/path_configuration.h>
00032 #include <data_struct/static_memory_gremlin.h>
00033 #include <processes/process_control.h>
00034 #include <processes/process_entry.h>
00035 
00036 #undef BASE_LOG
00037 #define BASE_LOG(to_print) program_wide_logger().log(to_print)
00038 #undef LOG
00039 #define LOG(to_print) CLASS_EMERGENCY_LOG(program_wide_logger(), to_print)
00040 
00041 class await_app_exit : public application_shell
00042 {
00043 public:
00044   await_app_exit() : application_shell("await_app_exit") {}
00045   IMPLEMENT_CLASS_NAME("await_app_exit");
00046   int execute();
00047 };
00048 
00049 int await_app_exit::execute()
00050 {
00051   FUNCDEF("execute");
00052   SET_DEFAULT_COMBO_LOGGER;
00053   if (__argc < 3) {
00054     BASE_LOG("This program needs two parameters on the command line.  The first is an");
00055     BASE_LOG("application name (e.g. 'blofeld.exe' is a valid example--no path should be");
00056     BASE_LOG("included but the .exe suffix must be included) to seek out in the process");
00057     BASE_LOG("list and the second parameter is the time to wait for it to exit (in seconds).");
00058     BASE_LOG("This program will not exit until the specified application is no longer");
00059     BASE_LOG("running or the timeout elapses.  If the timeout elapses, then a failure exit");
00060     BASE_LOG("will occur from this program so that it is known that the target application");
00061     BASE_LOG("never exited.");
00062     return 2;
00063   }
00064 
00065   istring app_name = __argv[1];  // get the app's name.
00066   istring duration = __argv[2];  // get the time to wait.
00067   int timeout = duration.convert(0) * 1000;
00068   if (timeout < 0) {
00069     LOG(istring("The timeout specified is invalid: ") + duration);
00070     return 3;
00071   }
00072 
00073   // now see if that app is even running.
00074   process_control querier;
00075   process_entry_array processes;
00076   querier.query_processes(processes);
00077   int_set pids;
00078   time_stamp when_to_leave(timeout);  // when we should stop checking.
00079 
00080   // wait for the app to go away.
00081   while (querier.find_process_in_list(processes, app_name, pids)) {
00082     // the program of interest is still running.
00083     portable::sleep_ms(100);
00084     querier.query_processes(processes);
00085     if (time_stamp() > when_to_leave) {
00086       LOG(istring("The timeout elapsed and ") + app_name + "is still running.");
00087       return 4;
00088     }
00089   }
00090   LOG(istring("The ") + app_name + " process has exited.");
00091   return 0;
00092 }
00093 
00094 HOOPLE_MAIN(await_app_exit, )
00095 
00096 #ifdef __BUILD_STATIC_APPLICATION__
00097   // static dependencies found by buildor_gen_deps.sh:
00098   #include <basis/array.cpp>
00099   #include <basis/byte_array.cpp>
00100   #include <basis/callstack_tracker.cpp>
00101   #include <basis/chaos.cpp>
00102   #include <basis/convert_utf.cpp>
00103   #include <basis/definitions.cpp>
00104   #include <basis/earth_time.cpp>
00105   #include <basis/guards.cpp>
00106   #include <basis/istring.cpp>
00107   #include <basis/log_base.cpp>
00108   #include <basis/memory_checker.cpp>
00109   #include <basis/mutex.cpp>
00110   #include <basis/object_base.cpp>
00111   #include <basis/outcome.cpp>
00112   #include <basis/packable.cpp>
00113   #include <basis/portable.cpp>
00114   #include <basis/sequence.cpp>
00115   #include <basis/set.cpp>
00116   #include <basis/utility.cpp>
00117   #include <basis/version_record.cpp>
00118   #include <data_struct/amorph.cpp>
00119   #include <data_struct/bit_vector.cpp>
00120   #include <data_struct/byte_hasher.cpp>
00121   #include <data_struct/configurator.cpp>
00122   #include <data_struct/hash_table.cpp>
00123   #include <data_struct/pointer_hash.cpp>
00124   #include <data_struct/stack.cpp>
00125   #include <data_struct/static_memory_gremlin.cpp>
00126   #include <data_struct/string_hash.cpp>
00127   #include <data_struct/string_hasher.cpp>
00128   #include <data_struct/string_table.cpp>
00129   #include <data_struct/symbol_table.cpp>
00130   #include <data_struct/table_configurator.cpp>
00131   #include <loggers/console_logger.cpp>
00132   #include <loggers/file_logger.cpp>
00133   #include <loggers/locked_logger.cpp>
00134   #include <loggers/null_logger.cpp>
00135   #include <loggers/program_wide_logger.cpp>
00136   #include <mechanisms/time_stamp.cpp>
00137   #include <opsystem/application_base.cpp>
00138   #include <opsystem/application_shell.cpp>
00139   #include <opsystem/byte_filer.cpp>
00140   #include <opsystem/command_line.cpp>
00141   #include <opsystem/critical_events.cpp>
00142   #include <opsystem/directory.cpp>
00143   #include <opsystem/filename.cpp>
00144   #include <opsystem/ini_config.cpp>
00145   #include <opsystem/ini_parser.cpp>
00146   #include <opsystem/path_configuration.cpp>
00147   #include <opsystem/rendezvous.cpp>
00148   #include <processes/process_control.cpp>
00149   #include <processes/process_entry.cpp>
00150   #include <textual/byte_format.cpp>
00151   #include <textual/parser_bits.cpp>
00152   #include <textual/string_manipulation.cpp>
00153   #include <textual/tokenizer.cpp>
00154 #endif // __BUILD_STATIC_APPLICATION__
00155 

Generated on Wed Nov 19 04:28:34 2008 for HOOPLE Libraries by  doxygen 1.5.1