Skip to content

Commit

Permalink
V2025.1.0-next (#56)
Browse files Browse the repository at this point in the history
* Version Bump

* System - Fix `Process::kill()` on Windows

* Update Changelog
  • Loading branch information
nlogozzo authored Jan 3, 2025
1 parent 58b3cd6 commit bdd2b30
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 37 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 2025.1.0 (next)
### Breaking Changes
None
### New APIs
None
### Fixes
#### System
- Fixed an issue where `Process::kill()` did not work on Windows

## 2024.12.1
### Breaking Changes
None
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ endif()
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")

#libnick Definition
project ("libnick" LANGUAGES C CXX VERSION 2024.12.1 DESCRIPTION "A cross-platform base for native Nickvision applications.")
project ("libnick" LANGUAGES C CXX VERSION 2025.1.0 DESCRIPTION "A cross-platform base for native Nickvision applications.")
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
include(CTest)
Expand Down
4 changes: 2 additions & 2 deletions Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ PROJECT_NAME = "libnick"
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER = "2024.12.1"
PROJECT_NUMBER = "2025.1.0"

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand Down Expand Up @@ -2416,7 +2416,7 @@ PERLMOD_MAKEVAR_PREFIX =
# C-preprocessor directives found in the sources and include files.
# The default value is: YES.

ENABLE_PREPROCESSING = YES
ENABLE_PREPROCESSING = NO

# If the MACRO_EXPANSION tag is set to YES, doxygen will expand all macro names
# in the source code. If set to NO, only conditional compilation will be
Expand Down
1 change: 1 addition & 0 deletions include/system/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ namespace Nickvision::System
HANDLE m_read;
HANDLE m_write;
PROCESS_INFORMATION m_pi;
HANDLE m_job;
#else
int m_pipe[2];
pid_t m_pid;
Expand Down
8 changes: 4 additions & 4 deletions manual/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

libnick provides Nickvision apps with a common set of cross-platform APIs for managing system and desktop app functionality such as network management, taskbar icons, translations, app updates, and more.

## 2024.12.1
## 2025.1.0 (next)
### Breaking Changes
None
### New APIs
#### Network
- Added `Nickvision::Network::Socket` class
### Fixes
None
### Fixes
#### System
- Fixed an issue where `Process::kill()` did not work on Windows

## Dependencies
The following are a list of dependencies used by libnick.
Expand Down
Binary file modified resources/libnick-r.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions resources/libnick-r.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified resources/libnick.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions resources/libnick.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 37 additions & 21 deletions src/system/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ namespace Nickvision::System
#ifdef _WIN32
m_read{ nullptr },
m_write{ nullptr },
m_pi{ 0 }
m_pi{ 0 },
m_job{ nullptr }
#else
m_pid{ -1 }
#endif
Expand All @@ -45,6 +46,18 @@ namespace Nickvision::System
{
throw std::runtime_error("Failed to create pipe.");
}
//Create job
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli{ 0 };
jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
m_job = CreateJobObjectW(nullptr, nullptr);
if(!m_job)
{
std::cerr << CodeHelpers::getLastSystemError() << std::endl;
CloseHandle(m_read);
CloseHandle(m_write);
throw std::runtime_error("Failed to create job object.");
}
SetInformationJobObject(m_job, JobObjectExtendedLimitInformation, &jeli, sizeof(jeli));
//Create process arguments
std::wstring appArgs{ L"\"" + m_path.wstring() + L"\"" };
for(const std::string& arg : m_args)
Expand All @@ -65,13 +78,14 @@ namespace Nickvision::System
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
//Create process
if(!CreateProcessW(nullptr, appArgs.data(), nullptr, nullptr, TRUE, CREATE_SUSPENDED | CREATE_NEW_PROCESS_GROUP, nullptr, nullptr, &si, &m_pi))
if(!CreateProcessW(nullptr, appArgs.data(), nullptr, nullptr, TRUE, CREATE_SUSPENDED, nullptr, nullptr, &si, &m_pi))
{
std::cerr << CodeHelpers::getLastSystemError() << std::endl;
CloseHandle(m_read);
CloseHandle(m_write);
throw std::runtime_error("Failed to create process.");
}
AssignProcessToJobObject(m_job, m_pi.hProcess);
#else
if(pipe(m_pipe) < 0)
{
Expand All @@ -88,6 +102,7 @@ namespace Nickvision::System
m_watchThread.join();
}
#ifdef _WIN32
CloseHandle(m_job);
CloseHandle(m_read);
CloseHandle(m_write);
CloseHandle(m_pi.hProcess);
Expand Down Expand Up @@ -199,37 +214,25 @@ namespace Nickvision::System
return false;
}
//Kill child processes spawned by the process
#ifdef _WIN32
if(!GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, m_pi.dwProcessId))
{
std::cerr << CodeHelpers::getLastSystemError() << std::endl;
return false;
}
#else
#ifndef _WIN32
::kill(-m_pid, SIGTERM);
#endif
//Kill process
#ifdef _WIN32
if(!TerminateProcess(m_pi.hProcess, 0))
CloseHandle(m_job);
m_job = nullptr;
#else
if(::kill(m_pid, SIGTERM) < 0)
#endif
{
std::cerr << CodeHelpers::getLastSystemError() << std::endl;
return false;
}
m_exitCode = -1;
m_running = false;
m_completed = true;
#endif
return true;
}

int Process::waitForExit()
{
if(hasCompleted())
{
return m_exitCode;
}
while(!hasCompleted())
{
std::this_thread::sleep_for(std::chrono::milliseconds(PROCESS_WAIT_TIMEOUT));
Expand Down Expand Up @@ -294,9 +297,22 @@ namespace Nickvision::System
}
std::unique_lock<std::mutex> lock{ m_mutex };
#ifdef _WIN32
DWORD exitCode{ 0 };
GetExitCodeProcess(m_pi.hProcess, &exitCode);
m_exitCode = static_cast<int>(exitCode);
if(!m_job)
{
m_exitCode = -1;
}
else
{
DWORD exitCode{ 0 };
if(GetExitCodeProcess(m_pi.hProcess, &exitCode))
{
m_exitCode = static_cast<int>(exitCode);
}
else
{
m_exitCode = -1;
}
}
#else
m_exitCode = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
#endif
Expand Down
6 changes: 1 addition & 5 deletions tests/processtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,12 @@ TEST_F(ProcessTest, Start)
TEST_F(ProcessTest, Kill)
{
ASSERT_TRUE(m_proc->kill());
ASSERT_EQ(m_proc->waitForExit(), -1);
ASSERT_FALSE(m_proc->isRunning());
ASSERT_TRUE(m_proc->hasCompleted());
ASSERT_EQ(m_proc->getExitCode(), -1);
}

TEST_F(ProcessTest, Wait)
{
ASSERT_EQ(m_proc->waitForExit(), -1);
}

TEST_F(ProcessTest, Destroy)
{
ASSERT_NO_THROW(m_proc.reset());
Expand Down
6 changes: 6 additions & 0 deletions tests/webtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ TEST(WebTests, DownloadFile1)

TEST(WebTests, FetchJsonString1)
{
#ifdef _APPLE_
if(Environment::hasVariable("GITHUB_ACTIONS"))
{
GTEST_SKIP();
}
#endif
boost::json::value json = Web::fetchJson("https://api.github.com/repos/nickvisionapps/denaro/tags");
ASSERT_FALSE(json.is_null());
ASSERT_TRUE(json.is_array());
Expand Down

0 comments on commit bdd2b30

Please sign in to comment.