Skip to content

Commit

Permalink
Handle more signal cases along with a non-file lock case
Browse files Browse the repository at this point in the history
  • Loading branch information
Slackadays committed Oct 4, 2024
1 parent 8235d4f commit ef88c17
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/cb/src/clipboard.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* The Clipboard Project - Cut, copy, and paste anything, anytime, anywhere, all from the terminal.
Copyright (C) 2023 Jackson Huff and other contributors on GitHub.com
Copyright (C) 2024 Jackson Huff and other contributors on GitHub.com
SPDX-License-Identifier: GPL-3.0-or-later
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -171,7 +171,7 @@ void Clipboard::getLock() {
if (kill(pid, 0) == -1) break;
#endif
if (!isLocked()) break;
std::this_thread::sleep_for(std::chrono::milliseconds(250));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
writeToFile(metadata.lock, std::to_string(thisPID()));
Expand Down
14 changes: 12 additions & 2 deletions src/cb/src/clipboard.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* The Clipboard Project - Cut, copy, and paste anything, anytime, anywhere, all from the terminal.
Copyright (C) 2023 Jackson Huff and other contributors on GitHub.com
Copyright (C) 2024 Jackson Huff and other contributors on GitHub.com
SPDX-License-Identifier: GPL-3.0-or-later
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -310,7 +310,17 @@ class Clipboard {
std::vector<std::string> ignoreSecrets();
void applyIgnoreRules();
bool isUnused();
bool isLocked() { return fs::exists(metadata.lock); }
bool isLocked() {
if (!fs::is_regular_file(metadata.lock)) {
if (fs::exists(metadata.lock)) // Handle the case where the lock file is not a regular file
fs::remove(metadata.lock);
return false;
}
// auto pid = std::stoi(fileContents(metadata.lock).value());
// Check if the PID

return true;
}
void getLock();
void releaseLock() { fs::remove(metadata.lock); }
std::string name() const { return this_name; }
Expand Down
13 changes: 10 additions & 3 deletions src/cb/src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ void setupHandlers() {
#endif
});

signal(SIGINT, [](int) {
auto exitCleanly = [](int) {
fprintf(stderr, "%s", formatColors("[blank]").data());
if (!stopIndicator(false)) {
// Indicator thread is not currently running. TODO: Write an unbuffered newline, and maybe a cancelation
Expand All @@ -257,7 +257,11 @@ void setupHandlers() {
indicator.join();
exit(EXIT_FAILURE);
}
});
};

signal(SIGINT, exitCleanly);
signal(SIGTERM, exitCleanly);
signal(SIGQUIT, exitCleanly);

forker.atFork([]() {
// As the indicator thread still exists in memory in the forked process,
Expand Down Expand Up @@ -377,7 +381,10 @@ void setupVariables(int& argc, char* argv[]) {

arguments.assign(argv + 1, argv + argc);

clipboard_invocation = argv[0];
if (argv[0][0] != nullptr)
clipboard_invocation = argv[0];
else
clipboard_invocation = "cb";
}

template <typename T>
Expand Down

0 comments on commit ef88c17

Please sign in to comment.