feisty meow concerns codebase 2.140
await_app_exit.cpp
Go to the documentation of this file.
1/*
2* Name : await_app_exit
3* Author : Chris Koeritz
4* Purpose: *
5* This program waits for a particular application to exit before this app *
6* itself exits. This allows a pause while another possibly slow process is *
7* leaving. *
8* Copyright (c) 2003-$now By Author. This program is free software; you can *
9* redistribute it and/or modify it under the terms of the GNU General Public *
10* License as published by the Free Software Foundation; either version 2 of *
11* the License or (at your option) any later version. This is online at: *
12* http://www.fsf.org/copyleft/gpl.html *
13* Please send any updates to: fred@gruntose.com *
14\*****************************************************************************/
15
16#include <basis/functions.h>
17#include <basis/astring.h>
18#include <structures/set.h>
19#include <timely/time_control.h>
20#include <timely/time_stamp.h>
24#include <loggers/file_logger.h>
25#include <filesystem/filename.h>
31
32using namespace application;
33using namespace basis;
34using namespace loggers;
35using namespace filesystem;
36using namespace processes;
37using namespace structures;
38using namespace textual;
39using namespace timely;
40
41#undef BASE_LOG
42#define BASE_LOG(to_print) EMERGENCY_LOG(program_wide_logger::get(), astring(to_print))
43#undef LOG
44#define LOG(to_print) CLASS_EMERGENCY_LOG(program_wide_logger::get(), astring(to_print))
45
46class await_app_exit : public application_shell
47{
48public:
49 await_app_exit() : application_shell() {}
50 DEFINE_CLASS_NAME("await_app_exit");
51 int execute();
52};
53
54int await_app_exit::execute()
55{
56 FUNCDEF("execute");
58 if (_global_argc < 3) {
59 BASE_LOG("This program needs two parameters on the command line. The first is an");
60 BASE_LOG("application name (e.g. 'blofeld.exe' is a valid example--no path should be");
61 BASE_LOG("included but the .exe suffix must be included) to seek out in the process");
62 BASE_LOG("list and the second parameter is the time to wait for it to exit (in seconds).");
63 BASE_LOG("This program will not exit until the specified application is no longer");
64 BASE_LOG("running or the timeout elapses. If the timeout elapses, then a failure exit");
65 BASE_LOG("will occur from this program so that it is known that the target application");
66 BASE_LOG("never exited.");
67 return 2;
68 }
69
70 astring app_name = _global_argv[1]; // get the app's name.
71 astring duration = _global_argv[2]; // get the time to wait.
72 int timeout = duration.convert(0) * 1000;
73 if (timeout < 0) {
74 LOG(astring("The timeout specified is invalid: ") + duration);
75 return 3;
76 }
77
78 // now see if that app is even running.
79 process_control querier;
82 int_set pids;
83 time_stamp when_to_leave(timeout); // when we should stop checking.
84
85 // wait for the app to go away.
86 while (querier.find_process_in_list(processes, app_name, pids)) {
87 // the program of interest is still running.
90 if (time_stamp() > when_to_leave) {
91 LOG(astring("The timeout elapsed and ") + app_name + " is still running.");
92 return 4;
93 }
94 }
95 LOG(astring("The ") + app_name + " process has exited.");
96 return 0;
97}
98
99HOOPLE_MAIN(await_app_exit, )
100
101#ifdef __BUILD_STATIC_APPLICATION__
102 // static dependencies found by buildor_gen_deps.sh:
103 #include <algorithms/sorts.cpp>
108 #include <basis/astring.cpp>
110 #include <basis/environment.cpp>
111 #include <basis/guards.cpp>
112 #include <basis/mutex.cpp>
113 #include <basis/utf_conversion.cpp>
121 #include <filesystem/directory.cpp>
122 #include <filesystem/filename.cpp>
123 #include <loggers/combo_logger.cpp>
126 #include <loggers/file_logger.cpp>
131 #include <structures/checksums.cpp>
138 #include <textual/parser_bits.cpp>
140 #include <timely/earth_time.cpp>
141 #include <timely/time_control.cpp>
142 #include <timely/time_stamp.cpp>
143#endif // __BUILD_STATIC_APPLICATION__
144
#define BASE_LOG(to_print)
#define LOG(to_print)
The application_shell is a base object for console programs.
virtual int execute()=0
< retrieves the command line from the /proc hierarchy on linux.
application_shell()
constructs an application_shell to serve as the root of the program.
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
int convert(int default_value) const
Converts the string into a corresponding integer.
Definition astring.cpp:760
Provides a bridge to the operating system for information on processes.
static bool find_process_in_list(const process_entry_array &processes, const basis::astring &app_name, structures::int_set &pids)
uses a pre-existing list of "processes" to search for the "app_name".
bool query_processes(process_entry_array &to_fill)
finds the processes that are running and drops them into "to_fill".
a handy class that implements an array of process entries.
A simple object that wraps a templated set of ints.
Definition set.h:156
static void sleep_ms(basis::un_int msec)
a system independent name for a forced snooze measured in milliseconds.
Represents a point in time relative to the operating system startup time.
Definition time_stamp.h:38
#define SETUP_COMBO_LOGGER
a macro that retasks the program-wide logger as a combo_logger.
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition enhance_cpp.h:42
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition enhance_cpp.h:54
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition hoople_main.h:61
Implements an application lock to ensure only one is running at once.
char ** _global_argv
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
A platform independent way to obtain the timestamp of a file.
A logger that sends to the console screen using the standard output device.
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55
#include <time.h>