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>
24 #include <loggers/console_logger.h>
29 
30 //HOOPLE_STARTUP_CODE;
31 //hmmm: missing
32 
33 using namespace application;
34 using namespace basis;
35 using namespace filesystem;
36 using namespace loggers;
37 using namespace processes;
38 using namespace structures;
39 
40 int main(int argc, char *argv[])
41 {
42  console_logger out;
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:
90  #include <basis/astring.cpp>
91  #include <basis/common_outcomes.cpp>
92  #include <basis/environment.cpp>
93  #include <basis/guards.cpp>
94  #include <basis/mutex.cpp>
95  #include <basis/utf_conversion.cpp>
102  #include <filesystem/byte_filer.cpp>
103  #include <filesystem/directory.cpp>
104  #include <filesystem/filename.cpp>
105  #include <loggers/combo_logger.cpp>
106  #include <loggers/console_logger.cpp>
107  #include <loggers/critical_events.cpp>
108  #include <loggers/file_logger.cpp>
111  #include <processes/process_entry.cpp>
112  #include <structures/bit_vector.cpp>
113  #include <structures/checksums.cpp>
117  #include <structures/string_table.cpp>
119  #include <textual/byte_formatter.cpp>
120  #include <textual/parser_bits.cpp>
122  #include <timely/earth_time.cpp>
123  #include <timely/time_stamp.cpp>
124 #endif // __BUILD_STATIC_APPLICATION__
125 
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.
Definition: command_line.h:69
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:528
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.
Definition: process_entry.h:60
#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
string path
Definition: eml_to_txt.py:139
A platform independent way to obtain the timestamp of a file.
Definition: byte_filer.cpp:37
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[])
Definition: zap_process.cpp:40