Skip to content

Commit

Permalink
Add option for graceful process termination via TASKKILL
Browse files Browse the repository at this point in the history
  • Loading branch information
gheskett committed May 11, 2021
1 parent e6fe173 commit a8f7937
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ An example of this could be to play an obnoxious audio file as an alarm.
Usage: `pidtimer.exe <[days]:[hours]:[minutes]:[seconds].[milliseconds]> [OPTIONAL ARGS] ...`

OPTIONAL ARGUMENTS:
- `-k, --kill` `<PID>`
- `-k, --kill` `<PID>` (Gracefully terminate a process)
- `-f, --force` `<PID>` (Force close a process)
- `-o, --open` `<file path + cmd args>` (Use escape characters for quotes!)

USAGE EXAMPLES:
- `pidtimer.exe 3:07:42:13.962 --kill 3479`
- `pidtimer.exe 420:69 -k 42069 -o "\"funky music.mp3\"" -k 19573`
- `pidtimer.exe 420:69 -k 42069 -o "\"funky music.mp3\"" -f 19573`
- `pidtimer.exe 1:30:00 -o "send_message.exe \"This is a message string!\""`
9 changes: 5 additions & 4 deletions pidtimer/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Argument {
public:
bool isPID = false;
uint16_t pid = 0;
bool force = 0;
std::string filepath = "";
std::string cmdline = "";
Argument(bool a, const char* b, const char* c);
Expand All @@ -32,9 +33,9 @@ class Argument {
std::string defineHelp(const char*); // Sets help text
bool isValidPID(const char*); // Checks if PID is valid
int64_t calcDuration(const char*); // Calculates time duration to use in milliseconds (string to int64_t)
std::string calcDurStr(int64_t duration); // Reverse calculation of time duration in milliseconds (uint64_t to string)
int doPIDStuffs(uint16_t pid); // Actual brains of the PID termination work
int newProcess(std::string filename, std::string cmdline); // Opens new process when called
std::string processFirst(const char* in); // Parse escape characters from first command line arg
std::string calcDurStr(int64_t); // Reverse calculation of time duration in milliseconds (uint64_t to string)
int doPIDStuffs(uint16_t, bool); // Actual brains of the PID termination work
int newProcess(std::string, std::string); // Opens new process when called
std::string processFirst(const char*); // Parse escape characters from first command line arg

#endif //PCH_H
46 changes: 31 additions & 15 deletions pidtimer/pidtimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
* Usage: pidtimer.exe <[days]:[hours]:[minutes]:[seconds].[milliseconds]> [OPTIONAL ARGS] ...
*
* OPTIONAL ARGUMENTS:
* -k, --kill <PID>
* -k, --kill <PID> (Gracefully terminate a process)
* -f, --force <PID> (Force close a process)
* -o, --open <file path + cmd args> (Use escape characters for quotes!)
*
* USAGE EXAMPLES:
* pidtimer.exe 3:07:42:13.962 --kill 3479
* pidtimer.exe 420:69 -k 42069 -o "\"funky music.mp3\"" -k 19573
* pidtimer.exe 420:69 -k 42069 -o "\"funky music.mp3\"" -f 19573
* pidtimer.exe 1:30:00 -o "send_message.exe \"This is a message string!\""
*
*/
Expand All @@ -33,6 +34,7 @@ Argument::Argument(bool a, const char* b, const char *c) {
isPID = a;
if (isPID) {
pid = (uint16_t) atoi(b);
force = (atoi(c) != 0);
}
else {
filepath = b;
Expand Down Expand Up @@ -72,7 +74,7 @@ int main(int argc, char **argv)

for (int i = 2; i < argc; i += 2) {
// kill PID argument
if (!strcmp(argv[i], "-k") || !strcmp(argv[i], "--kill")) {
if (!strcmp(argv[i], "-k") || !strcmp(argv[i], "--kill") || !strcmp(argv[i], "-f") || !strcmp(argv[i], "--force")) {
try {
if (!isValidPID(argv[i + 1])) {
printf("WARNING: Invalid PID value: %s\n", argv[i + 1]);
Expand All @@ -87,7 +89,12 @@ int main(int argc, char **argv)
}
CloseHandle(process);

arguments.emplace_back(true, argv[i + 1], "");
if (!strcmp(argv[i], "-f") || !strcmp(argv[i], "--force")) {
arguments.emplace_back(true, argv[i + 1], "1");
}
else {
arguments.emplace_back(true, argv[i + 1], "0");
}
}
catch(exception) {
printf("WARNING: Unable to parse PID of argument %d!\n", i + 2);
Expand Down Expand Up @@ -155,7 +162,7 @@ int main(int argc, char **argv)
// Do what?
for (Argument& i : arguments) {
if (i.isPID) {
doPIDStuffs(i.pid);
doPIDStuffs(i.pid, i.force);
}
else {
newProcess(i.filepath, i.cmdline);
Expand All @@ -173,10 +180,11 @@ string defineHelp(const char *arg) {
string s1 = "\nUsage: ";
string s2 = " <[days]:[hours]:[minutes]:[seconds].[milliseconds]> [OPTIONAL ARGS] ...\n\n"
"OPTIONAL ARGUMENTS:\n"
" -k, --kill <PID>\n"
" -k, --kill <PID> (Gracefully terminate a process)\n"
" -f, --force <PID> (Force close a process)\n"
" -o, --open <file path + cmd args> (Use escape characters for quotes!)\n\nUSAGE EXAMPLES:\n ";
string s3 = " 3:07:42:13.962 --kill 3479\n ";
string s4 = " 420:69 -k 42069 -o \"\\\"funky music.mp3\\\"\" -k 19573\n ";
string s4 = " 420:69 -k 42069 -o \"\\\"funky music.mp3\\\"\" -f 19573\n ";
string s5 = " 1:30:00 -o \"send_message.exe \\\"This is a message string!\\\"\"\n";

return s1 + str + s2 + str + s3 + str + s4 + str + s5;
Expand Down Expand Up @@ -289,7 +297,7 @@ string calcDurStr(int64_t duration) {
}

// Actual brains of the PID termination work
int doPIDStuffs(uint16_t pid) {
int doPIDStuffs(uint16_t pid, bool forceClose) {
HANDLE process = OpenProcess(PROCESS_TERMINATE, true, pid);
if (!process) {
printf("PID %d: Process already closed!\n", pid);
Expand All @@ -298,15 +306,23 @@ int doPIDStuffs(uint16_t pid) {
}

int ret = 0;
if (!TerminateProcess(process, 1)) {
printf("PID %d: Process already closed!\n", pid);
ret = 2;
}
else {
printf("PID %d: Process terminated successfully!\n", pid);
if (forceClose) {
if (!TerminateProcess(process, 1)) {
printf("PID %d: Process already closed!\n", pid);
ret = 2;
}
else {
printf("PID %d: Process terminated successfully!\n", pid);
}
CloseHandle(process);
return ret;
}

CloseHandle(process);
return ret;

string sysCommand = "TASKKILL /PID " + to_string(pid);

return system(sysCommand.c_str());
}

// Opens new process when called
Expand Down

0 comments on commit a8f7937

Please sign in to comment.