feisty meow concerns codebase 2.140
zap_process.cpp
Go to the documentation of this file.
1/*****************************************************************************\
2* *
3* Name : zap_process *
4* Author : Chris Koeritz *
5* *
6* Purpose: *
7* *
8* Whacks a process named on the command line, if possible. *
9* *
10*******************************************************************************
11* Copyright (c) 2000-$now By Author. This program is free software; you can *
12* redistribute it and/or modify it under the terms of the GNU General Public *
13* License as published by the Free Software Foundation; either version 2 of *
14* the License or (at your option) any later version. This is online at: *
15* http://www.fsf.org/copyleft/gpl.html *
16* Please send any updates to: fred@gruntose.com *
17\*****************************************************************************/
18
21#include <basis/astring.h>
22#include <basis/guards.h>
23#include <filesystem/filename.h>
29
30//HOOPLE_STARTUP_CODE;
31//hmmm: missing
32
33using namespace application;
34using namespace basis;
35using namespace filesystem;
36using namespace loggers;
37using namespace processes;
38using namespace structures;
39
40int main(int argc, char *argv[])
41{
43 command_line cmds(argc, argv);
44 if ( (cmds.entries() < 1)
45 || (cmds.get(0).type() != command_parameter::VALUE) ) {
46 out.log(cmds.program_name().basename().raw() + " usage:\n"
47 "this takes a single parameter, which is the name of a program\n"
48 "to hunt down and eradicate. this will zap the program without\n"
49 "any warning or any chance for it to save its state.", ALWAYS_PRINT);
50 return 1;
51 }
52 astring program_name = cmds.get(0).text();
53 program_name.to_lower();
55 process_control proc_con;
56 if (!proc_con.query_processes(processes))
57 non_continuable_error("procto", "main", "failed to query processes!");
58 bool found = false;
59 bool success = true;
60 for (int i = 0; i < processes.length(); i++) {
61 filename path = processes[i].path();
62// out.log(astring("got process path: ") + path.raw(), ALWAYS_PRINT);
63 astring base = path.basename().raw().lower();
64 if (base == program_name) {
65 found = true;
66// out.log("would whack this entry:");
67// out.log(processes[i].text_form());
68 bool ret = proc_con.zap_process(processes[i]._process_id);
69 if (ret)
70 out.log(a_sprintf("Killed process %d [", processes[i]._process_id)
71 + program_name + astring("]"), ALWAYS_PRINT);
72 else {
73 out.log(astring(astring::SPRINTF, "Failed to zap process %d [",
74 processes[i]._process_id) + program_name + astring("]"), ALWAYS_PRINT);
75 success = false;
76 }
77 }
78 }
79 if (!found)
80 out.log(astring("Could not find the program named ") + program_name, ALWAYS_PRINT);
81 if (!success) return 123;
82 else return 0;
83}
84
85#ifdef __BUILD_STATIC_APPLICATION__
86 // static dependencies found by buildor_gen_deps.sh:
87 #include <algorithms/sorts.cpp>
92 #include <basis/astring.cpp>
94 #include <basis/environment.cpp>
95 #include <basis/guards.cpp>
96 #include <basis/mutex.cpp>
105 #include <filesystem/directory.cpp>
106 #include <filesystem/filename.cpp>
107 #include <loggers/combo_logger.cpp>
110 #include <loggers/file_logger.cpp>
115 #include <structures/checksums.cpp>
122 #include <textual/parser_bits.cpp>
124 #include <timely/earth_time.cpp>
125 #include <timely/time_stamp.cpp>
126#endif // __BUILD_STATIC_APPLICATION__
127
int entries() const
Returns the number of fields found on the command line.
filesystem::filename program_name() const
Returns the program name found in the command line.
const command_parameter & get(int field) const
Returns the parameter at the "field" specified.
const basis::astring & text() const
observes the string contents.
parameter_types type() const
observes the type of the parameter.
a_sprintf is a specialization of astring that provides printf style support.
Definition astring.h:440
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
void to_lower()
to_lower modifies "this" by replacing capitals with lower-case.
Definition astring.cpp:531
Provides operations commonly needed on file names.
Definition filename.h:64
const basis::astring & raw() const
returns the astring that we're holding onto for the path.
Definition filename.cpp:97
filename basename() const
returns the base of the filename; no directory.
Definition filename.cpp:385
virtual basis::outcome log(const basis::base_string &info, int filter)
sends the string "info" to the standard output device.
Provides a bridge to the operating system for information on processes.
bool zap_process(basis::un_int to_zap)
preemptively zaps the process "to_zap".
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.
#define non_continuable_error(c, f, i)
an extra piece of information used, if available, in bounds_halt below.
Implements an application lock to ensure only one is running at once.
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
Aids in achievement of platform independence.
int main(int argc, char *argv[])